Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Laivu zaidimas 2.0. Pavel Trostianko. ISf-17/1.
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- #include <ctime>
- #include <windows.h>
- #include <conio.h>
- using namespace std;
- // Langeliu savybes
- const char Empty_Cell = '#'; // Tuscias langelis
- const char Ship_Cell = '$'; // Laivas
- const char Dead_Cell = '*'; // Skestas laivas
- const char Missed_Cell = 'O'; // Nepataiko
- const char Error_Cell = char(7); // Error
- // Zaidimu parametrai
- int Ship_Start_X, Ship_Start_Y;
- int Ship_End_X, Ship_End_Y;
- // Virsutines kordinates
- const string Arena_Top = "\t0123456789"; // musu Arena
- // Testable
- bool game_go = false, game_sb = false, game_eb = false;
- class game_arena {
- private:
- int shipss[4] = {4,3,2,1};
- char arena[10][10];
- public:
- // Starting Our Game
- void Game_Starting()
- {
- system("COLOR 6");
- cout << "\n\n\n\n\n\n";
- cout << "\t\Laivu Zaidimas 2.0. Pavel Trostianko ISf-17/1.\n\n\n\n";
- cout << "\t\ Press Enter to Start Game... \n\n\n\n";
- getch();
- system("COLOR 07");
- }
- // Clear Our Screen
- void Clear_Arena() // funkcija kuri nustato lanko langeliu vertes
- {
- for(int arena_y = 0; arena_y < 10; arena_y++)
- {
- for(int arena_x=0; arena_x < 10; arena_x++)
- {
- arena[arena_y][arena_x] = Empty_Cell; // priskiria X savybe visiems langeliams
- }
- }
- }
- // Show Our Screen
- void Show_Arena(bool trk) // funkcija kuri rodo musu lauka ekrane
- {
- cout << Arena_Top << endl; // deda virsutines kordinates i virsu
- for(int columns = 0; columns < 10; columns++)
- {
- cout << columns << "\t";
- for(int fields = 0; fields < 10; fields++) //prideda po viena lauko langelius
- {
- cout << arena[columns][fields];
- }
- cout << endl;
- }
- if(trk == true)for(int i=0; i<4; i++){cout << endl; cout << i+1 << " Cells ships you can add: " << shipss[i] << endl;}
- }
- bool place_ship(int x1, int y1, int x2, int y2){
- int temp;
- if(x1>x2){temp=x1; x1=x2; x2=temp;}
- if(y1>y2){temp=y1; y1=y2; y2=temp;}
- if(x2-x1>3 || y2-y1>3 || x1<0 || y1<0 || x2>9 || y2>9 || (x2-x1>0 && y2-y1>0) )
- return false;
- for(int y=0; y<=y2-y1; y++)
- for(int x=0; x<=x2-x1; x++)
- if(arena[x1+x] [y1+y] != Empty_Cell ||
- arena[x1+x] [int(abs(y1+y-1))] != Empty_Cell || // up
- arena[x1+x+1-(x1+x+1)/10][int(abs(y1+y-1))] != Empty_Cell || // right-up
- arena[x1+x+1-(x1+x+1)/10][y1+y] != Empty_Cell || // right
- arena[x1+x+1-(x1+x+1)/10][y1+y+1-(y1+y+1)/10]!= Empty_Cell || // right-down
- arena[x1+x] [y1+y+1-(y1+y+1)/10]!= Empty_Cell || // down
- arena[int(abs(x1+x-1))] [y1+y+1-(y1+y+1)/10]!= Empty_Cell || // left-down
- arena[int(abs(x1+x-1))] [y1+y] != Empty_Cell || // left
- arena[int(abs(x1+x-1))] [int(abs(y1+y-1))] != Empty_Cell // left-up
- ) return false;
- if(x1==x2 && y1==y2) {if(shipss[0]==0)return false; shipss[0]--;}
- if(x2-x1==1 || y2-y1==1){if(shipss[1]==0)return false; shipss[1]--;}
- if(x2-x1==2 || y2-y1==2){if(shipss[2]==0)return false; shipss[2]--;}
- if(x2-x1==3 || y2-y1==3){if(shipss[3]==0)return false; shipss[3]--;}
- for(int y=0; y<=y2-y1; y++)
- for(int x=0; x<=x2-x1; x++)
- arena[x1+x][y1+y] = Ship_Cell;
- return true;
- }
- void placeAllShips(){
- int x1,y1,x2,y2;
- for(int ilgis=4; ilgis>=1; ilgis--)
- for(int kiek=1; kiek<=5-ilgis; kiek++){
- x1=rand()%10; y1=rand()%10;
- if(rand()%2 == 0 ) { x2=x1+ilgis-1; y2=y1; }
- else{ y2=y1+ilgis-1; x2=x1;}
- if(!place_ship(x1,y1,x2,y2))
- kiek--;
- }
- }
- char shoot(int x, int y){
- if(x>=0 && x<10 && y>=0 && y<10){
- if(arena[x][y] == Ship_Cell) {arena[x][y] = Dead_Cell;return arena[x][y];}
- else if(arena[x][y] == Empty_Cell) {arena[x][y]=Missed_Cell;return arena[x][y];}
- }
- return Error_Cell;
- }
- bool ceh_or_not_lost()
- { int tikrinmas;tikrinmas=0;
- for(int y=0; y<10; y++){
- for(int x=0; x<10; x++)
- if(arena[x][y] == Dead_Cell)tikrinmas++;
- if(tikrinmas==20)return false;
- else return true;
- }}
- void show_att()
- {
- cout << Arena_Top << endl;
- for(int y=0; y<10; y++){
- cout << y+1 << '\t';
- for(int x=0; x<10; x++)
- if (arena[x][y] != Ship_Cell)cout<<arena[x][y];
- else{cout<< Empty_Cell;}
- cout<<endl;
- }
- }
- bool xxxtentacion(int x, int y, bool HARD)
- { int ter,ter_shoot;
- if(HARD==false){
- do{ter=rand()%4;
- 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;}
- 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;}
- 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;}
- 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);}
- else if(HARD==true){while(true){
- 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;}
- 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;}}
- 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;}}
- 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;}}
- 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;}}}}
- }
- void testing()
- {
- int ter=0;
- bool tik=false;
- for(int y=0; y<10; y++)
- for(int x=0; x<10; x++)
- if(arena[x][y] == Dead_Cell){ tik=false;
- 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) &&
- (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)){
- 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;
- 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;
- }//KAI LAIVAS 1 LANGELIO
- 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 ){
- 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;}
- if(tik==false)
- for(int i=0; i<ter;i++){
- if(arena[(x+1+i)-((x+1+i)/10)][y] == Empty_Cell)arena[(x+1+i)-((x+1+i)/10)][y]=Missed_Cell;
- if(arena[int(abs(x-1+i))][y] == Empty_Cell) arena[int(abs(x-1+i))][y]=Missed_Cell;
- if( arena[x+i][int(abs(y-1))] == Empty_Cell) arena[x+i][int(abs(y-1))]=Missed_Cell;
- if(arena[x+i][(y+1)-((y+1)/10)] == Empty_Cell) arena[x+i][(y+1)-((y+1)/10)]=Missed_Cell;
- 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;
- 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;
- 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;
- 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;
- tik=true;}
- }
- 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;}
- if(tik==false)
- for(int i=0; i<ter;i++)
- {
- if(arena[(x+1)-((x+1)/10)][y+i] == Empty_Cell)arena[(x+1)-((x+1)/10)][y+i]=Missed_Cell;
- if(arena[int(abs(x-1))][y+i] == Empty_Cell) arena[int(abs(x-1))][y+i]=Missed_Cell;
- if( arena[x][int(abs(y-1+i))] == Empty_Cell) arena[x][int(abs(y-1+i))]=Missed_Cell;
- if(arena[x][(y+1+i)-((y+1+i)/10)] == Empty_Cell) arena[x][(y+1+i)-((y+1+i)/10)]=Missed_Cell;
- 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;
- 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;
- 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;
- 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;
- tik=true;
- }
- }}
- }
- }
- };
- void FindingPlaceForObject(int x, int y){
- COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- int main()
- {
- while(true)
- {
- int cc=0, x1, x2, y1, y2;
- bool error=false;
- srand(time(NULL));
- game_arena mano, pc;
- mano.Game_Starting();
- mano.Clear_Arena();
- pc.Clear_Arena();
- do{system("CLS");mano.Show_Arena(true);
- if(error==true)
- {
- cout << " Error !" << endl;
- error=false;
- }
- cout << endl;
- cout<<"Write down coordinates where you want to place your ship (Y, X): ";
- cin>>x1>>y1>>x2>>y2;
- if(!mano.place_ship(x1,y1,x2,y2))
- {
- error=true;
- cc--;
- }
- else{mano.place_ship(x1,y1,x2,y2);
- cc++;
- if(x1==8520)
- {
- mano.placeAllShips();
- cc = 10;
- }}
- }while(cc != 10);
- pc.placeAllShips();
- char ter;
- int atk_x, atk_y,atk_2_x,atk_2_y;
- bool rtr=false;
- do{
- do{kai_intelktas_tupas:
- if(rtr==true){mano.xxxtentacion(atk_2_x, atk_2_y, true);rtr=false;}
- atk_x=rand()%10; atk_y=rand()%10;
- ter=mano.shoot(atk_x,atk_y);}while(ter==Error_Cell);
- mano.testing();
- if(!mano.ceh_or_not_lost()){cout<<endl;cout << "GAME OVER"; game_go=true;getch();}
- system("CLS");
- cout<<"\t My\n";
- mano.Show_Arena(false);
- cout<<"\n\t Enemy\n";
- UserPlace:
- pc.show_att();if(!pc.ceh_or_not_lost()){cout<<endl;cout << "YOU WIN";game_go=true;getch();}
- do
- {
- cout<<"Сoordinates: (X, Y) ";
- cin>>atk_x>>atk_y;ter=pc.shoot(atk_x, atk_y); if(ter==Error_Cell){
- cout<<"Eroor!\n"<< ter;
- }
- }
- while(ter==Error_Cell);
- pc.testing();
- if(ter==Dead_Cell){goto UserPlace;}
- if(GetAsyncKeyState(VK_ESCAPE) != 0){game_go=true;}
- }
- while(game_go!=true);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement