-
Notifications
You must be signed in to change notification settings - Fork 0
/
pong_environment_training.py
70 lines (48 loc) · 1.22 KB
/
pong_environment_training.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
import time
import numpy.random as rand
import sys
world_dim = {'ball_y':12, 'paddle': 12}
num_possible_moves = 3
global num_reward, num_punishment, state
num_reward = 1
num_punishment = 1
state = (1,0) # ball_x, ball_y, paddle
last_outcomes = [0]
beam_next = False
beam_timer = 2
def getWorldDim():
return [world_dim['ball_y'], world_dim['paddle']]
def getActionDim():
return num_possible_moves
def checkValid():
global state
if state[1] < 0:
state = (state[0], 0) # -= 1
if state[1] > world_dim['paddle']-1:
state = (state[0], world_dim['paddle']-1) # -= 1
def randomBeam():
global state
state = ( rand.randint(0, world_dim['ball_y']), rand.randint(0, world_dim['paddle']) )
def move(direction):
global num_reward, num_punishment, state, beam_next, beam_timer
if direction == 2:
direction = -1 #convert to pong actions
if direction == -1:
state = (state[0], state[1]-1) # -= 1
elif direction == 1:
state = (state[0], state[1]+1)
if beam_next:
if beam_timer <= 0:
randomBeam()
beam_next = False
beam_timer = 2
else:
beam_timer -= 1
checkValid()
outcome = -10
if (state[0] == state[1]):
outcome = 600
beam_next = True
return [state, outcome]
def getState():
return state