-
Notifications
You must be signed in to change notification settings - Fork 0
/
randomPlayer.py
53 lines (40 loc) · 1.68 KB
/
randomPlayer.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
# -*- 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
print("I am playing ", self._board.move_to_str(move))
# 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 :(!!")