#include <windows.h>
#include<iostream>
using namespace std;
class SubwayRun {
private:
int subwayWidth;
int time;
int score;
public:
SubwayRun(int w) {
subwayWidth = w;
time = 0;
score = 0;
}
void startGame() {
int x = subwayWidth / 2 - 1;
char ch, key;
int y = 0;
while (true) {
for (int i = 0; i < subwayWidth; i++) {
if (i == x) cout << 'O';
else cout << '-';
}
cout << " Score:" << score << endl;
ch = ' '; key = ' ';
if (kbhit()) {
ch = getch();
if (ch == '\r') ch = getch();
key = ch;
}
switch (key) {
case 'a': x--; break;
case 'd': x++; break;
default: break;
}
if (time % 2 == 0) {
int randNum = rand() % 4;
for (int i = 0; i < subwayWidth; i++) {
if (i == randNum || i == randNum + 1 || i == randNum + 2)
cout << 'X';
else
cout << '-';
}
y = 0;
}
else {
for (int i = 0; i < subwayWidth; i++) {
cout << '-';
}
y += 2;
}
cout << endl;
if (x == randNum || x == randNum + 1 || x == randNum + 2) {
system("cls");
cout << "Game Over!Your score is:" << score << endl;
break;
}
if (y >= 8) {
y = 0;
score += 10;
}
time++;
}
}
};
int main()
{
SubWayRun sw(8);
sw.startGame();
return 0;
}