def vsComp(): '''Play tictactoe against a Computer as Player 2''' #ask player 1 for name, player 2 is named Computer name1 = input('Player 1, please enter your name: ') name2 = 'Computer' #counter to keep track whether Player 1 or computer is playing counter = 0 #set starting board boardlist = [[0, 0, 0], [0, 0, 0], [0, 0, 0]] #Announces whose turn it is and makes sure the game is not over yet while not checkgame(boardlist) == 0: #if it is Player 1's turn if counter % 2 == 0: #ask Player 1 for coordinates print(name1, end=", ") x = int(input("Enter x: ")) y = int(input("Enter y: ")) #change list (map of game) according to player 1's moves boardlist = makemove(x, y, boardlist, 1) #display board after move board(boardlist) #break out of loop if last move won the game if haswon(boardlist, name1): print('Congratulations,', name1, ", you won!") break #increase counter and print an empty line counter += 1 print('\n') #if it is the computer's turn elif counter % 2 != 0: #generate a random move from the computer gametuple = randomMove(boardlist, 2) #extract list from the tuple boardlist = modifyBoard(boardlist, gametuple, 2) #display board after computer's move board(boardlist) #break out of the loop if the last move won if haswon(boardlist, name2): print("The Computer won. Nobody's perfect.") break #increase counter and print an empty line counter += 1 print('\n')
def tictactoe(): '''Plays an interactive game of tictactoe. Asks players to input coordinates that describe where the symbol is to be placed. x gives information about the row (1-3), y gives information about the column (1-3).''' # counter to keep track of whether it is player 1's or player 2's turn counter = 0 #starting board, all tiles are empty boardlist = [[0,0,0],[0,0,0],[0,0,0]] #ask players for their names name1 = input('Player 1, please enter your name: ') name2 = input('Player 2, please enter your name: ') #repeat as long as there are still free tiles while not checkgame(boardlist) == 0: #player 1's turn if counter%2 == 0: #ask for coordinates to place the symbol on the board print(name1, end=", ") x = int(input("Enter x: ")) y = int(input("Enter y: ")) #update boardlist based on user input of coordinates boardlist = makemove(x, y, boardlist, 1) #display board board(boardlist) #check whether player 1's move just won the game, if so, break out of the while loop and end the game if haswon(boardlist, 1): print('Congratulations', name1, ", you won!") break #update counter counter += 1 #player 2's turn else: #analogous to comments for player 1 print(name2, end=", ") x = int(input("Enter x: ")) y = int(input("Enter y: ")) boardlist = makemove(x, y, boardlist, 2) board(boardlist) if haswon(boardlist, 2): print('Congratulations', name2, ", you won!") break counter += 1
def smartCompMatch(list): '''BONUS WORK: start a match of tictactoe, player 1 is a smart computer, player 2 is a computer that makes random moves''' #analogous to comments in compMatch boardlist = list counter = 0 while True: if counter % 2 == 0: player = 1 gametuple = smartMove(boardlist, player) else: player = 2 gametuple = randomMove(boardlist, player) boardlist = modifyBoard(boardlist, gametuple, player) board(boardlist) counter += 1 if haswon(boardlist, player): print('Congratulations, Player', player, ", you won!") break if checkgame(boardlist) == 0: break
def compMatch(list): '''Start a computer match of tictactoe in which player 1 and player 2 are played by a computer that makes random moves''' boardlist = list counter = 0 while True: #determine whether player 1 or player 2 is playing if counter % 2 == 0: player = 1 else: player = 2 #generate coordinates and new list, all stored in a tuple gametuple = randomMove(boardlist, player) #extract list from tuple and display board according on the computers move boardlist = modifyBoard(boardlist, gametuple, player) board(boardlist) counter += 1 #break out of loop if a winning position is on the board if haswon(boardlist, player): print('Congratulations, Player', player, ", you won!") break #break if all tiles are occupied if checkgame(boardlist) == 0: break
def isOver(self): '''returns True if the game is over and False otherwise''' #if all tiles are occupied, return True, as the game is over if checkgame(self.fields) == 0: return True #if there is a winner, game is over return haswon(self.fields, self.current)
def getResult(self): '''returns the current result: 1, if player 1 won, 2 if palyer 2 won, 0 otherwise''' #if there is a winner, either 1 or 2 will be returned if haswon(self.fields, self.current) == True: #because self.current is changed after the move in the method makemove, it is actually the opposite player of self.current that won if self.current == 1: return 2 else: return 1 #if there is no winner, 0 will be returned else: return 0