Skip to content

peetm/Penneys-Game-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Penney's Game - Python GUI

An interactive graphical application for exploring the fascinating properties of Penney's Game, a counterintuitive coin-flipping game that demonstrates non-transitive probability.

What is Penney's Game?

Penney's Game is a two-player game where:

  1. Each player chooses a sequence of three coin flips (e.g., HHT, THH, HTT)
  2. A coin is flipped repeatedly until one player's sequence appears
  3. The player whose sequence appears first wins

The Surprise: The game is non-transitive - there's no single "best" pattern! For every pattern you choose, there exists a counter-pattern that beats it approximately 2/3 of the time.

Key Insights

  • Waiting Times Paradox: HTT appears faster (~8 flips) than HTH (~10 flips) on average, even though both seem equally likely
  • Optimal Strategy: Always let your opponent choose first, then pick the counter-pattern to win ~67% of games
  • Non-Transitivity: Like rock-paper-scissors, but with coins - every pattern has a counter that dominates it

Features

1. ⏱ Waiting Times Analysis

Calculates how many coin flips it takes on average for each pattern to appear:

  • Reveals which patterns appear faster
  • Explains the counterintuitive differences
  • Demonstrates overlap effects

2. 🎯 Best Counters

For each pattern, finds which opponent pattern beats it most effectively:

  • Shows win rates for optimal counters
  • Demonstrates that every pattern has a weakness
  • Proves the ~67% advantage of counter-patterns

3. 📊 Dominance Matrix

Complete 8×8 win-rate matrix for all pattern matchups:

  • Color-coded cells (Green=Advantage, Red=Disadvantage, Yellow=Even)
  • Shows all possible head-to-head results
  • Visualizes the non-transitive structure

4. 🎮 Optimal Strategy Guide

Game-theoretically optimal counter-strategies:

  • Shows which pattern to pick against any opponent
  • Explains the strategy rules
  • Verifies win rates through simulation

5. 🎲 Interactive Gameplay

Play matches between any two patterns:

  • Select patterns for both players
  • Simulate 1000 games instantly
  • See which pattern dominates the matchup

Requirements

  • Python 3.x
  • Tkinter (usually included with Python)
  • Standard library modules: random, itertools, threading

Installation

No installation required! Just run the script:

python3 penneys_game_gui.py

Or make it executable on Unix-like systems:

chmod +x penneys_game_gui.py
./penneys_game_gui.py

Usage

Running Analyses

  1. Select a Tab: Choose which aspect of the game to explore
  2. Set Parameters: Adjust the number of simulations (more = more accurate)
  3. Click "Run Analysis": Results appear in a few seconds
  4. Explore: Try different patterns and matchups

Configuration

Default simulation counts can be adjusted at the top of the code:

DEFAULT_WAITING_TIME_SIMULATIONS = 10000  # Waiting time analysis
DEFAULT_MATCHUP_GAMES            = 10000  # Best counter analysis
DEFAULT_MATRIX_GAMES             = 10000  # Dominance matrix
DEFAULT_STRATEGY_GAMES           = 5000   # Strategy verification
DEFAULT_INTERACTIVE_GAMES        = 1000   # Interactive play

The Mathematics Behind It

Why Counter-Patterns Win ~67%

The counter-pattern is constructed to "intercept" the opponent's pattern. For example:

  • Opponent picks: HTH
  • Your counter: HHT

When coins show HT, your opponent needs an H to complete HTH. But if an H appears, you immediately complete HHT and win! This gives you a 2:1 advantage.

Strategy Rules

For any opponent pattern ABC:

  1. Take the second letter: B
  2. Prepend its opposite: opposite(B)
  3. Add the first letter: opposite(B) + A

Example: Opponent picks HTH → Take T, prepend H, add H = HHT

(Special cases for HHH and TTT use different rules)

Waiting Time Formula

The average waiting time for a pattern depends on its internal overlap structure. Patterns like HTT have less overlap than HTH, making them appear faster on average.

Educational Value

This application is perfect for:

  • Probability Students: Exploring non-intuitive probability concepts
  • Game Theory: Understanding non-transitive games
  • Mathematics Education: Visualizing complex patterns
  • Recreational Mathematics: Discovering surprising results

Example Session

1. Open the "Waiting Times" tab
   → See that HTT (8 flips) appears faster than HTH (10 flips)

2. Check "Optimal Strategy" tab
   → Learn that HHT beats HTH 67% of the time

3. Try "Interactive Play"
   → Select HTH vs HHT
   → Simulate 1000 games
   → Confirm HHT wins ~670 times

4. Explore "Dominance Matrix"
   → See the complete web of advantages
   → Discover no pattern dominates all others

Technical Details

  • Threading: All simulations run in background threads to keep the UI responsive
  • Simulation Quality: Higher simulation counts provide more accurate results but take longer
  • Color Coding: Green (>60% win rate), Red (<40%), Yellow (40-60%)
  • Canvas-Based Matrix: Scrollable grid for the 8×8 dominance matrix

Performance Notes

  • Waiting Times: ~1-3 seconds (10,000 simulations per pattern)
  • Best Counters: ~2-5 seconds (10,000 games per matchup)
  • Dominance Matrix: ~10-20 seconds (28 unique matchups × 10,000 games)
  • Strategy Analysis: ~2-5 seconds (8 matchups × 5,000 games)
  • Interactive Play: Instant (1,000 games)

Further Reading

Penney's Game was first described by Walter Penney in 1969 and has since become a classic example in probability theory and game theory.

Key concepts demonstrated:

  • Non-transitive games
  • Conditional probability
  • Pattern overlap
  • Optimal decision theory

License

Free to use and modify for educational purposes.

Credits

Based on the mathematical work of Walter Penney and subsequent analysis by mathematicians exploring non-transitive games.

Tips for Exploration

  1. Start with Waiting Times: Understand why patterns appear at different speeds
  2. Learn the Strategy: See how counter-patterns dominate
  3. Play Interactively: Test specific matchups you're curious about
  4. Study the Matrix: See the complete picture of all matchups
  5. Impress Your Friends: Challenge them to pick first, then win 2/3 of the time!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages