Skip to content

Commit

Permalink
Update marin002.py
Browse files Browse the repository at this point in the history
  • Loading branch information
m2116002 authored Jan 21, 2024
1 parent 4237ff9 commit b8d6aa9
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions marin002.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,20 +217,53 @@ def move(self, board, color: int)->tuple[int, int]:
selected_move = random.choice(valid_moves)
return selected_move

### 自分の作ったAIをここに貼る

import random

class MosAI(OthelloAI):
def __init__(self, face, name):
class IchigoAI(OthelloAI):
def __init__(self, face, name, depth=3):
self.face = face
self.name = name
self.depth = depth

def move(self, board, color: int)->tuple[int, int]:
"""
ボードが与えられたとき、どこに置くか(row,col)を返す
"""
_, move = self.minimax(board, color, self.depth, float('-inf'), float('inf'), True)
return move

def minimax(self, board, color, depth, alpha, beta, maximizing_player):
if depth == 0 or len(get_valid_moves(board, color)) == 0:
return self.evaluate(board, color), None

valid_moves = get_valid_moves(board, color)
# ランダムに選ぶ
selected_move = random.choice(valid_moves)
return selected_move
if maximizing_player:
max_eval = float('-inf')
best_move = None
for move in valid_moves:
new_board = board.copy()
flip_stones(new_board, move[0], move[1], color)
eval, _ = self.minimax(new_board, -color, depth - 1, alpha, beta, False)
if eval > max_eval:
max_eval = eval
best_move = move
alpha = max(alpha, eval)
if beta <= alpha:
break
return max_eval, best_move
else:
min_eval = float('inf')
best_move = None
for move in valid_moves:
new_board = board.copy()
flip_stones(new_board, move[0], move[1], color)
eval, _ = self.minimax(new_board, -color, depth - 1, alpha, beta, True)
if eval < min_eval:
min_eval = eval
best_move = move
beta = min(beta, eval)
if beta <= alpha:
break
return min_eval, best_move

def evaluate(self, board, color):
# A simple evaluation function: the difference in the number of pieces.
return count_board(board, color) - count_board(board, -color)

0 comments on commit b8d6aa9

Please sign in to comment.