-
Notifications
You must be signed in to change notification settings - Fork 0
/
AGame.py
174 lines (149 loc) · 5.61 KB
/
AGame.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
import numpy as np
import random
from IPython import display
import time
import os
class space:
def __init__(self, stype,dimension):
self.space_type = stype
self.n = dimension
def __repr__(self):
return(''.join([self.space_type,'(',str(self.n),')']))
def chunks(l, n):
result = []
for i in range(0, len(l), n):
result.append( l[i:i + n])
return(result)
def showLine(txt,width = 64,wall = '|'):
n = width - 4
if len(txt)>n:
txt = txt[0:n]
front = round((n-len(txt))/2)
end = n-front -len(txt)
print(wall+' '+' '*front+txt+' '*end+' '+wall)
def screen(txtList,height = 20,width = 64,roof = '=',wall = '|'):
os.system('cls' if os.name == 'nt' else 'clear')
n = width - 4
m = height -4
if len(txtList)>m:
txtList = txtList[0:m]
front = round((m-len(txtList))/2)
end = m-front -len(txtList)
print(roof*width)
for i in range(front+1):
print(wall,' '*n,wall)
for txt in txtList:
showLine(txt,width = width,wall = wall)
for i in range(end+1):
print(wall,' '*n,wall)
print(roof*width)
class player:
def __init__(self,name = 'Robot'):
self.name = name
self.ki = 0
self.score = 0
def gatherKi(self):
if self.ki <3:
self.ki = self.ki+1
def attack(self):
if self.ki>0:
self.ki = self.ki-1
def win(self):
self.score = self.score +1
def reset(self):
self.ki = 0
# self.score = 0
class aGame:
def __init__(self,name1='Robot',name2='Xiaoli Hu'):
self.observation_space = space('Discrete',16)
self.action_space = space('Discrete',3)
self.P1 = player(name = name1)
self.P2 = player(name = name2)
self.score = 0
self.observation = 0
self.table = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])-1
self.reward = [0,0]
self.done = False
self.info = 'HAHAHAHA'
self.actionNames = ['GATHERS KI','ATTACKS','DEFEND']
# self.actionNames = ['攒气','攻击','防御']
self.a1 = 2
self.a2 = 2
def reset(self):
self.P1.reset()
self.P2.reset()
self.updateObservation()
self.observation = [0,0]
self.reward = [0,0]
self.done = False
return(self.observation)
def updateObservation(self):
self.observation = [self.table[self.P1.ki,self.P2.ki],self.table[self.P2.ki,self.P1.ki]]
def step(self,a1,a2):
if self.P1.ki ==0 and a1 ==1:
a1 = 2
if self.P2.ki ==0 and a2 ==1:
a2 = 2
self.a1 = a1
self.a2 = a2
self.reward = [0,0]
if not self.done:
# ATTACK VS KI
if a1 == 0:
#if np.random.rand()>0.0:
self.P1.gatherKi()
if a2 == 1:
self.reward = [-1,1]
self.done = True
self.info = ['Player '+self.P2.name+' '+self.actionNames[self.a2]+' and '+' wins!']
self.P2.win()
if a2 == 0:
#if np.random.rand()>0.0:
self.P2.gatherKi()
if a1 == 1:
self.reward = [1,-1]
self.done = True
self.info = ['Player '+self.P1.name+ ' '+self.actionNames[self.a1]+' and '+' wins!']
self.P1.win()
#KI BURST
if a1 ==2 and a2==1 and self.P2.ki==3:
self.reward = [-1,1]
self.done = True
self.info = ['Ki Burst!!','Player '+self.P2.name+ ' '+self.actionNames[self.a2]+' and '+' wins!']
self.P2.win()
if a1 ==1 and a2==2 and self.P1.ki==3:
self.reward = [1,-1]
self.done = True
self.info = ['Ki Burst!!','Player '+self.P1.name+ ' '+self.actionNames[self.a1]+' and '+' wins!']
self.P1.win()
#DOUBLE ATTACKS
if a1 == 1 and a2 ==1:
if self.P1.ki == 3 and self.P2.ki <3:
self.reward = [1,-1]
self.done = True
self.info = ['Ki Burst!!','Player '+self.P1.name+ ' '+self.actionNames[self.a1]+' and '+' wins!']
self.P1.win()
if self.P2.ki == 3 and self.P1.ki <3:
self.reward = [-1,1]
self.done = True
self.info = ['Ki Burst!!','Player '+self.P2.name+ ' '+self.actionNames[self.a2]+' and '+' wins!']
self.P2.win()
#PENALIZE ATTACK
if a1 == 1:
self.P1.attack()
if a2 == 1:
self.P2.attack()
self.updateObservation()
return self.observation,self.reward,self.done,self.info
def render(self):
if self.done:
screen(self.info)
else:
screen(['XXXX A weird Game XXXX',' ',
self.P1.name+' '+self.actionNames[self.a1],' ',
'===========================================', ' ',
self.P1.name+' Ki: ['+'*'*self.P1.ki+'-'*(3-self.P1.ki)+']',' ',
self.P2.name+' Ki: ['+'*'*self.P2.ki+'-'*(3-self.P2.ki)+']',' ',
'===========================================',' ',
self.P2.name+' '+self.actionNames[self.a2],' ',
'[1]GATHER Ki [2]ATTACK [3]DEFEND [4]Exit Game'])