-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex03.py
112 lines (89 loc) · 3.48 KB
/
ex03.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
def printGrid(grid):
print('\n')
print(f' {grid[0][0]} | {grid[0][1]} | {grid[0][2]} | {grid[0][3]}')
print('---|---|---|---')
print(f' {grid[1][0]} | {grid[1][1]} | {grid[1][2]} | {grid[1][3]}')
print('---|---|---|---')
print(f' {grid[2][0]} | {grid[2][1]} | {grid[2][2]} | {grid[2][3]}')
print('---|---|---|---')
print(f' {grid[3][0]} | {grid[3][1]} | {grid[3][2]} | {grid[3][3]}')
print('\n')
def swapPositions(grid, c1, c2):
tmpVal = grid[c1[1]][c1[0]]
grid[c1[1]][c1[0]] = grid[c2[1]][c2[0]]
grid[c2[1]][c2[0]] = tmpVal
def extractColumns(grid):
cols = []
for x in range(4):
col = []
for y in range(4):
col.append(grid[y][x])
cols.append(col)
return cols
def findShapes(grid, x, y):
verticalPart = [grid[i][x] for i in range(y, y + 3)] if y <= 1 else [grid[i][x] for i in range(y - 2, y + 1)]
verticalLeftPart = [grid[i][x - 1] for i in range(y, y + 3)] if y <= 1 else [grid[i][x - 1] for i in range(y - 2, y + 1)]
verticalRightPart = [grid[i][x + 1] for i in range(y, y + 3)] if y <= 1 else [grid[i][x + 1] for i in range(y - 2, y + 1)]
if verticalPart.count(verticalPart[0]) == 3:
return 'T'
if verticalLeftPart.count(verticalLeftPart[0]) == 3 or verticalRightPart.count(verticalRightPart[0]) == 3:
return 'L'
return ''
def findMatches(grid):
# check rows
for ind, row in enumerate(grid):
mostFrequent = max(row, key=row.count)
if row.count(mostFrequent) == 4:
return 'Legal move'
elif row[row.index(mostFrequent) : row.index(mostFrequent) + 3].count(mostFrequent) == 3:
shapes = findShapes(grid, row.index(mostFrequent) + 1, ind)[0]
if shapes:
return f'BOMB! {shapes} shape'
else:
return 'Legal move'
#check columns
for ind, col in enumerate(extractColumns(grid)):
mostFrequent = max(col, key=col.count)
if col.count(mostFrequent) == 4:
return 'Legal move'
elif col.count(mostFrequent) == 3 and col.index(mostFrequent) != 0:
return 'Legal move'
return 'Illegal move'
def validateMove(inp, grid):
splitInput = inp.split(' ')
if len(splitInput) != 3:
return 'Invalid input'
x = splitInput[0]
y = splitInput[1]
dir = splitInput[2]
if not x.isdigit() or not y.isdigit():
return 'Input valid integer numbers'
# can now cast safely to int
x = int(x)
y = int(y)
if (x < 0 or x > 3) or (y < 0 or y > 3):
return 'Values for x and y must be between 0 and 3'
if dir not in ['up', 'down', 'left', 'right']:
return 'Value for dir must be one of [up, down, left, right]'
# input should be valid
if (dir == 'up' and y == 0) or (dir == 'down' and y == 3) or (dir == 'left' and x == 0) or (dir == 'right' and x == 3):
return 'Out of bounds move'
newGrid = [row[:] for row in grid]
c1 = [x, y]
c2 = [x, y]
if dir == 'up':
c2[1] = y - 1
elif dir == 'down':
c2[1] = y + 1
elif dir == 'left':
c2[0] = x - 1
elif dir == 'right':
c2[0] = x + 1
swapPositions(newGrid, c1, c2)
print('Your move:')
printGrid(newGrid)
return findMatches(newGrid)
initialGrid = [[2, 2, 9, 2], [8, 2, 2, 5], [9, 6, 2, 4], [6, 9, 4, 7]]
printGrid(initialGrid)
print('Insert coordinates for your move, and a direction, which must be one of [up, down, left, right].\nCoordinates start from the top left, which is 0 0 and end on the bottom right, which is 3 3. Please provide the x value first, followed by the y value and then the direction value.\nA valid input looks like "1 2 down"\n\n')
print(validateMove(input(), initialGrid))