Example #1
0
    def _game_turn(self, board, human_player: str, goes_first: int):
        player = self.side_value[human_player]
        new_board = None

        if goes_first == player:
            if self.it == 0:
                print("Human goes first!")
                print("-" * 25)

            new_board = self._human_turn(board, player)
            print_board(new_board)

            if check_win(new_board, player):
                return player
            if check_tie(new_board):
                return 0

            new_board = self._bot_turn(new_board, player * -1)
            print_board(new_board)

            if check_win(new_board, player * -1):
                return player * -1
            if check_tie(new_board):
                return 0

        else:
            if self.it == 0:
                print("Bot goes first!")
                print("-" * 25)
            new_board = self._bot_turn(board, player * -1)
            print_board(new_board)

            if check_win(new_board, player * -1):
                return player * -1
            if check_tie(new_board):
                return 0

            new_board = self._human_turn(new_board, player)
            print_board(new_board)

            if check_win(new_board, player):
                return player
            if check_tie(new_board):
                return 0

        return new_board
Example #2
0
def click_handler(mouse_x, mouse_y):
    """
    handles clicks and calls appropriate function
    :return:
    """
    if turn.click_nb == 1:
        print("CLICK 1")
        clicked_square, clicked_square_ix = board.get_square_by_coords(
            mouse_x, mouse_y)
        pawn = clicked_square.occupant
        if pawn:
            print("PAWN WAS CLICKED")
            if pawn.color == turn.turn_color:
                print("GOOD PAWN WAS CLICKED")
                turn.clicked_pawn = pawn
                turn.click_nb = 2
            else:
                print("BAD PAWN...")
                # Bad pawn
                return False
        else:
            print("EMPTY SQUARE")
            # Empty square
            return False
    else:
        print("CLICK 2")
        start = board.get_pawn_ix(turn.clicked_pawn)
        dst_square, dst_ix = board.get_square_by_coords(mouse_x, mouse_y)
        clicked_pawn = dst_square.occupant

        if clicked_pawn and clicked_pawn.color == turn.turn_color:
            turn.clicked_pawn = clicked_pawn
            return True

        # check if it's a chain kill --> do this pawn has the right move ?
        if turn.chain_kill_pawn and not turn.chain_kill_pawn == clicked_pawn:
            return False

        move_result = board.move_pawn(start, dst_ix)
        if move_result == 1:
            print("PAWN MOVED")
            turn.next_turn()
        elif move_result == 2:
            turn.chain_kill_pawn = clicked_pawn
            if not board.can_eat(dst_ix)[0]:
                turn.next_turn()
        elif move_result == 0:
            print("BAD MOVE")
            return False

        win, winner = board.check_win()
        if win:
            print(f"{winner} win !")

    return True
Example #3
0
 def test_check_win(self):
     test_board = [['X', 'O', 'O'], ['O', 'X', 'X'], [' ', 'X', ' ']]
     board.place_piece(test_board, (2, 2), True)
     self.assertTrue(board.check_win(test_board, (2, 2), True))
     self.assertFalse(board.check_win(test_board, (2, 2), False))
     board.remove_piece(test_board, (2, 2))
     board.place_piece(test_board, (2, 0), False)
     self.assertFalse(board.check_win(test_board, (2, 0), True))
     self.assertFalse(board.check_win(test_board, (2, 0), False))
     board.place_piece(test_board, (1, 0), True)
     board.place_piece(test_board, (1, 1), False)
     self.assertFalse(board.check_win(test_board, (1, 1), True))
     self.assertTrue(board.check_win(test_board, (1, 1), False))
     # impossible scenario in the actual game (both players win)
     board.place_piece(test_board, (2, 0), True)
     board.place_piece(test_board, (2, 1), False)
     self.assertTrue(board.check_win(test_board, (2, 0), True))
     self.assertTrue(board.check_win(test_board, (2, 1), False))
Example #4
0
  def search(self, board, depth, player_num, heuristic):
    if player_num == 1:
      opp_num = 2
    else:
      opp_num = 1

    # Check if leaf
    if depth == 0 or len(board.valid_moves()) == 0 or board.check_win(player_num)[0] or board.check_win(opp_num)[0]:
      return heuristic(board, player_num)

    # Get all possible board states from the given board
    possible_moves = []
    for col in board.valid_moves():
      board_copy = copy.deepcopy(board)
      board_copy.add(col, player_num)
      possible_moves.append(board_copy)

    # Run search recursively
    h = float("-inf")
    for move in possible_moves:
      h = max(h, -self.search(move, depth - 1, opp_num, heuristic))
    return h
Example #5
0
def game_loop(board, is_player1, use_bot, diff):
    global is_won, is_tie, moves_remaining
    while not (is_won or is_tie):
        print("Moves left: %d" % moves_remaining)
        pos = None
        if (not is_player1) and use_bot:
            pos = minimax.best_move(board, False, diff, moves_remaining)
        else:
            bd.print_board(board)
            pos = bd.get_input(board)
        bd.place_piece(board, pos, is_player1)
        moves_remaining = moves_remaining - 1
        is_won = bd.check_win(board, pos, is_player1)
        is_tie = (not is_won) and (moves_remaining == 0)
        if (is_won or is_tie):
            break
        is_player1 = not is_player1
    bd.print_board(board)
    if is_tie:
        print("TIE!")
    else:
        player = 1 if is_player1 else 2
        print("Player %d is the winner!" % player)
Example #6
0
 def has_won(self, move):
     is_player1 = 1 if self.gamestate == X_TURN_STATE else 0
     return board.check_win(self.game_board, move, is_player1)
Example #7
0

board.print_board(peices)

gameRunning = 1

while gameRunning == 1:
    move = raw_input("Player X: ")
    result = board.add_character_to_2d_array_with_linear_number(peices, move, "X")
    if result != 0:
        peices = result
        board.print_board(peices)
    else:
        print "bad boy. no stealing spots! you forfeit your turn."

    result = board.check_win(peices, "X")
    if result == 1:
        print "Player X wins"
        sys.exit()

    move = raw_input("Player O: ")
    result = board.add_character_to_2d_array_with_linear_number(peices, move, "O")

    if result != 0:
        peices = result
        board.print_board(peices)
    else:
        print "bad boy. no stealing spots! you forfeit your turn."

    result = board.check_win(peices, "O")
    if result == 1: