From 61159b2a3c38cb1f8f74ed02eb236d53aa94a2ad Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Thu, 29 Mar 2018 19:25:55 +0200 Subject: [PATCH 1/7] added random seed in function whoGoesFirst() --- TicTacToe.py | 1 + 1 file changed, 1 insertion(+) diff --git a/TicTacToe.py b/TicTacToe.py index 2fc184ee829..8945b6c1265 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -28,6 +28,7 @@ def inputPlayerLetter(): def whoGoesFirst(): # Randomly choose the player who goes first. + random.seed() if random.randint(0, 1) == 0: return 'computer' else: From 51091ba1d3436607b727d8eb8c1c26acea7c7e66 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Thu, 29 Mar 2018 19:31:46 +0200 Subject: [PATCH 2/7] added a check for the makeMove()-function --- TicTacToe.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/TicTacToe.py b/TicTacToe.py index 8945b6c1265..432d4647b8a 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -40,7 +40,10 @@ def playAgain(): return input().lower().startswith('y') def makeMove(board, letter, move): - board[move] = letter + if board[move] == ' ': + board[move] = letter + else: + raise Exception("makeMove: the field is not empty!") def isWinner(bo, le): # Given a board and a player's letter, this function returns True if that player has won. From 3e548fdbd5280f0983698598f08898b53936a7b9 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Thu, 29 Mar 2018 19:41:41 +0200 Subject: [PATCH 3/7] added random seed in function chooseRandomMoveFromList(...) --- TicTacToe.py | 1 + 1 file changed, 1 insertion(+) diff --git a/TicTacToe.py b/TicTacToe.py index 432d4647b8a..dcfa99e5ed4 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -81,6 +81,7 @@ def getPlayerMove(board): def chooseRandomMoveFromList(board, movesList): # Returns a valid move from the passed list on the passed board. # Returns None if there is no valid move. + random.seed() possibleMoves = [] for i in movesList: if isSpaceFree(board, i): From 36ef76d22e74c14f51a54c1f546d32ef09b2710d Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Thu, 29 Mar 2018 19:43:43 +0200 Subject: [PATCH 4/7] simple change in function chooseRandomMoveFromList(...) --- TicTacToe.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TicTacToe.py b/TicTacToe.py index dcfa99e5ed4..a5ffcf6c315 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -87,7 +87,7 @@ def chooseRandomMoveFromList(board, movesList): if isSpaceFree(board, i): possibleMoves.append(i) - if len(possibleMoves) != 0: + if len(possibleMoves) > 0: return random.choice(possibleMoves) else: return None From 7248a3f6a4953690a6644d310a22ab481b6d5562 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Thu, 29 Mar 2018 19:47:05 +0200 Subject: [PATCH 5/7] added a main function --- TicTacToe.py | 90 +++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/TicTacToe.py b/TicTacToe.py index a5ffcf6c315..72c14a9d70f 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -135,53 +135,55 @@ def isBoardFull(board): return False return True - -print('Welcome to Tic Tac Toe!') - -while True: - # Reset the board - theBoard = [' '] * 10 - playerLetter, computerLetter = inputPlayerLetter() - turn = whoGoesFirst() - print('The ' + turn + ' will go first.') - gameIsPlaying = True - - while gameIsPlaying: - if turn == 'player': - # Player's turn. - drawBoard(theBoard) - move = getPlayerMove(theBoard) - makeMove(theBoard, playerLetter, move) - - if isWinner(theBoard, playerLetter): +def main(): + print('Welcome to Tic Tac Toe!') + + while True: + # Reset the board + theBoard = [' '] * 10 + playerLetter, computerLetter = inputPlayerLetter() + turn = whoGoesFirst() + print('The ' + turn + ' will go first.') + gameIsPlaying = True + + while gameIsPlaying: + if turn == 'player': + # Player's turn. drawBoard(theBoard) - print('Hooray! You have won the game!') - gameIsPlaying = False - else: - if isBoardFull(theBoard): + move = getPlayerMove(theBoard) + makeMove(theBoard, playerLetter, move) + + if isWinner(theBoard, playerLetter): drawBoard(theBoard) - print('The game is a tie!') - break + print('Hooray! You have won the game!') + gameIsPlaying = False else: - turn = 'computer' - - else: - # Computer's turn. - move = getComputerMove(theBoard, computerLetter) - makeMove(theBoard, computerLetter, move) - - if isWinner(theBoard, computerLetter): - drawBoard(theBoard) - print('The computer has beaten you! You lose.') - gameIsPlaying = False + if isBoardFull(theBoard): + drawBoard(theBoard) + print('The game is a tie!') + break + else: + turn = 'computer' + else: - if isBoardFull(theBoard): + # Computer's turn. + move = getComputerMove(theBoard, computerLetter) + makeMove(theBoard, computerLetter, move) + + if isWinner(theBoard, computerLetter): drawBoard(theBoard) - print('The game is a tie!') - break + print('The computer has beaten you! You lose.') + gameIsPlaying = False else: - turn = 'player' - - if not playAgain(): - break - + if isBoardFull(theBoard): + drawBoard(theBoard) + print('The game is a tie!') + break + else: + turn = 'player' + + if not playAgain(): + break + +if __name__ == "__main__": + main() \ No newline at end of file From 59c7ff41b1eede0c925e76871c7f454e225c7df1 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Thu, 29 Mar 2018 19:57:45 +0200 Subject: [PATCH 6/7] added numbered play field --- TicTacToe.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/TicTacToe.py b/TicTacToe.py index 72c14a9d70f..b8ac7e8c161 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -40,7 +40,7 @@ def playAgain(): return input().lower().startswith('y') def makeMove(board, letter, move): - if board[move] == ' ': + if isSpaceFree(board,move): board[move] = letter else: raise Exception("makeMove: the field is not empty!") @@ -68,7 +68,7 @@ def getBoardCopy(board): def isSpaceFree(board, move): # Return true if the passed move is free on the passed board. - return board[move] == ' ' + return board[move].isdigit() def getPlayerMove(board): # Let the player type in his move. @@ -141,6 +141,8 @@ def main(): while True: # Reset the board theBoard = [' '] * 10 + for i in range(9,0,-1): + theBoard[i] = str(i) playerLetter, computerLetter = inputPlayerLetter() turn = whoGoesFirst() print('The ' + turn + ' will go first.') From 135a9df1c8371c83ed12d0cf5629ac96f9156327 Mon Sep 17 00:00:00 2001 From: Christian Bender Date: Thu, 29 Mar 2018 20:01:10 +0200 Subject: [PATCH 7/7] added random seed in main() --- TicTacToe.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/TicTacToe.py b/TicTacToe.py index b8ac7e8c161..1245206dcac 100644 --- a/TicTacToe.py +++ b/TicTacToe.py @@ -28,7 +28,6 @@ def inputPlayerLetter(): def whoGoesFirst(): # Randomly choose the player who goes first. - random.seed() if random.randint(0, 1) == 0: return 'computer' else: @@ -81,7 +80,6 @@ def getPlayerMove(board): def chooseRandomMoveFromList(board, movesList): # Returns a valid move from the passed list on the passed board. # Returns None if there is no valid move. - random.seed() possibleMoves = [] for i in movesList: if isSpaceFree(board, i): @@ -137,7 +135,7 @@ def isBoardFull(board): def main(): print('Welcome to Tic Tac Toe!') - + random.seed() while True: # Reset the board theBoard = [' '] * 10