-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTable.py
More file actions
94 lines (76 loc) · 3 KB
/
Table.py
File metadata and controls
94 lines (76 loc) · 3 KB
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
from Value import Value
from Cube import Cube
import math
import random
'''A table to put'''
class Table:
def __init__(self, rows = 10, cols = 20):
self.__rows = rows
self.__cols = cols
self.__cubes = [[None for x in range(cols)] for y in range(rows)]
@property
def rows(self):
return self.__rows
@property
def cols(self):
return self.__cols
def ValueHeigth(self, value: int, cols_by_digit = 1):
vs = str(value)
max_digit = max([int(d) for d in vs])
return math.ceil(max_digit / cols_by_digit)
def ValueWidth(self, value: int, cols_by_digit = 1):
return sum([(x if x < cols_by_digit else cols_by_digit) for x in [int(x) for x in str(value)]])
def SetValue(self, value: int, cols_by_digit = 1):
initial_row = int(self.rows / 2 - self.ValueHeigth(value, cols_by_digit) / 2)
initial_col = int(self.cols / 2 + self.ValueWidth(value, cols_by_digit) / 2)
vs = str(value)
p = len(vs)
col = 0
for d in [int(x) for x in reversed(vs)]:
if p == len(vs):
col = initial_col - (d if d < cols_by_digit else cols_by_digit)
else:
if d < cols_by_digit:
col -= d
else:
col -= cols_by_digit
self.SetDigit(d, p, len(vs), cols_by_digit, row = initial_row, col = col)
p -= 1
def SetDigit(self, d: int, p: int, l: int, cols: int, row: int, col: int):
pass
def SetCube(self, cube: Cube, row = 1, col = 1):
self.__cubes[row][col] = cube
cube.row = row
cube.col = col
def SetCubes(self, cubes, row = 1, col = 1):
for r in range(len(cubes)):
for c in range(len(cubes[r])):
self.SetCube(cubes[r][c], row + r, col + c)
def Show(self):
for r in range(self.__rows):
for c in range(self.__cols):
self.DrawCube(self.__cubes[r][c])
print("")
def DrawCube(self, cubes):
pass
def RandomSet(self, color: str, count: int):
for i in range(count):
not_free = True
while not_free:
r = random.choice(range(self.rows))
c = random.choice(range(self.cols))
not_free = self.IsFree(r, c) == False
cube = Cube(color, r, c)
self.SetCube(cube, r, c)
def IsFree(self, row: int, col: int):
return self.__cubes[row][col] == None
def CountCubes(self, color: str = "ALL"):
if color == "ALL":
count = sum([sum(1 for x in r if x != None) for r in self.__cubes])
else:
count = sum([sum(1 for x in r if (x != None and x.color == color)) for r in self.__cubes])
return count
def CountCubesByColors(self):
colors = ["R","A","V"]
counts = {c: self.CountCubes(c) for c in colors}
return counts