class Client: def __init__(self): self.playerID = None self.board = None self.server = ServerProxy("http://localhost:8000") def createGame(self): self.playerID = self.server.createGame() def startGame(self,playerLetter): self.server.startGame(self.playerID,playerLetter) def makeMove(self,move): self.server.move(self.playerID,move) def endGame(self): self.server.endGame(self.playerID) def inputPlayerLetter(self): letter = '' while not (letter == 'X' or letter == 'O'): print('Do you want to be X or O?') letter = input().upper() return letter def getPlayerMove(self): # Let the player type in their move. move = ' ' while move not in '1 2 3 4 5 6 7 8 9'.split(): print('What is your next move? (1-9)') move = input() return int(move) def getBoard(self): self.board = self.server.getBoard(self.playerID) def drawBoard(board): print(' | |') print(' ' + board[7] + ' | ' + board[8] + ' | ' + board[9]) print(' | |') print('-----------') print(' | |') print(' ' + board[4] + ' | ' + board[5] + ' | ' + board[6]) print(' | |') print('-----------') print(' | |') print(' ' + board[1] + ' | ' + board[2] + ' | ' + board[3]) print(' | |') def parse(self,response): return True
from AI import ReversiSimpleAI game_server = ServerProxy("http://127.0.0.1:8006") game_id = game_server.start_game('*****@*****.**') ai = ReversiSimpleAI() step = 0 while True: print '===============================' board = Board(game_server.get_board_state(game_id)) board.pprint() x, y = ai.get_best_move(board, 1) state = game_server.move(x, y, game_id) step += 1 board = Board(game_server.get_board_state(game_id)) print '_______________________________' board.pprint() print state if state[1] == 'game_over': if board.score(1) > board.score(2): print "YOU'VE WON!!!", 'in ', step, 'steps' if board.score(1) == board.score(2): print 'EQUAL SCORE', 'in ', step, 'steps' if board.score(1) < board.score(2): print "YOU'VE LOST", 'in ', step, 'steps'
letter = raw_input('Choose any letter to start (O or X): ') if letter.upper() != 'O' and letter.upper() != 'X': continue else: command, result = parse_command(server.start(tid, letter)) if command == '201': running = True else: print 'An error occured, please try again later.' else: board = parse_board(server.board(tid)) draw_board(board) turn = server.turn(tid) if turn == 'Human': move = raw_input("It's your turn. Please enter a move (1-9)") command, result = parse_command(server.move(tid, int(move))) if command == '403': print("You entered invalid move. ") if command == '402': print("FATAL1: Fatal error occured. Try to restart the game.") break if command == '405': print("It is cpu's turn.") elif command == '204': command, result = parse_command(server.won(tid)) if command == '402': print("FATAL3: Fatal error occured. Try to restart the game.") break if command == '206': print 'You Won!' again = None