-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenviroment.py
217 lines (163 loc) · 5.3 KB
/
enviroment.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
207
208
209
210
211
212
213
214
215
216
217
from enum import Enum
from itertools import product
from math import factorial
from random import choices, randint, sample
from time import time
from typing import Literal, Optional, Self
import numpy as np
from scipy.special import softmax
def random(*shape):
"""Random numbers between -1 and +1"""
return np.random.uniform(-1, 1, shape)
def inside_out(dct) -> dict:
return {value: key for key, value in dct.items()}
Shade = Literal[1, 2, 3, 4, 5, 6]
Color = Literal[7, 8, 9, 10, 11]
ReprColor: dict[str, Color] = {"r": 7, "b": 8, "g": 9, "p": 10, "y": 11}
ColorRepr: dict[Color, str] = inside_out(ReprColor)
Null = Literal[0]
Restriction = Color | Shade | Null
Position = tuple[int, int]
Die = tuple[Color, Shade]
Tile = tuple[Color | Null, Shade | Null] # list? (prob bad to be list)
def restriction(tile: Tile):
return tile[0] or tile[1]
def tile_die(tile: Tile) -> Optional[tuple[Color, Shade]]:
if tile[0] != 0 and tile[1] != 0:
return tile[0], tile[1]
def fulfills(die: Die, restriction: Restriction) -> bool:
"""Whether the dice can meet the requirements of the restriction."""
return restriction == 0 or die[0] == restriction or die[1] == restriction
def can_fit(die: Die, tile: Tile) -> bool:
"""Whether the dice can meet the requirements of the tile (if any)."""
return tile_die(tile) == 0 and fulfills(die, restriction(tile))
def make_grid(*tiles: Tile):
grid = []
for tile in tiles:
grid += tile
return grid
# TODO create chunk class
outer_tiles: list[Position] = [
(0, 0),
(0, 1),
(0, 2),
(0, 3),
(0, 4),
(3, 0),
(3, 1),
(3, 2),
(3, 3),
(3, 4),
(1, 0),
(2, 0),
(1, 4),
(2, 4),
]
def adjacent(pos: Position):
return [
(pos[0] - 1, pos[1]),
(pos[0] + 1, pos[1]),
(pos[0], pos[1] - 1),
(pos[0], pos[1] + 1),
]
def diagonal(pos: Position):
return [
(pos[0] - 1, pos[1] - 1),
(pos[0] + 1, pos[1] + 1),
(pos[0] + 1, pos[1] - 1),
(pos[0] - 1, pos[1] + 1),
]
def get_legal_moves(grid, pool: list[Die]):
# 3 for color, shade, pos, 6 shades, 5 colors, outer_tiles
# legal = np.zeros(3 * 6 * 5 * len(outer_tiles))
legal = np.zeros(13 * len(outer_tiles)) # ? how 13?
grid = make_grid(*grid)
for pos in outer_tiles: # store outer_pos tiles
tile = index_grid(grid, pos)
illegals = set()
for adjacent in tile_adjacent(pos):
try:
adjacent = index_grid(grid, adjacent)
except IndexError:
continue
if restriction(adjacent) != 0:
illegals.add(restriction(adjacent))
for i, (die) in enumerate(pool):
if fulfills(die, restriction(tile)) and all(
not fulfills(die, illegal) for illegal in illegals
):
legal[i * 3 : i * 3 + 3] = (*die, pos)
return legal
def get_legal_positions(grid, pool: list[Die]):
# 3 for color, shade, pos, 6 shades, 5 colors, outer_tiles
# legal = np.zeros(3 * 6 * 5 * len(outer_tiles))
legal = np.zeros(13 * len(outer_tiles)) # ? how 13?
for y, x in outer_tiles: # store outer_pos tiles
tile = grid[y][x]
illegals = set()
for adjacent in adjacent((y, x)):
try:
adjacent = index_grid(grid, adjacent)
except IndexError:
continue
if restriction(adjacent) != 0:
illegals.add(restriction(adjacent))
for i, (die) in enumerate(pool):
if fulfills(die, restriction(tile)) and all(
not fulfills(die, illegal) for illegal in illegals
):
legal[i * 3 : i * 3 + 3] = (*die, pos)
return legal
def make_grid_repr(tiles: list[list[str]]):
grid = []
for row in tiles:
new_row = []
for tile in row:
if tile == " ":
new_row.append((0, 0))
elif ReprColor.get(tile):
new_row.append((ReprColor.get(tile), 0))
else:
new_row.append((0, int(tile)))
grid.append(new_row)
return grid
grid = make_grid_repr(
[
["g", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
[" ", " ", " ", " ", " "],
]
)
print(grid)
print(get_legal_moves(grid, [(7, 2)]))
class GridNetwork:
def __init__(self, height, width, input_size):
self.height = height
self.width = width
self.input_size = input_size
self.randomize_weights_biases()
def randomize_weights_biases(self):
self.biases = []
# # Color
# self.biases.append(random(self.height, self.width, 5))
# Value
self.biases.append(random(self.height, self.width))
self.weights = []
self.weights.append(random(self.input_size, self.height, self.width, 2))
def flood_dice(self, legal_moves, grid):
pass
# A
# legal_moves + grid = legal_move
# B
# Dice 1 ->
# Grid
# o-x-o
# o/o\o
# o o o
# Filled Tiles get added to adjacent dice
# Dice 1 gets added to all legal positions
# highest firing position is winner
# highest firing dice with highest position is the networks choice
# all weights connected just not all used every time
# color = [0,0,0,0,1], [0,0,0,1,0] etc