How does the current Game2048 class work?

In this short notebook, we introduce how the class method should be called in our experiment code.


In [1]:
from game import Game2048

A game round demo


In [2]:
g = Game2048(game_mode=False) # False means AI mode

In [3]:
g.print_game()
g.active_player


[0, 0, 4, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
Out[3]:
'Agent'

In [4]:
moves = g.moves_available()
moves


Out[4]:
[0, 1, 3]

In [5]:
# We can perform an action for the "Agent"
g.perform_move(moves[0])


Out[5]:
True

In [6]:
g.print_game()
g.active_player # switched


[4, 0, 0, 0]
[2, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
Out[6]:
'Computer'

In [7]:
# Computer's turn, no need for a move input
g.perform_move() # Return whether the board changed


Out[7]:
True

In [8]:
g.print_game()


[4, 0, 0, 0]
[2, 0, 0, 0]
[0, 0, 0, 0]
[0, 4, 0, 0]

In [9]:
# Again, we can perform an action for the "Agent"
moves = g.moves_available()
g.perform_move(moves[0])


Out[9]:
True

In [10]:
g.print_game()
g.active_player


[4, 0, 0, 0]
[2, 0, 0, 0]
[0, 0, 0, 0]
[4, 0, 0, 0]
Out[10]:
'Computer'

In [11]:
g.perform_move()
g.print_game()
g.active_player


[4, 0, 0, 0]
[2, 0, 0, 0]
[0, 0, 0, 0]
[4, 0, 0, 2]
Out[11]:
'Agent'

Let's check whether Game2048.moves_available() works well

We expect that moves is [0, 1, 2, 3] here.


In [12]:
g = Game2048('', game_mode=False)
g.board = [
    [2, 0, 0, 0],
    [2, 0, 0, 0],
    [2, 0, 0, 0],
    [2, 0, 0, 0]
]
g.print_game()
g.active_player


[2, 0, 0, 0]
[2, 0, 0, 0]
[2, 0, 0, 0]
[2, 0, 0, 0]
Out[12]:
'Agent'

We expect g.moves_available() return [1, 2, 3].


In [13]:
g.moves_available()


Out[13]:
[1, 2, 3]

In [ ]: