letter = 'X' while game.empty_squares(): if letter == 'O': square = o_player.get_move(game) else: 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('') 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) if print_game: print('It\'s a tie!') if __name__ == '__main__': x_player = SmartComputerPlayer('X') o_player = HumanPlayer('O') t = TicTacToe() play(t, x_player, o_player, print_game=True)
return letter # ends the loop and exits the game letter = 'O' if letter == 'X' else 'X' # switches player time.sleep(0.8) if print_game: print('It\'s a tie!') if __name__ == '__main__': x_player = HumanPlayer('X') print("You are the X player.") while True: a = input( "do you want to play against random computer player(r) or smart computer player(s) or against another human player(h): " ) if a == "r": o_player = RandomComputerPlayer('O') break elif a == "s": o_player = SmartComputerPlayer('O') break elif a == "h": o_player = HumanPlayer('O') break else: print('Enter valid choice!') t = TicTacToe() play(t, x_player, o_player, print_game=True)
while game.empty_squares(): if letter == 'O': square = o_player.get_move(game) else: 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('') 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( .9) #opcjonalne opóźnienie wykonanie poleceń, tutaj 0.9 sekundy if print_game: print('It\'s a tie!') if __name__ == '__main__': x_player = SmartComputerPlayer('X') #smart komputer uzywa znaku x #x_player = RandomComputerPlayer('X') # wywołac mozna tylko jedno AI, w razie potrzeb, wywolujemy tylko obiekt randomAI o_player = HumanPlayer('O') #gracz ludzki uzywa znaku o t = TicTacToe() #stworzenie obiektu play(t, x_player, o_player, print_game=True) #wywołanie funkcji play
print("It's a tie!") game.board = game.generate_board() return play(game, player2, player1) move = player.get_move(game) if game.make_move(move[0], move[1], player.piece): print(game) if game.current_winner: player.score += 1 print("{} WINS!!!".format(player.piece)) print(game.score_board(player1, player2)) game.current_winner = None prompt = str(input( "Do you want to try again (Y/N)")).strip()[0].upper() if prompt == 'Y': game.board = game.generate_board() return play(game, player2, player1) else: print("\t\tFINAL SCORE") print(game.score_board(player1, player2)) sys.exit("THANKS FOR PLAYING.") sleep(0.8) print(game) print("{} was placed at row: {}, col: {}".format( player.piece, move[0], move[1])) if __name__ == "__main__": game = TicTacToe() player1 = SmartComputerPlayer('X') player2 = SmartComputerPlayer('O') play(game, player1, player2)
if choice_letter == 'O': place = player_o.get_move(game) else: place = player_x.get_move(game) if game.make_move(place, choice_letter): if print_game: print(f"{choice_letter}, moved to {place}") game.print_board() print() # Check the winner if game.winner: if print_game: print(choice_letter + ' Wins!!!') return choice_letter # Switching players choice_letter = 'X' if choice_letter == 'O' else 'O' if print_game: print("Tie") if __name__ == '__main__': player_x = RealPlayer('X') player_y = SmartComputerPlayer('O') # player_y = ComputerPlayer('O') game_ttt = TicTacToe() play_loop(game_ttt, player_x, player_y)
else: square = o_player.get_move(game) # Make move if game.make_move(square, letter): if print_game: print(f"{letter} makes a move to square {square}") game.print_board() print(" ") if game.winner: # check if winner is not None(if someone won) if print_game: print(f"{letter} wins!") return letter # After the move completes, alternate the letter letter = "o" if letter == "x" else "x" # switch the letter # if no empty squares and winner is None, It's a tie if game.winner is None: print("It's a Tie!") return None if __name__ == "__main__": x_player = HumanPlayer("x") o_player = SmartComputerPlayer("o") # o_player = RandomComputerPlayer("o") #, if you want to win t = TicTacToe() play(t, x_player, o_player, True)