Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. import time
  2. import random
  3.  
  4. """
  5. Board file, should only be used from the game file,
  6. and not manipulated directly.
  7. """
  8.  
  9. class Board(object):
  10. """
  11. Board Class used to hold a respresentation of the current Game Board
  12. By Daniel Anderson
  13. """
  14. def __init__(self):
  15. """#creates a new empty game board
  16. #2d array of 0 Empty, 1 Human, 2 Computer
  17. """
  18. self.game_board = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  19.  
  20. def winning_board(self):
  21. """
  22. checks to see if this board has a winner
  23. """
  24. #horizontal and vertical
  25. for i in range(0, 2):
  26. if self.game_board[i][0] == self.game_board[i][1] == self.game_board[i][2] != 0:
  27. return True
  28. if self.game_board[0][i] == self.game_board[1][i] == self.game_board[2][i] != 0:
  29. return True
  30.  
  31. #diagonals
  32. if self.game_board[0][0] == self.game_board[1][1] == self.game_board[2][2] != 0:
  33. return True
  34. if self.game_board[0][2] == self.game_board[1][1] == self.game_board[2][0] != 0:
  35. return True
  36.  
  37. return False
  38.  
  39. def can_put(self, xcoord, ycoord):
  40. """Return true or false if a new piece can be place at that position
  41. Arguments:
  42. xcoord- row index,
  43. ycoord - y index
  44. """
  45. return self.game_board[xcoord][ycoord] == 0
  46.  
  47. def put(self, xcoord, ycoord, player):
  48. """Places a new tile on the game board
  49. player must be [1, 2]
  50. xcoord, ycoord must be [0, 1, 2]
  51. """
  52. self.game_board[xcoord][ycoord] = player
  53.  
  54. def out_of_moves(self):
  55. """returns true if there are no more available squares """
  56. for i in self.game_board:
  57. for j in i:
  58. if j == 0:
  59. return False
  60. return True
  61.  
  62. #returns a list of all possible moves
  63. def possible_moves(self):
  64. """returns a list of all possible moves """
  65. move_list = []
  66. for i in range(0, 3):
  67. for j in range(0, 3):
  68. if self.game_board[i][j] == 0:
  69. move_list.append([i, j])
  70. return move_list
  71.  
  72. def optimal_move(self):
  73. """return the best move out of the list of moves"""
  74. move_list = self.possible_moves()
  75. return move_list[0]
  76.  
  77. def print_board(self):
  78. """Displays the game board"""
  79. print '----------'
  80. for i in self.game_board:
  81. print i
  82. print "--------------"
  83.  
  84.  
  85. """
  86. The Game file used to play TicTacToe
  87. By Daniel Anderson
  88. To start the game please run 'python game.py'
  89. """
  90.  
  91. class Game(object):
  92. """The game object used to run the game"""
  93. def __init__(self):
  94. self.board = Board()
  95. display_game_info()
  96. self.collect_player_info()
  97. self.playx()
  98. self.curr_player = 1
  99.  
  100. def switch_playcoorders(self):
  101. """Used to switch players during the game."""
  102. if self.curr_player == 1:
  103. self.curr_player = 2
  104. else:
  105. self.curr_player = 1
  106.  
  107. def collect_player_info(self):
  108. """collects the initial information about each player"""
  109. #get the users name
  110. self.player_name = raw_input('Enter your name: ')
  111. #who goes first
  112. while True:
  113. print "Please tell us who should go first"
  114. print "0 -- RANDOM"
  115. print "1 -- HUMAN"
  116. print "2 -- COMPUTER"
  117. self.curr_player = int(raw_input("Please enter a 0, 1, or 2: "))
  118. if self.curr_player in [0, 1, 2]:
  119. if self.curr_player == 0:
  120. self.curr_player = random.randint(1, 2)
  121. break
  122. else:
  123. print "I am sorry, we didnt not recognize the input, try again \n"
  124.  
  125. def playx(self):
  126. """The bulk of the game, it mainly has one running loop
  127. Where you continue to input moves to the game"""
  128. print "----------------- HAVE FUN ----------------- "
  129. while not self.board.out_of_moves():
  130. print "Here is the current Board"
  131. self.board.print_board()
  132. if self.curr_player == 1:
  133. print "It is currently your turn " + self.player_name
  134. print "Good Luck."
  135. xcoord = int(raw_input("Please input the x coordinate of your move"))
  136. ycoord = int(raw_input("Please input the y coordinate of your move"))
  137. if xcoord in [0, 1, 2] and ycoord in [0, 1, 2]:
  138. if self.board.can_put(xcoord, ycoord):
  139. self.board.put(xcoord, ycoord, self.curr_player)
  140. if self.board.winning_board():
  141. print "Congradulations" + self.player_name + ", you won!"
  142. break
  143. self.switch_playcoorders()
  144. else:
  145. print "your input a valid position, but that position isn't available"
  146. else:
  147. print "Those coordinates are not valid, please try again"
  148. else:
  149. print "It is currently the computers turn:"
  150. print "Please Wait while he makes a move"
  151. time.sleep(2)
  152. optimal_move = self.board.optimal_move()
  153. self.board.put(optimal_move[0], optimal_move[1], self.curr_player)
  154. if self.board.winning_board():
  155. print "Too bad the computer won."
  156. break
  157. self.switch_playcoorders()
  158. print "Here was the winning Board:"
  159. print self.board.print_board()
  160. print "Thank you for playing"
  161. print "This Game was made by Daniel Anderson"
  162.  
  163. def display_game_info():
  164. """Basic function to display game info"""
  165. print "Welcome to my version of Tic Tac Toe\n"
  166. print "By Daniel Anderson\n"
  167. print "I hope you came ready to play\n"
  168.  
  169. #executing the game
  170. Game()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement