# Author: Peter Andreae # Date: 02 Dec 2011 # A little Peg Puzzle, Sometimes called # "Traffic Jam" or "Toads and Frogs" # See http://mathforum.org/alejandre/java/jam/Jam.html for an example. # # Program to allow user to try to solve the linear peg solitaire problem: # setup: 7 holes, 3 red pegs in left 3 holes, 3 green pegs in right 3 holes. # Goal: exchange the positions of the red and green pegs # Movement rules: # Red pegs can only move to the right, # Left pegs can only move to the right. # A peg can move into the hole if it is next to the hole # A peg can jump over a peg of the opposite color if the hole is on the other side # # Design: # # Classes: # PegBoard represents a board # # TopLevel # create interface # canvas # three buttons for reset, move green, move red # create board # draw board # # PegBoard class: # Fields: board = an array of 7 strings ("Red", "Green", or "Black") # Functions: # reset() # draw(canvas) # moveRed() # moveGreen() # checkComplete() # # initialise: # create an array of 7 strings # # reset # set the first three cells to be red # set the middle cell to be empty # set the last three cells to be green # # # drawBoard # erase screen # for each cell # compute x position of cell # if cell contains red, draw red peg in hole # if cell contains green, draw green peg in hole # if cell contains hole, draw just a hole # # moveRed # For each cell except the rightmost # if cell contains red and cell+1 contains hole # swap contents of cell and cell+1 # exit # For each cell except the rightmost two cells # if cell contains red and cell+1 contains green, and cell+2 contains hole # swap contents of this cell and cell+2 # exit # Report "Can't move a red peg" # # moveGreen # For each cell except the leftmost # if cell contains green and cell-1 contains hole # swap contents of cell and cell-1 # exit # For each cell except the leftmost two cells # if cell contains green and cell-1 contains red, and cell-2 contains hole # swap contents of this cell and cell-2 # exit # Report "Can't move a green peg" # # # checkComplete # set answer to true # For first three cells # if cell does not contain green # set answer to false # For last three cells # if cell does not contain red # set answer to false # return answer # from tkinter import * from tkinter.messagebox import showerror, showwarning def main() : global window global canvas global board window = Tk() board = PegBoard(3) # root.title('Peg Puzzle') canvas = Canvas(window, width=450, height=300, bg = 'white') canvas.pack() Button(window, text="Move Green", command=performGreenAction).pack() Button(window, text="Move Red", command=performRedAction).pack() Button(window, text="Restart", command=restart).pack() board.draw() window.mainloop() def performGreenAction() : board.moveGreen() board.draw() board.reportIfComplete() def performRedAction() : board.moveRed() board.draw() board.reportIfComplete() def restart() : board.reset() board.draw() #---------------------------------------------------------------- class PegBoard() : def __init__(self, pegs) : self.pegs = pegs # The number of green pegs (same as number of red pegs) self.size = pegs*2 + 1 self.cells = [] for i in range(self.size) : self.cells.append("") self.reset() def moveRed(self) : for i in range(self.size-1) : # Can't move red peg in rightmost cell if self.cells[i] == "Red" : if self.cells[i+1] == "Black" : self.cells[i] = "Black" self.cells[i+1] = "Red" return if i < self.size-2 and self.cells[i+1] == "Green" and self.cells[i+2] == "Black" : self.cells[i] = "Black" self.cells[i+2] = "Red" return showerror("", "Can't move a red peg in this configuration!") def moveGreen(self) : print("moving green") for i in range(1,self.size) : # Can't move green peg in leftmost cell if self.cells[i] == "Green" : if self.cells[i-1] == "Black" : self.cells[i] = "Black" self.cells[i-1] = "Green" return if i>1 and self.cells[i-1] == "Red" and self.cells[i-2] == "Black" : self.cells[i] = "Black" self.cells[i-2] = "Green" return showerror("", "Can't move a green peg in this configuration!") def reset(self) : for i in range(self.pegs) : self.cells[i] = "Red" self.cells[self.pegs] = "Black" for i in range(self.pegs+1, self.size) : self.cells[i] = "Green" def reportIfComplete(self) : for i in range(self.pegs) : if board[i] != "Green" : showerror("not green at ", i) return False for i in range(self.pegs+1,self.size) : if board[i] != "Red" : return False showerror("", "You solved it!!") return True def draw(self) : canvas.delete(ALL) X = 40 # position of center of first cell of board Y = 40 cellRad = 25 holeRad = 10 pegRad = 15 for cell in self.cells : canvas.create_rectangle(X-cellRad, Y-cellRad, X+cellRad, Y+cellRad, fill="orange", outline="black") canvas.create_oval(X-pegRad, Y-pegRad, X+pegRad, Y+pegRad, fill=cell) X = X + 2 * cellRad canvas.update() def print(self) : print() print("board: ") for cell in board : print(cell) print() print() # ------------------------------------------- print("where does this go") main()