Пример #1
0
 def test_merge(self):
     new_matrix = Game2048.merge(self, self.matrix)
     self.assertEqual(new_matrix, self.matrix)
     new_matrix = Game2048.merge(self, Game2048.compress(self, self.matrix))
     self.assertEqual(
         new_matrix,
         [[4, 0, 0, 0], [2, 4, 2, 0], [4, 4, 0, 0], [4, 0, 0, 0]])
Пример #2
0
def test_check_finish():
    # 1st test:
    board_state_in_progress = [
        [0, 0, 2, 0],
        [0, 0, 0, 8],
        [16, 0, 0, 0],
        [0, 0, 0, 4]
    ]

    test_game = Game2048(board_state_in_progress)
    test_game.check_finish()
    assert test_game.get_game_status() == Game2048.GameStatus.IN_PROGRESS

    # 2st test:
    full_board_state_in_finish = [
        [8, 4, 2, 4],
        [4, 2, 4, 8],
        [16, 32, 16, 2],
        [2, 16, 8, 4]
    ]

    test_game = Game2048(full_board_state_in_finish)
    test_game.check_finish()
    assert test_game.get_game_status() == Game2048.GameStatus.LOSS

    # 3st test:
    full_board_state_not_in_finish = [
        [8, 2, 16, 4],
        [4, 2, 4, 8],
        [16, 32, 16, 2],
        [2, 16, 8, 4]
    ]

    test_game = Game2048(full_board_state_not_in_finish)
    test_game.check_finish()
    assert test_game.get_game_status() == Game2048.GameStatus.IN_PROGRESS

    # 4st test:
    full_board_state2_not_in_finish = [
        [8, 2, 16, 4],
        [4, 8, 4, 8],
        [16, 32, 32, 2],
        [2, 16, 8, 4]
    ]

    test_game = Game2048(full_board_state2_not_in_finish)
    test_game.check_finish()
    assert test_game.get_game_status() == Game2048.GameStatus.IN_PROGRESS
