Exemple #1
0
def subtest_search_fns(gen_pattern, color, own_sqs, defcon):
    pattern = get_pattern(gen_pattern, color)
    length = pattern.size

    for i in range(SIDE_LEN):
        for j in range(SIDE_LEN):
            for d in range(NUM_DIRECTIONS):
                board = new_board()
                if apply_pattern(board, pattern, (i, j), d):
                    (row_inc, col_inc) = increments(d)
                    start = (i, j)
                    end = (i + row_inc * (length - 1),
                           j + col_inc * (length - 1))

                    subtest_search_board(board, gen_pattern, color, start, end)
                    subtest_search_point(board, gen_pattern, color, start, end)
                    subtest_search_point_own(board, gen_pattern, color,
                                             own_sqs, start, end)

                    subtest_search_board_next_sq(board, gen_pattern, color,
                                                 own_sqs, defcon, start, end)
                    subtest_search_point_next_sq(board, gen_pattern, color,
                                                 own_sqs, defcon, start, end)
                    subtest_search_point_own_next_sq(board, gen_pattern, color,
                                                     own_sqs, defcon, start,
                                                     end)
def main():
    moves = 0
    instructions()
    human, computer = x_or_o()
    curr_player = pick_first_player(human, computer)
    board = new_board()

    while moves < 9:
        if curr_player == human:
            board[human_move(
                board
            )] = human  # Set the board[human_num] to the human pick of X or O
            curr_player = computer  # Switch to the other player
            print_board(board)
            # Check to see if there's a winner after 5 moves.
            if moves >= 5:  # After 5 moves, check if there's a winner
                if winner(board) == human:
                    print("GAME OVER! You have bested me this time, Player.")
                    break
        else:
            board[comp_move(
                board, computer, human
            )] = computer  # Set the board[comp_num] to the opposite of the human pick of X or O
            curr_player = human  # Switch to the other player
            print_board(board)
            if moves >= 5:  # After 5 moves, check if there's a winner
                if winner(board) == computer:
                    print("MWHAHAHA! Computers have taken over the world.")
                    break
        moves += 1
    if moves == 9:  # If there's no winner at 9 moves, it's a tie
        print("TIE! We are of equal intelligence.")
    restart()
Exemple #3
0
def main():
    instructions()
    flag = True  # If start of game
    board = new_board()
    if flag:
        add_new_tile(board)
        add_new_tile(board)
        curr_board(board)
        flag = False
Exemple #4
0
    def __init__(self, board=new_board(), turn=BLACK, strict_stone_count=True):

        # State Integrity Checks.
        assert board.shape == (SIDE_LEN, SIDE_LEN)
        assert turn in COLORS

        black_total = 0
        white_total = 0

        for (i, j), val in np.ndenumerate(board):
            if i in (0, SIDE_LEN - 1) or j in (0, SIDE_LEN - 1):
                assert val == WALL
            elif val == BLACK:
                black_total += 1
            elif val == WHITE:
                white_total += 1
            elif val == EMPTY:
                pass
            else:
                raise Exception(f"Invalid item on board: {val}")

        if strict_stone_count:
            if black_total == white_total + 1:
                assert turn == WHITE
            elif black_total == white_total:
                assert turn == BLACK
            else:
                raise Exception(f"""Invalid number of stones on the board:\
                                Black: {black_total}, White: {white_total}""")

        # Calculate game status.
        status = Status.ONGOING

        black_won = len(search_board(board, P_WIN.pattern, BLACK)) > 0
        white_won = len(search_board(board, P_WIN.pattern, WHITE)) > 0

        if black_won and white_won:
            raise Exception("Both BLACK and WHITE cannot have won!")
        elif black_won:
            status = Status.BLACK_WON
            assert turn == WHITE
        elif white_won:
            status = Status.WHITE_WON
            assert turn == BLACK

        # Copy onto self.
        self.board = np.copy(board.astype(np.byte))
        self.turn = turn
        self.status = status
Exemple #5
0
    def __init__(self, n=3, bot_type="random"):
        self.bot_type = bot_type

        if bot_type not in ("random", "minimax"):
            raise ValueError("Only random and minimax bot available")

        self.board = new_board(n=n)
        self.it = None

        self.game_states = {-1: "Player X win!", 0: "Tie!", 1: "Player O win!"}
        self.side_value = {"O": 1, "X": -1}

        self.ascii_hello = pyfiglet.figlet_format("Tic Tac Toe Game", width=60)
        self.ascii_win = pyfiglet.figlet_format("You win!", width=60)
        self.ascii_lose = pyfiglet.figlet_format("You lose!", width=60)
        self.ascii_tie = pyfiglet.figlet_format("Tie!", width=60)
def play_cli():
    deck = CARDS.make_deck()
    CARDS.shuffle_deck(deck)
    board = BOARD.new_board(deck)

    while True:
        moves = MOVES.find_moves(board)
        BOARD.show_board(board)
        print('------------')

        if not moves:
            print('No moves. Game over.')
            break

        for i in range(len(moves)):
            print('{}: {}'.format(i + 1, moves[i]))

        print()
        g = input('Move: ')

        MOVES.make_move(moves[int(g) - 1], board)
        print()
Exemple #7
0
def main():
    instructions()
    board = new_board()
    display_board(board)
    moves = Moves()
    while True:
        player_input = input("Move: ").upper()
        if player_input == 'A':
            moves.move_left(board)
        elif player_input == 'D':
            moves.move_right(board)
        elif player_input == 'W':
            moves.move_up(board)
        elif player_input == "S":
            moves.move_down(board)
        elif player_input == "Q":
            exit()
        else:
            print("You did not press W, A, S, or D. Please try again.")
            continue
        display_board(board)
        check_win_lose(board)