Exemplo n.º 1
0
class TestPlayer(unittest.TestCase):
    def setUp(self):
        self.playerAiN = AiPlayer('noir', lvl)
        self.playerAiB = AiPlayer('blanc', lvl)
        self.playerHuN = HumanPlayer('noir')
        self.playerhuN = HumanPlayer('blanc')

    def testInitPlayer(self):
        playerN = Player('noir')
        playerB = Player('blanc')
        self.assertIsInstance(playerN, Player)
        self.assertEqual(playerN.color, -1)
        self.assertEqual(playerB.color, 1)

    def testInitHuman(self):
        player1 = HumanPlayer('noir')
        self.assertIsInstance(player1, (Player, HumanPlayer))
        self.assertEqual(player1.type, 'player')
        self.assertEqual(player1.color, -1)

    def testInitAi(self):
        self.assertIsInstance(self.playerAiB, (Player, AiPlayer))
        self.assertEqual(self.playerAiB.type, 'ai')
        self.assertEqual(self.playerAiB.color, 1)
        self.assertEqual(self.playerAiB.level, lvl)
        self.assertIsNone(self.playerAiB.grid)

    def testMoveAi(self):
        grid = Grid()
        self.playerAiN.updateGrid(grid)
        self.assertIn(self.playerAiN.askMove(),
                      self.playerAiN.actionPossible(grid, -1))
Exemplo n.º 2
0
def main():
    if __name__ == '__main__':
        game1 = Game([
            AiPlayer('Оптимус Прайм'),
            AiPlayer('Мегатрон'),
            AiPlayer('Бендер')
        ])
        game1.go()
Exemplo n.º 3
0
    def __init__(self,
                 p1_type=None,
                 p2_type=None,
                 verbose=True,
                 model1=None,
                 model2=None,
                 tree_tau=DET_TREE_TAU):

        if p1_type is None or p2_type is None:
            p1_type, p2_type = self.get_player_types()

        p1_type = p1_type[0].lower()
        p2_type = p2_type[0].lower()

        if p1_type == 'h':
            self.player_one = HumanPlayer(player_num=1)
        elif p1_type == 'g':
            self.player_one = GreedyPlayer(player_num=1)
        else:
            self.player_one = AiPlayer(player_num=1,
                                       model=model1,
                                       tree_tau=tree_tau)

        if p2_type == 'h':
            self.player_two = HumanPlayer(player_num=2)
        elif p2_type == 'g':
            self.player_two = GreedyPlayer(player_num=2)
        else:
            self.player_two = AiPlayer(
                player_num=2,
                model=(model1 if model2 is None else model2),
                tree_tau=tree_tau)

        self.cur_player = self.player_one
        self.next_player = self.player_two
        self.verbose = verbose
        self.board = Board()
Exemplo n.º 4
0
    def __init__(self, exe_path, turn_limit_s, thread):
        """
         BRIEF  Set up the UI
      """
        super().__init__()

        self.board = ChessBoard(self)

        if os.path.isfile(exe_path):
            self.player_b = AiPlayer(exe_path, turn_limit_s, Player.BLACK,
                                     thread, self.board)
            self.player_w = AiPlayer(exe_path, turn_limit_s, Player.WHITE,
                                     thread, self.board)
        else:
            self.player_b = Player(Player.BLACK, thread, self.board)
            self.player_w = Player(Player.WHITE, thread, self.board)

        player_options_b = PlayerOptions(self.player_b, self)
        board_controls = BoardControls(self.board)
        player_options_w = PlayerOptions(self.player_w, self)

        v_layout = QVBoxLayout()
        v_layout.addStretch()
        v_layout.addWidget(player_options_b)
        v_layout.addStretch()
        v_layout.addWidget(board_controls)
        v_layout.addStretch()
        v_layout.addWidget(player_options_w)
        v_layout.addStretch()

        h_layout = QHBoxLayout()
        h_layout.addWidget(self.board)
        h_layout.addLayout(v_layout)
        h_layout.addSpacing(50)

        self.setLayout(h_layout)
Exemplo n.º 5
0
    def __init__(self, size=3):
        """
        Main game logic class
        """
        # Initialise vars
        self.board = Board(size)
        self.view = View(self.board)
        self._turn_number = 0

        # Initialise players
        self.nought_player = HumanPlayer(self.board, True)
        self.cross_player = AiPlayer(
            self.board, False, algorithm=AiPlayer.RANDOM_DEFENSIVE_ALGORITHM)
        self.players = [self.nought_player, self.cross_player]

        # Shuffle player list to randomise first player
        random.shuffle(self.players)
Exemplo n.º 6
0
 def setUp(self):
     self.playerAiN = AiPlayer('noir', lvl)
     self.playerAiB = AiPlayer('blanc', lvl)
     self.playerHuN = HumanPlayer('noir')
     self.playerhuN = HumanPlayer('blanc')
Exemplo n.º 7
0
            player_symbol = 'O' if player_symbol == 'X' else 'X'

    print('It\'s a draw!')


print(
    'Select game mode. \n[1] Human vs Human\n[2] AI vs Human \n[3] Human vs AI \n[4] AI vs AI'
)
game_mode = None
player_x = None
player_o = None
while not game_mode:
    game_mode = input('Game mode: ')
    if game_mode == '1':
        player_x = HumanPlayer('X')
        player_o = HumanPlayer('O')
    elif game_mode == '2':
        player_x = AiPlayer('X')
        player_o = HumanPlayer('O')
    elif game_mode == '3':
        player_x = HumanPlayer('X')
        player_o = AiPlayer('O')
    elif game_mode == '4':
        player_x = AiPlayer('X')
        player_o = AiPlayer('O')
    else:
        print("Out of range")
        game_mode = None
game_instance = TicTacToe()
start_game(game_instance, player_x, player_o)
Exemplo n.º 8
0
    def play_game(self):
        """This function serves as the main game loop."""
        current_player = self.player_1

        # Only prints board out before selection if it's HumanPlayer's turn
        if type(current_player).__name__ == 'HumanPlayer':
            self.print_board(self.board)

        while not self.winner and not self.tie:
            time.sleep(.5)

            # Gets the move of player whose turn it is.
            move = current_player.get_move(self)

            # Executes move returned by the current player
            self.make_move(self.board, move, current_player)
            self.print_board(self.board)

            if self.winner:
                print(f'Player {self.winner} wins!')
            elif self.tie:
                print('It\'s a tie!')

            # If there is no winner or tie, switch whose turn it is.
            current_player = self.player_1 if current_player == self.player_2 else self.player_2


if __name__ == '__main__':
    t = Tic_Tac_Toe(AiPlayer('O'), RandomPlayer('X'))
    t.play_game()
Exemplo n.º 9
0

class Game:
    def __init__(self, players):
        self.players = players
        self.barrels = list(range(1, 91))
        random.shuffle(self.barrels)

    def __str__(self):
        return "{}".format(self.barrels)

    def go(self):
        i = 1
        while self.barrels:
            barrel = self.barrels.pop()
            print('*' * 40)
            print("Ход № {}. Выпал бочонок № {}. Карточки:".format(i, barrel))
            try:
                for player in self.players:
                    print(player.card)
                    player.strike(barrel)
            except (WinnerException, LoserException) as e:
                print(e)
                break
            i += 1


if __name__ == '__main__':
    game1 = Game([Player('Кирилл'), AiPlayer('Мегатрон')])
    game1.go()