From babd922f21e9e7fb80e351ecb12fba4b4df01239 Mon Sep 17 00:00:00 2001 From: Robert Perrin Date: Thu, 3 Nov 2022 11:06:36 -0700 Subject: [PATCH 1/2] not working --- board.py | 62 ++++++++++++++++++++++++++++++----------------------- computer.py | 12 +++++------ player.py | 8 +++---- ttt.py | 30 ++++++++++++++------------ 4 files changed, 61 insertions(+), 51 deletions(-) diff --git a/board.py b/board.py index 86a5d99..f74430d 100644 --- a/board.py +++ b/board.py @@ -1,29 +1,37 @@ import os -class Board(): - def CheckForWinner(self, winCondList, moveCount, thereIsWinner): - for list in winCondList: - if list[0] == list[1] and list[0] == list[2]: - if list[0] == "X": - print("You are the winner!") - thereIsWinner = True - exit() - elif list[0] == "O": - print("Get wrecked nerd!") - thereIsWinner = True - exit() - else: - if moveCount == 9 and thereIsWinner == False: - print("It's a draw") - exit() - return winCondList - - def PrintBoard(self, positions): - print(positions[1] + "|" + positions[2] + "|" + positions[3]) - print("-----") - print(positions[4] + "|" + positions[5] + "|" + positions[6]) - print("-----") - print(positions[7] + "|" + positions[8] + "|" + positions[9]) - print() - def ClearBoard(self): - clear = lambda : os.system('tput reset') \ No newline at end of file +def CheckForWinner(winCondList, winCondPlayer, winCondComp, win): + topRow = [1,2,3] + midRow = [4,5,6] + bottomRow = [7,8,9] + leftCol = [1,4,7] + midCol = [2,5,8] + rightCol = [3,6,9] + diagnal1 = [1,5,9] + diagnal2 = [3,5,7] + winCondList = [topRow, midRow, bottomRow, leftCol, midCol, rightCol, diagnal1, diagnal2] + for list in winCondList: + if list in winCondPlayer: + win = 1 + print("You are the winner!") + quit() + elif list in winCondComp: + win = 1 + print("Youre are not the winner - in fact not even close...I mean it's binary") + quit() + else: + win = 0 + pass + return win + + +def PrintBoard(positions): + print(positions[1] + "|" + positions[2] + "|" + positions[3]) + print("-----") + print(positions[4] + "|" + positions[5] + "|" + positions[6]) + print("-----") + print(positions[7] + "|" + positions[8] + "|" + positions[9]) + print() + +def ClearBoard(): + clear = lambda : os.system('tput reset') \ No newline at end of file diff --git a/computer.py b/computer.py index 78a1c0e..9b49193 100644 --- a/computer.py +++ b/computer.py @@ -3,7 +3,7 @@ class Computer(): #picks a 'random' move when no block or win move is available #the middle space has the highest weight followed by the four corners - def ComputerStarterMoves (self, positions, winCondList): + def ComputerStarterMoves (self, positions, winCondComp): priorityMoveAvailable = False priorityMoves = [5,1,3,7,9] for value in priorityMoves: @@ -17,10 +17,10 @@ def ComputerStarterMoves (self, positions, winCondList): while positions[compMove] == "O" or positions[compMove] == "X": compMove = random.choice(list(positions)) positions[compMove] = "O" - winCondList = [list(map(lambda y: y if y != compMove else 'O', i)) for i in winCondList] - return winCondList + winCondComp.append(compMove) + return winCondComp - def ComputerMoveWinOrBlock (self, positions, noBestMove, userTurn, winCondList): + def ComputerMoveWinOrBlock (self, positions, noBestMove, userTurn, winCondComp, winCondList): for list in winCondList: xCounter = 0 oCounter = 0 @@ -52,6 +52,6 @@ def ComputerMoveWinOrBlock (self, positions, noBestMove, userTurn, winCondList): noBestMove = False userTurn = True if noBestMove == False: - winCondList = [list(map(lambda y: y if y != compMove else 'O', i)) for i in winCondList] - return winCondList + winCondComp.append(compMove) + return winCondComp \ No newline at end of file diff --git a/player.py b/player.py index fa83507..70e33e3 100644 --- a/player.py +++ b/player.py @@ -1,10 +1,10 @@ import board class Player(): - def GamePrep (self): + def GamePrep(self): PlayerName = input("Please enter your name: ") PlayersToken = input("Which piece would you like to plays, X or O?") - def PlayerMove (self, positions, winCondList): + def PlayerMove(self, positions, winCondPlayer): gettingInput = True while gettingInput: try: @@ -19,5 +19,5 @@ def PlayerMove (self, positions, winCondList): selection = int(input("that move is invalid please choose a legal move: ")) else: positions[selection] = "X" - winCondList = [list(map(lambda x: x if x != selection else 'X', i)) for i in winCondList] - return winCondList \ No newline at end of file + winCondPlayer.append(selection) + return winCondPlayer \ No newline at end of file diff --git a/ttt.py b/ttt.py index 1ae8611..91c430d 100644 --- a/ttt.py +++ b/ttt.py @@ -1,14 +1,15 @@ -import board import player import computer import random +from board import CheckForWinner, PrintBoard, ClearBoard -thereIsWinner = False #make board positions = {1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9"} #lists for 3 consecutive moves +winCondPlayer = [] +winCondComp = [] topRow = [1,2,3] midRow = [4,5,6] bottomRow = [7,8,9] @@ -18,31 +19,32 @@ diagnal1 = [1,5,9] diagnal2 = [3,5,7] winCondList = [topRow, midRow, bottomRow, leftCol, midCol, rightCol, diagnal1, diagnal2] +win = 0 #start game cycle -board.Board().PrintBoard(positions) +player.Player().GamePrep() +PrintBoard(positions) userTurn = True moveCount = 0 -while moveCount < 10: +while moveCount < 10 and win == 0: if userTurn == True: - player.Player().PlayerMove(positions, winCondList) - board.Board().PrintBoard(positions) + winCondPlayer = player.Player().PlayerMove(positions, winCondPlayer) + PrintBoard(positions) + print(winCondPlayer) + print(winCondComp) moveCount += 1 - board.Board().CheckForWinner(winCondList, moveCount, thereIsWinner) + win = CheckForWinner(win, winCondPlayer, winCondComp, moveCount) userTurn = False - else: noBestMove = True - computer.Computer().ComputerMoveWinOrBlock(positions, noBestMove, userTurn, winCondList) - - + computer.Computer().ComputerMoveWinOrBlock(positions, noBestMove, userTurn, winCondComp, winCondList) if noBestMove == True: - computer.Computer().ComputerStarterMoves(positions, winCondList) + winCondComp = computer.Computer().ComputerStarterMoves(positions, winCondComp) noBestMove = False - board.Board().PrintBoard(positions) - board.Board().CheckForWinner(winCondList, moveCount, thereIsWinner) + PrintBoard(positions) + win = CheckForWinner(win, moveCount, winCondPlayer, winCondComp) userTurn = True moveCount += 1 From 37bb638127455dbe1e3bb5392b8e0a0ac9058eff Mon Sep 17 00:00:00 2001 From: Robert Perrin Date: Thu, 3 Nov 2022 14:17:08 -0700 Subject: [PATCH 2/2] refactoring --- board.py | 25 +++++++------ computer.py | 100 +++++++++++++++++++++++++--------------------------- ttt.py | 30 +++++++++------- 3 files changed, 78 insertions(+), 77 deletions(-) diff --git a/board.py b/board.py index f74430d..22912ce 100644 --- a/board.py +++ b/board.py @@ -1,6 +1,8 @@ import os -def CheckForWinner(winCondList, winCondPlayer, winCondComp, win): +def CheckForWinner(winCond, win, moveCount): + winCond = list(map(int, str(winCond))) + winCond = sorted(winCond) topRow = [1,2,3] midRow = [4,5,6] bottomRow = [7,8,9] @@ -10,19 +12,16 @@ def CheckForWinner(winCondList, winCondPlayer, winCondComp, win): diagnal1 = [1,5,9] diagnal2 = [3,5,7] winCondList = [topRow, midRow, bottomRow, leftCol, midCol, rightCol, diagnal1, diagnal2] - for list in winCondList: - if list in winCondPlayer: - win = 1 - print("You are the winner!") - quit() - elif list in winCondComp: - win = 1 - print("Youre are not the winner - in fact not even close...I mean it's binary") - quit() - else: - win = 0 - pass + if winCond is not None: + result = all(elem in winCondList for elem in winCond) + if result: + print(result) + win = 1 return win + else: + print("no win") + + def PrintBoard(positions): diff --git a/computer.py b/computer.py index 9b49193..80a7320 100644 --- a/computer.py +++ b/computer.py @@ -1,57 +1,55 @@ import board import random -class Computer(): #picks a 'random' move when no block or win move is available #the middle space has the highest weight followed by the four corners - def ComputerStarterMoves (self, positions, winCondComp): - priorityMoveAvailable = False - priorityMoves = [5,1,3,7,9] - for value in priorityMoves: - if positions[value] != "X" and positions[value] != "O": - compMove = value - positions[compMove] = "O" - priorityMoveAvailable = True - break - if priorityMoveAvailable == False: +def ComputerStarterMoves (positions, winCond): + priorityMoveAvailable = False + priorityMoves = [5,1,3,7,9] + for value in priorityMoves: + if positions[value] != "X" and positions[value] != "O": + compMove = value + positions[compMove] = "O" + priorityMoveAvailable = True + break + if priorityMoveAvailable == False: + compMove = random.choice(list(positions)) + while positions[compMove] == "O" or positions[compMove] == "X": compMove = random.choice(list(positions)) - while positions[compMove] == "O" or positions[compMove] == "X": - compMove = random.choice(list(positions)) - positions[compMove] = "O" - winCondComp.append(compMove) - return winCondComp - - def ComputerMoveWinOrBlock (self, positions, noBestMove, userTurn, winCondComp, winCondList): - for list in winCondList: - xCounter = 0 - oCounter = 0 - if userTurn == True: - break - for value in list: - if value == "O": - oCounter += 1 - if oCounter == 2: - for index, item in enumerate(list): - if list[index] != "O" and list[index] != "X": - compMove = list[index] - positions[list[index]] = "O" - noBestMove = False - userTurn = True - for list in winCondList: - xCounter = 0 - oCounter = 0 - if userTurn == True: - break - for value in list: - if value == "X": - xCounter += 1 - if xCounter == 2: - for index, item in enumerate(list): - if list[index] != "O" and list[index] != "X": - compMove = list[index] - positions[list[index]] = "O" - noBestMove = False - userTurn = True - if noBestMove == False: - winCondComp.append(compMove) + positions[compMove] = "O" + winCondComp = list(map(int, str(compMove))) return winCondComp - \ No newline at end of file + +def ComputerMoveWinOrBlock (positions, noBestMove, userTurn, winCond, winCondList): + for list in winCondList: + xCounter = 0 + oCounter = 0 + if userTurn == True: + break + for value in list: + if value == "O": + oCounter += 1 + if oCounter == 2: + for index, item in enumerate(list): + if list[index] != "O" and list[index] != "X": + compMove = list[index] + positions[list[index]] = "O" + noBestMove = False + userTurn = True + for list in winCondLitxs: + xCounter = 0 + oCounter = 0 + if userTurn == True: + break + for value in list: + if value == "X": + xCounter += 1 + if xCounter == 2: + for index, item in enumerate(list): + if list[index] != "O" and list[index] != "X": + compMove = list[index] + positions[list[index]] = "O" + noBestMove = False + userTurn = True + if noBestMove == False: + winCond.append(compMove) + return winCond \ No newline at end of file diff --git a/ttt.py b/ttt.py index 91c430d..2633674 100644 --- a/ttt.py +++ b/ttt.py @@ -1,15 +1,13 @@ import player -import computer -import random from board import CheckForWinner, PrintBoard, ClearBoard +from computer import ComputerStarterMoves, ComputerMoveWinOrBlock #make board positions = {1:"1",2:"2",3:"3",4:"4",5:"5",6:"6",7:"7",8:"8",9:"9"} #lists for 3 consecutive moves -winCondPlayer = [] -winCondComp = [] +winCond = [] topRow = [1,2,3] midRow = [4,5,6] bottomRow = [7,8,9] @@ -26,25 +24,31 @@ PrintBoard(positions) userTurn = True moveCount = 0 - - -while moveCount < 10 and win == 0: +while win == 0: if userTurn == True: - winCondPlayer = player.Player().PlayerMove(positions, winCondPlayer) + winCondPlayer = player.Player().PlayerMove(positions, winCond) PrintBoard(positions) print(winCondPlayer) - print(winCondComp) moveCount += 1 - win = CheckForWinner(win, winCondPlayer, winCondComp, moveCount) + winCond = winCondPlayer + try: + win = CheckForWinner(win, moveCount, winCond) + except: + pass userTurn = False else: noBestMove = True - computer.Computer().ComputerMoveWinOrBlock(positions, noBestMove, userTurn, winCondComp, winCondList) if noBestMove == True: - winCondComp = computer.Computer().ComputerStarterMoves(positions, winCondComp) + winCondComp = ComputerStarterMoves(positions) noBestMove = False + else: winCondComp = ComputerMoveWinOrBlock(positions, noBestMove, userTurn, winCond, winCondList) + print(winCondComp) PrintBoard(positions) - win = CheckForWinner(win, moveCount, winCondPlayer, winCondComp) + winCond = winCondComp + try: + win = CheckForWinner(win, moveCount, winCond) + except: + pass userTurn = True moveCount += 1