Last active
August 12, 2018 07:03
-
-
Save shiracamus/110280a94873c99a241e3ba8e1888a31 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import random | |
SIZE = 4 | |
DIGITS = 4 | |
class Game: | |
def __init__(self, board=None, verbose=True): | |
self.board = [row[:] for row in board or [[0] * SIZE] * SIZE] | |
self.verbose = verbose | |
self.score = 0 | |
def show(self): | |
separator = ' -' + '-' * (DIGITS + len(' | ')) * SIZE | |
print(separator) | |
for row in self.board: | |
print(' | ' + ' | '.join(f'{tile:{DIGITS}}' for tile in row) + ' |') | |
print(separator) | |
def scoring(self, tile): | |
self.score += tile * 2 | |
if self.verbose: | |
print(f'{tile}+{tile}={tile*2}') | |
def move_left(self): | |
moved = False | |
for row in self.board: | |
for left in range(SIZE - 1): | |
for right in range(left + 1, SIZE): | |
if row[right] == 0: | |
continue | |
if row[left] == 0: | |
row[left] = row[right] | |
row[right] = 0 | |
moved = True | |
continue | |
if row[left] == row[right]: | |
self.scoring(row[right]) | |
row[left] += row[right] | |
row[right] = 0 | |
moved = True | |
break | |
if row[left] != row[right]: | |
break | |
return moved | |
def rotate_left(self): | |
self.board = [list(row) for row in zip(*self.board)][::-1] | |
def rotate_right(self): | |
self.board = [list(row)[::-1] for row in zip(*self.board)] | |
def rotate_turn(self): | |
self.board = [row[::-1] for row in self.board][::-1] | |
def flick_left(self): | |
moved = self.move_left() | |
return moved | |
def flick_right(self): | |
self.rotate_turn() | |
moved = self.move_left() | |
self.rotate_turn() | |
return moved | |
def flick_up(self): | |
self.rotate_left() | |
moved = self.move_left() | |
self.rotate_right() | |
return moved | |
def flick_down(self): | |
self.rotate_right() | |
moved = self.move_left() | |
self.rotate_left() | |
return moved | |
def playable(self): | |
return any(flick(Game(self.board, verbose=False)) | |
for flick in (Game.flick_left, Game.flick_right, | |
Game.flick_up, Game.flick_down)) | |
def put_tile(self): | |
zeros = [(y, x) | |
for y in range(SIZE) | |
for x in range(SIZE) | |
if self.board[y][x] == 0] | |
y, x = random.choice(zeros) | |
self.board[y][x] = random.choice((2, 4)) | |
def play(): | |
game = Game() | |
game.put_tile() | |
game.put_tile() | |
game.show() | |
key_flick = {'r': game.flick_right, | |
'l': game.flick_left, | |
'u': game.flick_up, | |
'd': game.flick_down} | |
try: | |
count = 0 | |
while game.playable(): | |
key = input(f'input {count}th move>>> ') | |
if key not in key_flick: | |
print('err') | |
elif key_flick[key](): | |
game.put_tile() | |
game.show() | |
print(f'score = {game.score}') | |
count += 1 | |
except (KeyboardInterrupt, EOFError): | |
print() | |
print('#######################') | |
print('Game Over') | |
print(f'Final Score = {game.score}') | |
print('#######################') | |
if __name__ == '__main__': | |
play() |
This file contains hidden or 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
from game2048 import Game | |
def flick_left_test(): | |
""" | |
>>> game = Game() | |
>>> game.flick_left() | |
False | |
>>> game.board | |
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] | |
>>> game.score | |
0 | |
>>> game = Game([[2,0,0,0],[2,4,0,0],[2,4,2,0],[2,4,2,4]]) | |
>>> game.flick_left() | |
False | |
>>> game.board | |
[[2, 0, 0, 0], [2, 4, 0, 0], [2, 4, 2, 0], [2, 4, 2, 4]] | |
>>> game.score | |
0 | |
>>> game = Game([[0,0,0,0],[0,0,0,2],[0,0,2,0],[0,0,2,2]]) | |
>>> game.flick_left() | |
2+2=4 | |
True | |
>>> game.board | |
[[0, 0, 0, 0], [2, 0, 0, 0], [2, 0, 0, 0], [4, 0, 0, 0]] | |
>>> game.score | |
4 | |
>>> game = Game([[0,2,0,0],[0,2,0,2],[0,2,2,0],[0,2,2,2]]) | |
>>> game.flick_left() | |
2+2=4 | |
2+2=4 | |
2+2=4 | |
True | |
>>> game.board | |
[[2, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0], [4, 2, 0, 0]] | |
>>> game.score | |
12 | |
>>> game = Game([[2,0,0,0],[2,0,0,2],[2,0,2,0],[2,0,2,2]]) | |
>>> game.flick_left() | |
2+2=4 | |
2+2=4 | |
2+2=4 | |
True | |
>>> game.board | |
[[2, 0, 0, 0], [4, 0, 0, 0], [4, 0, 0, 0], [4, 2, 0, 0]] | |
>>> game.score | |
12 | |
>>> game = Game([[2,2,0,0],[2,2,0,2],[2,2,2,0],[2,2,2,2]]) | |
>>> game.flick_left() | |
2+2=4 | |
2+2=4 | |
2+2=4 | |
2+2=4 | |
2+2=4 | |
True | |
>>> game.board | |
[[4, 0, 0, 0], [4, 2, 0, 0], [4, 2, 0, 0], [4, 4, 0, 0]] | |
>>> game.score | |
20 | |
>>> game = Game([[4,4,4,4],[4,4,4,2],[4,4,2,4],[4,4,2,2]]) | |
>>> game.flick_left() | |
4+4=8 | |
4+4=8 | |
4+4=8 | |
4+4=8 | |
4+4=8 | |
2+2=4 | |
True | |
>>> game.board | |
[[8, 8, 0, 0], [8, 4, 2, 0], [8, 2, 4, 0], [8, 4, 0, 0]] | |
>>> game.score | |
44 | |
>>> game = Game([[4,2,4,4],[4,2,4,2],[4,2,2,4],[4,2,2,2]]) | |
>>> game.flick_left() | |
4+4=8 | |
2+2=4 | |
2+2=4 | |
True | |
>>> game.board | |
[[4, 2, 8, 0], [4, 2, 4, 2], [4, 4, 4, 0], [4, 4, 2, 0]] | |
>>> game.score | |
16 | |
>>> game = Game([[2,4,4,4],[2,4,4,2],[2,4,2,4],[2,4,2,2]]) | |
>>> game.flick_left() | |
4+4=8 | |
4+4=8 | |
2+2=4 | |
True | |
>>> game.board | |
[[2, 8, 4, 0], [2, 8, 2, 0], [2, 4, 2, 4], [2, 4, 4, 0]] | |
>>> game.score | |
20 | |
>>> game = Game([[2,2,4,4],[2,2,4,2],[2,2,2,4],[2,2,2,2]]) | |
>>> game.flick_left() | |
2+2=4 | |
4+4=8 | |
2+2=4 | |
2+2=4 | |
2+2=4 | |
2+2=4 | |
True | |
>>> game.board | |
[[4, 8, 0, 0], [4, 4, 2, 0], [4, 2, 4, 0], [4, 4, 0, 0]] | |
>>> game.score | |
28 | |
""" | |
def rotate_test(): | |
""" | |
>>> game = Game([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]) | |
>>> game.rotate_left() | |
>>> game.board | |
[[4, 8, 12, 16], [3, 7, 11, 15], [2, 6, 10, 14], [1, 5, 9, 13]] | |
>>> game = Game([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]) | |
>>> game.rotate_right() | |
>>> game.board | |
[[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]] | |
>>> game = Game([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]) | |
>>> game.rotate_turn() | |
>>> game.board | |
[[16, 15, 14, 13], [12, 11, 10, 9], [8, 7, 6, 5], [4, 3, 2, 1]] | |
""" | |
if __name__ == '__main__': | |
import doctest | |
doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment