Skip to content

Instantly share code, notes, and snippets.

Created March 22, 2014 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/9699729 to your computer and use it in GitHub Desktop.
Save anonymous/9699729 to your computer and use it in GitHub Desktop.
console 2048 (python 3)
import copy, random
empty_state = [[None, None, None], [None, None, None],
[None, None, None]]
score = 0
indices = []
for x in range(3):
for y in range(3):
indices.append([x, y])
def game_over(quit = False):
global score
if not quit:
print("Game Over")
print("Score =", score)
exit()
def add(state):
print('adding item to state:', state)
global indices
if random.randint(0, 9) > 0:
new_value = 2
else:
new_value = 4
empties = [index for index in indices if (state[index[0]][index[1]] ==
None)]
if empties == []:
game_over()
index = random.choice(empties)
new_state = copy.deepcopy(state)
new_state[index[0]][index[1]] = new_value
return new_state
def init():
return add(add(empty_state))
def left(state):
global score
print('shifting left from state:', state)
legal = False
new_state = copy.deepcopy(state)
for row_idx in range(len(state)):
old_row = state[row_idx]
new_row = new_state[row_idx]
#shifting left
if old_row[0] == None:
if old_row[1] == None:
if old_row[2]:
new_row[0] = old_row[2]
new_row[2] = None
legal = True
else:
new_row[0] = old_row[1]
new_row[1] = old_row[2]
new_row[2] = None
legal = True
elif old_row[1] == None:
if old_row[2]:
new_row[1] = old_row[2]
new_row[2] = None
legal = True
# doubling
if new_row[0] and new_row[0] == new_row[1]:
new_row[0] = new_row[0]*2
score += new_row[0]
new_row[1] = new_row[2]
new_row[2] = None
legal = True
elif new_row[1] and new_row[1] == new_row[2]:
new_row[1] = new_row[1]*2
score += new_row[1]
new_row[2] = None
legal = True
new_state[row_idx] = new_row
if legal:
new_state = add(new_state)
else:
print("No such move")
return new_state
def right(state):
global score
print('shifting right from state:', state)
legal = False
new_state = copy.deepcopy(state)
for row_idx in range(len(state)):
old_row = state[row_idx]
new_row = new_state[row_idx]
#shifting right
if old_row[2] == None:
if old_row[1] == None:
if old_row[0]:
new_row[2] = old_row[0]
new_row[0] = None
legal = True
else:
new_row[2] = old_row[1]
new_row[1] = old_row[0]
new_row[0] = None
legal = True
elif old_row[1] == None:
if old_row[0]:
new_row[1] = old_row[0]
new_row[0] = None
legal = True
# doubling
if new_row[2] and new_row[2] == new_row[1]:
new_row[2] = new_row[2]*2
score += new_row[2]
new_row[1] = new_row[0]
new_row[0] = None
legal = True
elif new_row[1] and new_row[1] == new_row[0]:
new_row[1] = new_row[1]*2
score += new_row[1]
new_row[0] = None
legal = True
new_state[row_idx] = new_row
if legal:
new_state = add(new_state)
else:
print("No such move")
return new_state
def up(state):
global score
print('shifting up from state:', state)
legal = False
new_state = copy.deepcopy(state)
#shifting up
for col_idx in range(len(state[0])):
if new_state[0][col_idx] == None:
if new_state[1][col_idx] == None:
if new_state[2][col_idx]:
new_state[0][col_idx] = new_state[2][col_idx]
new_state[2][col_idx] = None
legal = True
else:
new_state[0][col_idx] = new_state[1][col_idx]
new_state[1][col_idx] = new_state[2][col_idx]
new_state[2][col_idx] = None
legal = True
elif new_state[1][col_idx] == None and new_state[2][col_idx]:
new_state[1][col_idx] = new_state[2][col_idx]
new_state[2][col_idx] = None
legal = True
#doubling
for col_idx in range(len(new_state[0])):
if new_state[0][col_idx] and new_state[0][col_idx] == new_state[1][col_idx]:
new_state[0][col_idx] = new_state[0][col_idx] * 2
new_state[1][col_idx] = new_state[2][col_idx]
new_state[2][col_idx] = None
score += new_state[0][col_idx]
legal = True
elif new_state[1][col_idx] and new_state[1][col_idx] == new_state[2][col_idx]:
new_state[1][col_idx] = new_state[1][col_idx] * 2
new_state[2][col_idx] = None
score += new_state[1][col_idx]
legal = True
if legal:
new_state = add(new_state)
else:
print("No such move")
return new_state
def down(state):
global score
print('shifting down from state:', state)
legal = False
new_state = copy.deepcopy(state)
#shifting down
for col_idx in range(len(state[2])):
if new_state[2][col_idx] == None:
if new_state[1][col_idx] == None:
if new_state[0][col_idx]:
new_state[2][col_idx] = new_state[0][col_idx]
new_state[0][col_idx] = None
legal = True
else:
new_state[2][col_idx] = new_state[1][col_idx]
new_state[1][col_idx] = new_state[0][col_idx]
new_state[0][col_idx] = None
legal = True
elif new_state[1][col_idx] == None and new_state[0][col_idx]:
new_state[1][col_idx] = new_state[0][col_idx]
new_state[0][col_idx] = None
legal = True
#doubling
for col_idx in range(len(new_state[2])):
if new_state[2][col_idx] and new_state[2][col_idx] == new_state[1][col_idx]:
new_state[2][col_idx] = new_state[2][col_idx] * 2
new_state[1][col_idx] = new_state[0][col_idx]
new_state[0][col_idx] = None
score += new_state[2][col_idx]
legal = True
elif new_state[1][col_idx] and new_state[1][col_idx] == new_state[0][col_idx]:
new_state[1][col_idx] = new_state[1][col_idx] * 2
new_state[0][col_idx] = None
score += new_state[1][col_idx]
legal = True
if legal:
new_state = add(new_state)
else:
print("No such move")
return new_state
def state_print(state):
global score
for row in state:
print(" |--------+--------+--------| ")
print(" | ", end="")
for box in row:
if box:
print('{:^6}'.format(str(box)), end = '')
else:
print(" ", end = '')
print(" | ", end = '')
print()
print(" |--------+--------+--------| ")
print("Score:",score)
def main():
curr_state = init()
WASD = False
while True:
state_print(curr_state)
move = input().lower()
counter = 0
while move not in ['l', 'r', 'u', 'd', 'q', 'x', '?', 'exit',
'quit', 'left', 'right', 'up', 'down',
'w', 'a', 's', 'd', 'help']:
print("Use 'l', 'r', 'u', 'd' for moves, '?' for help,",
"or 'q' to quit.")
counter += 1
if counter > 20:
print("Ok, maybe your 'q' key is broken. You can quit.")
exit()
move = input().lower()
if move in ['w', 'a', 's']:
WASD = True
print("Entering WASD mode")
elif move in ['r', 'u', 'l']:
WASD = False
if move in ['x', 'q', 'exit', 'quit']:
print("Goodbye!")
game_over(quit = True)
if move in ['l', 'left', 'a']:
print("Moving left...")
curr_state = left(curr_state)
if move in ['r', 'right'] or (move == 'd' and WASD):
print("Moving right...")
curr_state = right(curr_state)
if move in ['u', 'up', 'w']:
print("Moving up...")
curr_state = up(curr_state)
if move in ['down', 's'] or (move == 'd' and not WASD):
print("Moving down...")
curr_state = down(curr_state)
if move in ['help', '?']:
print("Use (u)p, (d)own, (l)eft, (r)ight to move.")
print("WASD mode activated with w, a, or s.")
print("Return to UDLR mode with u, l, or r.")
print("Use e(x)it to quit.")
# other commands are, um... easter eggs, yeah.
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment