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

50 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
"""This is the famous random player whici (almost) always looses."""
import time
import Goban
from random import choice
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
def getPlayerName(self):
return "Random Player"
def getPlayerMove(self):
if self._board.is_game_over():
print("Referee told me to play but the game is over!")
return "PASS"
moves = self._board.legal_moves() # Dont use weak_legal_moves() here!
move = choice(moves)
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
return Goban.Board.flat_to_name(move)
def playOpponentMove(self, move):
print("Opponent played ", move, "i.e. ", move) # New here
# the board needs an internal represetation to push the move. Not a string
self._board.push(Goban.Board.name_to_flat(move))
def newGame(self, color):
self._mycolor = color
self._opponent = Goban.Board.flip(color)
def endGame(self, winner):
if self._mycolor == winner:
print("I won!!!")
else:
print("I lost :(!!")