Skip to main content

Farkle Simulation

Author

Ronen Huang

Time Frame

November 2021 to December 2021 (modified for Python usage), January 2025 to Present (add deep Q-Learning).

Install Farkle Simulation

The Farkle Simulation can be downloaded by pip.

pip install farkle-simulation

Farkle

The rules of Farkle can be seen in the In a Nutshell section of https://farkle.games/official-rules/. In this case, there are two players with Player 1 as the first player and Player 2 as the second player.

The scoring system can be seen in the table below

Dice to Keep Score
Three 1s or Straight 1000
Three Different Pairs 750
Three 6s 600
Three 5s 500
Three 4s 400
Three 3s 300
Three 2s 200
One 1 100
One 5 50

A player begins their turn with all six dice.

  • If there are scoring combinations, a player can choose any and
    • Either keep rolling or stop
    • When number of dice is 0, it resets to 6
  • Otherwise, a player has "farkled" and the turn score is 0

Once a player reaches 10,000 points, they have won the game.

Strategies

The implementation of the strategies is in strategy.py.

The strategies that Player 1 and Player 2 can use are naive strategy, simple RL strategy, and custom strategy (manual). The turn function goes through one turn.

from farkle_simulation.components.strategy import\
  NaiveStrategy, SimpleRLStrategy, CustomStrategy

naive_strategy = NaiveStrategy()
simple_rl_strategy = SimpleRLStrategy()
custom_strategy = CustomStrategy()

naive_turn_score = naive_strategy.turn(current_score, advantage)
simple_rl_turn_score = simple_rl_strategy.turn(current_score, advantage)
custom_turn_score = custom_strategy.turn(current_score, advantage)

Custom Strategy

The player chooses the action each roll via typing 1 to 7 on the command line.

Naive Strategy

For each roll

  • Choose action that maximizes roll score
  • Stop if number of dice is less than or equal to 2 and disadvantage of less than 1,000.

Simple Reinforcment Learning Strategy

For each roll

  • Choose action that maximizes reward based on
    • Number of Rolls
      # The gamma is reward factor decrease by roll.
      actual_reward = gamma ** np.log2(num_rolls + 1) * roll_score
      
    • Distance to 10,000 and Current Score
      # Increases as close to 10,000.
      distance_factor = distance_scale ** (current_score / 10000)
      
    • Advantage
      # Increase as more behind.
      advantage_factor = 1
      if advantage < 0:
          advantage_factor *= max(
            np.emath.logn(advantage_scale, -advantage), 1
          )
      

The architecture of the deep Q-learning networks takes 11 dimensional input state

  • Distance
  • Advantage
  • Current Turn Score
  • Roll Maxes

and predicts reward for each of 7 possible actions

  • Keep 1 to 6 dice
  • Stop

This can be seen in architecture.py.

The training process can be seen at simple_farkle_rl.py. The best model state dictionary is saved as simple_action_reward_state_dict.pt.

from farkle_simulation.components.simple_farkle_rl import train

train()

The plot of turn score by current score can be seen in training_simple_rl.jpg and the table in training_simple_rl.csv. Training Simple RL

Simulation

The implementation of the simulation is in simulation.py.

The convergence_plot function plots the expected probability Player 1 wins using some strategy with Player 2 using some other strategy.

from farkle_simulation.components.simulation import convergence_plot

convergence_plot(naive_strategy, simple_rl_strategy)
convergence_plot(simple_rl_strategy, naive_strategy)

The convergence plots can be seen at convergence_naive_simple_rl.jpg Convergence Plot Naive vs Simple RL and convergence_simple_rl_naive.jpg Convergence Plot Simple RL vs Naive

The histogram function plots the average probability Player 1 wins using some strategy with Player 2 using some other strategy.

from farkle_simulation.components.simulation import histogram

histogram(naive_strategy, simple_rl_strategy)
histogram(simple_rl_strategy, naive_strategy)

The histograms can be seen at histogram_naive_simple_rl.jpg Histogram Naive vs Simple RL and histogram_simple_rl_naive.jpg Histogram Simple RL vs Naive

Pipeline

To train the simple RL agent and compare the strategies through convergence plot and histogram.

from farkle_simulation.pipeline import train_simulate

train_simulate()

To play a game against the simple RL agent.

from farkle_simulation.pipeline import play_game

play_game()

Input 1 to play as first player and 2 to play as second player.

Player 1 (1) or Player (2)? 1

Input action to chose for dice combination.

Dice Combination - (1, 1, 1, 1, 1, 4)
Legal Moves - (1) Keep 1 - max 100 remaining dice 5, (2) Keep 2 - max 200 remaining dice 4, (3) Keep 3 - max 1000 remaining dice 3, (4) Keep 4 - max 1100 remaining dice 2, (5) Keep 5 - max 1200 remaining dice 1, (7) No Roll - max 1200 remaining dice 0
Action - 7

The output of action is.

Current Turn Score - 1200
Current Score - 1200
Turn Score - 1200
Player 1 Score - 1200 Player 2 Score - 0

The output of a "farkle" is.

Dice Combination - (2, 6)
Farkled
Turn Score - 0

Release files for farkle-simulation 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for farkle-simulation 0.1.0
File Size Uploaded
farkle_simulation-0.1.0.tar.gz 42.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for farkle-simulation 0.1.0
File Interpreter ABI Platform
farkle_simulation-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 84.3 kB

Release files / farkle_simulation-0.1.0.tar.gz

Download URL farkle_simulation-0.1.0.tar.gz
Size 42.7 kB
Tags Source
SHA-256 checksum
How to use checksums
c0f773119f41c2583efe0462773b0a14387d378f9acffd98d3f5d2f9b3824c9b
BLAKE2b-256 checksum
How to use checksums
2886423a17fb4936cb80656e08e81938fdc2919c232d30393c52f6083ba0284c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 17, 2025.

Transparency log

Release files / farkle_simulation-0.1.0-py3-none-any.whl

Download URL farkle_simulation-0.1.0-py3-none-any.whl
Size 41.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
353f44fd65d26ec7f824ce113d4b3872e7afb6e6629ea28bfc42ad9e878f8f14
BLAKE2b-256 checksum
How to use checksums
eb5b6c658429ee17d89f14c41eb6d1fec8be3c9dc4657f756e6fca1eb6475264
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Apr 17, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page