Advertisement
Guest User

Snake Game + Auto AI

a guest
Jul 26th, 2017
655
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.10 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5.  
  6. int GRID_SIZE = 10;
  7. int LENGTH_MAX = GRID_SIZE*GRID_SIZE;
  8.  
  9. int abs(int);
  10. bool noCollision(int, int, char**);
  11. void game_over();
  12.  
  13. int main(int argv, const char* argc[])
  14. {
  15.     int speed = 40;
  16.     if (argv > 1)
  17.     {
  18.         int n;
  19.         cout<<"Enter n for Grid Size of (nxn): ";
  20.         cin>>n;
  21.         GRID_SIZE = n;
  22.         LENGTH_MAX = n*n;
  23.     }
  24.     if (argv > 2)
  25.     {
  26.         cout<<"(bigger number means bigger slowdown)"<<endl;
  27.         cout<<"Enter Display Speed, 0 for manual press: ";
  28.         cin>>speed;
  29.     }
  30.  
  31.     srand(time(0));
  32.     char** grid;
  33.     grid = new char*[GRID_SIZE];
  34.     for (int i = 0; i < GRID_SIZE; i++)
  35.         grid[i] = new char[GRID_SIZE];
  36.     int locationx = GRID_SIZE/2, locationy = GRID_SIZE/2, foodx, foody, score = 0;
  37.     int* locx = new int[LENGTH_MAX], *locy = new int[LENGTH_MAX];
  38.     string direction;
  39.     bool eaten = 1;
  40.  
  41.  
  42.     ///init
  43.     for (int i = 0; i < GRID_SIZE; i++)
  44.     {
  45.         for (int j = 0; j < GRID_SIZE; j++)
  46.             grid[i][j] = ' ';
  47.     }
  48.  
  49.     grid[locationx][locationy] = 'O';
  50.     direction = "right";
  51.  
  52.  
  53.     system("cls");
  54.     while(1)
  55.     {
  56.  
  57.         ///Spawner
  58.         while (eaten == 1)
  59.         {
  60.             eaten = 0;
  61.             foodx = rand()%GRID_SIZE; foody = rand()%GRID_SIZE;
  62.             if (grid[foodx][foody] == ' ')
  63.                 grid[foodx][foody] = '+';
  64.             else eaten = 1;
  65.  
  66.             if (score == LENGTH_MAX-1 && eaten == 1)
  67.                 eaten = 0;
  68.         }
  69.  
  70.  
  71.         for (int i = 0; i < GRID_SIZE*2+2; i++)
  72.             cout<<"_";
  73.         cout<<endl;
  74.  
  75.         for (int i = 0; i < GRID_SIZE; i++)
  76.         {
  77.             cout<<"|";
  78.             for (int j = 0; j < GRID_SIZE; j++)
  79.             {
  80.                 cout<<grid[i][j]<<" ";
  81.             }
  82.             cout<<"|"<<endl;
  83.         }
  84.  
  85.         for (int i = 0; i < GRID_SIZE*2+2; i++)
  86.             cout<<"_";
  87.         cout<<endl;
  88.         cout<<"Score: "<<score;
  89.  
  90.         ///WIN CONDITION
  91.         if (score == LENGTH_MAX)
  92.         {
  93.             cout<<"\nCONGRATULATIONS! YOU WON!\n";
  94.             system("pause");
  95.             return score;
  96.         }
  97.  
  98.  
  99.         for (int i = 0; i < score; i++)
  100.         {
  101.             grid[locx[i]][locy[i]] = ' ';
  102.         }
  103.  
  104.  
  105.  
  106.         ///collision
  107.         {
  108.             if (locationx == foodx && locationy == foody)
  109.             {
  110.                 score++;
  111.                 eaten = 1;
  112.             }
  113.         }
  114.  
  115.         grid[locationx][locationy] = ' ';
  116.  
  117.         for(int i = score-1; i > 0; i--)
  118.         {
  119.             locx[i] = locx[i-1];
  120.             locy[i] = locy[i-1];
  121.         }
  122.  
  123.         if (score > 0)
  124.         {
  125.             locx[0] = locationx;
  126.             locy[0] = locationy;
  127.         }
  128.  
  129.         for (int i = 0; i < score; i++)
  130.         {
  131.             grid[locx[i]][locy[i]] = 'o';
  132.         }
  133.  
  134.  
  135.         //int x = rand()%3;
  136.  
  137.         int move;
  138.  
  139.  
  140.         ///AI
  141.  
  142.         if (direction == "down")
  143.         {
  144.             if (foodx - locationx > 0)
  145.             {
  146.                 if (noCollision(locationx+1, locationy, grid))
  147.                 {
  148.                     move = 0;
  149.                 }
  150.                 else
  151.                 {
  152.                     if (foody-locationy > 0)
  153.                     {
  154.                         if (noCollision(locationx, locationy+1, grid))
  155.                         {
  156.                             move = 1;
  157.                         }
  158.                         else if (noCollision(locationx, locationy-1, grid))
  159.                         {
  160.                             move = 2;
  161.                         }
  162.                         else {game_over(); return 1;}
  163.                     }
  164.                     else
  165.                     {
  166.                         if (noCollision(locationx, locationy-1, grid))
  167.                         {
  168.                             move = 2;
  169.                         }
  170.                         else if (noCollision(locationx, locationy+1, grid))
  171.                         {
  172.                             move = 1;
  173.                         }
  174.                         else {game_over(); return 1;}
  175.                     }
  176.                 }
  177.             }
  178.             else
  179.             {
  180.                 if (foody-locationy > 0)
  181.                 {
  182.                     if (noCollision(locationx, locationy+1, grid))
  183.                         {
  184.                             move = 1;
  185.                         }
  186.                     else if (noCollision(locationx, locationy-1, grid))
  187.                         {
  188.                             move = 2;
  189.                         }
  190.                     else if (noCollision(locationx+1, locationy, grid))
  191.                         {
  192.                             move = 0;
  193.                         }
  194.                     else {game_over(); return 1;}
  195.                 }
  196.                 else
  197.                 {
  198.                     if (noCollision(locationx, locationy-1, grid))
  199.                     {
  200.                         move = 2;
  201.                     }
  202.                     else if (noCollision(locationx, locationy+1, grid))
  203.                     {
  204.                         move = 1;
  205.                     }
  206.                     else if (noCollision(locationx+1, locationy, grid))
  207.                     {
  208.                         move = 0;
  209.                     }
  210.                     else {game_over(); return 1;}
  211.                 }
  212.             }
  213.         }
  214.  
  215.         else if (direction == "up")
  216.         {
  217.             if (foodx - locationx < 0)
  218.             {
  219.                 if (noCollision(locationx-1, locationy, grid))
  220.                 {
  221.                     move = 0;
  222.                 }
  223.                 else
  224.                 {
  225.                     if (foody-locationy > 0)
  226.                     {
  227.                         if (noCollision(locationx, locationy+1, grid))
  228.                         {
  229.                             move = 2;
  230.                         }
  231.                         else if (noCollision(locationx, locationy-1, grid))
  232.                         {
  233.                             move = 1;
  234.                         }
  235.                         else {game_over(); return 1;}
  236.                     }
  237.                     else
  238.                     {
  239.                         if (noCollision(locationx, locationy-1, grid))
  240.                         {
  241.                             move = 1;
  242.                         }
  243.                         else if (noCollision(locationx, locationy+1, grid))
  244.                         {
  245.                             move = 2;
  246.                         }
  247.                         else {game_over(); return 1;}
  248.                     }
  249.                 }
  250.             }
  251.             else
  252.             {
  253.                 if (foody-locationy > 0)
  254.                 {
  255.                     if (noCollision(locationx, locationy+1, grid))
  256.                         {
  257.                             move = 2;
  258.                         }
  259.                     else if (noCollision(locationx, locationy-1, grid))
  260.                         {
  261.                             move = 1;
  262.                         }
  263.                     else if (noCollision(locationx-1, locationy, grid))
  264.                         {
  265.                             move = 0;
  266.                         }
  267.                     else {game_over(); return 1;}
  268.                 }
  269.                 else
  270.                 {
  271.                     if (noCollision(locationx, locationy-1, grid))
  272.                     {
  273.                         move = 1;
  274.                     }
  275.                     else if (noCollision(locationx, locationy+1, grid))
  276.                     {
  277.                         move = 2;
  278.                     }
  279.                     else if (noCollision(locationx-1, locationy, grid))
  280.                     {
  281.                         move = 0;
  282.                     }
  283.                     else {game_over(); return 1;}
  284.                 }
  285.             }
  286.         }
  287.  
  288.         else if (direction == "right")
  289.         {
  290.             if (foody - locationy > 0)
  291.             {
  292.                 if (noCollision(locationx, locationy+1, grid))
  293.                 {
  294.                     move = 0;
  295.                 }
  296.                 else
  297.                 {
  298.                     if (foodx-locationx > 0)
  299.                     {
  300.                         if (noCollision(locationx+1, locationy, grid))
  301.                         {
  302.                             move = 2;
  303.                         }
  304.                         else if (noCollision(locationx-1, locationy, grid))
  305.                         {
  306.                             move = 1;
  307.                         }
  308.                         else {game_over(); return 1;}
  309.                     }
  310.                     else
  311.                     {
  312.                         if (noCollision(locationx-1, locationy, grid))
  313.                         {
  314.                             move = 1;
  315.                         }
  316.                         else if (noCollision(locationx+1, locationy, grid))
  317.                         {
  318.                             move = 2;
  319.                         }
  320.                         else {game_over(); return 1;}
  321.                     }
  322.                 }
  323.             }
  324.             else
  325.             {
  326.                 if (foodx-locationx > 0)
  327.                 {
  328.                     if (noCollision(locationx+1, locationy, grid))
  329.                         {
  330.                             move = 2;
  331.                         }
  332.                     else if (noCollision(locationx-1, locationy, grid))
  333.                         {
  334.                             move = 1;
  335.                         }
  336.                     else if (noCollision(locationx, locationy+1, grid))
  337.                         {
  338.                             move = 0;
  339.                         }
  340.                     else {game_over(); return 1;}
  341.                 }
  342.                 else
  343.                 {
  344.                     if (noCollision(locationx-1, locationy, grid))
  345.                     {
  346.                         move = 1;
  347.                     }
  348.                     else if (noCollision(locationx+1, locationy, grid))
  349.                     {
  350.                         move = 2;
  351.                     }
  352.                     else if (noCollision(locationx, locationy+1, grid))
  353.                     {
  354.                         move = 0;
  355.                     }
  356.                     else {game_over(); return 1;}
  357.                 }
  358.             }
  359.         }
  360.  
  361.         else if (direction == "left")
  362.         {
  363.             if (foody - locationy < 0)
  364.             {
  365.                 if (noCollision(locationx, locationy-1, grid))
  366.                 {
  367.                     move = 0;
  368.                 }
  369.                 else
  370.                 {
  371.                     if (foodx-locationx > 0)
  372.                     {
  373.                         if (noCollision(locationx+1, locationy, grid))
  374.                         {
  375.                             move = 1;
  376.                         }
  377.                         else if (noCollision(locationx-1, locationy, grid))
  378.                         {
  379.                             move = 2;
  380.                         }
  381.                         else {game_over(); return 1;}
  382.                     }
  383.                     else
  384.                     {
  385.                         if (noCollision(locationx-1, locationy, grid))
  386.                         {
  387.                             move = 2;
  388.                         }
  389.                         else if (noCollision(locationx+1, locationy, grid))
  390.                         {
  391.                             move = 1;
  392.                         }
  393.                         else {game_over(); return 1;}
  394.                     }
  395.                 }
  396.             }
  397.             else
  398.             {
  399.                 if (foodx-locationx > 0)
  400.                 {
  401.                     if (noCollision(locationx+1, locationy, grid))
  402.                         {
  403.                             move = 1;
  404.                         }
  405.                     else if (noCollision(locationx-1, locationy, grid))
  406.                         {
  407.                             move = 2;
  408.                         }
  409.                     else if (noCollision(locationx, locationy-1, grid))
  410.                         {
  411.                             move = 0;
  412.                         }
  413.                     else {game_over(); return 1;}
  414.                 }
  415.                 else
  416.                 {
  417.                     if (noCollision(locationx-1, locationy, grid))
  418.                     {
  419.                         move = 2;
  420.                     }
  421.                     else if (noCollision(locationx+1, locationy, grid))
  422.                     {
  423.                         move = 1;
  424.                     }
  425.                     else if (noCollision(locationx, locationy-1, grid))
  426.                     {
  427.                         move = 0;
  428.                     }
  429.                     else {game_over(); return 1;}
  430.                 }
  431.             }
  432.         }
  433.  
  434.         ///DEBUG
  435.         //cout<<endl;
  436.         //cout<<"Snake: "<<locationx<<" "<<locationy<<" "<<direction<<" "<<move<<endl;
  437.         //cout<<"Food:  "<<foodx<<" "<<foody<<endl;
  438.  
  439.         switch(move)
  440.         {
  441.         case 0: //move same direction
  442.             {
  443.                 if (direction == "right")
  444.                 {
  445.                     locationy++;
  446.                 }
  447.                 else if (direction == "left")
  448.                 {
  449.                     locationy--;
  450.                 }
  451.                 else if (direction == "up")
  452.                 {
  453.                     locationx--;
  454.                 }
  455.                 else if (direction == "down")
  456.                 {
  457.                     locationx++;
  458.                 }
  459.             } break;
  460.  
  461.         case 1:
  462.             {
  463.                if (direction == "right")
  464.                 {
  465.                     locationx--;
  466.                     direction = "up";
  467.                 }
  468.                 else if (direction == "left")
  469.                 {
  470.                     locationx++;
  471.                     direction = "down";
  472.                 }
  473.                 else if (direction == "up")
  474.                 {
  475.                     locationy--;
  476.                     direction = "left";
  477.                 }
  478.                 else if (direction == "down")
  479.                 {
  480.                     locationy++;
  481.                     direction = "right";
  482.                 }
  483.             } break;
  484.  
  485.         case 2:
  486.             {
  487.                 if (direction == "right")
  488.                 {
  489.                     locationx++;
  490.                     direction = "down";
  491.                 }
  492.                 else if (direction == "left")
  493.                 {
  494.                     locationx--;
  495.                     direction = "up";
  496.                 }
  497.                 else if (direction == "up")
  498.                 {
  499.                     locationy++;
  500.                     direction = "right";
  501.                 }
  502.                 else if (direction == "down")
  503.                 {
  504.                     locationy--;
  505.                     direction = "left";
  506.                 }
  507.             } break;
  508.  
  509.         }
  510.  
  511.         if (locationx < 0)
  512.             locationx = GRID_SIZE-1;
  513.         else if (locationx > GRID_SIZE-1)
  514.             locationx = 0;
  515.  
  516.         if (locationy < 0)
  517.             locationy = GRID_SIZE-1;
  518.         else if (locationy > GRID_SIZE-1)
  519.             locationy = 0;
  520.  
  521.         ///Location Handler
  522.         grid[locationx][locationy] = 'O';
  523.  
  524.  
  525.  
  526.  
  527.  
  528.         /*for (int i = 0; i < 100; i++)
  529.         {
  530.             system("");
  531.         }*/
  532.  
  533.         if (speed == 0)
  534.             system("pause >nul");
  535.         else
  536.         {
  537.             for (int i = 0; i < 10 + speed; i++)
  538.             {
  539.                 system("");
  540.             }
  541.         }
  542.         system("cls");
  543.  
  544.     }
  545. }
  546.  
  547.  
  548. int abs(int a)
  549. {
  550.     if (a < 0)
  551.         return -a;
  552.     else return a;
  553.  
  554. }
  555.  
  556.  
  557. bool noCollision(int x, int y, char** grid)
  558. {
  559.     if (x > GRID_SIZE-1)
  560.         x = 0;
  561.     else if (x < 0)
  562.         x = GRID_SIZE-1;
  563.  
  564.     if (y > GRID_SIZE-1)
  565.         y = 0;
  566.     else if (y < 0)
  567.         y = GRID_SIZE-1;
  568.  
  569.         //cout<<endl<<"x: "<<x<<" y: "<<y<<" '"<<grid[x][y]<<"'";
  570.  
  571.     //return (grid[x][y] == ' ' || grid[x][y] == '+');
  572.     return (grid[x][y] != 'o');
  573. }
  574.  
  575. void game_over()
  576. {
  577.     cerr<<endl<<"GAME OVER!"<<endl;
  578.     system("pause");
  579. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement