-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.py
206 lines (170 loc) · 5.27 KB
/
logic.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#
# CS1010FC --- Programming Methodology
#
# Mission N Solutions
#
# Note that written answers are commented out to allow us to run your
# code easily while grading your problem set.
import random
import constants as c
#######
# Task 1a #
#######
# [Marking Scheme]
# Points to note:
# Matrix elements must be equal but not identical
# 1 mark for creating the correct matrix
def new_game(n):
matrix = []
for i in range(n):
matrix.append([0] * n)
matrix = add_two(matrix)
matrix = add_two(matrix)
return matrix
###########
# Task 1b #
###########
# [Marking Scheme]
# Points to note:
# Must ensure that it is created on a zero entry
# 1 mark for creating the correct loop
def add_two(mat):
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
while mat[a][b] != 0:
a = random.randint(0, len(mat)-1)
b = random.randint(0, len(mat)-1)
mat[a][b] = 2
return mat
###########
# Task 1c #
###########
# [Marking Scheme]
# Points to note:
# Matrix elements must be equal but not identical
# 0 marks for completely wrong solutions
# 1 mark for getting only one condition correct
# 2 marks for getting two of the three conditions
# 3 marks for correct checking
def game_state(mat):
# check for win cell
for i in range(len(mat)):
for j in range(len(mat[0])):
if mat[i][j] == 2048:
return 'win'
# check for any zero entries
for i in range(len(mat)):
for j in range(len(mat[0])):
if mat[i][j] == 0:
return 'not over'
# check for same cells that touch each other
for i in range(len(mat)-1):
# intentionally reduced to check the row on the right and below
# more elegant to use exceptions but most likely this will be their solution
for j in range(len(mat[0])-1):
if mat[i][j] == mat[i+1][j] or mat[i][j+1] == mat[i][j]:
return 'not over'
for k in range(len(mat)-1): # to check the left/right entries on the last row
if mat[len(mat)-1][k] == mat[len(mat)-1][k+1]:
return 'not over'
for j in range(len(mat)-1): # check up/down entries on last column
if mat[j][len(mat)-1] == mat[j+1][len(mat)-1]:
return 'not over'
return 'lose'
###########
# Task 2a #
###########
# [Marking Scheme]
# Points to note:
# 0 marks for completely incorrect solutions
# 1 mark for solutions that show general understanding
# 2 marks for correct solutions that work for all sizes of matrices
def reverse(mat):
new = []
for i in range(len(mat)):
new.append([])
for j in range(len(mat[0])):
new[i].append(mat[i][len(mat[0])-j-1])
return new
###########
# Task 2b #
###########
# [Marking Scheme]
# Points to note:
# 0 marks for completely incorrect solutions
# 1 mark for solutions that show general understanding
# 2 marks for correct solutions that work for all sizes of matrices
def transpose(mat):
new = []
for i in range(len(mat[0])):
new.append([])
for j in range(len(mat)):
new[i].append(mat[j][i])
return new
##########
# Task 3 #
##########
# [Marking Scheme]
# Points to note:
# The way to do movement is compress -> merge -> compress again
# Basically if they can solve one side, and use transpose and reverse correctly they should
# be able to solve the entire thing just by flipping the matrix around
# No idea how to grade this one at the moment. I have it pegged to 8 (which gives you like,
# 2 per up/down/left/right?) But if you get one correct likely to get all correct so...
# Check the down one. Reverse/transpose if ordered wrongly will give you wrong result.
def cover_up(mat):
new = []
for j in range(c.GRID_LEN):
partial_new = []
for i in range(c.GRID_LEN):
partial_new.append(0)
new.append(partial_new)
done = False
for i in range(c.GRID_LEN):
count = 0
for j in range(c.GRID_LEN):
if mat[i][j] != 0:
new[i][count] = mat[i][j]
if j != count:
done = True
count += 1
return new, done
def merge(mat, done):
rew = 0
for i in range(c.GRID_LEN):
for j in range(c.GRID_LEN-1):
if mat[i][j] == mat[i][j+1] and mat[i][j] != 0:
mat[i][j] *= 2
mat[i][j+1] = 0
rew += mat[i][j]
done = True
return mat, done, rew
def up(game):
# return matrix after shifting up
game = transpose(game)
game, done = cover_up(game)
game, done, rew = merge(game, done)
game = cover_up(game)[0]
game = transpose(game)
return game, done, rew
def down(game):
game = reverse(transpose(game))
game, done = cover_up(game)
game, done, rew = merge(game, done)
game = cover_up(game)[0]
game = transpose(reverse(game))
return game, done, rew
def left(game):
# return matrix after shifting left
game, done = cover_up(game)
game, done, rew = merge(game, done)
game = cover_up(game)[0]
return game, done, rew
def right(game):
# return matrix after shifting right
game = reverse(game)
game, done = cover_up(game)
game, done, rew = merge(game, done)
game = cover_up(game)[0]
game = reverse(game)
return game, done, rew