def main(): if len(sys.argv) == 1: print 'Usage: ./dummy.py TEAM_NAME' sys.exit(1) print sys.argv[1] STEPS = 7 #Initialize a points collection #Point(x,y,s): s == 0 is self point, s == 1 is opponent point game = Game(steps=STEPS) #Maintains a list of positions. #Update a list on seeing an opponent's move and self's choosen move points = Points() while True: try: s = raw_input() except EOFError: # print "EOFError" # Don't pollute output, output anything to stderr. break #Indicates Game Over if ("Game Over:" in s or "Wins" in s): break # Get the first tuple of the server input tuples = s.split() if len(tuples): #Get opponent's move; It will be at the start ftuple = tuples[0] p = ftuple.split(',') opp_x = int(p[0]) opp_y = int(p[1]) #Client makes sure there are no two stones on the same position. #Server does this job so its needless. Will remove it later for k in range(len(points)): if points[k].x == opp_x and points[k].y == opp_y: print "Error! You can only put one stone at a position" break #### points.append(Point(opp_x, opp_y, 1)) pt = game.Play(points) else: #Game started.You make the very first move pt = game.Play(points) #Record your move points.append(Point(pt[0], pt[1], 0)) #Write to Stdout print "%d,%d " % (pt[0], pt[1])
class Play: def __init__(self, AIModel, playerRole, inputter=DefaultInputter, displayer=DefaultDisplayer): self.aiModel = AIModel self.playerRole = playerRole self.inputter = inputter self.displayer = displayer def Start(self): self.game = Game() while not self.game.IsFull(): self.displayer(self.game) if self.playerRole == self.game.GetCurrentPlayer(): while True: inputPos = self.inputter() x, y = self.game.PosToXY(inputPos) if self.game.IsEmpty(x, y): break else: print("Place already taken up") continue else: x, y = Train.GetNextBestMove(self.aiModel, self.game) self.game.Play(self.game.GetCurrentPlayer(), x, y) if self.game.GetWinner() != Game.EMPTY: self.displayer(self.game) print("Player %s wins ! " % Game.PLAYER_ATTR[self.game.GetWinner()]['name']) break elif self.game.IsFull(): self.displayer(self.game) print("It's a tie !") break
def PlayGame(self, game : Game, cb = None): winner = game.GetWinner() steps = 1 while not game.IsFull(): winner = game.GetWinner() if winner != Game.EMPTY: break if game.GetCurrentPlayer() == Game.O: x, y = self.GetNextBestMove(self.modelO, game) else: x, y = self.GetNextBestMove(self.modelX, game) game.Play(game.GetCurrentPlayer(), x, y) steps += 1 if cb: cb(game.clone()) return winner, steps
from train import Train from model import ModelThreeDensesReluSigmoidMasked, ModelDenseSigmoidMasked from game import Game if __name__ == '__main__': modelO = ModelThreeDensesReluSigmoidMasked(midDim1=27, midDim2=27) modelX = ModelThreeDensesReluSigmoidMasked(midDim1=27, midDim2=27) trainer = Train(modelO, modelX) modelO.LoadWeights('O7.h5') modelX.LoadWeights('X7.h5') game = Game() game.Play(Game.O, 0, 0) game.Play(Game.O, 1, 1) game.Play(Game.X, 0, 1) game.Play(Game.X, 1, 0) print(game) result, steps = trainer.PlayGame(game, lambda g: print(g)) print("result: %f, steps: %d" % (result, steps)) print("----") game = Game() game.Play(Game.O, 0, 1) game.Play(Game.O, 1, 1) game.Play(Game.O, 1, 2) game.Play(Game.X, 0, 0) game.Play(Game.X, 1, 0) game.Play(Game.X, 2, 1)
from vector2 import Vector2 from humanPlayer import HumanPlayer from board import Board from game import Game from aiPlayer import AIPlayer from lstmModel import LSTMAIModel from LogOutputter import LogOutputter lstmLogs = [ LogOutputter('lstmModel_0_log.txt'), LogOutputter('lstmModel_1_log.txt') ] lstmModel_0 = LSTMAIModel(0, lstmLogs[0]) aiPlayer_0 = AIPlayer(lstmModel_0, lstmLogs[0]) lstmModel_1 = LSTMAIModel(1, lstmLogs[1]) aiPlayer_1 = AIPlayer(lstmModel_1, lstmLogs[1]) players = [aiPlayer_0, aiPlayer_1] numShipsOfSize = { 2 : 1, 3 : 2, 4 : 1, 5 : 1 } boardSize = Vector2(10, 10) while True: aiPlayer_0.ClearState() aiPlayer_1.ClearState() board = Board(boardSize, numShipsOfSize, players) game = Game(board) game.Play()
def TestGame(): print(Game().GetBoard()) assert (Game().GetWinner() == Game.EMPTY) game = Game() game.Play(Game.O, 0, 0) game.Play(Game.O, 0, 1) game.Play(Game.O, 0, 2) print(game) assert (game.GetWinner() == Game.O) game = Game() game.Play(Game.X, 1, 0) game.Play(Game.X, 0, 0) game.Play(Game.X, 2, 0) print(game) assert (game.GetWinner() == Game.X) game = Game() game.Play(Game.X, 0, 0) game.Play(Game.X, 1, 1) game.Play(Game.X, 2, 2) print(game) assert (game.GetWinner() == Game.X) game = Game() game.Play(Game.O, 0, 0) game.Play(Game.O, 1, 1) game.Play(Game.O, 2, 2) print(game) assert (game.GetWinner() == Game.O) game = Game() game.Play(Game.O, 2, 0) game.Play(Game.O, 1, 1) game.Play(Game.O, 0, 2) print(game) assert (game.GetWinner() == Game.O) game = Game() game.Play(Game.O, 1, 0) game.Play(Game.O, 1, 1) game.Play(Game.O, 1, 2) print(game) assert (game.GetWinner() == Game.O) game = Game() game.Play(Game.O, 0, 0) game.Play(Game.O, 0, 2) game.Play(Game.O, 1, 2) game.Play(Game.X, 1, 0) game.Play(Game.X, 1, 1) game.Play(Game.X, 2, 1) game.Play(Game.O, 2, 2) print(game) assert (game.GetWinner() == Game.O) def RandomGames(): game = Game.GetRandomGame() assert (game.GetWinner() == Game.EMPTY) return game for i in range(1000): RandomGames() for i in range(4): print(RandomGames()) print(Game().AsVector()) game = Game() assert (not game.IsFull()) for i in range(3): for j in range(3): game.Play(Game.O, i, j) assert (game.IsFull()) game = Game() game.Play(Game.O, 0, 2) game.Play(Game.O, 1, 2) game.Play(Game.O, 2, 2) print(game) assert (game.GetWinner() == Game.O) game = Game() game.Play(Game.X, 0, 2) game.Play(Game.X, 1, 2) game.Play(Game.X, 2, 2) print(game) assert (game.GetWinner() == Game.X)
p.s = 1 else: p.s = 0 if __name__ == '__main__': STEPS = 10 #Initialize a points collection #Point(x,y,s): s == 0 is self point, s == 1 is opponent point #You must make sure there is no two points of the same position! points = Points(); game = Game(steps = STEPS) for i in range(STEPS): print("Playing red for round {}".format(i)) pt = game.Play(points) points.append(Point(pt[0],pt[1],0)) vor = Voronoi(points) print("Red placed a stone at {}".format(pt)) print("Current area: {}".format(game.Area(points, vor.polygons))) game.Draw(points, vor.polygons) print("Playing blue for round {}".format(i)) reverse(points) pt = game.Play(points) points.append(Point(pt[0],pt[1],0)) reverse(points) vor = Voronoi(points) print("Blue placed a stone at {}".format(pt)) print("Current area: {}".format(game.Area(points, vor.polygons))) game.Draw(points, vor.polygons)