-
Notifications
You must be signed in to change notification settings - Fork 47
/
jin051.py
56 lines (46 loc) · 1.9 KB
/
jin051.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from othello2023.othello import OthelloAI
class mizukikun(OthelloAI):
def __init__(self):
self.face = '💦' # 自分の好きな絵文字
self.name = '瑞稀' # 自分の好きな名前
def _get_valid_moves(self, board, piece):
valid_moves = []
for r in range(board.size):
for c in range(board.size):
if board.is_valid_move(r, c, piece):
valid_moves.append((r, c))
return valid_moves
def _evaluate_board(self, board, piece):
return board.count_pieces(piece) - board.count_pieces(board.opponent(piece))
def _minimax(self, board, depth, maximizing_player, piece):
if depth == 0 or board.is_game_over():
return self._evaluate_board(board, piece)
valid_moves = self._get_valid_moves(board, piece)
if maximizing_player:
max_eval = float('-inf')
for move in valid_moves:
new_board = board.copy()
new_board.play_move(move)
eval = self._minimax(new_board, depth - 1, False, piece)
max_eval = max(max_eval, eval)
return max_eval
else:
min_eval = float('inf')
for move in valid_moves:
new_board = board.copy()
new_board.play_move(move)
eval = self._minimax(new_board, depth - 1, True, piece)
min_eval = min(min_eval, eval)
return min_eval
def move(self, board, piece):
valid_moves = self._get_valid_moves(board, piece)
best_move = None
best_eval = float('-inf')
for move in valid_moves:
new_board = board.copy()
new_board.play_move(move)
eval = self._minimax(new_board, 3, False, piece)
if eval > best_eval:
best_eval = eval
best_move = move
return best_move