From 0b2521441ffa86d3601a913d0ebc0094c62f9259 Mon Sep 17 00:00:00 2001 From: Scott Milliorn Date: Mon, 24 May 2021 14:25:41 -0700 Subject: [PATCH 1/2] print_move --- game.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/game.py b/game.py index 35f4a1a..3209192 100644 --- a/game.py +++ b/game.py @@ -1,12 +1,12 @@ """ Tic Tac Toe class + game play implementation by Kylie Ying -YouTube Kylie Ying: https://www.youtube.com/ycubed -Twitch KylieYing: https://www.twitch.tv/kylieying -Twitter @kylieyying: https://twitter.com/kylieyying -Instagram @kylieyying: https://www.instagram.com/kylieyying/ +YouTube Kylie Ying: https://www.youtube.com/ycubed +Twitch KylieYing: https://www.twitch.tv/kylieying +Twitter @kylieyying: https://twitter.com/kylieyying +Instagram @kylieyying: https://www.instagram.com/kylieyying/ Website: https://www.kylieying.com -Github: https://www.github.com/kying18 -Programmer Beast Mode Spotify playlist: https://open.spotify.com/playlist/4Akns5EUb3gzmlXIdsJkPs?si=qGc4ubKRRYmPHAJAIrCxVQ +Github: https://www.github.com/kying18 +Programmer Beast Mode Spotify playlist: https://open.spotify.com/playlist/4Akns5EUb3gzmlXIdsJkPs?si=qGc4ubKRRYmPHAJAIrCxVQ """ import math @@ -88,10 +88,7 @@ def play(game, x_player, o_player, print_game=True): square = x_player.get_move(game) if game.make_move(square, letter): - if print_game: - print(letter + ' makes a move to square {}'.format(square)) - game.print_board() - print('') + print_move(game, print_game, letter, square) if game.current_winner: if print_game: @@ -104,6 +101,12 @@ def play(game, x_player, o_player, print_game=True): if print_game: print('It\'s a tie!') +def print_move(game, print_game, letter, square): + if print_game: + print(letter + ' makes a move to square {}'.format(square)) + game.print_board() + print('') + if __name__ == '__main__': From f4c768a0d778d48bdd41d2a8b7064bf9d838aa22 Mon Sep 17 00:00:00 2001 From: Scott Milliorn Date: Mon, 24 May 2021 14:31:47 -0700 Subject: [PATCH 2/2] set_square, print_tie, reduced complexity --- game.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/game.py b/game.py index 3209192..15341dd 100644 --- a/game.py +++ b/game.py @@ -76,31 +76,37 @@ def available_moves(self): def play(game, x_player, o_player, print_game=True): - if print_game: game.print_board_nums() letter = 'X' while game.empty_squares(): - if letter == 'O': - square = o_player.get_move(game) - else: - square = x_player.get_move(game) + square = set_square(game, x_player, o_player, letter) if game.make_move(square, letter): - print_move(game, print_game, letter, square) - if game.current_winner: if print_game: print(letter + ' wins!') return letter # ends the loop and exits the game letter = 'O' if letter == 'X' else 'X' # switches player - time.sleep(.8) + print_tie(print_game) + + +def set_square(game, x_player, o_player, letter): + if letter == 'O': + square = o_player.get_move(game) + else: + square = x_player.get_move(game) + return square + + +def print_tie(print_game): if print_game: print('It\'s a tie!') + def print_move(game, print_game, letter, square): if print_game: print(letter + ' makes a move to square {}'.format(square)) @@ -108,7 +114,6 @@ def print_move(game, print_game, letter, square): print('') - if __name__ == '__main__': x_player = SmartComputerPlayer('X') o_player = HumanPlayer('O')