Skip to content

Commit e32d538

Browse files
Merge pull request #2521 from Shikhar9425/patch-5
Reversi.py
2 parents 6c1242b + aa0cf61 commit e32d538

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

AI-driven AI Reversi/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Package/Script Name: AI-driven AI Reversi
2+
3+
Short Description: AI-driven AI Reversi is a Python script that implements an AI player for the game "Reversi," also known as "Othello." The AI player uses the MiniMax algorithm with Alpha-Beta Pruning to make strategic moves and compete against human players or other AI agents.
4+
5+
Functionalities/Script:
6+
7+
ReversiGame class: Represents the Reversi game and handles game logic.
8+
AI_agent class: Implements the AI player using the MiniMax algorithm with Alpha-Beta Pruning to make intelligent moves.
9+
Heuristic evaluation function: Calculates the utility value for a given game state to guide the AI's decision-making process.
10+
Board representation: Displays the game board with the pieces of both players.
11+
Setup Instructions:
12+
13+
Make sure you have Python installed on your system (Python 3.6 or higher).
14+
Download the AI_reversi.py file from the repository or package.
15+
Explanation of Script:
16+
The AI-driven AI Reversi script enables you to play the classic Reversi game against an AI player that uses the MiniMax algorithm with Alpha-Beta Pruning. The AI player aims to find the best move that maximizes its chances of winning while minimizing the opponent's opportunities.
17+
18+
Usage:
19+
20+
Run the AI_reversi.py script.
21+
bash
22+
Copy code
23+
python AI_reversi.py
24+
The game window will open, displaying the Reversi board with the initial setup.
25+
The game will prompt the players to take turns and make their moves using the mouse.
26+
Output:
27+
The AI-driven AI Reversi script will display a graphical game window showing the Reversi board with the pieces of both players. The script will guide you through the game, and you can play against the AI player that uses the MiniMax algorithm to make intelligent moves.
28+
29+
Author:
30+
Shikhar9425

AI-driven AI Reversi/Reversi.py

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

Comments
 (0)