def setUp(self): self.four_by_one_board = PowerBoard((4, 1)) self.normal_board = PowerBoard((4, 4)) self.normal_board.place_value_at_coordinate(2, (1, 1)) self.normal_board.place_value_at_coordinate(2, (2, 1)) self.normal_board.place_value_at_coordinate(2, (1, 2)) self.normal_board.place_value_at_coordinate(4, (2, 2))
from module_4.GraphicsModule4 import Gfx from module_4.PowerBoard import PowerBoard import pygame import sys if __name__ == '__main__': pb = PowerBoard((4, 4)) pb.add_random_tile() gfx = Gfx(pb, 30, (600, 600)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_DOWN: pb.move_and_add_random_tile('d') if event.key == pygame.K_UP: pb.move_and_add_random_tile('u') if event.key == pygame.K_RIGHT: pb.move_and_add_random_tile('r') if event.key == pygame.K_LEFT: pb.move_and_add_random_tile('l') gfx.draw()
class TestPowerBoard(TestCase): def setUp(self): self.four_by_one_board = PowerBoard((4, 1)) self.normal_board = PowerBoard((4, 4)) self.normal_board.place_value_at_coordinate(2, (1, 1)) self.normal_board.place_value_at_coordinate(2, (2, 1)) self.normal_board.place_value_at_coordinate(2, (1, 2)) self.normal_board.place_value_at_coordinate(4, (2, 2)) def test_it_generates_board_of_correct_size(self): a = self.four_by_one_board.get_board() self.assertEqual(len(a), 1) self.assertEqual(len(a[0]), 4) b = self.normal_board.get_board() self.assertEqual(len(b), 4) self.assertEqual(len(b[0]), 4) def test_it_places_a_piece_correctly(self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (3, 0)) self.assertEqual(self.four_by_one_board.get_board(), [x, x, x, 2]) def test_it_slides_the_piece_all_the_way_to_the_wall_on_a_simple_board( self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (3, 0)) self.four_by_one_board.move_pieces('l') self.assertEqual(self.four_by_one_board.get_board(), [[2, x, x, x]]) def test_it_combines_two_pieces_with_equal_values_to_one_of_double_value_on_a_simple_board( self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (1, 0)) self.four_by_one_board.place_value_at_coordinate(2, (3, 0)) self.assertEqual(self.four_by_one_board.get_board(), (x, 2, x, 2)) self.four_by_one_board.move_pieces('l') self.assertEqual(self.four_by_one_board.get_board(), [4, x, x, x]) def test_it_does_not_combine_two_pieces_with_unequal_values_on_a_simple_board( self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (1, 0)) self.four_by_one_board.place_value_at_coordinate(4, (3, 0)) self.assertEqual(self.four_by_one_board.get_board(), [[x, 2, x, 4]]) self.four_by_one_board.move_pieces('l') self.assertEqual(self.four_by_one_board.get_board(), [[2, 4, x, x]]) def test_it_generates_pieces_on_normal_board_correctly(self): x = PowerBoard.ABSENCE supposed_normal_board = list( itertools.chain([x, x, x, x], [x, 2, 2, x], [x, 2, 4, x], [x, x, x, x])) self.assertEqual(self.normal_board.get_board(), supposed_normal_board) def test_it_moves_left_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list( itertools.chain([x, x, x, x], [4, x, x, x], [2, 4, x, x], [x, x, x, x])) self.normal_board.move_pieces('l') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_moves_right_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list( itertools.chain([x, x, x, x], [x, x, x, 4], [x, x, 2, 4], [x, x, x, x])) self.normal_board.move_pieces('r') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_moves_up_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list( itertools.chain([x, 4, 2, x], [x, x, 4, x], [x, x, x, x], [x, x, x, x])) self.normal_board.move_pieces('u') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_moves_down_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list( itertools.chain([x, x, x, x], [x, x, x, x], [x, x, 2, x], [x, 4, 4, x])) self.normal_board.move_pieces('d') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_calculates_score_correctly(self): score_before = self.normal_board.points self.normal_board.move_pieces('l') score_after = self.normal_board.points self.assertEqual(score_before + 4, score_after, 'Four more points after two 2s are combined') def test_it_places_a_random_tile_when_told_to(self): a = self.normal_board.get_empty_spaces() self.normal_board.add_random_tile() b = self.normal_board.get_empty_spaces() self.assertEqual(len(b) + 1, len(a), "One less empty tile") def test_it_gets_the_right_tile_evaluation_sequence_for_each_direction( self): u = self.normal_board.get_tile_evaluation_sequence('u') d = self.normal_board.get_tile_evaluation_sequence('d') l = self.normal_board.get_tile_evaluation_sequence('l') r = self.normal_board.get_tile_evaluation_sequence('r') expected_u = {((0, 0), (0, 1), (0, 2), (0, 3)), ((1, 0), (1, 1), (1, 2), (1, 3)), ((2, 0), (2, 1), (2, 2), (2, 3)), ((3, 0), (3, 1), (3, 2), (3, 3))} expected_d = {((0, 3), (0, 2), (0, 1), (0, 0)), ((1, 3), (1, 2), (1, 1), (1, 0)), ((2, 3), (2, 2), (2, 1), (2, 0)), ((3, 3), (3, 2), (3, 1), (3, 0))} expected_l = {((0, 0), (1, 0), (2, 0), (3, 0)), ((0, 1), (1, 1), (2, 1), (3, 1)), ((0, 2), (1, 2), (2, 2), (3, 2)), ((0, 3), (1, 3), (2, 3), (3, 3))} expected_r = {((3, 0), (2, 0), (1, 0), (0, 0)), ((3, 1), (2, 1), (1, 1), (0, 1)), ((3, 2), (2, 2), (1, 2), (0, 2)), ((3, 3), (2, 3), (1, 3), (0, 3))} self.assertEqual(u, expected_u, "right sequence for up") self.assertEqual(d, expected_d, "right sequence for down") self.assertEqual(l, expected_l, "right sequence for left") self.assertEqual(r, expected_r, "right sequence for right")
no_of_epochs = int(sys.argv[1]) raw_topology = sys.argv[2] error_function = sys.argv[3] hidden_layer_topology = [int(raw) for raw in raw_topology.split(";")] pba = PowerBoardAnn(51, hidden_layer_topology, 4) pba.read_pickle() error_rates = pba.do_training(no_of_epochs) start_time = time.time() ann_best_tiles = [] random_best_tiles = [] for i in range(50): pb = PowerBoard((4, 4)) pb.add_random_tile() while True: if pb.is_game_over(): pb.print_to_console() ann_best_tiles.append(max(pb.get_board())) break network_output = pba.evaluate_one_board( convert_list_to_2d(pb.get_board())) pb.move_and_add_random_tile(directions[network_output]) """ possible_directions = pb.get_possible_move_directions() for choice in network_output: if directions[choice] in possible_directions: pb.move_and_add_random_tile(directions[choice]) break
class TestPowerBoard(TestCase): def setUp(self): self.four_by_one_board = PowerBoard((4, 1)) self.normal_board = PowerBoard((4, 4)) self.normal_board.place_value_at_coordinate(2, (1, 1)) self.normal_board.place_value_at_coordinate(2, (2, 1)) self.normal_board.place_value_at_coordinate(2, (1, 2)) self.normal_board.place_value_at_coordinate(4, (2, 2)) def test_it_generates_board_of_correct_size(self): a = self.four_by_one_board.get_board() self.assertEqual(len(a), 1) self.assertEqual(len(a[0]), 4) b = self.normal_board.get_board() self.assertEqual(len(b), 4) self.assertEqual(len(b[0]), 4) def test_it_places_a_piece_correctly(self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (3, 0)) self.assertEqual(self.four_by_one_board.get_board(), [x, x, x, 2]) def test_it_slides_the_piece_all_the_way_to_the_wall_on_a_simple_board(self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (3, 0)) self.four_by_one_board.move_pieces('l') self.assertEqual(self.four_by_one_board.get_board(), [[2, x, x, x]]) def test_it_combines_two_pieces_with_equal_values_to_one_of_double_value_on_a_simple_board(self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (1, 0)) self.four_by_one_board.place_value_at_coordinate(2, (3, 0)) self.assertEqual(self.four_by_one_board.get_board(), (x, 2, x, 2)) self.four_by_one_board.move_pieces('l') self.assertEqual(self.four_by_one_board.get_board(), [4, x, x, x]) def test_it_does_not_combine_two_pieces_with_unequal_values_on_a_simple_board(self): x = PowerBoard.ABSENCE self.four_by_one_board.place_value_at_coordinate(2, (1, 0)) self.four_by_one_board.place_value_at_coordinate(4, (3, 0)) self.assertEqual(self.four_by_one_board.get_board(), [[x, 2, x, 4]]) self.four_by_one_board.move_pieces('l') self.assertEqual(self.four_by_one_board.get_board(), [[2, 4, x, x]]) def test_it_generates_pieces_on_normal_board_correctly(self): x = PowerBoard.ABSENCE supposed_normal_board = list(itertools.chain([x, x, x, x], [x, 2, 2, x], [x, 2, 4, x], [x, x, x, x])) self.assertEqual(self.normal_board.get_board(), supposed_normal_board) def test_it_moves_left_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list(itertools.chain([x, x, x, x], [4, x, x, x], [2, 4, x, x], [x, x, x, x])) self.normal_board.move_pieces('l') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_moves_right_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list(itertools.chain([x, x, x, x], [x, x, x, 4], [x, x, 2, 4], [x, x, x, x])) self.normal_board.move_pieces('r') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_moves_up_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list(itertools.chain([x, 4, 2, x], [x, x, 4, x], [x, x, x, x], [x, x, x, x])) self.normal_board.move_pieces('u') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_moves_down_correctly(self): x = PowerBoard.ABSENCE supposed_slided_board = list(itertools.chain([x, x, x, x], [x, x, x, x], [x, x, 2, x], [x, 4, 4, x])) self.normal_board.move_pieces('d') self.assertEqual(self.normal_board.get_board(), supposed_slided_board) def test_it_calculates_score_correctly(self): score_before = self.normal_board.points self.normal_board.move_pieces('l') score_after = self.normal_board.points self.assertEqual(score_before+4, score_after, 'Four more points after two 2s are combined') def test_it_places_a_random_tile_when_told_to(self): a = self.normal_board.get_empty_spaces() self.normal_board.add_random_tile() b = self.normal_board.get_empty_spaces() self.assertEqual(len(b)+1, len(a), "One less empty tile") def test_it_gets_the_right_tile_evaluation_sequence_for_each_direction(self): u = self.normal_board.get_tile_evaluation_sequence('u') d = self.normal_board.get_tile_evaluation_sequence('d') l = self.normal_board.get_tile_evaluation_sequence('l') r = self.normal_board.get_tile_evaluation_sequence('r') expected_u = {((0, 0), (0, 1), (0, 2), (0, 3)), ((1, 0), (1, 1), (1, 2), (1, 3)), ((2, 0), (2, 1), (2, 2), (2, 3)), ((3, 0), (3, 1), (3, 2), (3, 3))} expected_d = {((0, 3), (0, 2), (0, 1), (0, 0)), ((1, 3), (1, 2), (1, 1), (1, 0)), ((2, 3), (2, 2), (2, 1), (2, 0)), ((3, 3), (3, 2), (3, 1), (3, 0))} expected_l = {((0, 0), (1, 0), (2, 0), (3, 0)), ((0, 1), (1, 1), (2, 1), (3, 1)), ((0, 2), (1, 2), (2, 2), (3, 2)), ((0, 3), (1, 3), (2, 3), (3, 3))} expected_r = {((3, 0), (2, 0), (1, 0), (0, 0)), ((3, 1), (2, 1), (1, 1), (0, 1)), ((3, 2), (2, 2), (1, 2), (0, 2)), ((3, 3), (2, 3), (1, 3), (0, 3))} self.assertEqual(u, expected_u, "right sequence for up") self.assertEqual(d, expected_d, "right sequence for down") self.assertEqual(l, expected_l, "right sequence for left") self.assertEqual(r, expected_r, "right sequence for right")
#from module_4.GraphicsModule4 import Gfx from module_4.PowerBoard import PowerBoard from module_4.PowerBoardState import PowerBoardState #import pygame import sys from time import time if __name__ == '__main__': for i in range(10): pb = PowerBoard((4, 4)) pb.add_random_tile() #gfx = Gfx(pb, 30, (600, 600)) a = time() game_over = False while True: """ for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() """ b = time() if not game_over: empties = len(pb.get_empty_spaces()) search_state = PowerBoardState(PowerBoardState.get_recursion_depth_roof(empties)) search_state.board = pb.get_board() decision = search_state.decision() with open("2048moves.txt", 'a') as log: log.write("\n\n" + str(pb.get_board())) log.write("\n" + decision)
from module_4.GraphicsModule4 import Gfx from module_4.PowerBoard import PowerBoard from module_4.PowerBoardState import PowerBoardState import pygame import sys from time import time if __name__ == '__main__': pb = PowerBoard((4, 4)) pb.add_random_tile() gfx = Gfx(pb, 30, (600, 600)) a = time() game_over = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() b = time() if not game_over: empties = len(pb.get_empty_spaces()) search_state = PowerBoardState(PowerBoardState.get_recursion_depth_roof(empties)) search_state.board = pb.get_board() decision = search_state.decision() pb.move_and_add_random_tile(decision) c = time() print(c-b) if pb.is_game_over(): game_over = True
no_of_epochs = int(sys.argv[1]) raw_topology = sys.argv[2] error_function = sys.argv[3] hidden_layer_topology = [int(raw) for raw in raw_topology.split(";")] pba = PowerBoardAnn(51, hidden_layer_topology, 4) pba.read_pickle() error_rates = pba.do_training(no_of_epochs) start_time = time.time() ann_best_tiles = [] random_best_tiles = [] for i in range(50): pb = PowerBoard((4, 4)) pb.add_random_tile() while True: if pb.is_game_over(): pb.print_to_console() ann_best_tiles.append(max(pb.get_board())) break network_output = pba.evaluate_one_board(convert_list_to_2d(pb.get_board())) pb.move_and_add_random_tile(directions[network_output]) """ possible_directions = pb.get_possible_move_directions() for choice in network_output: if directions[choice] in possible_directions: pb.move_and_add_random_tile(directions[choice]) break """
#from module_4.GraphicsModule4 import Gfx from module_4.PowerBoard import PowerBoard from module_4.PowerBoardState import PowerBoardState #import pygame import sys from time import time if __name__ == '__main__': for i in range(10): pb = PowerBoard((4, 4)) pb.add_random_tile() #gfx = Gfx(pb, 30, (600, 600)) a = time() game_over = False while True: """ for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() """ b = time() if not game_over: empties = len(pb.get_empty_spaces()) search_state = PowerBoardState( PowerBoardState.get_recursion_depth_roof(empties)) search_state.board = pb.get_board() decision = search_state.decision() with open("2048moves.txt", 'a') as log: log.write("\n\n" + str(pb.get_board()))
from module_4.GraphicsModule4 import Gfx from module_4.PowerBoard import PowerBoard from module_4.PowerBoardState import PowerBoardState import pygame import sys from time import time if __name__ == '__main__': pb = PowerBoard((4, 4)) pb.add_random_tile() gfx = Gfx(pb, 30, (600, 600)) a = time() game_over = False while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() b = time() if not game_over: empties = len(pb.get_empty_spaces()) search_state = PowerBoardState( PowerBoardState.get_recursion_depth_roof(empties)) search_state.board = pb.get_board() decision = search_state.decision() pb.move_and_add_random_tile(decision) c = time() print(c - b) if pb.is_game_over(): game_over = True