Created
October 6, 2021 02:39
-
-
Save jpark9013/d46b63c7bee2056c4f0d0d4d73797b46 to your computer and use it in GitHub Desktop.
2048 game
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
import random | |
class Board: | |
_DIGITS = 4 | |
def __init__(self, height=4, width=4): | |
self.h = height | |
self.w = width | |
self.board = [[0 for _ in range(self.w)] for _ in range(self.h)] | |
# string representation of the board | |
def __str__(self): | |
width = self.w * (self._DIGITS + 3) + 1 | |
line = "".join("-" for _ in range(width)) | |
matrix = [] | |
for i in range(self.h): | |
matrix.append(line) | |
cur_line = [] | |
for j in range(self.w): | |
if self.board[i][j] != 0: | |
num = str(self.board[i][j]) | |
else: | |
num = "" | |
spaces = "".join(" " for i in range(self._DIGITS - len(num) + 1)) | |
cur_line.extend(("- ", num, spaces)) | |
cur_line.append("-") | |
matrix.append("".join(cur_line)) | |
matrix.append(line) | |
return "\n".join(matrix) | |
# spawns either a 2 or 4 on a random part of the grid | |
def spawn(self): | |
positions = [(i, j) for j in range(self.w) for i in range(self.h) if self.board[i][j] == 0] | |
assert len(positions) > 0 | |
square = random.choice(positions) | |
value = random.choice((2, 4)) | |
self.board[square[0]][square[1]] = value | |
# returns a boolean for if the game has ended (all squares occupied) | |
def ended(self): | |
for row in self.board: | |
for square in row: | |
if square == 0: | |
return False | |
return True | |
# for the following 4 functions | |
# if nothing is moved, return False | |
# else, return True | |
def move_down(self): | |
flag = False | |
for j in range(self.w): | |
for k in range(self.h, 1, -1): | |
cur = 0 | |
for i in range(k): | |
if self.board[i][j] in (0, cur): | |
flag = True | |
self.board[i][j] += cur | |
if i: | |
self.board[i - 1][j] = 0 | |
else: | |
cur = self.board[i][j] | |
return flag | |
def move_up(self): | |
flag = False | |
for j in range(self.w): | |
for k in range(1, self.h): | |
cur = 0 | |
for i in range(k, -1, -1): | |
if self.board[i][j] in (0, cur): | |
flag = True | |
self.board[i][j] += cur | |
if i < k: | |
self.board[i + 1][j] = 0 | |
else: | |
cur = self.board[i][j] | |
return flag | |
def move_right(self): | |
flag = False | |
for i in range(self.h): | |
for k in range(self.w, 1, -1): | |
cur = 0 | |
for j in range(k): | |
if self.board[i][j] in (0, cur): | |
flag = True | |
self.board[i][j] += cur | |
if j: | |
self.board[i][j - 1] = 0 | |
else: | |
cur = self.board[i][j] | |
return flag | |
def move_left(self): | |
flag = False | |
for i in range(self.h): | |
for k in range(1, self.w): | |
cur = 0 | |
for j in range(k, -1, -1): | |
if self.board[i][j] in (0, cur): | |
flag = True | |
self.board[i][j] += cur | |
if j < k: | |
self.board[i][j + 1] = 0 | |
else: | |
cur = self.board[i][j] | |
return flag | |
def score(self): | |
return sum(self.board[i][j] for j in range(self.w) for i in range(self.h)) | |
@classmethod | |
def play_game(cls): | |
def print_sep(): | |
print("---------------------------------") | |
cboard = cls() | |
def apply(char): | |
if char == "U": | |
return cboard.move_up() | |
elif char == "D": | |
return cboard.move_down() | |
elif char == "L": | |
return cboard.move_left() | |
else: | |
return cboard.move_right() | |
def get_dir(): | |
prompt = "Please input either U, D, L, or R.\n" | |
direction = input(prompt) | |
while direction not in ("U", "D", "L", "R"): | |
direction = input(prompt) | |
return direction | |
def apply_move(): | |
d = get_dir() | |
while not apply(d): | |
d = get_dir() | |
while not cboard.ended(): | |
cboard.spawn() | |
print(cboard) | |
print_sep() | |
apply_move() | |
print_sep() | |
print("Your final score is: ", cboard.score()) | |
if __name__ == "__main__": | |
Board.play_game() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment