-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateClass.py
87 lines (79 loc) · 2.44 KB
/
stateClass.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
import numpy as np
from entityClass import entity, entityBuild, gmc
from func import xMax, yMax, oneHot
from posClass import genRandPos
def genRandEntities():
nameList = ["r","p","o","b"]
objList = []
objList.append(entityBuild(genRandPos(), "y", "x", randDir=True))
for name in nameList:
exit = False
pos = None
while not exit:
pos = genRandPos()
if(pos!=objList[0].pos):exit=True
objList.append(entityBuild(pos, name, "x", randDir=True))
return objList
class state:
def __init__(self, l=None):
if(l==None):l = genRandEntities()
self.entities = l
def validate(self) -> bool:
e: entity
for e in self.entities:
if gmc(e.pos) == " ": return False
if(e.turnt and not e.vuln):return False
if e.name=="y":
for e2 in [x for x in self.entities if x != e]:
if e.pos==e2.pos:return False
return True
def getInput(self):
out = []
for entity in self.entities:
out.append([entity.name,entity.pos])
return out
def step(self):
out = []
e: entity
for e in self.entities:
if(e.name=="y"):self.pac = e
if(e.name=="b"):self.blue = e
for e in self.entities:
out.append([e.name,e.step(self.pac.pos,self.pac.dir,self.blue.pos)])
return out
class stateHolder:
def __init__(self,s=None):
if(s==None):s= state()
self.input = {}
for entity in s.entities:
self.input[entity.name] = entity.pos
self.output = {}
step = s.step()
for entry in step:
if entry[0] != 'y':
self.output[entry[0]] = entry[1]
def __str__(self):
out = ""
for key in self.input.keys():
out+= key+":"+str(self.input[key])+"|"
out += "["
for key in self.output.keys():
out+= key+":"+self.output[key]+"|"
out +="]"
return out
def inMatrix(self):
val = []
for x in self.input.values():
position = x.asArr()
position[0] /= xMax
position[1] /= yMax
val.append(position)
return val
def outMatrix(self):
val = []
for x in self.output.values():
val.append(x)
return np.array(oneHot(val))
stat = stateHolder()
print(stat.output)
print(stat.outMatrix())