Package MeatEngine :: Package AI :: Module humanPlayer
[hide private]
[frames] | no frames]

Source Code for Module MeatEngine.AI.humanPlayer

1 -class HumanTicTacToePlayer:
2 - def __init__(self, side):
3 """ side: 1=X, 2=O """ 4 self.side=side
5
6 - def getMove(self, board):
7 """ 8 displays the board to the human, who returns a move number (or 9 0 to quit) 10 """ 11 12 r1=board.squares[:3] 13 r2=board.squares[3:6] 14 r3=board.squares[6:] 15 16 print r1 17 print r2 18 print r3 19 20 prompt="player %d enter move (1-9) or 0 to quit:"%self.side 21 22 i=raw_input(prompt) 23 24 return int(i)
25 26 27 if __name__=="__main__": 28 29 import boardRep 30 import randomPlayer 31 32 p1=HumanTicTacToePlayer(1) 33 #p2=HumanTicTacToePlayer(2) 34 p2=randomPlayer.RandomTicTacToePlayer(2) 35 36 37 b=boardRep.TicTacToeBoard() 38 39 movingPlayer=0 40 players=[p1,p2] 41 42 while not b.isOver(): 43 m=players[movingPlayer].getMove(b) 44 if m==0: 45 break 46 m=m-1 47 48 x=m%3 49 y=(m-x)/3 50 if b.isLegalMove(x,y,movingPlayer+1): 51 b.move(x,y,movingPlayer+1) 52 movingPlayer += 1 53 movingPlayer %= 2 54 else: 55 print "illegal move, try again",x,y 56 57 print "canonical version of the board:" 58 r,nb=b.getCanonicalCopy() 59 print r,nb.hashVal() 60 nb.display() 61 62 w=b.getWinner() 63 winString={-1:"Draw", 64 0:"Ongoing", 65 1:"Player 1 wins", 66 2:"Player 2 wins"}[w] 67 print winString 68