HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Solution to the cracker barrel peg game / triangle game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thecrackerbarreltrianglegamesolutionpeg

Problem

I wrote a small script that finds the solution to the cracker barrel peg game / triangle game.

Gist

```
"""A small script that finds the solution to
the cracker barrel peg game / triangle game.

Instructions along with one of the game's solution can be found here:
http://www.joenord.com/puzzles/peggame/

It finds the solution by repeatedly playing games and making random
decisions until a game leaves one peg. The slot's are numbered
0 - 14 in sequential order (Top to Bottom, Left to Right)

/0\
/1 2\
/3 4 5\
etc etc

Another objective of this game is to leave the board with
8 pegs and no possible jumps. To find that solution change the
final if statement as follows:

if triangle.total_peg() == 8:

Language python 2.7
"""

import random

class Slot:
"""
Represents a single slot opening of the triangle game.
This class holds the slot number, it's possible jump
dictionary and helper functions around maintaining slots.
"""

def __init__(self, num, jump_dict):
self.num = num
self.jump_dict = jump_dict
self.peg = True

def has_peg(self):
return self.peg

def add_peg(self):
self.peg = True

def remove_peg(self):
self.peg = False

def possible_jump(self, board):
""" Determine possible jumps for a given peg

:param board: The board to check possible jumps against
:return: Dictionary of possible jumps
"""
assert self.has_peg()
possible_jump_dict = {}
for jump_over in self.jump_dict:
jump_to = self.jump_dict[jump_over]
if board[jump_over].has_peg() and not board[jump_to].has_peg():
possible_jump_dict[jump_over] = jump_to
return possible_jump_dict

class Triangle:
"""
Represents a single board of the triangle game
"""

def __init__(self):
"""
Initializes the board for a new game.
The board consists of slot 15 objects
"""

Solution

Another objective of this game is to leave the board with
8 pegs and no possible jumps. To find that solution change the
final if statement as follows:


if triangle.total_peg() == 8:

Wait, I, the user must change the code? Well I can totally do that but it does not fell professional, I suggest:


Another objective of this game is to leave the board with
8 pegs and no possible jumps. To find that solution change the
following constant to True:

EIGHT_PEGS_SOLUTION = False


and then:

while True:
    total_games += 1
    triangle = Triangle()
    triangle.play_one_game()
    pegs_wanted = 8 if EIGHT_PEGS_SOLUTION else 1 
    if triangle.total_peg() == pegs_wanted:
        break


Now the user has the only task of swapping a boolean value and there is a much better professional feeling.

Code Snippets

EIGHT_PEGS_SOLUTION = False
while True:
    total_games += 1
    triangle = Triangle()
    triangle.play_one_game()
    pegs_wanted = 8 if EIGHT_PEGS_SOLUTION else 1 
    if triangle.total_peg() == pegs_wanted:
        break

Context

StackExchange Code Review Q#88629, answer score: 3

Revisions (0)

No revisions yet.