Last active
September 3, 2018 22:34
-
-
Save ltbringer/5ae0af7189b22b8098dc1c66ebb4a3da to your computer and use it in GitHub Desktop.
reinforcement_tic_tac_toe_snippet_2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def player_move(self, input_symbol, item_x, item_y): | |
""" | |
The method which facilitates insertion of values into the board matrix. | |
params: | |
- input_symbol: 'X' or 'O' | |
- item_x int: The row of the matrix in which item has been inserted. | |
- item_y int: The column of the matrix in which the item has been inserted. | |
""" | |
symbol = None | |
if input_symbol == self.sym_o.get('mark'): | |
# If 'O' was inserted | |
symbol = self.sym_o | |
elif input_symbol == self.sym_x.get('mark'): | |
# If 'X' was inserted | |
symbol = self.sym_x | |
else: | |
# invalid symbol | |
return | |
self.board[item_x][item_y] = symbol.get('value') | |
# insert the integer corresponding to the symbol in to the matrix. | |
self.draw_board() | |
# Show the board in a human friendly format for evaluation. | |
if self.is_winning_move(symbol.get('mark'), symbol.get('value'), item_x, item_y): | |
# If this move was a winning move, declare the symbol as the winner. | |
print('Winner is: {}'.format(self.winner)) | |
def play(self, item_x, item_y): | |
""" | |
The method exposed to a human user | |
facilitates insertion of values into the board matrix. | |
params: | |
- input_symbol: 'X' or 'O' | |
- item_x int: The row of the matrix in which item has been inserted. | |
- item_y int: The column of the matrix in which the item has been inserted. | |
""" | |
max_limit, _ = self.board.shape | |
if item_x > max_limit - 1 or item_y > max_limit: | |
# If the row, column values dont' exist in the board matrix. | |
# Exit without inserting it into the board. | |
return | |
self.player_move(self.player_sym.get('mark'), item_x, item_y) | |
def bot_play(self, item_x, item_y): | |
""" | |
The method exposed to a bot | |
facilitates insertion of values into the board matrix. | |
params: | |
- input_symbol: 'X' or 'O' | |
- item_x int: The row of the matrix in which item has been inserted. | |
- item_y int: The column of the matrix in which the item has been inserted. | |
""" | |
max_limit, _ = self.board.shape | |
if item_x > max_limit - 1 or item_y > max_limit: | |
return | |
self.player_move(self.bot_sym.get('mark'), item_x, item_y) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment