Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.91 KB | None | 0 0
  1.     def next_move(self):
  2.         """Return a (possibly empty) list of legal moves for current player.
  3.  
  4.            Assumptions:
  5.            The list will be empty if either player has already won, and
  6.            the order of moves in the list is not significant
  7.  
  8.            Returns:
  9.            A list of pairs of legal moves of the form (r, c) such that
  10.            self._state[0][r][c] is currently unoccupied and the current player
  11.            may move by occupying it.
  12.        """
  13.        
  14.         move_list = []
  15.         player = self._state[1]
  16.         opponent = self.opponent()
  17.         if self.winner(player):
  18.             return move_list
  19.         if self.winner(opponent):
  20.             return move_list
  21.         for i in range(len(self._state[0])):
  22.             for b in range(len(self._state[0][i])):
  23.                 if  self._state[0][i][b]== None:
  24.                     move_list.append((i,b))
  25.         return move_list
  26.  
  27.     def make_move(self, move):
  28.         """Apply move to current game.
  29.  
  30.           Arguments:
  31.           move:  A pair (r, c) representing square self._state[0][r][c]
  32.  
  33.           Assumptions:
  34.           (r, c) are valid coordinates for self._state[0].  If they represent
  35.           a valid move for current player, then the current player occupies
  36.           that position and the opponent becomes the current player.
  37.           Otherwise self._state is left unchanged.
  38.  
  39.           Returns:
  40.           New TicTacToe gamestate with move recorded on
  41.           self._state[0] and current player replaced by opponent, if
  42.           this is legal.  Otherwise return None.
  43.        """
  44.        
  45.        
  46.        
  47.         if self._state[move[0]][move[1]] != None:
  48.             return None
  49.         else:
  50.             board = copy.deepcopy(self._state[0])
  51.             board[move[0]][move[1]] = self.player()
  52.             opp = self.opponent()
  53.             return TicTacToe((board, opp))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement