Advertisement
rengetsu

Laivu_Musio_Zaidimas

Oct 6th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.07 KB | None | 0 0
  1. //Laivu zaidimas 2.0. Pavel Trostianko. ISf-17/1.
  2.  
  3. #include <iostream>
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <windows.h>
  8. #include <conio.h>
  9.  
  10. using namespace std;
  11.  
  12. // Langeliu savybes
  13. const char Empty_Cell  = '#'; // Tuscias langelis
  14. const char Ship_Cell   = '$'; // Laivas
  15. const char Dead_Cell   = '*'; // Skestas laivas
  16. const char Missed_Cell = 'O'; // Nepataiko
  17. const char Error_Cell  = char(7); // Error
  18.  
  19. // Zaidimu parametrai
  20. int Ship_Start_X, Ship_Start_Y;
  21. int Ship_End_X,   Ship_End_Y;
  22.  
  23. // Virsutines kordinates
  24. const string Arena_Top = "\t0123456789"; // musu Arena
  25.  
  26. // Testable
  27. bool  game_go = false, game_sb = false, game_eb = false;
  28.  
  29. class game_arena {
  30.  
  31.     private:
  32.         int shipss[4] = {4,3,2,1};
  33.         char arena[10][10];
  34.     public:
  35.  
  36.         // Starting Our Game
  37.         void Game_Starting()
  38.         {
  39.             system("COLOR 6");
  40.             cout << "\n\n\n\n\n\n";
  41.             cout << "\t\Laivu Zaidimas 2.0. Pavel Trostianko ISf-17/1.\n\n\n\n";
  42.             cout << "\t\      Press Enter to Start Game... \n\n\n\n";
  43.             getch();
  44.             system("COLOR 07");
  45.         }
  46.  
  47.         // Clear Our Screen
  48.         void Clear_Arena() // funkcija kuri nustato lanko langeliu vertes
  49.         {
  50.             for(int arena_y = 0; arena_y < 10; arena_y++)
  51.             {
  52.                 for(int arena_x=0; arena_x < 10; arena_x++)
  53.                 {
  54.                     arena[arena_y][arena_x] = Empty_Cell; // priskiria X savybe visiems langeliams
  55.                 }
  56.             }
  57.         }
  58.  
  59.         // Show Our Screen
  60.         void Show_Arena(bool trk) // funkcija kuri rodo musu lauka ekrane
  61.         {
  62.             cout << Arena_Top << endl; // deda virsutines kordinates i virsu
  63.             for(int columns = 0; columns < 10; columns++)
  64.             {
  65.                 cout << columns << "\t";
  66.                 for(int fields = 0; fields < 10; fields++) //prideda po viena lauko langelius
  67.                 {
  68.                     cout << arena[columns][fields];
  69.                 }
  70.                 cout << endl;
  71.             }
  72.             if(trk == true)for(int i=0; i<4; i++){cout << endl; cout << i+1 << " Cells ships you can add: " << shipss[i] << endl;}
  73.         }
  74.  
  75.   bool place_ship(int x1, int y1, int x2, int y2){
  76.     int temp;
  77.     if(x1>x2){temp=x1; x1=x2; x2=temp;}
  78.     if(y1>y2){temp=y1; y1=y2; y2=temp;}
  79.     if(x2-x1>3 || y2-y1>3 || x1<0 || y1<0 || x2>9 || y2>9 || (x2-x1>0 && y2-y1>0) )
  80.        return false;
  81.  
  82.     for(int y=0; y<=y2-y1; y++)
  83.       for(int x=0; x<=x2-x1; x++)
  84.         if(arena[x1+x]              [y1+y]              != Empty_Cell ||
  85.            arena[x1+x]              [int(abs(y1+y-1))]  != Empty_Cell || // up
  86.            arena[x1+x+1-(x1+x+1)/10][int(abs(y1+y-1))]  != Empty_Cell || // right-up
  87.            arena[x1+x+1-(x1+x+1)/10][y1+y]              != Empty_Cell || // right
  88.            arena[x1+x+1-(x1+x+1)/10][y1+y+1-(y1+y+1)/10]!= Empty_Cell || // right-down
  89.            arena[x1+x]              [y1+y+1-(y1+y+1)/10]!= Empty_Cell || // down
  90.            arena[int(abs(x1+x-1))]  [y1+y+1-(y1+y+1)/10]!= Empty_Cell || // left-down
  91.            arena[int(abs(x1+x-1))]  [y1+y]              != Empty_Cell || // left
  92.            arena[int(abs(x1+x-1))]  [int(abs(y1+y-1))]  != Empty_Cell // left-up
  93.            ) return false;
  94.  
  95.     if(x1==x2   && y1==y2)  {if(shipss[0]==0)return false; shipss[0]--;}
  96.     if(x2-x1==1 || y2-y1==1){if(shipss[1]==0)return false; shipss[1]--;}
  97.     if(x2-x1==2 || y2-y1==2){if(shipss[2]==0)return false; shipss[2]--;}
  98.     if(x2-x1==3 || y2-y1==3){if(shipss[3]==0)return false; shipss[3]--;}
  99.     for(int y=0; y<=y2-y1; y++)
  100.       for(int x=0; x<=x2-x1; x++)
  101.         arena[x1+x][y1+y] = Ship_Cell;
  102.     return true;
  103.   }
  104.   void placeAllShips(){
  105.     int x1,y1,x2,y2;
  106.     for(int ilgis=4; ilgis>=1; ilgis--)
  107.       for(int kiek=1; kiek<=5-ilgis; kiek++){
  108.         x1=rand()%10; y1=rand()%10;
  109.         if(rand()%2 == 0 ) { x2=x1+ilgis-1; y2=y1; }
  110.           else{ y2=y1+ilgis-1; x2=x1;}
  111.         if(!place_ship(x1,y1,x2,y2))
  112.           kiek--;
  113.       }
  114.   }
  115.   char shoot(int x, int y){
  116.     if(x>=0 && x<10 && y>=0 && y<10){
  117.       if(arena[x][y] == Ship_Cell) {arena[x][y] = Dead_Cell;return arena[x][y];}
  118.       else if(arena[x][y] == Empty_Cell) {arena[x][y]=Missed_Cell;return arena[x][y];}
  119.     }
  120.     return Error_Cell;
  121.  
  122.   }
  123. bool ceh_or_not_lost()
  124. { int tikrinmas;tikrinmas=0;
  125.   for(int y=0; y<10; y++){
  126.       for(int x=0; x<10; x++)
  127.         if(arena[x][y] == Dead_Cell)tikrinmas++;
  128.         if(tikrinmas==20)return false;
  129.         else return true;
  130. }}
  131. void show_att()
  132. {
  133.    cout << Arena_Top << endl;
  134.     for(int y=0; y<10; y++){
  135.       cout << y+1 << '\t';
  136.       for(int x=0; x<10; x++)
  137.           if (arena[x][y] != Ship_Cell)cout<<arena[x][y];
  138.           else{cout<< Empty_Cell;}
  139.       cout<<endl;
  140.       }
  141.  
  142. }
  143. bool xxxtentacion(int x, int y, bool HARD)
  144. {  int ter,ter_shoot;
  145. if(HARD==false){
  146.   do{ter=rand()%4;
  147.   if((arena[x+1][y] == Empty_Cell || arena[x+1][y] == Ship_Cell) && ter==0){for(int i=0;i<4;i++){ter_shoot=shoot(x+1+i,y); if(ter_shoot==Missed_Cell)break;}break;}
  148.   if((arena[x-1][y] == Empty_Cell || arena[x-1][y] == Ship_Cell) && ter==1){for(int i=0;i<4;i++){ter_shoot=shoot(x-1-i,y); if(ter_shoot==Missed_Cell)break;}break;}
  149.   if((arena[x][y+1] == Empty_Cell || arena[x][y-1] == Ship_Cell) && ter==2){for(int i=0;i<4;i++){ter_shoot=shoot(x,y+1+i); if(ter_shoot==Missed_Cell)break;}break;}
  150.   if((arena[x][y-1] == Empty_Cell || arena[x][y-1] == Ship_Cell) && ter==3){for(int i=0;i<4;i++){ter_shoot=shoot(x,y-1-i); if(ter_shoot==Missed_Cell)break;}break;}}while(true);}
  151.   else if(HARD==true){while(true){
  152.   if( arena[x+1][y]!= Ship_Cell && arena[x-1][y]!= Ship_Cell && arena[x][y-1]!= Ship_Cell && arena[x][y+1] != Ship_Cell){break;}
  153.   if( arena[x+1][y]== Ship_Cell){for(int i=0;i<4;i++){ter_shoot=shoot(x+1+i,y); if(ter_shoot==Missed_Cell)break;}}
  154.   if( arena[x-1][y]== Ship_Cell){for(int i=0;i<4;i++){ter_shoot=shoot(x-1-i,y); if(ter_shoot==Missed_Cell)break;}}
  155.   if( arena[x][y-1]== Ship_Cell){for(int i=0;i<4;i++){ter_shoot=shoot(x,y-1-i); if(ter_shoot==Missed_Cell)break;}}
  156.   if( arena[x][y+1]== Ship_Cell){for(int i=0;i<4;i++){ter_shoot=shoot(x,y+1+i); if(ter_shoot==Missed_Cell)break;}}}}
  157.   }
  158.  
  159. void testing()
  160. {
  161.     int ter=0;
  162.     bool tik=false;
  163.  
  164.     for(int y=0; y<10; y++)
  165.       for(int x=0; x<10; x++)
  166.         if(arena[x][y] == Dead_Cell){ tik=false;
  167.                 if((arena[(x+1)-((x+1)/10)][y]!= Dead_Cell && arena[int(abs(x-1))][y]!=Dead_Cell && arena[x][int(abs(y-1))]!=Dead_Cell && arena[x][(y+1)-((y+1)/10)]!=Dead_Cell) &&
  168.                    (arena[(x+1)-((x+1)/10)][y] != Ship_Cell && arena[int(abs(x-1))][y] != Ship_Cell && arena[x][int(abs(y-1))]!=Ship_Cell && arena[x][(y+1)-((y+1)/10)] != Ship_Cell)){
  169.                     arena[(x+1)-((x+1)/10)][y]=Missed_Cell; arena[int(abs(x-1))][y]=Missed_Cell; arena[x][int(abs(y-1))]=Missed_Cell; arena[x][(y+1)-((y+1)/10)]=Missed_Cell;
  170.                     arena[(x+1)-((x+1)/10)][y+1-(y+1)/10]=Missed_Cell; arena[(x+1)-((x+1)/10)][int(abs(y-1))]=Missed_Cell; arena[int(abs(x-1))][(y+1)-((y+1)/10)]=Missed_Cell; arena[int(abs(x-1))][int(abs(y-1))]=Missed_Cell;
  171.                    }//KAI LAIVAS 1 LANGELIO
  172.                  if((arena[(x+1)-((x+1)/10)][y]==Dead_Cell || arena[x][(y+1)-((y+1)/10)]==Dead_Cell) && arena[int(abs(x-1))][y] != Ship_Cell && arena[x][int(abs(y-1))] != Ship_Cell ){
  173.                     if(arena[(x+1)-((x+1)/10)][y]==Dead_Cell){ter=1;for(int i=0; i<3;i++){if(arena[(x+1+i)-((x+1+i)/10)][y]==Dead_Cell)ter++;if(arena[(x+i+1)-((x+1+i)/10)][y] == Ship_Cell)tik=true;}
  174.                      if(tik==false)
  175.                      for(int i=0; i<ter;i++){
  176.                             if(arena[(x+1+i)-((x+1+i)/10)][y] == Empty_Cell)arena[(x+1+i)-((x+1+i)/10)][y]=Missed_Cell;
  177.                             if(arena[int(abs(x-1+i))][y] == Empty_Cell)     arena[int(abs(x-1+i))][y]=Missed_Cell;
  178.                             if( arena[x+i][int(abs(y-1))] == Empty_Cell)    arena[x+i][int(abs(y-1))]=Missed_Cell;
  179.                             if(arena[x+i][(y+1)-((y+1)/10)] == Empty_Cell)  arena[x+i][(y+1)-((y+1)/10)]=Missed_Cell;
  180.                             if(arena[(x+1+i)-((x+1+i)/10)][y+1-(y+1)/10] == Empty_Cell)arena[(x+1+i)-((x+1+i)/10)][y+1-(y+1)/10]=Missed_Cell;
  181.                             if(arena[(x+1+i)-((x+1+i)/10)][int(abs(y-1))] == Empty_Cell)arena[(x+1+i)-((x+1+i)/10)][int(abs(y-1))]=Missed_Cell;
  182.                             if(arena[int(abs(x-1+i))][(y+1)-((y+1)/10)] == Empty_Cell)arena[int(abs(x-1+i))][(y+1)-((y+1)/10)]=Missed_Cell;
  183.                             if(arena[int(abs(x-1+i))][int(abs(y-1))] == Empty_Cell)arena[int(abs(x-1+i))][int(abs(y-1))]=Missed_Cell;
  184.                             tik=true;}
  185.                     }
  186.                     else if(arena[x][(y+1)-((y+1)/10)]==Dead_Cell){ter=1;for(int i=0; i<3;i++){if(arena[x][(y+1+i)-((y+1+i)/10)]==Dead_Cell)ter++;if(arena[x][(y+1+i)-((y+1+i)/10)] == Ship_Cell)tik=true;}
  187.                     if(tik==false)
  188.                      for(int i=0; i<ter;i++)
  189.                      {
  190.                             if(arena[(x+1)-((x+1)/10)][y+i] == Empty_Cell)arena[(x+1)-((x+1)/10)][y+i]=Missed_Cell;
  191.                            if(arena[int(abs(x-1))][y+i] == Empty_Cell)     arena[int(abs(x-1))][y+i]=Missed_Cell;
  192.                             if( arena[x][int(abs(y-1+i))] == Empty_Cell)    arena[x][int(abs(y-1+i))]=Missed_Cell;
  193.                             if(arena[x][(y+1+i)-((y+1+i)/10)] == Empty_Cell)  arena[x][(y+1+i)-((y+1+i)/10)]=Missed_Cell;
  194.                             if(arena[(x+1)-((x+1)/10)][y+1+i-(y+1+i)/10] == Empty_Cell)arena[(x+1)-((x+1)/10)][y+1+i-(y+1+i)/10]=Missed_Cell;
  195.                             if(arena[(x+1)-((x+1)/10)][int(abs(y-1+i))] == Empty_Cell)arena[(x+1)-((x+1)/10)][int(abs(y-1+i))]=Missed_Cell;
  196.                             if(arena[int(abs(x-1))][(y+1+i)-((y+1+i)/10)] == Empty_Cell)arena[int(abs(x-1))][(y+1+i)-((y+1+i)/10)]=Missed_Cell;
  197.                             if(arena[int(abs(x-1))][int(abs(y-1+i))] == Empty_Cell)arena[int(abs(x-1))][int(abs(y-1+i))]=Missed_Cell;
  198.                             tik=true;
  199.                      }
  200.                     }}
  201.                  }
  202.                 }
  203. };
  204.  
  205. void FindingPlaceForObject(int x, int y){
  206.     COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  207.     }
  208.  
  209. int main()
  210. {
  211.     while(true)
  212.     {
  213.     int cc=0, x1, x2, y1, y2;
  214.     bool error=false;
  215.  
  216.     srand(time(NULL));
  217.     game_arena mano, pc;
  218.     mano.Game_Starting();
  219.     mano.Clear_Arena();
  220.     pc.Clear_Arena();
  221.  
  222.     do{system("CLS");mano.Show_Arena(true);
  223.  
  224.     if(error==true)
  225.     {
  226.         cout << " Error !" << endl;
  227.         error=false;
  228.     }
  229.  
  230.     cout << endl;
  231.     cout<<"Write down coordinates where you want to place your ship (Y, X): ";
  232.     cin>>x1>>y1>>x2>>y2;
  233.  
  234.     if(!mano.place_ship(x1,y1,x2,y2))
  235.     {
  236.         error=true;
  237.         cc--;
  238.     }
  239.     else{mano.place_ship(x1,y1,x2,y2);
  240.     cc++;
  241.     if(x1==8520)
  242.     {
  243.         mano.placeAllShips();
  244.         cc = 10;
  245.     }}
  246.     }while(cc != 10);
  247.     pc.placeAllShips();
  248.  
  249.     char ter;
  250.     int  atk_x, atk_y,atk_2_x,atk_2_y;
  251.     bool rtr=false;
  252.  
  253.     do{
  254.         do{kai_intelktas_tupas:
  255.         if(rtr==true){mano.xxxtentacion(atk_2_x, atk_2_y, true);rtr=false;}
  256.         atk_x=rand()%10; atk_y=rand()%10;
  257.         ter=mano.shoot(atk_x,atk_y);}while(ter==Error_Cell);
  258.         mano.testing();
  259.  
  260.         if(!mano.ceh_or_not_lost()){cout<<endl;cout << "GAME OVER"; game_go=true;getch();}
  261.         system("CLS");
  262.         cout<<"\t  My\n";
  263.         mano.Show_Arena(false);
  264.         cout<<"\n\t  Enemy\n";
  265.         UserPlace:
  266.         pc.show_att();if(!pc.ceh_or_not_lost()){cout<<endl;cout << "YOU WIN";game_go=true;getch();}
  267.         do
  268.             {
  269.         cout<<"Сoordinates: (X, Y) ";
  270.         cin>>atk_x>>atk_y;ter=pc.shoot(atk_x, atk_y); if(ter==Error_Cell){
  271.             cout<<"Eroor!\n"<< ter;
  272.             }
  273.         }
  274.         while(ter==Error_Cell);
  275.         pc.testing();
  276.         if(ter==Dead_Cell){goto UserPlace;}
  277.         if(GetAsyncKeyState(VK_ESCAPE) != 0){game_go=true;}
  278.     }
  279.   while(game_go!=true);
  280.   }
  281. return 0;
  282. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement