|
| 1 | +import copy |
| 2 | + |
| 3 | +# Reversi Board Size |
| 4 | +BOARD_SIZE = 8 |
| 5 | + |
| 6 | +# Define player colors |
| 7 | +EMPTY = 0 |
| 8 | +BLACK = 1 |
| 9 | +WHITE = 2 |
| 10 | + |
| 11 | +# Define directions to explore in the board |
| 12 | +DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] |
| 13 | + |
| 14 | +def create_board(): |
| 15 | + return [[EMPTY for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)] |
| 16 | + |
| 17 | +def print_board(board): |
| 18 | + print(" " + " ".join(str(i) for i in range(BOARD_SIZE))) |
| 19 | + for i in range(BOARD_SIZE): |
| 20 | + print(f"{i} |" + " ".join(str(board[i][j]) for j in range(BOARD_SIZE))) |
| 21 | + |
| 22 | +def is_valid_move(board, player, row, col): |
| 23 | + if board[row][col] != EMPTY: |
| 24 | + return False |
| 25 | + |
| 26 | + for dr, dc in DIRECTIONS: |
| 27 | + r, c = row + dr, col + dc |
| 28 | + while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and board[r][c] != EMPTY and board[r][c] != player: |
| 29 | + r, c = r + dr, c + dc |
| 30 | + if 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and board[r][c] == player: |
| 31 | + return True |
| 32 | + return False |
| 33 | + |
| 34 | +def make_move(board, player, row, col): |
| 35 | + if not is_valid_move(board, player, row, col): |
| 36 | + return False |
| 37 | + |
| 38 | + board[row][col] = player |
| 39 | + for dr, dc in DIRECTIONS: |
| 40 | + r, c = row + dr, col + dc |
| 41 | + to_flip = [] |
| 42 | + while 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and board[r][c] != EMPTY and board[r][c] != player: |
| 43 | + to_flip.append((r, c)) |
| 44 | + r, c = r + dr, c + dc |
| 45 | + if 0 <= r < BOARD_SIZE and 0 <= c < BOARD_SIZE and board[r][c] == player: |
| 46 | + for flip_row, flip_col in to_flip: |
| 47 | + board[flip_row][flip_col] = player |
| 48 | + break |
| 49 | + return True |
| 50 | + |
| 51 | +def get_valid_moves(board, player): |
| 52 | + valid_moves = [] |
| 53 | + for i in range(BOARD_SIZE): |
| 54 | + for j in range(BOARD_SIZE): |
| 55 | + if is_valid_move(board, player, i, j): |
| 56 | + valid_moves.append((i, j)) |
| 57 | + return valid_moves |
| 58 | + |
| 59 | +def count_discs(board): |
| 60 | + black_count = sum(row.count(BLACK) for row in board) |
| 61 | + white_count = sum(row.count(WHITE) for row in board) |
| 62 | + return black_count, white_count |
| 63 | + |
| 64 | +def evaluate_board(board, player): |
| 65 | + black_count, white_count = count_discs(board) |
| 66 | + if player == BLACK: |
| 67 | + return black_count - white_count |
| 68 | + else: |
| 69 | + return white_count - black_count |
| 70 | + |
| 71 | +def minimax(board, depth, player, alpha, beta, maximizing_player): |
| 72 | + if depth == 0: |
| 73 | + return evaluate_board(board, player) |
| 74 | + |
| 75 | + valid_moves = get_valid_moves(board, player) |
| 76 | + if maximizing_player: |
| 77 | + max_eval = float('-inf') |
| 78 | + for row, col in valid_moves: |
| 79 | + new_board = copy.deepcopy(board) |
| 80 | + make_move(new_board, player, row, col) |
| 81 | + eval = minimax(new_board, depth - 1, player, alpha, beta, False) |
| 82 | + max_eval = max(max_eval, eval) |
| 83 | + alpha = max(alpha, eval) |
| 84 | + if beta <= alpha: |
| 85 | + break |
| 86 | + return max_eval |
| 87 | + else: |
| 88 | + min_eval = float('inf') |
| 89 | + for row, col in valid_moves: |
| 90 | + new_board = copy.deepcopy(board) |
| 91 | + make_move(new_board, player, row, col) |
| 92 | + eval = minimax(new_board, depth - 1, player, alpha, beta, True) |
| 93 | + min_eval = min(min_eval, eval) |
| 94 | + beta = min(beta, eval) |
| 95 | + if beta <= alpha: |
| 96 | + break |
| 97 | + return min_eval |
| 98 | + |
| 99 | +def find_best_move(board, player): |
| 100 | + valid_moves = get_valid_moves(board, player) |
| 101 | + best_move = None |
| 102 | + best_eval = float('-inf') |
| 103 | + |
| 104 | + for row, col in valid_moves: |
| 105 | + new_board = copy.deepcopy(board) |
| 106 | + make_move(new_board, player, row, col) |
| 107 | + move_eval = minimax(new_board, depth=3, player=player, alpha=float('-inf'), beta=float('inf'), maximizing_player=False) |
| 108 | + |
| 109 | + if move_eval > best_eval: |
| 110 | + best_eval = move_eval |
| 111 | + best_move = (row, col) |
| 112 | + |
| 113 | + return best_move |
| 114 | + |
| 115 | +def main(): |
| 116 | + board = create_board() |
| 117 | + board[3][3], board[4][4] = WHITE, WHITE |
| 118 | + board[3][4], board[4][3] = BLACK, BLACK |
| 119 | + |
| 120 | + current_player = BLACK |
| 121 | + |
| 122 | + while True: |
| 123 | + print_board(board) |
| 124 | + |
| 125 | + if current_player == BLACK: |
| 126 | + row, col = find_best_move(board, BLACK) |
| 127 | + print(f"AI (BLACK) chooses: ({row}, {col})") |
| 128 | + else: |
| 129 | + valid_moves = get_valid_moves(board, WHITE) |
| 130 | + print("Valid moves for White:", valid_moves) |
| 131 | + row = int(input("Enter row: ")) |
| 132 | + col = int(input("Enter col: ")) |
| 133 | + |
| 134 | + if not make_move(board, current_player, row, col): |
| 135 | + print("Invalid move! Try again.") |
| 136 | + continue |
| 137 | + |
| 138 | + black_count, white_count = count_discs(board) |
| 139 | + print(f"Black: {black_count}, White: {white_count}") |
| 140 | + |
| 141 | + if not get_valid_moves(board, BLACK) and not get_valid_moves(board, WHITE): |
| 142 | + print("Game Over!") |
| 143 | + if black_count > white_count: |
| 144 | + print("Black wins!") |
| 145 | + elif black_count < white_count: |
| 146 | + print("White wins!") |
| 147 | + else: |
| 148 | + print("It's a draw!") |
| 149 | + break |
| 150 | + |
| 151 | + current_player = WHITE if current_player == BLACK else BLACK |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + main() |
0 commit comments