@OrlandoWong/

Wobbly2048.py

No description

Files
• main.py
main.py
```1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
```
```# Implementation of 2048

# Section 1: Boardprinting and movement functions
import random

def board_printer(board):
""" prints the current 2048 board"""
for i in range(size):
for j in range(size):
tile = " " if board[i][j] == 0 else board[i][j]
print( "|{}".format(tile), end="")
print("|")

def move_left(board):
""" Left movement. Input: board """
# 1) count remove all empty spaces (the 0's)
# 2) combine adjacent matching numbers,
# 3) re-add the empty spaces.
new_board = []
for row in range(size):
# 1: counting and removing all empty spaces
count = board[row].count(0)
newline = [num for num in board[row] if num != 0]

x = 0
while x < len(newline)-1:
if newline[x] == newline[x+1]:
newline[x] *= 2
del newline[x+1]
count += 1
x += 1

newline.extend([0]*count)
new_board.append(newline)
return new_board

def move_right(board):
""" Right movement - same as left"""
new_board = []
for row in range(size):
count = board[row].count(0)
newline = [num for num in board[row] if num != 0]

x = 0
while x < len(newline)-1:
if newline[x] == newline[x+1]:
newline[x] *= 2
del newline[x+1]
count += 1
x += 1
newline = [0]*count + newline
new_board.append(newline)
return new_board

def rotate90(board):
"""used to rotate the board clockwise 90 degrees"""
new_board = [[0 for _ in range(size)] for _ in range(size)]
for i in range(size):
for j in range(size):
new_board[j][size-1-i] = board[i][j]
return new_board

def rotateC90(board):
""" Rotates board counter clockwise 90 """
new_board = [[0 for _ in range(size)] for _ in range(size)]
for i in range(size):
for j in range(size):
new_board[size-1-j][i] = board[i][j]
return new_board

def move_up(board):
""" Moves board up"""
return rotateC90(move_right(rotate90(board)))

def move_down(board):
""" Moves board down"""
return rotateC90(move_left(rotate90(board)))

# Section 2: Spawn Mechanics
class BoardIsFull(Exception):
"""Raised when board is full"""
pass

def spawn(board):
""" Randomly spawn a 2 or 4 in an empty space """

if random.randint(1, 5) < 4:
spawn = 2
else:
spawn = 4

# Search for available empty spaces to place the spawn in
empty = [(i, j) for i in range(size) for j in range(size) if board[i][j] == 0]
if len(empty) == 0:
raise BoardIsFull
else:
tile = random.randint(0, len(empty)-1)
row = empty[tile][0]
col = empty[tile][1]
board[row][col] = spawn
return board

# Section 3: Game Over and Win condition functions

def no_more_moves(board):
"""Check for no a losing board"""
for i in range(size-1):
j = 0
while j < size-1:      # stop once you reach the second to last digit of the board
if board[i][j] == board[i][j+1]:    # check if the adjacent tile is equal
return False
if board[i][j] == board[i+1][j]:   # check if the below tile is equal
return False
j += 1
return True

# Section 4: Main function, game console'''

def main():
global size
size = int(input("How big of a board would you like? (Max 10 please)\n"))
board = [[0 for _ in range(size)] for _ in range(size)]

board = spawn(board)
print("this is the initial board")
board_printer(board)

run = True

while run:
move = input("Enter w,a,s,d  for movement. q to quit: ")
if move == 'a':
board = move_left(board)
elif move == 's':
board = move_down(board)
elif move == 'd':
board = move_right(board)
elif move == 'w':
board = move_up(board)
elif move == 'q':
run = False
print("Game Over!")

try:
board = spawn(board)
board_printer(board)
except BoardIsFull:
if no_more_moves(board): #function to check if no moves remaining
print("Game Over")
run = False
else: # Board still has moves
board_printer(board)

main()```
Fetching token