Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. # utility methods --------------------------------------------------------------
  2.  
  3. # return an empty 3x3 grid, represented by a list of lists with only None values
  4. def init_grid():
  5. grid = []
  6. for _ in range(3):
  7. grid.append([None, None, None])
  8. return grid
  9.  
  10. # get a player's next move as a tuple of integer values
  11. def get_move(turn_of, grid):
  12. move = None
  13. # loop until a valid move is selected (i.e. until move is not None)
  14. while not move:
  15. if turn_of == 'player_1':
  16. raw_move_input = raw_input('\n' + 'Player 1, enter your move: ')
  17. else:
  18. raw_move_input = raw_input('\n' + 'Player 2, enter your move: ')
  19. move = validate_move_input(raw_move_input, grid)
  20. return move
  21.  
  22. # if raw_move_input represents a valid move, then return a parsed representation
  23. # of it as a tuple of integer values; otherwise, return None
  24. def validate_move_input(raw_move_input, grid):
  25. move = None
  26. # check that a comma was supplied to separate values
  27. if ',' in raw_move_input:
  28. raw_move_components = raw_move_input.split(',')
  29. # check that the raw input can be converted into a pair of integers
  30. if raw_move_components[0].isdigit() and \
  31. raw_move_components[1].isdigit():
  32. # convert the components to integers
  33. move_components = [int(raw_move_components[0]),
  34. int(raw_move_components[1])]
  35. # check that the integer inputs are bounded by 0 and 2
  36. if (0 <= move_components[0] and move_components[0] <= 2) and \
  37. (0 <= move_components[1] and move_components[1] <= 2):
  38. (x, y) = (move_components[0], move_components[1])
  39. # check that a symbol hasn't already been placed at that position
  40. if not grid[x][y]:
  41. move = (x, y)
  42. else:
  43. print '\n' + 'Invalid input. A symbol has already been ' + \
  44. 'placed there'
  45. else:
  46. print '\n' + 'Invalid input. The components are not in range'
  47. else:
  48. print '\n' + 'Invalid input. Remember to supply 2 integer values'
  49. else:
  50. print '\n' + 'Invalid input. Remember to separate values by a comma'
  51. return move
  52.  
  53. # pretty-print the grid
  54. def print_grid(grid):
  55. print
  56. for i in range(0,len(grid)):
  57. row_string = str(grid[i][0]) + ' | ' + str(grid[i][1]) + ' | ' + \
  58. str(grid[i][2])
  59. print row_string.replace('None', ' ')
  60.  
  61. # check if any game-ending conditions are satisfied
  62. # game can end if:
  63. # - there is a row, column, or diagonal containing only 1 symbol
  64. # (i.e. a player has won)
  65. # - symbols have been placed in all 9 positions (i.e. tie game)
  66. def check_if_done(grid):
  67. done = False
  68. winner = get_winner(grid)
  69. # if winner is not None, then announce them as a winner and return True
  70. if winner:
  71. done = True
  72. if winner == 'player_1':
  73. print '\n' + 'Player 1 is the winner!'
  74. else:
  75. print '\n' + 'Player 2 is the winner!'
  76. elif check_if_full_grid(grid):
  77. done = True
  78. print '\n' + 'The game was a tie.'
  79. return done
  80.  
  81. # if a player_1 has won the game, return player_1
  82. # if player_2 has won the game, return player_2
  83. # else return None
  84. def get_winner(grid):
  85. # check the rows
  86. for i in range(0, len(grid)):
  87. if check_squares_for_win(grid[i][0], grid[i][1], grid[i][2]):
  88. return from_symbol_to_player(grid[i][0])
  89. # check the columns
  90. for i in range(0, len(grid)):
  91. if check_squares_for_win(grid[0][i], grid[1][i], grid[2][i]):
  92. return from_symbol_to_player(grid[0][i])
  93. # check the diagonals
  94. # top-left to bottom-right
  95. if check_squares_for_win(grid[0][0], grid[1][1], grid[2][2]):
  96. return from_symbol_to_player(grid[0][0])
  97. # top-right to bottom-left
  98. if check_squares_for_win(grid[0][2], grid[1][1], grid[2][0]):
  99. return from_symbol_to_player(grid[2][0])
  100. return None
  101.  
  102. # check to see if a sequence of squares have equal values
  103. def check_squares_for_win(x, y, z):
  104. is_win = False
  105. # make sure at least one value isn't None
  106. if x:
  107. # check that all 3 values are equal
  108. if x == y and y == z:
  109. is_win = True
  110. return is_win
  111.  
  112. # get the player who uses a particular symbol
  113. def from_symbol_to_player(symbol):
  114. for player in player_symbols.keys():
  115. if player_symbols[player] == symbol:
  116. return player
  117. # this line should never be reached, if it is, then symbol doesn't match
  118. # any player in player_symbols
  119. assert False
  120.  
  121. # check if the grid is full with symbols
  122. def check_if_full_grid(grid):
  123. for row in grid:
  124. for square in row:
  125. if square == None:
  126. return False
  127. return True
  128.  
  129. def switch_player(turn_of):
  130. if turn_of == 'player_1':
  131. return 'player_2'
  132. else:
  133. return 'player_1'
  134.  
  135.  
  136. # initializations --------------------------------------------------------------
  137. player_symbols = {'player_1': 'x', 'player_2': 'o'}
  138. grid = init_grid()
  139. turn_of = 'player_1'
  140. game_over = False
  141.  
  142.  
  143. # game loop --------------------------------------------------------------------
  144. while not game_over:
  145. (x,y) = get_move(turn_of, grid) # loop until a valid move is selected
  146. grid[x][y] = player_symbols[turn_of]
  147. print_grid(grid)
  148. game_over = check_if_done(grid)
  149. turn_of = switch_player(turn_of)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement