go-ai/go_player/myPlayer.py
2025-05-17 15:36:19 +02:00

78 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
"""This is the file you have to modify for the tournament. Your default AI player must be called by this module, in the
myPlayer class.
Right now, this class contains the copy of the randomPlayer. But you have to change this!
"""
import time
import Goban
from random import choice
from moveSearch import IDDFS, alphabeta
from playerInterface import *
class myPlayer(PlayerInterface):
"""
Example of a random player for the go. The only tricky part is to be able to handle
the internal representation of moves given by legal_moves() and used by push() and
to translate them to the GO-move strings "A1", ..., "J8", "PASS". Easy!
"""
def __init__(self):
self._board = Goban.Board()
self._mycolor = None
self.moveCount = 0
def getPlayerName(self):
return "xXx_7h3_5cRuM_M45T3r_xXx"
@staticmethod
def simple_heuristic(board, color):
# Simple stone difference heuristic
score = board.compute_score()
return (
score[0] - score[1] if color == Goban.Board._BLACK else score[1] - score[0]
)
def getPlayerMove(self):
if self._board.is_game_over():
print("Referee told me to play but the game is over!")
return "PASS"
if self.moveCount < 10:
max_depth = 1
elif self.moveCount < 20:
max_depth = 2
elif self.moveCount < 40:
max_depth = 3
else:
max_depth = 24
move = IDDFS(
self._board, self.simple_heuristic, self._mycolor, duration=1., maxdepth=max_depth
) # IDDFS(self._board, self.simple_heuristic, self._mycolor, 1.)
self._board.push(move)
# New here: allows to consider internal representations of moves
# move is an internal representation. To communicate with the interface I need to change if to a string
self.moveCount += 1 if Goban.Board.flat_to_name(move) != "PASS" else 0
return Goban.Board.flat_to_name(move)
def playOpponentMove(self, move):
print("Opponent played ", move) # New here
# the board needs an internal represetation to push the move. Not a string
self.moveCount += 1 if move != "PASS" else 0
self._board.push(Goban.Board.name_to_flat(move))
def newGame(self, color):
self._mycolor = color
self._opponent = Goban.Board.flip(color)
self.moveCount = 0
def endGame(self, winner):
if self._mycolor == winner:
print("I won!!!")
else:
print("I lost :(!!")