def analysis(): total = 0 maxScore = 0 for i in range(100): board = Board_2048() agent = ExpectimaxAgent(1) game = True score = 0 while game: move = agent.getAction(board) if move == 'right': new_board, moves, s = board.right() elif move == 'left': new_board, moves, s = board.left() elif move == 'up': new_board, moves, s = board.up() elif move == 'down': new_board, moves, s = board.down() board = new_board score += s if board.check_end(): game = False #board.print_board() #print "Final Score: " + str(score) total += score if score > maxScore: maxScore = score print "Average Score After 100 Games Depth 1: " + str(float(total / 100)) print "Max(Depth 1): " + str(maxScore) maxScore = 0 total = 0 for i in range(100): board = Board_2048() agent = ExpectimaxAgent() game = True score = 0 while game: move = agent.getAction(board) if move == 'right': new_board, moves, s = board.right() elif move == 'left': new_board, moves, s = board.left() elif move == 'up': new_board, moves, s = board.up() elif move == 'down': new_board, moves, s = board.down() board = new_board score += s if board.check_end(): game = False #board.print_board() #print "Final Score: " + str(score) total += score if score > maxScore: maxScore = score print "Average Score After 100 Games Depth 2: " + str(float(total / 100)) print "Max(Depth 2): " + str(maxScore)
def main(): board = Board_2048() agent = ExpectimaxAgent() game = True score = 0 print "Game with Expectimax depth 2 with moves shown:" #board.print_board() #print score while game: move = agent.getAction(board) if move == 'right': new_board, moves, s = board.right() elif move == 'left': new_board, moves, s = board.left() elif move == 'up': new_board, moves, s = board.up() elif move == 'down': new_board, moves, s = board.down() board = new_board score += s #print move #board.print_board() #print score if board.check_end(): game = False print board.print_board() print "Final Score: " + str(score) print print analysis()
def test_single_tile_left(self): b = Board_2048([[0, 0, 0], [0, 2, 0], [0, 0, 0]]) b, motions = b.move_left() self.assertEqual(b.rows, [[0, 0, 0], [2, 0, 0], [0, 0, 0]]) self.assertEqual(motions, {(1,1, 1,0)})
def test_two_tiles_right_combining(self): b = Board_2048([[0, 0, 0], [4, 0, 4], [0, 0, 0]]) b, motions = b.move_right() self.assertEqual(b.rows, [[0, 0, 0], [0, 0, 8], [0, 0, 0]]) self.assertEqual(motions, {(1,0, 1,2)})
def test_two_tiles_down_without_combining(self): b = Board_2048([[0, 0, 4], [0, 0, 2], [0, 0, 0]]) b, motions = b.move_down() self.assertEqual(b.rows, [[0, 0, 0], [0, 0, 4], [0, 0, 2]]) self.assertEqual(motions, {(0,2, 1,2), (1,2, 2,2)})
def test_two_tiles_up_without_touching(self): b = Board_2048([[0, 0, 0], [2, 0, 2], [0, 0, 0]]) b, motions = b.move_up() self.assertEqual(b.rows, [[2, 0, 2], [0, 0, 0], [0, 0, 0]]) self.assertEqual(motions, {(1,0, 0,0), (1,2, 0,2)})
def test_single_tile_left_without_moving(self): b = Board_2048([[0, 0, 0], [2, 0, 0], [0, 0, 0]]) b, motions = b.move_left() self.assertEqual(b.rows, [[0, 0, 0], [2, 0, 0], [0, 0, 0]]) self.assertEqual(motions, set())
def main(): board = Board_2048() agent = ExpectimaxAgent() game = True score = 0 old_board = board while game: #board.print_board() row1, row2, row3 = board.board_string() #print score stdscr = curses.initscr() curses.cbreak() stdscr.keypad(1) stdscr.addstr(10, 30, row1) stdscr.addstr(12, 30, row2) stdscr.addstr(14, 30, row3) stdscr.addstr(16, 30, "Score: " + str(score)) stdscr.addstr(18, 30, agent.getAction(board)) key = stdscr.getch() #register key press up down left right if key == curses.KEY_UP: new_board, moves, new_score = board.up() elif key == curses.KEY_LEFT: new_board, moves, new_score = board.left() elif key == curses.KEY_DOWN: new_board, moves, new_score = board.down() elif key == curses.KEY_RIGHT: new_board, moves, new_score = board.right() else: new_board = board new_score = 0 board = new_board score += new_score if board.check_end(): game = False curses.endwin() board.print_board() print "Final Score: " + str(score)
def main(): board = Board_2048() screen = pygame.display.set_mode((320, 320)) pygame.font.init() myfont = pygame.font.SysFont("Comic Sans MS", 30) clock = pygame.time.Clock() score = -1 add_score = +1 tiles = list() tile_images = {} tile = None # todo: change to a list of tiles moves = list() for number in ('0', '2', '4', '8', '16', '32', '64', '128', '256', '512'): filename = 'static/{}.jpg'.format(number) image = pygame.image.load(filename) image = image.convert_alpha() image = pygame.transform.smoothscale(image, (76, 76)) tile_images[number] = image while True: old_board = copy.deepcopy(board) for event in pygame.event.get(): if event.type == c.KEYDOWN: if event.key == c.K_ESCAPE: sys.exit(0) # NEW: elif event.key == c.K_RIGHT: board, moves, add_score = board.right() print moves elif event.key == c.K_LEFT: board, moves, add_score = board.left() print moves elif event.key == c.K_UP: board, moves, add_score = board.up() print moves elif event.key == c.K_DOWN: board, moves, add_score = board.down() print moves while moves: i1, j1, i2, j2 = moves.pop() # todo: make a tile for every item number = old_board.rows[i1][j1] image = tile_images[str(number)] tiles.append(Tile(image, j1 * 80, i1 * 80, j2 * 80, i2 * 80)) if add_score: score += add_score add_score = 0 label = 'Score: %s' % score label_1 = myfont.render(label, 1, (0, 0, 0)) screen.fill((255, 255, 255)) for a in range(0, 3): for b in range(0, 3): e = 80 * a f = 80 * b pygame.draw.rect(screen, (0, 0, 0), (e, f, 80, 80), 1) for i in range(0, 3): j = 0 for number in board.rows[i]: x = 80 * j + 2 y = 80 * i + 2 j += 1 number = '%s' % number screen.blit(tile_images[number], (x, y)) for tile in tiles: if tile is not None: tile.blit(screen) tile.move() if tile.done(): tiles.remove(tile) screen.blit(label_1, (2, 250)) pygame.display.flip() clock.tick(60) moves = set()