def test_move_with_pop(self): g = Game() g.nextPiece = 1 g.move(4) expected = [[Game.EMPTY] * SIZE for x in range(SIZE)] self.assertEqual(g.board, expected)
def __init__(self, iterations=5): results = [] situations = [] logging.basicConfig() for i in range(0, iterations): g = Game(print_board=False) round_situations = [] while not g.game_over: choices = g.available_cols() choice = random.choice(choices) round_situations.append(self.game_to_sit(g, choice)) g.place_piece(choice) for situation in round_situations: results.append(g.points) situations.extend(round_situations) #self.pipeline = Pipeline([ # ('min/max scaler', MinMaxScaler(feature_range=(0.0, 1.0))), # ('neural network', Regressor( self.nn = Regressor(layers=[ Layer("Rectifier", units=100), Layer("Linear")], learning_rate=0.00002, n_iter=10) #self.pipeline.fit(np.array(situations), np.array(results)) print np.array(situations).shape self.nn.fit(np.array(situations), np.array(results))
def naive_scores(iterations): scores = [] for i in range(iterations): g = Game(print_board=False, classic_mode=False) while not g.game_over: choices = g.available_cols() choice = random.choice(choices) g.place_piece(choice) scores.append(g.points) return max(scores), sum(scores)/len(scores)
def simulate_game(tup): net, classic_mode = tup game = Game(print_board=False, sleep=False, classic_mode=classic_mode) while not game.game_over: moves = net.serial_activate(game_to_sit(game)) #move = moves.index(max(moves)) moves_tuples = zip(moves, range(len(moves))) moves_tuples.sort() for move in moves_tuples: if game.place_piece(move[1]): break return game.points
def test_move_with_chain_pop(self): g = Game() g.board[0][0] = 2 g.board[1][0] = 2 g.board[4][0] = 2 g.nextPiece = 3 g.move(2) expected = [[Game.EMPTY] * SIZE for x in range(SIZE)] expected[4][0] = 2 self.assertEqual(g.board, expected)
def test_pop_gray(self): g = Game() g.board[4][0] = Game.GRAY g.nextPiece = 2 g.getPiece = lambda: 4 g.move(4) self.assertTrue(g.board[4][0] == 4) self.assertEqual(g.board[4][1], Game.EMPTY)
def test_drop(self): g = Game() g.board[0][1] = 2 g.board[4][0] = 1 g.board[4][1] = 2 g.board[4][3] = 3 g.board[4][6] = 4 g.dropPieces() expected = [[Game.EMPTY] * SIZE for x in range(SIZE)] expected[0][0] = 2 expected[4][0] = 1 expected[4][1] = 2 expected[4][2] = 3 expected[4][3] = 4 self.assertEqual(g.board, expected)
def test_can_move(self): g = Game() for i in range(SIZE): self.assertTrue(g.canMove(i)) g.board[0] = [2] * SIZE self.assertFalse(g.canMove(0)) for i in range(1, SIZE): self.assertTrue(g.canMove(i))
def test_drop(self): g = Game() g.board[4][0] = 2 g.nextPiece = 3 g.move(4) expected = [[Game.EMPTY] * SIZE for x in range(SIZE)] expected[4][0] = 3 self.assertEqual(g.board, expected)
def test_pop_mask(self): g = Game() g.board[0][0] = 2 g.board[0][1] = 2 g.board[0][2] = 3 expected = [[False] * SIZE for x in range(SIZE)] expected[0][2] = True self.assertEqual(g.getPopMask(), expected)
def test_score(self): g = Game() g.board[0][0:3] = [4, 5, 7] g.board[1][0:3] = [4, 1, 4] g.board[2][0:2] = [6, 7] g.board[3][0] = 3 g.board[4][0:2] = [6, 5] g.board[6][0:3] = [5, 4, 4] g.nextPiece = 5 g.piecesInRound = 1 g.score = 21 g.move(3) expected = [[Game.EMPTY] * SIZE for x in range(SIZE)] expected[0][0:3] = [Game.DGRAY, 4, 7] expected[1][0:2] = [Game.GRAY, 1] expected[2][0:3] = [Game.DGRAY, 6, 7] expected[3][0:2] = [Game.DGRAY, 3] expected[4][0:2] = [Game.DGRAY, 6] expected[5][0] = Game.DGRAY expected[6][0:2] = [Game.DGRAY, 5] self.assertEqual(g.board, expected) self.assertEqual(g.score, 17198)
# hidden_layer_sizes=(5, 2), random_state=1) #clf.train(situations, results) def game_to_sit(self, game, choice): sit = [float(item) / 9 for sublist in game.board for item in sublist] sit.append(float(choice) / 7) sit.append(float(game.level) / 100) assert(float(game.level) / 100) sit.append(float(game.pieces_left) / 30) return sit def pick_move(self, game): choices = game.available_cols() max_choice, max_val = None, 0 for c in choices: sit = np.array([self.game_to_sit(game, c)]) final_score_predict = self.nn.predict(sit) if final_score_predict > max_val: max_val = final_score_predict max_choice = c return max_choice if __name__ == "__main__": l = Learner(100) g = Game(sleep=True) while not g.game_over: move = l.pick_move(g) g.place_piece(move) #choices = g.available_cols() #break