Advertisement
asweigart

Peg Solitaire

May 27th, 2019
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. # Peg Solitaire by Al Sweigart al@inventwithpython.com
  2.  
  3. # More info at https://en.wikipedia.org/wiki/Peg_solitaire
  4.  
  5. EMPTY = '.'
  6. MARBLE = 'O'
  7. NORTH = 'north'
  8. SOUTH = 'south'
  9. EAST = 'east'
  10. WEST = 'west'
  11. ALL_SPACES = 'C1 D1 E1 C2 D2 E2 A3 B3 C3 D3 E3 F3 G3 A4 B4 C4 D4 E4 F4 G4 A5 B5 C5 D5 E5 F5 G5 C6 D6 E6 C7 D7 E7'.split()
  12.  
  13. def getNewBoard():
  14.     board = {}
  15.     # Set every space on the board to a marble:
  16.     for space in ALL_SPACES:
  17.         board[space] = MARBLE
  18.     # Set the center space to be empty:
  19.     board['D4'] = EMPTY
  20.     return board
  21.  
  22.  
  23. def displayBoard(board):
  24.     spaces = []
  25.     for space in ALL_SPACES:
  26.         spaces.append(board[space])
  27.  
  28.     print('''
  29.  ABCDEFG
  30. 1   {}{}{}
  31. 2   {}{}{}
  32. 3 {}{}{}{}{}{}{}
  33. 4 {}{}{}{}{}{}{}
  34. 5 {}{}{}{}{}{}{}
  35. 6   {}{}{}
  36. 7   {}{}{}
  37. '''.format(*spaces))
  38.  
  39.  
  40. def canMoveInDirection(board, space, direction):
  41.     x, y = space # Split up `space` into the x and y coordinates.
  42.  
  43.     if direction == NORTH:
  44.         neighborSpace = x + str(int(y) - 1) # E.g. convert y of 3 to '2'
  45.         secondNeighborSpace = x + str(int(y) - 2) # E.g. convert y of 3 to '1'
  46.     elif direction == SOUTH:
  47.         neighborSpace = x + str(int(y) + 1) # E.g. convert y of 3 to '4'
  48.         secondNeighborSpace = x + str(int(y) + 2) # E.g. convert y of 3 to '5'
  49.     elif direction == WEST:
  50.         neighborSpace = chr(ord(x) - 1) + y # E.g. convert 'C' to 'B'
  51.         secondNeighborSpace = chr(ord(x) - 2) + y # E.g. convert 'C' to 'A'
  52.     elif direction == EAST:
  53.         neighborSpace = chr(ord(x) + 1) + y # E.g. convert 'C' to 'D'
  54.         secondNeighborSpace = chr(ord(x) + 2) + y # E.g. convert 'C' to 'E'
  55.  
  56.     # Check if the neighboring space exists:
  57.     if neighborSpace in ALL_SPACES:
  58.         # Check if there is a marble in the neighboring space:
  59.         if board[neighborSpace] == MARBLE:
  60.             # Check if the neighbor's neighboring space exists:
  61.             if secondNeighborSpace in ALL_SPACES:
  62.                 # Check if there is an empty space there:
  63.                 if board[secondNeighborSpace] == EMPTY:
  64.                     return True
  65.     return False
  66.  
  67.  
  68. def getMoveableMarbles(board):
  69.     moveableMarbles = [] # Contain a list of spaces whose marble can jump.
  70.     for space in ALL_SPACES:
  71.         if board[space] == EMPTY:
  72.             continue # There's no marble here, so it's not a valid move.
  73.  
  74.         # Determine if the marble at this space can move:
  75.         if canMoveInDirection(board, space, NORTH) or \
  76.            canMoveInDirection(board, space, SOUTH) or \
  77.            canMoveInDirection(board, space, WEST) or \
  78.            canMoveInDirection(board, space, EAST):
  79.             moveableMarbles.append(space)
  80.             continue
  81.  
  82.     return moveableMarbles
  83.  
  84.  
  85. def getPlayerMove(board):
  86.     while True:
  87.         # Ask the player to enter a valid move:
  88.         moveableMarbles = getMoveableMarbles(board)
  89.         print('Enter the marble you want to move:')
  90.         print(' '.join(moveableMarbles))
  91.         space = input().upper()
  92.  
  93.         if space in moveableMarbles:
  94.             break # If the space has a moveable marble, break out of the loop.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement