TicTacToe: Decouple TicTacToeGame.tictactoe() string from move result.

This allows the storage update to be refactored into one block before
sending the result text.
This commit is contained in:
neiljp (Neil Pilgrim) 2017-12-09 21:03:59 -08:00 committed by showell
parent 5843801d13
commit 33e8517b87

View file

@ -224,7 +224,7 @@ class TicTacToeGame(object):
if board[row][column] != "_": if board[row][column] != "_":
return_string += output_mode("That space is already filled, sorry!", mode) return_string += output_mode("That space is already filled, sorry!", mode)
return return_string return ("filled",return_string)
else: else:
board[row][column] = "x" board[row][column] = "x"
@ -233,11 +233,11 @@ class TicTacToeGame(object):
# Check to see if the user won/drew after they made their move. If not, it's the computer's turn. # Check to see if the user won/drew after they made their move. If not, it's the computer's turn.
if self.win_conditions(board, self.triplets): if self.win_conditions(board, self.triplets):
return_string += output_mode("Game over! You've won!", mode) return_string += output_mode("Game over! You've won!", mode)
return return_string return ("player_win", return_string)
if self.board_is_full(board): if self.board_is_full(board):
return_string += output_mode("It's a draw! Neither of us was able to win.", mode) return_string += output_mode("It's a draw! Neither of us was able to win.", mode)
return return_string return ("draw", return_string)
return_string += output_mode("My turn:\n", mode) return_string += output_mode("My turn:\n", mode)
self.computer_move(board) self.computer_move(board)
@ -247,10 +247,10 @@ class TicTacToeGame(object):
# in checking.) If the computer didn't win, the user gets another turn. # in checking.) If the computer didn't win, the user gets another turn.
if self.win_conditions(board, self.triplets): if self.win_conditions(board, self.triplets):
return_string += output_mode("Game over! I've won!", mode) return_string += output_mode("Game over! I've won!", mode)
return return_string return ("computer_win", return_string)
return_string += output_mode("Your turn! Enter a coordinate or type help.", mode) return_string += output_mode("Your turn! Enter a coordinate or type help.", mode)
return return_string return ("next_turn", return_string)
# ------------------------------------- # -------------------------------------
flat_initial = sum(initial_board, []) flat_initial = sum(initial_board, [])
@ -288,6 +288,7 @@ class ticTacToeHandler(object):
storage.put(original_sender, user_board) storage.put(original_sender, user_board)
user_game = TicTacToeGame(user_board) if user_board else None user_game = TicTacToeGame(user_board) if user_board else None
move = None
if command == 'new': if command == 'new':
if user_game and not first_time(user_game.board): if user_game and not first_time(user_game.board):
return_content = "You're already playing a game! Type **@tictactoe help** or **@ttt help** to see valid inputs." return_content = "You're already playing a game! Type **@tictactoe help** or **@ttt help** to see valid inputs."
@ -297,16 +298,20 @@ class ticTacToeHandler(object):
elif command == 'help': elif command == 'help':
return_content = TicTacToeGame.detailed_help_message return_content = TicTacToeGame.detailed_help_message
elif (user_game) and TicTacToeGame.check_validity(user_game, TicTacToeGame.sanitize_move(user_game, command)): elif (user_game) and TicTacToeGame.check_validity(user_game, TicTacToeGame.sanitize_move(user_game, command)):
return_content = TicTacToeGame.tictactoe(user_game, user_board, command) move, return_content = TicTacToeGame.tictactoe(user_game, user_board, command)
storage.put(original_sender, user_board)
elif (user_game) and command == 'quit': elif (user_game) and command == 'quit':
storage.put(original_sender, None) move = "quit"
return_content = "You've successfully quit the game." return_content = "You've successfully quit the game."
else: else:
return_content = "Hmm, I didn't understand your input. Type **@tictactoe help** or **@ttt help** to see valid inputs." return_content = "Hmm, I didn't understand your input. Type **@tictactoe help** or **@ttt help** to see valid inputs."
if "Game over" in return_content or "draw" in return_content: if move is not None:
storage.put(original_sender, None) if any(reset_text in move for reset_text in ("win", "draw", "quit")):
storage.put(original_sender, None)
elif move is "next_turn":
storage.put(original_sender, user_board)
else: # "filled" => no change, user_board remains the same
pass
bot_handler.send_message(dict( bot_handler.send_message(dict(
type = 'private', type = 'private',