-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPong.cpp
157 lines (157 loc) · 4.41 KB
/
Pong.cpp
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
#include <iostream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
using namespace std;
class Pong {
private:
int pongX;
int pongY;
int pongVelX;
int pongVelY;
int width;
int height;
int playerX = 0;
int paddleWidth;
public:
int score = 0;
int episodes = 0;
bool is_done = false;
void reset () {
this->pongX = 0;
this->pongY = 0;
this->playerX = 0;
bool isPongVelX = rand() % 2;
bool isPongVelY = rand() % 2;
if (isPongVelX) {
this->pongVelX = 1;
} else {
this->pongVelX = -1;
}
if (isPongVelY) {
this->pongVelY = 1;
} else {
this->pongVelY = -1;
}
episodes++;
score = 0;
}
Pong (int width, int height, int paddleWidth = 1) {
reset();
this->width = width;
this->height = height;
this->paddleWidth = paddleWidth;
}
// 0 : Left
// 1 : Stay
// 2 : Right
int act (int action) {
int reward = 0;
is_done = false;
if (action > 2) {
throw invalid_argument("invalid action");
}
if (action == 0 && this->playerX - this->paddleWidth != -width + 1) {
this->playerX -= 1;
}
if (action == 2 && this->playerX + this->paddleWidth != width - 1) {
this->playerX += 1;
}
if (action == 0 && this->playerX - this->paddleWidth == -width + 1) {
reward = 0;
}
if (action == 2 && this->playerX + this->paddleWidth == width - 1) {
reward = 0;
}
// update pong position
this->pongX += this->pongVelX;
this->pongY += this->pongVelY;
// boundry
if (pongX == -width + 1 && pongVelX < 0) {
pongVelX *= -1;
}
if (pongX == width - 1 && pongVelX > 0) {
pongVelX *= -1;
}
// hits player paddle
if (abs(pongX - playerX) <= paddleWidth && pongY == -height + 2 && pongVelY < 0) {
pongVelY *= -1;
reward = 5;
score++;
}
if (pongY == -height + 1) {
reward = -10;
is_done = true;
this->reset();
}
// hit enemy paddle
if (pongY == height - 2 && pongVelY > 0) {
pongVelY *= -1;
}
return reward;
}
int coordX (int x) {
int widthOffset = this->width;
return x + widthOffset;
}
int coordY (int y) {
int heightOffset = this->height;
return (y * -1) + heightOffset;
}
void print_state () {
string a[(height * 2)+1][(width * 2)+1];
a[coordY(pongY)][coordX(pongX)] = "O";
a[coordY(height - 1)][coordX(pongX)] = "-";
a[coordY(-height + 1)][coordX(playerX)] = "-";
for (int i = 0; i <= this->paddleWidth; i++) {
a[coordY(-height + 1)][coordX(playerX + i)] = "-";
a[coordY(-height + 1)][coordX(playerX - i)] = "-";
}
for (int i = 0; i < (width * 2) + 3; i++) {
cout<<"#";
}
cout<<endl;
for (int i = 0; i < (height * 2) + 1; i++) {
cout<<"#";
for (int x = 0; x < (width * 2) + 1; x++) {
if (a[i][x] == "") {
cout<<" ";
} else {
cout<<a[i][x];
}
}
cout<<"#"<<endl;
}
for (int i = 0; i < (width * 2) + 3; i++) {
cout<<"#";
}
cout<<endl;
}
vector<double> return_big_state () {
double a[(height * 2)+1][(width * 2)+1];
a[coordY(pongY)][coordX(pongX)] = 255;
a[coordY(height - 0)][coordX(pongX)] = 100;
a[coordY(-height + 1)][coordX(playerX)] = 100;
vector<double> state;
for (int i = 0; i < (height * 2) + 1; i++) {
for (int x = 0; x < (width * 2) + 1; x++) {
state.push_back(a[i][x]);
}
}
return state;
}
vector<double> return_state () {
vector<double> state;
state.push_back(pongX);
state.push_back(pongY);
state.push_back(pongVelX);
state.push_back(pongVelY);
state.push_back(width);
state.push_back(height);
state.push_back(playerX);
state.push_back(paddleWidth);
return state;
}
};