Ejemplo n.º 1
0
            if game.current_winner:
                if print_game:
                    print(f'{letter} won! Game over.')
                return letter
            letter = 'O' if letter == 'X' else 'X'
    if print_game:  # eğer bu döngü bitmişse ve 94. satırdaki koşullu ifade, return yapmamışsa
        # (eğer kazanan olmazsa game.current_winner None çıktısı verir) bu durumda oyun beraberedir
        # çünkü kareler bitmiş demektir.
        print('It\'s a tie.')


if __name__ == '__main__':
    # x_wins = 0
    # o_wins = 0
    # ties = 0
    # for _ in range(100):
    #     player1 = GeniusComputerPlayer('X')
    #     player2 = RandomComputerPlayer('O')
    #     t = TicTacToe()
    #     result = play(t, player1, player2, print_game=False)
    #     if result == 'X':
    #         x_wins += 1
    #     elif result == 'O':
    #         o_wins += 1
    #     else:
    #         ties += 1
    # print(f'X won {x_wins} times, O won {o_wins} times and it was tie for {ties} times.')
    player1 = GeniusComputerPlayer('X')
    player2 = HumanPlayer('O')
    game = TicTacToe()
    play(game, player1, player2, print_game=True)
Ejemplo n.º 2
0
        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 + f' makes a move to square {square}')
                game.print_board()  # reprint the board
                print('')

            if game.current_winner:
                if print_game:
                    print(letter + ' wins!!')
                return letter

            letter = 'O' if letter == 'X' else 'X'  # switch players

        # break in play to slow game down slightly
        time.sleep(0.8)

    if print_game:
        print('It\'s a tie!')


if __name__ == '__main__':
    x_player = HumanPlayer('X')
    o_player = GeniusComputerPlayer('O')
    t = TicTacToe()
    print('begin game')
    play(t, x_player, o_player, print_game=True)
Ejemplo n.º 3
0
 def setUp(self):
     self.game = TicTacToe()
     self.human_player = HumanPlayer("X")
     self.computer_player = GeniusComputerPlayer("O")
Ejemplo n.º 4
0
        # Print the indices of the board
        if print_game:
            print("\nBoard indices:")
            self.print_board_indices()
        # Play until there is a winner or the board is full.
        while self.current_winner == None and not self.is_board_full():
            if print_game:
                print()
            # 1. Current player makes a move
            self.make_move(self.current_player)
            # 2. Check if the game is won
            if self.get_winner() != None:
                self.current_winner = self.current_player
            # 3. Print the board
            if print_game:
                self.print_board(self.board)
            # 4. Change current player
            self.current_player = o_player if self.current_player == x_player else x_player

        print()
        if self.current_winner != None:
            print(f"{self.current_winner.letter} is the winner!")
        else:
            print("It's a tie!")


if __name__ == "__main__":
    x_player = GeniusComputerPlayer("X")
    o_player = HumanPlayer("O")
    game = TicTacToe(x_player, o_player)
    game.play()
Ejemplo n.º 5
0
            "\nC - Computador - Computador\n").capitalize()

        if type == "A":
            x_player = HumanPlayer("X")
            o_player = HumanPlayer("O")
        elif type == "B":
            dificuldade = input(
                "Qual nível de computador você quer enfrentar?\nA - Fácil\nB - Difícil\n"
            ).capitalize()

            if dificuldade == "A":
                x_player = HumanPlayer("X")
                o_player = RandomComputerPlayer("O")
            else:
                x_player = HumanPlayer("X")
                o_player = GeniusComputerPlayer("O")
        else:
            nivel_pc1 = input(
                "Qual nível do computador 1?\nA - Fácil\nB - Difícil\n"
            ).capitalize()
            nivel_pc2 = input(
                "Qual nível do computador 2?\nA - Fácil\nB - Difícil\n"
            ).capitalize()

            x_player = RandomComputerPlayer(
                "X") if nivel_pc1 == "A" else GeniusComputerPlayer("X")
            o_player = RandomComputerPlayer(
                "O") if nivel_pc2 == "A" else GeniusComputerPlayer("O")

        t = TicTacToe()
        play(t, x_player, o_player, print_game=True)
Ejemplo n.º 6
0
        else:
            local = x_jogador.pega_jogo(jogo)
        #função que faz o movimento
        if jogo.faz_movimento(local, letra):
            if print_jogo:
                print(letra + f" faz o movimento para {local}")
                jogo.print_tela()
                print('') #linha vazia
            
                if jogo.enquanto_vencedor:
                    if print_jogo:
                        print(letra + " GANHOU!!!")
                        input()
                    return letra
            #altera a letra depois da jogada
            #muda o jogador
            if letra =='X':
                letra = 'O'
            else:
                letra = 'X'

    #pequena pausa
    time.sleep(0.8)
    if print_jogo:
        print("Empate")

if __name__ == '__main__':
    x_jogador = humanoplayer('X')
    o_jogador = GeniusComputerPlayer('O')
    t = velha()
    jogar(t, x_jogador, o_jogador, print_jogo=True)
Ejemplo n.º 7
0
        if letter == 'O':
            square = o_player.get_move(game)
        else:
            square = x_player.get_move(game)

        # Function to make a move
        if game.make_move(square, letter):
            if print_game:
                print(f"{letter} makes a move to square {square+1}")
                game.print_board()
                print('')

            if game.current_winner:
                if print_game:
                    print(f"{letter} wins")
                return letter

        letter = 'O' if letter == 'X' else 'X'

        # Tiny wait
        time.sleep(0.8)
    if ((print_game)):
        print("It's a tie")


if __name__ == '__main__':
    x_player = HumanPlayer("X")
    o_player = GeniusComputerPlayer("O")
    t = TicTacToe()
    play(t, x_player, o_player, print_game=True)
Ejemplo n.º 8
0
            break
        else:
            print('Invalid input. Please try again.\n')

    print()

    while True:
        p2 = input('Press (h) for human, (m) for machine. Player 2 will be:')
        p2 = p2.upper()
        if p2 == 'H' or p2 == 'M':
            break
        else:
            print('Invalid input. Please try again.\n')

    print()

    player1 = HumanPlayer('X') if p1 == 'H' else GeniusComputerPlayer('X')
    player2 = HumanPlayer('O') if p2 == 'H' else GeniusComputerPlayer('O')

    t = TicTacToe()

    winner = play(t, player1, player2)

    t.print_board()
    print()

    if winner == 'X' or winner == 'O':
        print(winner + ' player is the winner!!!')
    else:
        print('Tie!!!')