def __init__(self, gameState, ownTeamID): self.ownTeamID = ownTeamID self.path = None self.exploreTargets = deque() for i in xrange(0, gameState.battlefield.width - 1, 5): for j in xrange(0, gameState.battlefield.height - 1, 5): pos = game.Position(i, j) if gameState.battlefield.spotFree(pos): self.exploreTargets.append(pos)
def testPositionCreation(self): pos: game.Position = game.Position(1, 2) self.assertEqual(1, pos.row) self.assertEqual(2, pos.column) row, col = pos self.assertEqual(1, row) self.assertEqual(2, col)
def __init__(self, make_params_functions, filename="learn.txt", k=13): self.k = k self.saved_results = [] self.make_params_functions = make_params_functions for line in open(filename): fen, score = line.strip().split('\t')[:2] pos = game.Position() pos.set_fen(fen) params = [f(pos) for f in self.make_params_functions] self.saved_results.append((params, int(score)))
def handleGameEvent(self, g, client): if g.server.clientControls(client.clientID, g.gameState.activeTeamID): g.decrementAPs(20) pos = game.Position(self.proto.pos.x, self.proto.pos.y) sold = g.gameState.soldierOn(pos) if sold and sold.hps > 0 and game.vectorLength( game.subVectors( sold.position, g.gameState.getActiveSoldier().position)) < 10: g.killSoldier(sold) else: print "Warning: client", client.clientID, "sends command when not allowed"
def prepare_data(filename, position_functions): scores = [] features_values = [] for line in open(filename): fen, score = line.strip().split('\t')[:2] score = int(score) scores.append(score) pos = game.Position() pos.set_fen(fen) features = [f(pos) for f in position_functions] features_values.append(features) return features_values, scores
def testPiecesDrop(self): # Add in column one should drop to the bottom self.game.addPiece(game.Player.RED, 1) self.assertIsNone(self.game.getPlayer(game.Position(5, 1))) self.assertIsNone(self.game.getPlayer(game.Position(4, 1))) self.assertIsNone(self.game.getPlayer(game.Position(3, 1))) self.assertIsNone(self.game.getPlayer(game.Position(2, 1))) self.assertIsNone(self.game.getPlayer(game.Position(1, 1))) self.assertEqual(game.Player.RED, self.game.getPlayer(game.Position(0, 1)))
def check_get_score(your_function): pairs = [] for line in open(FILENAME): fen, score = line.strip().split('\t')[:2] pos = game.Position() pos.set_fen(fen) predicted_score = your_function(pos) pairs.append((int(score), predicted_score)) correct = 0 incorrect = 0 for i, (s1, p1) in enumerate(pairs): for s2, p2 in pairs[:i]: if sign(s1 - s2) == sign(p1 - p2): correct += 1 else: incorrect += 1 print("correct share: %.2f%%" % (100. * correct / float(correct + incorrect))) return (100. * correct / float(correct + incorrect))
def testAddMultiplePiecesSameColumn(self): ''' 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 x 0 0 0 0 0 0 x 0 0 0 0 0 0 x 0 0 0 ''' self.game.addPiece(game.Player.RED, 4) self.game.addPiece(game.Player.RED, 4) self.game.addPiece(game.Player.RED, 4) self.assertIsNone(self.game.getPlayer(game.Position(5, 4))) self.assertIsNone(self.game.getPlayer(game.Position(4, 4))) self.assertIsNone(self.game.getPlayer(game.Position(3, 4))) self.assertEqual(game.Player.RED, self.game.getPlayer(game.Position(2, 4))) self.assertEqual(game.Player.RED, self.game.getPlayer(game.Position(1, 4))) self.assertEqual(game.Player.RED, self.game.getPlayer(game.Position(0, 4)))
def protoSoldierToSoldier(soldier): pos = game.Position(soldier.position.x, soldier.position.y) s = game.Soldier(soldier.teamID, soldier.soldierID, pos) s.direction = soldier.direction s.hps = soldier.hps return s
def test_possible_moves(self): b = self.board b.player_info.pos = game.Position(1, 0) pm = b.possible_moves() for m in pm: print(m)
def test_paths(self): b = self.board paths = b.nearest_paths(game.Position(0, 0), [game.Position(2, 2)]) self.assertListEqual(paths, [[game.RIGHT, game.DOWN, game.RIGHT], [game.DOWN, game.DOWN, game.RIGHT]])
def test_get_tile(self): b = self.board self.assertEqual(b.get_tile(game.Position(0, 0)), game.Tile("0110")) self.assertEqual(b.get_tile(game.Position(2, 2)), game.Tile("0000"))
def test_dist(self): p = game.Position(1, 2) self.assertEqual(p.dist(p), 0) self.assertEqual(p.dist(game.Position(2, 0)), 3)
def test_add(self): p = game.Position(2, 5) + game.RIGHT + game.UP self.assertEqual((p.x, p.y), (3, 4))