-
Notifications
You must be signed in to change notification settings - Fork 279
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an interactive, manual control mode.
This is useful for e.g. if you just want some advice and are playing on a platform where automatic control isn't available (e.g. on a phone).
- Loading branch information
Showing
6 changed files
with
155 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import ctypes | ||
import os | ||
|
||
for suffix in ['so', 'dll', 'dylib']: | ||
dllfn = 'bin/2048.' + suffix | ||
if not os.path.isfile(dllfn): | ||
continue | ||
ailib = ctypes.CDLL(dllfn) | ||
break | ||
else: | ||
print("Couldn't find 2048 library bin/2048.{so,dll,dylib}! Make sure to build it first.") | ||
exit() | ||
|
||
ailib.init_tables() | ||
|
||
ailib.find_best_move.argtypes = [ctypes.c_uint64] | ||
ailib.score_toplevel_move.argtypes = [ctypes.c_uint64, ctypes.c_int] | ||
ailib.score_toplevel_move.restype = ctypes.c_float | ||
ailib.execute_move.argtypes = [ctypes.c_int, ctypes.c_uint64] | ||
ailib.execute_move.restype = ctypes.c_uint64 | ||
|
||
def to_c_board(m): | ||
board = 0 | ||
i = 0 | ||
for row in m: | ||
for c in row: | ||
board |= int(c) << (4*i) | ||
i += 1 | ||
return board | ||
|
||
def from_c_board(n): | ||
board = [] | ||
i = 0 | ||
for ri in range(4): | ||
row = [] | ||
for ci in range(4): | ||
row.append((n >> (4 * i)) & 0xf) | ||
i += 1 | ||
board.append(row) | ||
return board | ||
|
||
def to_c_index(n): | ||
return [0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768].index(n) | ||
|
||
def from_c_index(c): | ||
if c == 0: return 0 | ||
return 2**c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from ailib import ailib, to_c_board, from_c_board, to_c_index, from_c_index | ||
from gamectrl import Generic2048Control | ||
|
||
def print_board(m): | ||
for row in m: | ||
for c in row: | ||
print('%8d' % from_c_index(c), end=' ') | ||
print() | ||
|
||
class ManualControl(Generic2048Control): | ||
def __init__(self): | ||
print("Enter board one row at a time, with entries separated by spaces") | ||
board = [] | ||
for ri in range(4): | ||
board.append([to_c_index(int(c)) for c in input("Row %d: " % (ri + 1)).split()]) | ||
self.cur_board = board | ||
|
||
def get_status(self): | ||
return "running" | ||
|
||
def restart_game(self): | ||
print("Game over - time to restart!") | ||
|
||
def continue_game(self): | ||
pass | ||
|
||
def get_score(self): | ||
# don't care | ||
return 0 | ||
|
||
def get_board(self): | ||
print("Current board:") | ||
print_board(self.cur_board) | ||
|
||
updates = input("Enter updates in the form r,c,n (1-indexed row/column), separated by spaces: ") | ||
for item in updates.split(): | ||
r, c, n = map(int, item.split(",")) | ||
self.cur_board[r-1][c-1] = to_c_index(n) | ||
|
||
return self.cur_board | ||
|
||
def execute_move(self, move): | ||
print("EXECUTE MOVE:", ["up", "down", "left", "right"][move]) | ||
self.cur_board = from_c_board(ailib.execute_move(move, to_c_board(self.cur_board))) |