Пример #3
0
def main():
    game = Game2048()
    print('WELCOME TO GAME!')
    print(SimpleBoardStatePrinter.print(game.get_board_state()))

    while game.get_game_status() == Game2048.GameStatus.IN_PROGRESS:
        movement = None
        char = readchar()

        while movement is None:
            if char == 'w':
                movement = Game2048.Movements.UP
            elif char == 's':
                movement = Game2048.Movements.DOWN
            elif char == 'd':
                movement = Game2048.Movements.RIGHT
            elif char == 'a':
                movement = Game2048.Movements.LEFT
            else:
                continue

        game.full_turn(movement)
        print(SimpleBoardStatePrinter.print(game.get_board_state()))
        print('Step:', game.get_number_of_moves())
        print('Score:', game.get_score())

    if game.get_game_status() == Game2048.GameStatus.WIN:
        print('Thank you! That was a good game!')
    if game.get_game_status() == Game2048.GameStatus.LOSS:
        print('Thank you! Get lucky another time!')
 def test_verify_game_state_zero(self):
     game = Game2048(4)
     board = np.array(
         [[8, 16, 2, 2], [2, 4, 8, 0], [8, 0, 0, 0], [2, 2, 0, 0]],
         dtype=np.uint32)
     game.set_board(board)
     done = game.verify_game_state()
     self.assertEqual(done, False)
 def test_verify_game_state_merge(self):
     game = Game2048(4)
     board = np.array(
         [[2, 4, 2, 2], [2, 16, 8, 4], [4, 128, 64, 8], [16, 4, 2, 4]],
         dtype=np.uint32)
     game.set_board(board)
     done = game.verify_game_state()
     self.assertEqual(done, False)
 def test_down_total(self):
     game = Game2048(4)
     board = np.array(
         [[0, 0, 0, 0], [2, 0, 0, 4], [2, 8, 8, 16], [4, 16, 8, 2]],
         dtype=np.uint32)
     game.set_board(board)
     game.make_move(1)
     game.confirm_move()
     total = game.get_move_score()
     self.assertEqual(total, 20)
 def test_left_total(self):
     game = Game2048(4)
     board = np.array(
         [[8, 16, 2, 2], [2, 4, 8, 0], [8, 0, 0, 0], [2, 2, 0, 0]],
         dtype=np.uint32)
     game.set_board(board)
     game.make_move(3)
     game.confirm_move()
     total = game.get_move_score()
     self.assertEqual(total, 8)
 def test_right_total(self):
     game = Game2048(4)
     board = np.array(
         [[0, 2, 0, 0], [16, 2, 0, 0], [32, 16, 0, 0], [4, 2, 4, 4]],
         dtype=np.uint32)
     game.set_board(board)
     game.make_move(2)
     game.confirm_move()
     total = game.get_move_score()
     self.assertEqual(total, 8)
 def test_up_board(self):
     game = Game2048(4)
     board = np.array(
         [[2, 2, 2, 0], [8, 0, 0, 0], [0, 2, 0, 0], [0, 2, 0, 0]],
         dtype=np.uint32)
     game.set_board(board)
     game.make_move(0)
     game.confirm_move()
     board = game.get_board()
     np.testing.assert_array_equal(
         board,
         np.array([[2, 4, 2, 0], [8, 2, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))
 def test_right_board(self):
     game = Game2048(4)
     board = np.array(
         [[0, 2, 0, 0], [16, 2, 0, 0], [32, 16, 0, 0], [4, 2, 4, 4]],
         dtype=np.uint32)
     game.set_board(board)
     game.make_move(2)
     game.confirm_move()
     board = game.get_board()
     np.testing.assert_array_equal(
         board,
         np.array([[0, 0, 0, 2], [0, 0, 16, 2], [0, 0, 32, 16],
                   [0, 4, 2, 8]]))
 def test_down_board(self):
     game = Game2048(4)
     board = np.array(
         [[0, 0, 0, 0], [2, 0, 0, 4], [2, 8, 8, 16], [4, 16, 8, 2]],
         dtype=np.uint32)
     game.set_board(board)
     game.make_move(1)
     game.confirm_move()
     board = game.get_board()
     np.testing.assert_array_equal(
         board,
         np.array([[0, 0, 0, 0], [0, 0, 0, 4], [4, 8, 0, 16],
                   [4, 16, 16, 2]]))
Пример #12
0
def main(stdscr):
    initCurses(stdscr)
    input2048 = Input2048(stdscr)
    game2048 = Game2048()
    display2048 = Display2048()

    while True:
        stdscr.clear()
        display2048.display(stdscr, game2048)
        keyPressed = input2048.GetInput()
        input2048.HandleInput(keyPressed, game2048)

        if keyPressed == ord('q'):
            break
        if keyPressed == ord('n'):
            game2048.newGame()

    unInitCurses(stdscr)
Пример #13
0
 def test_up_button(self):
     new_matrix = Game2048.up_button(self.new_game, self.matrix)
     self.assertEqual(
         new_matrix,
         [[4, 4, 4, 4], [2, 2, 0, 4], [0, 0, 0, 0], [0, 0, 0, 0]])
Пример #14
0
 def test_compress(self):
     new_matrix = Game2048.compress(self, self.matrix)
     self.assertEqual(
         new_matrix,
         [[2, 2, 0, 0], [2, 4, 2, 0], [4, 2, 2, 0], [2, 2, 0, 0]])
Пример #15
0
 def test_reverse(self):
     new_matrix = Game2048.reverse(self.matrix)
     self.assertEqual(
         new_matrix,
         [[2, 0, 2, 0], [2, 4, 2, 0], [2, 0, 2, 4], [2, 0, 0, 2]])
Пример #16
0
 def test_transpose(self):
     new_matrix = Game2048.transpose(self.matrix)
     self.assertEqual(
         new_matrix,
         [[0, 0, 4, 2], [2, 2, 2, 0], [0, 4, 0, 0], [2, 2, 2, 2]])
Пример #17
0
 def test_left_button(self):
     new_matrix = Game2048.left_button(self.new_game, self.matrix)
     self.assertEqual(
         new_matrix,
         [[4, 0, 0, 0], [2, 4, 2, 0], [4, 4, 0, 0], [4, 0, 0, 0]])
Пример #18
0
def test_moving_merge():
    # 1st test:
    board_state_moving_4cells = [
        [0, 0, 2, 0],
        [0, 0, 0, 8],
        [16, 0, 0, 0],
        [0, 0, 0, 4]
    ]

    board_state_moving_4cells_up = '''
     16      0      2      8
      0      0      0      4
      0      0      0      0
      0      0      0      0
'''

    test_game = Game2048(board_state_moving_4cells)
    test_game.handle_turn(Game2048.Movements.UP)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_moving_4cells_up[1:]

    # 2nd test:
    board_state_moving_5cells = [
        [128, 0, 2, 0],
        [4, 0, 0, 8],
        [0, 16, 0, 0],
        [0, 0, 0, 2]
    ]

    board_state_moving_5cells_right = '''
      0      0    128      2
      0      0      4      8
      0      0      0     16
      0      0      0      2
'''

    test_game = Game2048(board_state_moving_5cells)
    test_game.handle_turn(Game2048.Movements.RIGHT)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_moving_5cells_right[1:]

    # 3rd test:
    board_state_merge_2cells = [
        [128, 128, 2, 0],
        [4, 0, 0, 4],
        [0, 0, 0, 0],
        [0, 0, 0, 2]
    ]

    board_state_merge_2cells_left = '''
    256      2      0      0
      8      0      0      0
      0      0      0      0
      2      0      0      0
'''

    test_game = Game2048(board_state_merge_2cells)
    test_game.handle_turn(Game2048.Movements.LEFT)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_merge_2cells_left[1:]

    # 4th test:
    board_state_merge_4cells = [
        [4,  8, 16, 0],
        [4, 2, 0, 4],
        [0, 2, 0, 0],
        [0, 0, 16, 4]
    ]

    board_state_merge_4cells_down = '''
      0      0      0      0
      0      0      0      0
      0      8      0      0
      8      4     32      8
'''

    test_game = Game2048(board_state_merge_4cells)
    test_game.handle_turn(Game2048.Movements.DOWN)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_merge_4cells_down[1:]

    # 5th test:
    board_state_merge_4cells_up = '''
      8      8     32      8
      0      4      0      0
      0      0      0      0
      0      0      0      0
'''

    test_game = Game2048(board_state_merge_4cells)
    test_game.handle_turn(Game2048.Movements.UP)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_merge_4cells_up[1:]

    # 6th test:
    board_state_merge_5cells = [
        [4, 16, 16, 4],
        [4, 4, 0, 16],
        [2, 2, 8, 8],
        [0, 0, 16, 16]
    ]

    board_state_merge_5cells_left = '''
      4     32      4      0
      8     16      0      0
      4     16      0      0
     32      0      0      0
'''

    test_game = Game2048(board_state_merge_5cells)
    test_game.handle_turn(Game2048.Movements.LEFT)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_merge_5cells_left[1:]

    # 7th test:
    board_state_edge_case1 = [
        [0, 0, 0, 0],
        [2, 2, 2, 2],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ]

    board_state_edge_case1_right = '''
      0      0      0      0
      0      0      4      4
      0      0      0      0
      0      0      0      0
'''

    test_game = Game2048(board_state_edge_case1)
    test_game.handle_turn(Game2048.Movements.RIGHT)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_edge_case1_right[1:]

    # 8th test:
    board_state_edge_case2 = [
        [0, 0, 0, 0],
        [2, 2, 0, 2],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ]

    board_state_edge_case2_right = '''
      0      0      0      0
      0      0      2      4
      0      0      0      0
      0      0      0      0
'''

    test_game = Game2048(board_state_edge_case2)
    test_game.handle_turn(Game2048.Movements.RIGHT)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_edge_case2_right[1:]

    # 9th test:
    board_state_edge_case3 = [
        [0, 0, 0, 0],
        [2, 2, 2, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
    ]

    board_state_edge_case3_left = '''
      0      0      0      0
      4      2      0      0
      0      0      0      0
      0      0      0      0
'''

    test_game = Game2048(board_state_edge_case3)
    test_game.handle_turn(Game2048.Movements.LEFT)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_edge_case3_left[1:]

    # 10th test:
    board_state_edge_case4 = [
        [0, 2, 0, 0],
        [0, 2, 0, 0],
        [0, 4, 0, 0],
        [0, 2, 0, 0]
    ]

    board_state_edge_case4_down = '''
      0      0      0      0
      0      4      0      0
      0      4      0      0
      0      2      0      0
'''

    test_game = Game2048(board_state_edge_case4)
    test_game.handle_turn(Game2048.Movements.DOWN)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_edge_case4_down[1:]

    # 11th test:
    board_state_edge_case5 = [
        [8, 16, 32, 64],
        [0, 2, 4, 32],
        [2, 0, 4, 16],
        [0, 0, 0, 16]
    ]

    board_state_edge_case5_up = '''
      8     16     32     64
      2      2      8     32
      0      0      0     32
      0      0      0      0
'''

    test_game = Game2048(board_state_edge_case5)
    test_game.handle_turn(Game2048.Movements.UP)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_edge_case5_up[1:]

    # 12th test:
    board_state_edge_case6 = [
        [0, 8, 0, 0],
        [0, 4, 0, 0],
        [0, 2, 0, 0],
        [0, 2, 0, 0]
    ]

    board_state_edge_case6_up = '''
      0      8      0      0
      0      4      0      0
      0      4      0      0
      0      0      0      0
'''

    test_game = Game2048(board_state_edge_case6)
    test_game.handle_turn(Game2048.Movements.UP)
    assert SimpleBoardStatePrinter.print(test_game.get_board_state()) == board_state_edge_case6_up[1:]
Пример #19
0
 def test_down_button(self):
     new_matrix = Game2048.down_button(self.new_game, self.matrix)
     self.assertEqual(
         new_matrix,
         [[0, 0, 0, 0], [0, 0, 0, 0], [4, 2, 0, 4], [2, 4, 4, 4]])
Пример #20
0
 def test_right_button(self):
     new_matrix = Game2048.right_button(self.new_game, self.matrix)
     self.assertEqual(
         new_matrix,
         [[0, 0, 0, 4], [0, 2, 4, 2], [0, 0, 4, 4], [0, 0, 0, 4]])
Пример #21
0
from game_2048 import Game2048
import numpy as np
from keras.models import load_model
import time

model = load_model('pretrained/dqn2048_cnn.h5')
model.summary()
game = Game2048()
game.reset()
s = game.get_state()
game_step = 0
while True:
    print(game.board)
    state = s[np.newaxis, :, :, np.newaxis]
    state = np.log(state + 1) / 16
    action_index = model.predict(state)

    s_, r, done = game.step(np.argmax(action_index))
    # print('action:', game.actions[action_index])
    # print('game:\n', s_, '\n')

    s = s_
    if done:
        print('final:\n', game.board)
        print('score:', game.get_score(), ' board sum:', np.sum(game.board), ' play step:', game.n_step)
        break

    game_step += 1
    time.sleep(1)
Пример #22
0
 def test_process(self):
     new_matrix = Game2048.process(self.new_game, self.matrix)
     self.assertEqual(
         new_matrix,
         [[4, 0, 0, 0], [2, 4, 2, 0], [4, 4, 0, 0], [4, 0, 0, 0]])
Пример #23
0
 def setUp(self):
     self.new_game = Game2048()
     self.matrix = [[0, 2, 0, 2], [0, 2, 4, 2], [4, 2, 0, 2], [2, 0, 0, 2]]