Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<ctime>
- using namespace std;
- int GRID_SIZE = 10;
- int LENGTH_MAX = GRID_SIZE*GRID_SIZE;
- int abs(int);
- bool noCollision(int, int, char**);
- void game_over();
- int main(int argv, const char* argc[])
- {
- int speed = 40;
- if (argv > 1)
- {
- int n;
- cout<<"Enter n for Grid Size of (nxn): ";
- cin>>n;
- GRID_SIZE = n;
- LENGTH_MAX = n*n;
- }
- if (argv > 2)
- {
- cout<<"(bigger number means bigger slowdown)"<<endl;
- cout<<"Enter Display Speed, 0 for manual press: ";
- cin>>speed;
- }
- srand(time(0));
- char** grid;
- grid = new char*[GRID_SIZE];
- for (int i = 0; i < GRID_SIZE; i++)
- grid[i] = new char[GRID_SIZE];
- int locationx = GRID_SIZE/2, locationy = GRID_SIZE/2, foodx, foody, score = 0;
- int* locx = new int[LENGTH_MAX], *locy = new int[LENGTH_MAX];
- string direction;
- bool eaten = 1;
- ///init
- for (int i = 0; i < GRID_SIZE; i++)
- {
- for (int j = 0; j < GRID_SIZE; j++)
- grid[i][j] = ' ';
- }
- grid[locationx][locationy] = 'O';
- direction = "right";
- system("cls");
- while(1)
- {
- ///Spawner
- while (eaten == 1)
- {
- eaten = 0;
- foodx = rand()%GRID_SIZE; foody = rand()%GRID_SIZE;
- if (grid[foodx][foody] == ' ')
- grid[foodx][foody] = '+';
- else eaten = 1;
- if (score == LENGTH_MAX-1 && eaten == 1)
- eaten = 0;
- }
- for (int i = 0; i < GRID_SIZE*2+2; i++)
- cout<<"_";
- cout<<endl;
- for (int i = 0; i < GRID_SIZE; i++)
- {
- cout<<"|";
- for (int j = 0; j < GRID_SIZE; j++)
- {
- cout<<grid[i][j]<<" ";
- }
- cout<<"|"<<endl;
- }
- for (int i = 0; i < GRID_SIZE*2+2; i++)
- cout<<"_";
- cout<<endl;
- cout<<"Score: "<<score;
- ///WIN CONDITION
- if (score == LENGTH_MAX)
- {
- cout<<"\nCONGRATULATIONS! YOU WON!\n";
- system("pause");
- return score;
- }
- for (int i = 0; i < score; i++)
- {
- grid[locx[i]][locy[i]] = ' ';
- }
- ///collision
- {
- if (locationx == foodx && locationy == foody)
- {
- score++;
- eaten = 1;
- }
- }
- grid[locationx][locationy] = ' ';
- for(int i = score-1; i > 0; i--)
- {
- locx[i] = locx[i-1];
- locy[i] = locy[i-1];
- }
- if (score > 0)
- {
- locx[0] = locationx;
- locy[0] = locationy;
- }
- for (int i = 0; i < score; i++)
- {
- grid[locx[i]][locy[i]] = 'o';
- }
- //int x = rand()%3;
- int move;
- ///AI
- if (direction == "down")
- {
- if (foodx - locationx > 0)
- {
- if (noCollision(locationx+1, locationy, grid))
- {
- move = 0;
- }
- else
- {
- if (foody-locationy > 0)
- {
- if (noCollision(locationx, locationy+1, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx, locationy-1, grid))
- {
- move = 2;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx, locationy-1, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx, locationy+1, grid))
- {
- move = 1;
- }
- else {game_over(); return 1;}
- }
- }
- }
- else
- {
- if (foody-locationy > 0)
- {
- if (noCollision(locationx, locationy+1, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx, locationy-1, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx+1, locationy, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx, locationy-1, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx, locationy+1, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx+1, locationy, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- }
- }
- else if (direction == "up")
- {
- if (foodx - locationx < 0)
- {
- if (noCollision(locationx-1, locationy, grid))
- {
- move = 0;
- }
- else
- {
- if (foody-locationy > 0)
- {
- if (noCollision(locationx, locationy+1, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx, locationy-1, grid))
- {
- move = 1;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx, locationy-1, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx, locationy+1, grid))
- {
- move = 2;
- }
- else {game_over(); return 1;}
- }
- }
- }
- else
- {
- if (foody-locationy > 0)
- {
- if (noCollision(locationx, locationy+1, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx, locationy-1, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx-1, locationy, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx, locationy-1, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx, locationy+1, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx-1, locationy, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- }
- }
- else if (direction == "right")
- {
- if (foody - locationy > 0)
- {
- if (noCollision(locationx, locationy+1, grid))
- {
- move = 0;
- }
- else
- {
- if (foodx-locationx > 0)
- {
- if (noCollision(locationx+1, locationy, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx-1, locationy, grid))
- {
- move = 1;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx-1, locationy, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx+1, locationy, grid))
- {
- move = 2;
- }
- else {game_over(); return 1;}
- }
- }
- }
- else
- {
- if (foodx-locationx > 0)
- {
- if (noCollision(locationx+1, locationy, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx-1, locationy, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx, locationy+1, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx-1, locationy, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx+1, locationy, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx, locationy+1, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- }
- }
- else if (direction == "left")
- {
- if (foody - locationy < 0)
- {
- if (noCollision(locationx, locationy-1, grid))
- {
- move = 0;
- }
- else
- {
- if (foodx-locationx > 0)
- {
- if (noCollision(locationx+1, locationy, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx-1, locationy, grid))
- {
- move = 2;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx-1, locationy, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx+1, locationy, grid))
- {
- move = 1;
- }
- else {game_over(); return 1;}
- }
- }
- }
- else
- {
- if (foodx-locationx > 0)
- {
- if (noCollision(locationx+1, locationy, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx-1, locationy, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx, locationy-1, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- else
- {
- if (noCollision(locationx-1, locationy, grid))
- {
- move = 2;
- }
- else if (noCollision(locationx+1, locationy, grid))
- {
- move = 1;
- }
- else if (noCollision(locationx, locationy-1, grid))
- {
- move = 0;
- }
- else {game_over(); return 1;}
- }
- }
- }
- ///DEBUG
- //cout<<endl;
- //cout<<"Snake: "<<locationx<<" "<<locationy<<" "<<direction<<" "<<move<<endl;
- //cout<<"Food: "<<foodx<<" "<<foody<<endl;
- switch(move)
- {
- case 0: //move same direction
- {
- if (direction == "right")
- {
- locationy++;
- }
- else if (direction == "left")
- {
- locationy--;
- }
- else if (direction == "up")
- {
- locationx--;
- }
- else if (direction == "down")
- {
- locationx++;
- }
- } break;
- case 1:
- {
- if (direction == "right")
- {
- locationx--;
- direction = "up";
- }
- else if (direction == "left")
- {
- locationx++;
- direction = "down";
- }
- else if (direction == "up")
- {
- locationy--;
- direction = "left";
- }
- else if (direction == "down")
- {
- locationy++;
- direction = "right";
- }
- } break;
- case 2:
- {
- if (direction == "right")
- {
- locationx++;
- direction = "down";
- }
- else if (direction == "left")
- {
- locationx--;
- direction = "up";
- }
- else if (direction == "up")
- {
- locationy++;
- direction = "right";
- }
- else if (direction == "down")
- {
- locationy--;
- direction = "left";
- }
- } break;
- }
- if (locationx < 0)
- locationx = GRID_SIZE-1;
- else if (locationx > GRID_SIZE-1)
- locationx = 0;
- if (locationy < 0)
- locationy = GRID_SIZE-1;
- else if (locationy > GRID_SIZE-1)
- locationy = 0;
- ///Location Handler
- grid[locationx][locationy] = 'O';
- /*for (int i = 0; i < 100; i++)
- {
- system("");
- }*/
- if (speed == 0)
- system("pause >nul");
- else
- {
- for (int i = 0; i < 10 + speed; i++)
- {
- system("");
- }
- }
- system("cls");
- }
- }
- int abs(int a)
- {
- if (a < 0)
- return -a;
- else return a;
- }
- bool noCollision(int x, int y, char** grid)
- {
- if (x > GRID_SIZE-1)
- x = 0;
- else if (x < 0)
- x = GRID_SIZE-1;
- if (y > GRID_SIZE-1)
- y = 0;
- else if (y < 0)
- y = GRID_SIZE-1;
- //cout<<endl<<"x: "<<x<<" y: "<<y<<" '"<<grid[x][y]<<"'";
- //return (grid[x][y] == ' ' || grid[x][y] == '+');
- return (grid[x][y] != 'o');
- }
- void game_over()
- {
- cerr<<endl<<"GAME OVER!"<<endl;
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement