#include <conio.h>
#include <iostream>
using namespace std;
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
class Setup{
private:
bool gameover;
int width;
int height;
int x;
int y;
public:
int fruitX;
int fruitY;
int score;
int* tailX;
int* tailY;
int nTail;
eDirection dir;
public:
Setup() {
width = 20;
height = 20;
tailX = new int[100];
tailY = new int[100];
return;
}
void set(){
gameover = false;
dir = STOP;
x = width / 2 - 1;
y = height / 2 - 1;
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
return;
}
void setWidth(int _width){
width = _width;
}
void setHeight(int _height){
height = _height;
}
void setGameover(bool _gameover){
gameover = _gameover;
}
int getWidth(){
return width;
}
int getHeight(){
return height;
}
bool getGameover(){
return gameover;
}
void setX(int _x){
x = _x;
}
void setY(int _y){
y = _y;
}
int getX(){
return x;
}
int getY(){
return y;
}
};
class Draw{
public:
Draw(){
system("cls");
}
void drawSnakeField(Setup setup){
system("cls");
for (int i = 0; i < setup.getWidth() + 1; i++)
cout << "#";
cout << endl;
return;
}
void drawAnotherStuff(Setup setup){
for (int i = 0; i < setup.getHeight(); i++) {
for (int j = 0; j < setup.getWidth(); j++) {
if (j == 0 || j == setup.getWidth() - 1)
cout << "#";
if (i == setup.getY() && j == setup.getX())
cout << "0";
else if (i == setup.fruitY && j == setup.fruitX)
cout << "F";
else {
bool print = false;
for (int k = 0; k < setup.nTail; k++) {
if (setup.tailX[k] == j && setup.tailY[k] == i) {
print = true;
cout << "o";
}
}
if (!print)
cout << " ";
}
}
cout << endl;
}
for (int i = 0; i < setup.getWidth() + 1; i++)
cout << "#";
cout << endl;
cout << "Score: " << setup.score << endl;
return;
}
};
class Input{
public:
Input(){
}
void inputAction(Setup setup) {
if (_kbhit()) {
switch (_getch()) {
case 'a':
setup.dir = LEFT;
break;
case 'd':
setup.dir = RIGHT;
break;
case 'w':
setup.dir = UP;
break;
case 's':
setup.dir = DOWN;
break;
case 'x':
setup.setGameover(true);
break;
}
}
return;
}
};
class Logic{
public:
Logic(){
}
void logicSnake(Setup setup) {
int prevX = setup.tailX[0];
int prevY = setup.tailY[0];
int prev2X, prev2Y;
setup.tailX[0] = setup.getX();
setup.tailY[0] =setup.getY();
for (int i = 1; i < setup.nTail; i++) {
prev2X = setup.tailX[i];
prev2Y = setup.tailY[i];
setup.tailX[i] = prevX;
setup.tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
}
switch (setup.dir)
{
case LEFT:
setup.setX(setup.getX()-1);
break;
case RIGHT:
setup.setX(setup.getX()+1);
break;
case UP:
setup.setY(setup.getY()-1);
break;
case DOWN:
setup.setY(setup.getY()+1);
break;
}
if (setup.getX() > setup.getWidth() || setup.getX() < 0 ||setup.getY() > setup.getHeight() ||setup.getY() < 0)
setup.setGameover(true);
for (int i = 0; i < setup.nTail; i++) {
if (setup.tailX[i] == setup.getX() && setup.tailY[i] ==setup.getY())
setup.setGameover(true);
}
if (setup.getX() == setup.fruitX && setup.getY() == setup.fruitY) {
setup.score += 10;
setup.fruitX = rand() % setup.getWidth();
setup.fruitY = rand() % setup.getHeight();
setup.nTail++;
}
return;
}
};
int main()
{
Setup setup;
setup.set();
Draw draw;
Input input;
Logic logic;
while (!setup.getGameover()) {
draw.drawSnakeField(setup);
draw.drawAnotherStuff(setup);
input.inputAction(setup);
logic.logicSnake(setup);
}
return 0;
}