Last active
March 10, 2016 08:54
-
-
Save zlsun/69e4657b0c14b0431f13 to your computer and use it in GitHub Desktop.
Revisions
-
zlsun revised this gist
Mar 10, 2016 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -2,15 +2,19 @@ from random import choice from os import system from readchar import readchar, readkey from colorama import init from termcolor import colored from math import log2 N = 4 SEED = [2] * 9 + [4] FGS = ['white', 'green', 'yellow', 'blue', 'cyan', 'magenta', 'red'] TERM = (24, 80) OFFSET = (TERM[0] // 2 - 2, TERM[1] // 2 - 10) pos = lambda y, x: '\x1b[%d;%dH' % (y, x) color = lambda i: colored('%4d' % i, FGS[int(log2(i)) % len(FGS)] if i else 'grey') formatted = lambda m: '\n'.join(pos(y, OFFSET[1]) + ' '.join(color(i) for i in l) for l, y in zip(m, range(OFFSET[0], OFFSET[0] + 4))) combine = lambda l: ([l[0] * 2] + combine(l[2:]) if l[0] == l[1] else [l[0]] + combine(l[1:])) if len(l) >= 2 else l expand = lambda l: [l[i] if i < len(l) else 0 for i in range(N)] merge_left = lambda l: expand(combine(list(filter(bool, list(l))))) @@ -27,6 +31,7 @@ trans = {'a': left, 'd': right, 'w': up, 's': down} m = [[0] * N for _ in range(N)] init() add_num(m) draw(m) while True: -
zlsun revised this gist
Mar 10, 2016 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,4 @@ #!/bin/python3 from random import choice from os import system from readchar import readchar, readkey -
zlsun revised this gist
Mar 10, 2016 . No changes.There are no files selected for viewing
-
zlsun revised this gist
Mar 10, 2016 . No changes.There are no files selected for viewing
-
zlsun created this gist
Mar 10, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,51 @@ #!/bin/python # -*- encoding: u8 -*- from random import choice from os import system from readchar import readchar, readkey from termcolor import colored from math import log2 N = 4 SEED = [2] * 9 + [4] FGS = ['white', 'green', 'yellow', 'blue', 'cyan', 'magenta', 'red'] color = lambda i: colored('%4d' % i, FGS[int(log2(i)) % len(FGS)] if i else 'grey') formatted = lambda m: '\n'.join(' '.join(color(i) for i in l) for l in m) combine = lambda l: ([l[0] * 2] + combine(l[2:]) if l[0] == l[1] else [l[0]] + combine(l[1:])) if len(l) >= 2 else l expand = lambda l: [l[i] if i < len(l) else 0 for i in range(N)] merge_left = lambda l: expand(combine(list(filter(bool, list(l))))) merge_right = lambda l: merge_left(l[::-1])[::-1] left = lambda m: list(map(merge_left, m)) right = lambda m: list(map(merge_right, m)) up = lambda m: list(map(list, zip(*left(zip(*m))))) down = lambda m: list(map(list, zip(*right(zip(*m))))) add_num_impl = lambda m, p: m[p[0]].__setitem__(p[1], choice(SEED)) add_num = lambda m: add_num_impl(m, choice([(x, y) for x in range(N) for y in range(N) if not m[x][y]])) win = lambda m: 2048 in sum(m, []) gameover = lambda m: all(m == t(m) for t in trans.values()) draw = lambda m: system("clear") or print(formatted(m)) trans = {'a': left, 'd': right, 'w': up, 's': down} m = [[0] * N for _ in range(N)] add_num(m) draw(m) while True: while True: move = readkey() if move in list(trans.keys()) + ['q']: break if move == 'q': print('Quit!') break n = trans[move](m) if n != m: add_num(n) m = n draw(m) if win(m): print(colored('(^_^) You Win!', 'yellow')) break elif gameover(m): print(colored('(>﹏<) Game Over!', 'red')) break