Advertisement
TroubleMaker84

Untitled

Mar 13th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 17.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <Windows.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8.  
  9. #define LUNGIME 92
  10. #define LATIME 20
  11. #define SIMBOL_MARGINE1 '-'
  12. #define SIMBOL_MARGINE2 '|'
  13.  
  14. using namespace std;
  15.  
  16. struct Monstru{
  17.     int HP, poz, pozy;
  18.     char FORMA[5][7], LASER;
  19. };
  20. Monstru M1, M2, M3, M4, R;
  21. struct wave {
  22.     int Wave_Size;
  23.     Monstru  Monster[Wave_Size];
  24.     int monster_type[Wave_Size];
  25.     }Wave1, Wave2,Wave3, Wave4;
  26.  
  27.  
  28. int sh = 1;
  29. bool GAMEEND = false;
  30. char matTer[LATIME][LUNGIME];
  31. short int pozNava = 4;
  32.  
  33. void m1 (){
  34.     M1.pozy = 88;
  35.     do
  36.         M1.poz = rand() % 19;
  37.     while (
  38.         M1.poz < 3 ||
  39.         matTer[M1.poz][M1.pozy] != '\0' ||
  40.         matTer[M1.poz][M1.pozy + 1] != '\0' ||
  41.         matTer[M1.poz - 1][M1.pozy + 1] != '\0' ||
  42.         matTer[M1.poz + 1][M1.pozy + 1] != '\0' ||
  43.         matTer[M1.poz - 1][M1.pozy + 2] != '\0'||
  44.         matTer[M1.poz][M1.pozy + 2] != '\0'||
  45.         matTer[M1.poz + 1][M1.pozy + 2] != '\0'
  46.     );
  47.     matTer[M1.poz][M1.pozy] = M1.FORMA[2][1];
  48.     matTer[M1.poz - 1][M1.pozy + 2] =
  49.     matTer[M1.poz][M1.pozy + 2] =
  50.     matTer[M1.poz + 1][M1.pozy + 2] =
  51.     matTer[M1.poz - 1][M1.pozy + 1] =
  52.     matTer[M1.poz][M1.pozy + 1] =
  53.     matTer[M1.poz + 1][M1.pozy + 1] =
  54.     M1.FORMA[2][2];
  55.  
  56. }
  57. void m2(){
  58.     M2.pozy = 88;
  59.     do
  60.         M2.poz = rand() % 18;
  61.     while (M2.poz < 3 || matTer[M2.poz][M2.pozy] != '\0' || matTer[M2.poz][M2.pozy + 1] != '\0' || matTer[M2.poz][M2.pozy + 2] != '\0');
  62.     matTer[M2.poz][M2.pozy] =
  63.     matTer[M2.poz][M2.pozy + 1] =
  64.     matTer[M2.poz][M2.pozy + 2] =
  65.     M2.FORMA[1][1];
  66. }
  67.  
  68. void m3(){
  69.     M3.pozy = 88;
  70.     do
  71.         M3.poz = rand() % 18;
  72.     while (
  73.         M3.poz < 3 ||
  74.         matTer[M3.poz][M3.pozy] != '\0' ||
  75.         matTer[M3.poz][M3.pozy + 1] != '\0' ||
  76.         matTer[M3.poz - 1][M3.pozy + 1] != '\0' ||
  77.         matTer[M3.poz + 1][M3.pozy + 1] != '\0' ||
  78.         matTer[M3.poz - 1][M3.pozy + 2] != '\0' ||
  79.         matTer[M3.poz + 1][M3.pozy + 2] != '\0'
  80.     );
  81.     matTer[M3.poz][M3.pozy] =
  82.     matTer[M3.poz][M3.pozy + 1] =
  83.     matTer[M3.poz - 1][M3.pozy + 1] =
  84.     matTer[M3.poz - 1][M3.pozy + 2] =
  85.     matTer[M3.poz + 1][M3.pozy + 1] =
  86.     matTer[M3.poz + 1][M3.pozy + 2] =
  87.     M3.FORMA[2][1];
  88. }
  89.  
  90. void m4(){
  91.     M4.pozy = 88;
  92.     do
  93.         M4.poz = rand() %  16;
  94.     while (
  95.         M4.poz < 5 ||
  96.         matTer[M4.poz - 1][M4.pozy - 1] != '\0' ||
  97.         matTer[M4.poz][M4.pozy - 1] != '\0' ||
  98.         matTer[M4.poz][M4.pozy + 2] != '\0' ||
  99.         matTer[M4.poz + 1][M4.pozy - 1] != '\0' ||
  100.         matTer[M4.poz - 1][M4.pozy - 2] != '\0' ||
  101.         matTer[M4.poz + 1][M4.pozy - 2] != '\0' ||
  102.         matTer[M4.poz - 1][M4.pozy] != '\0' ||
  103.         matTer[M4.poz - 1][M4.pozy + 1] != '\0' ||
  104.         matTer[M4.poz + 1][M4.pozy] != '\0'||
  105.         matTer[M4.poz + 1][M4.pozy+1] != '\0' ||
  106.         matTer[M4.poz - 1][M4.pozy+2] != '\0'||
  107.         matTer[M4.poz + 1][M4.pozy + 2] != '\0' ||
  108.         matTer[M4.poz][M4.pozy] != '\0' ||
  109.         matTer[M4.poz][M4.pozy + 1] != '\0'
  110.     );
  111.  
  112.     matTer[M4.poz -1][M4.pozy-1]=M4.FORMA[1][2];
  113.     matTer[M4.poz][M4.pozy-1]=matTer[M4.poz][M4.pozy+2]=M4.FORMA[2][2];
  114.     matTer[M4.poz +1][M4.pozy-1]=M4.FORMA[4][1];
  115.     matTer[M4.poz-1][M4.pozy-2]=matTer[M4.poz +1][M4.pozy-2]=M4.FORMA[1][1];
  116.     matTer[M4.poz -1 ][M4.pozy]=matTer[M4.poz -1][M4.pozy+1]=matTer[M4.poz +1][M4.pozy]=matTer[M4.poz +1][M4.pozy+1]=M4.FORMA[1][3];
  117.     matTer[M4.poz -1][M4.pozy+2]=M4.FORMA[1][6];
  118.     matTer[M4.poz+1][M4.pozy+2]=M4.FORMA[4][6];
  119.     matTer[M4.poz][M4.pozy]=matTer[M4.poz][M4.pozy+1]=M4.FORMA[3][3];
  120. }
  121.  
  122. void r(){
  123.     R.pozy = 86;
  124.     do
  125.         R.poz = rand () % 18;
  126.     while (
  127.         R.poz<2 ||
  128.         matTer[R.poz][R.pozy] != '\0' ||
  129.         matTer[R.poz - 1][R.pozy + 1] != '\0' ||
  130.         matTer[R.poz - 1][R.pozy + 2] != '\0' ||
  131.         matTer[R.poz - 1][R.pozy + 3] != '\0' ||
  132.         matTer[R.poz][R.pozy + 1] != '\0' ||
  133.         matTer[R.poz][R.pozy + 2] != '\0' ||
  134.         matTer[R.poz][R.pozy + 3] != '\0'
  135.     );
  136.     matTer[R.poz][R.pozy + 4] = R.FORMA[2][4];
  137.     matTer[R.poz - 1][R.pozy + 1] = matTer[R.poz - 1][R.pozy + 2] = matTer[R.poz - 1][R.pozy + 3] = R.FORMA[1][2];
  138.     matTer[R.poz][R.pozy + 1] = matTer[R.poz][R.pozy + 2] = matTer[R.poz][R.pozy + 3] = R.FORMA[1][2];
  139.     matTer[R.poz][R.pozy] = R.FORMA[2][1];
  140. }
  141.  
  142. void show_cursor(bool show){ //vizibilitate curosor in consola
  143.     HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  144.     CONSOLE_CURSOR_INFO infCursor;
  145.     GetConsoleCursorInfo(out, &infCursor);
  146.     infCursor.bVisible = show;
  147.     SetConsoleCursorInfo(out, &infCursor);
  148. }
  149.  
  150. int check (char x){
  151.     int i,j;
  152.     for(i = 1; i <= 3; i++)
  153.         for(j = 1; j <= 3; j++)
  154.             if (x == M1.FORMA[i][j])
  155.                 return 1;
  156.     if (x == M2.FORMA[1][1])
  157.         return 2;
  158.     for(i = 1; i <= 3; i++)
  159.         for(j = 1;j <= 3; j++)
  160.             if (x == M3.FORMA[i][j])
  161.                 return 3;
  162.     for(i = 1; i <= 5; i++)
  163.         for(j = 1; j <= 4; j++)
  164.             if (x == M4.FORMA[i][j])
  165.                 return 4;
  166.     for(i = 1; i <= 2; i++)
  167.         for(j = 1; j <= 4; j++)
  168.             if(x == R.FORMA[i][j])
  169.                 return 5;
  170.     return 0;
  171. }
  172.  
  173. void DEL_M1(){
  174.     for(int i = M1.poz - 1; i <= M1.poz + 1; i++)
  175.         for(int j = M1.pozy; j <= M1.pozy + 3; j++)
  176.                 matTer[i][j]='\0';
  177. }
  178. void DEL_M2(){
  179.     for(int j = M2.pozy; j <= M2.pozy + 2; j++)
  180.         matTer[M2.poz][j]='\0';
  181. }
  182. void DEL_M3(){
  183.     for(int i = M3.poz - 1; i <= M3.poz + 1; i++)
  184.         for(int j = M3.pozy; j <= M3.pozy + 2; j++)
  185.             if(matTer[i][j] == 'O')
  186.                 matTer[i][j] = '\0';
  187. }
  188. void DEL_M4(){
  189.     for(int i = M4.poz - 1; i <= M4.poz + 2; i++)
  190.         for(int j = M4.pozy - 2; j <= M4.pozy + 3; j++)
  191.             if(check(matTer[i][j]) == 4)
  192.                 matTer[i][j]='\0';
  193. }
  194.  
  195. void afisare(){
  196.     for (int i = 1; i <= LATIME - 1; i++){
  197.         for (int j = 1; j <= LUNGIME - 1; j++)
  198.                 cout << matTer[i][j];
  199.         cout << endl;
  200.         }
  201. }
  202.  
  203. void creare_teren(){
  204.     int i;
  205.     for (i = 1; i <= LATIME - 1; i++)
  206.         matTer[i][1] = matTer[i][LUNGIME - 1] = SIMBOL_MARGINE2;
  207.     for (i = 1; i <= LUNGIME - 1; i++)
  208.         matTer[1][i] = matTer[LATIME - 1][i] = SIMBOL_MARGINE1;
  209. }
  210.  
  211. void nava(){
  212.     matTer[2][2] = matTer[3][2] = matTer[4][2] = matTer[3][3] = (char)219;
  213. }
  214. void wave1{
  215.     int i;
  216.     wave1.Wave_size=20;
  217.     for(i=1;i<=wave1.Wave_Size;i++)
  218.         switch (rand()%4)
  219.             case 0: {Monstri[i]=M1;
  220.                     Monster_Type[i]=1;}
  221.             case 1: {Monstri[i]=M2;
  222.             Monster_Type[i]=2;}
  223.             case 2: {Monstri[i]=M3;
  224.             Monster_Type[i]=3;}
  225.             case 3: {Monstri[i]=M4;
  226.             Monster_Type[i]=4;}
  227.    
  228. }
  229. void miscare(){
  230.     char newKey;
  231.     int i,j;
  232.     if (_kbhit())
  233.         newKey = getch();
  234.     if(newKey == 's' ||newKey == 'S')
  235.         if(pozNava < 18){
  236.             matTer[pozNava + 1][2] = matTer[pozNava][2];
  237.             matTer[pozNava ][3] = matTer[pozNava-1][3];
  238.             matTer[pozNava - 2][2] = matTer[pozNava-1][3] = '\0';
  239.             pozNava++;
  240.         }
  241.     if(newKey == 'w' || newKey == 'W')
  242.         if(pozNava > 4){
  243.             matTer[pozNava - 3][2] = matTer[pozNava][2];
  244.             matTer[pozNava - 2][3] = matTer[pozNava - 1][3];
  245.             matTer[pozNava][2] = matTer[pozNava - 1][3] = '\0';
  246.             pozNava--;
  247.         }
  248.     if(newKey == (char)32){
  249.         if (matTer[pozNava - 1][4] != '\0')
  250.             matTer[pozNava - 1][4] = '\0';
  251.         matTer[pozNava - 1][4] = char(248);
  252.     }
  253.  
  254.     //sh, "shoot" in prima faza, era o variabila cu care voiam sa controlez rata la care trag navele lor
  255.     if(sh % 25 == 1 && M1.HP != 0)
  256.         matTer[M1.poz][M1.pozy - 1] = M1.LASER;
  257.     if(sh % 25 == 7 && M2.HP != 0)
  258.         matTer[M2.poz][M2.pozy - 1] = M2.LASER;
  259.     if(sh % 25 == 13 && M3.HP != 0)
  260.         matTer[M3.poz][M3.pozy - 1] = M3.LASER;
  261.     if(sh % 25 == 17 && M4.HP != 0)
  262.         matTer[M4.poz - 1][M4.pozy - 3] = matTer[M4.poz + 1][M4.pozy - 3] = M4.LASER;
  263.     sh++;
  264.     for(i = 2; i <= LATIME - 2; i++)
  265.         for(j = 2 ; j <= LUNGIME - 2; j++)
  266.         {if(matTer[i][j] == (char)248)
  267.             {
  268.                 if(
  269.                     matTer[i][j + 2] == M1.LASER ||
  270.                     matTer[i][j + 2] == M2.LASER ||
  271.                     matTer[i][j + 2] == M3.LASER ||
  272.                     matTer[i][j + 2] == M4.LASER ||
  273.                     matTer[i][j + 1] == M1.LASER ||
  274.                     matTer[i][j + 1] == M2.LASER ||
  275.                     matTer[i][j + 1] == M3.LASER ||
  276.                     matTer[i][j + 1] == M4.LASER
  277.                 )
  278.                     matTer[i][j + 2] = matTer[i][j + 1] = '\0';
  279.                 else
  280.                     if(matTer[i][j + 2] == '\0'){
  281.                         if(j < LUNGIME - 3){
  282.                             matTer[i][j + 2] = (char)248;
  283.                             matTer[i][j] = '\0';
  284.                             j = j + 2;
  285.                         }
  286.                         else
  287.                             matTer[i][j]='\0';
  288.                     }
  289.                     else{
  290.                         switch (check(matTer[i][j + 2]))
  291.                         {                                   //de aici si pana la urmatorul, verific ce "monstru" a lovit
  292.                         case 1:{                            // printr-un switch care verifica acel caracter care este in 2 pozitii
  293.                             M1.HP -= 1;                     //unde ar trebui sa se mute glontele
  294.                             if(M1.HP == 0)
  295.                             DEL_M1();
  296.                             break;
  297.                         }
  298.                         case 2:{
  299.                             M2.HP = 0; //daca distrugi nava, nici sa nu mai traga
  300.                             DEL_M2();
  301.                             break;
  302.                         }
  303.                         case 3:{
  304.                             M3.HP--;
  305.                             if(M3.HP == 0)
  306.                                 DEL_M3();
  307.                             break;
  308.                         }
  309.                         case 4:{
  310.                             if(matTer[M4.poz][M4.pozy]=='\0')
  311.                                     {
  312.                                     Sleep(10000);}
  313.                             M4.HP--;
  314.                             if(M4.HP==0){
  315.                                 DEL_M4();}
  316.                             break;
  317.                         }
  318.                         default: break;
  319.                         }
  320.                         matTer[i][j]='\0';
  321.                     }        //daca s-a intrat pe acel else, se loveste de ceva, fac ce fac, si dupa sterg glontele
  322.             }
  323.             else
  324.                 if(matTer[i][j] == M1.LASER || matTer[i][j] == M2.LASER || matTer[i][j] == M3.LASER || matTer[i][j] == M4.LASER){
  325.                     if (matTer[i][j - 2] == char(219) || matTer[i][j - 1] == char(219))
  326.                         GAMEEND=TRUE;       //daca esti lovit, se termina jocul
  327.                     else
  328.                         if(j==3){
  329.                             matTer[i][j] = '\0';      //daca glontele lui a ajuns la coloana 3, dispare
  330.                         }
  331.                         else
  332.                             if(j > 3){
  333.                                 matTer[i][j-1]=matTer[i][j];
  334.                                 matTer[i][j]='\0';
  335.                             }         //il tot mut cu o pozitie la fiecare parcurgere
  336.                 }
  337.                 else{
  338.                     if(sh % 3 == 0){     //"sh" a devenit si rata la care se misca navele, ca nu vreau sa o faca mereu
  339.                         if(matTer[i][j] != '\0' && matTer[i][j] != char(219))
  340.                             if(check(matTer[i][j])==1){              //toate if/else verifica muta componenta unei nave
  341.                                 if(M1.pozy == 3){       //si in caz ca ajunge la final, o face sa dispara
  342.                                     if(matTer[i][j - 1] == char(219))
  343.                                         GAMEEND=TRUE;
  344.                                     else{
  345.                                         DEL_M1();
  346.                                         M1.HP=0;
  347.                                     }
  348.                                 }
  349.                                 matTer[i][j - 1] = matTer[i][j];
  350.                                 if(i == M1.poz && j == M1.pozy)
  351.                                     M1.pozy--;
  352.                             }
  353.                             else
  354.                                 if(check(matTer[i][j]) == 2){
  355.                                     if(M2.pozy == 3){
  356.                                         if(matTer[i][j - 1] == char(219))
  357.                                             GAMEEND = TRUE;
  358.                                         else{
  359.                                             DEL_M2();
  360.                                             M2.HP = 0;
  361.                                         }
  362.                                     }
  363.                                     matTer[i][j - 1] = matTer[i][j];
  364.                                     if(i == M2.poz && j == M2.pozy)
  365.                                         M2.pozy--;
  366.                                 }
  367.                                 else
  368.                                     if(check(matTer[i][j]) == 3){
  369.                                         if(M3.pozy == 3){
  370.                                             if(matTer[i][j - 1] == char(219))
  371.                                                 GAMEEND = TRUE;
  372.                                             else{
  373.                                                 DEL_M3();
  374.                                                 M3.HP = 0;
  375.                                             }
  376.                                         }
  377.                                         matTer[i][j - 1] = matTer[i][j];
  378.                                         if(i == M3.poz && j == M3.pozy)
  379.                                             M3.pozy--;
  380.                                     }
  381.                                     else
  382.                                         if(check(matTer[i][j]) == 4){
  383.                                             if(M4.pozy == 4){
  384.                                                 if(matTer[i][j - 1] == char(219))
  385.                                                     GAMEEND = TRUE;
  386.                                                 else{
  387.                                                     DEL_M4();
  388.                                                     M4.HP = 0;
  389.                                                 }
  390.                                             }
  391.                                             matTer[i][j - 1] = matTer[i][j];
  392.                                             if(i == M4.poz && j == M4.pozy)
  393.                                                 M4.pozy--;
  394.                                         }
  395.                         if(matTer[i][j] != char(219))
  396.                             matTer[i][j] = '\0';
  397.                     }
  398.                 }
  399.  
  400.  
  401.         }}
  402.  
  403. int main (){
  404.     char x;
  405.     cout << "_______________________________________________________________________" << endl;
  406.     cout << "|                                                                      |" << endl;
  407.     cout << "|          *       *   ***  *    ***   **   *     *  ***               |" << endl;
  408.     cout << "|           *  *  *    **   *    *    *  *  *  *  *  **                |" << endl;
  409.     cout << "|            *   *     ***  ***  ***   **   *     *  ***               |" << endl;
  410.     cout << "|                                                                      |" << endl;
  411.     cout << "|                                                                      |" << endl;
  412.     cout << "|            PRESS 1 TO START GAME                                     |" << endl;
  413.     cout << "|            PRESS 2 TO EXIT                                           |" << endl;
  414.     cout << "_______________________________________________________________________" << endl;
  415.     cin >> x;
  416.     system("cls");
  417.     if(x == '1'){
  418.         creare_teren();
  419.         show_cursor(false);
  420.         M1.HP = 3;
  421.         M2.HP = 1;
  422.         M3.HP = 4;
  423.         M4.HP = 6;
  424.         M1.FORMA[2][1] = (char)174;
  425.         M1.FORMA[1][2] = M1.FORMA[2][2] = M1.FORMA[3][2] = (char)254;
  426.         M2.FORMA[1][1] = M2.FORMA[1][2] = M2.FORMA[1][3] = char(178);
  427.         M3.FORMA[2][1] = M3.FORMA[2][2] = M3.FORMA[1][2] = M3.FORMA[1][3] = M3.FORMA[3][2] = M3.FORMA[3][3] = 'O';
  428.         R.FORMA[2][1] = '<';
  429.         R.FORMA[1][2] = R.FORMA[1][3] = R.FORMA[2][2] = R.FORMA[2][3] = '_';
  430.         R.FORMA[2][4] = '|';
  431.         M4.FORMA[1][1] = M4.FORMA[4][1] = '-';
  432.         M4.FORMA[1][2] = char(201);
  433.         M4.FORMA[1][6] = char(187);
  434.         M4.FORMA[2][2] = M4.FORMA[3][2] = char(186);
  435.         M4.FORMA[1][3] = M4.FORMA[1][4] = M4.FORMA[1][5] = M4.FORMA[4][3] = M4.FORMA[4][4] = M4.FORMA[4][5] = char(205);
  436.         M4.FORMA[2][6] = M4.FORMA[3][6] = char(186);
  437.         M4.FORMA[4][1] = char(200);
  438.         M4.FORMA[4][6] = char(188);
  439.         M4.FORMA[3][2] = M4.FORMA[3][3] = ' ';
  440.         srand(time(NULL));
  441.         m1();
  442.         m2();
  443.         m3();
  444.         m4();
  445.         r();
  446.         nava();
  447.         M1.LASER = char (223);
  448.         M2.LASER = '=';
  449.         M3.LASER = char(249);
  450.         M4.LASER = '~';
  451.         afisare();
  452.         system("cls");
  453.         while(GAMEEND==FALSE){
  454.                 miscare();
  455.                 Sleep(50);
  456.                 system("cls");
  457.                 afisare();
  458.  
  459.                 if(M1.HP == 0 && M2.HP == 0 && M3.HP == 0 && M4.HP == 0){
  460.                     GAMEEND=TRUE;
  461.                     cout << "CONGRATS";
  462.                 }
  463.         }
  464.     }
  465.     else
  466.         if(x == '2')
  467.           cout << "GAME OVER";
  468.           exit(0);
  469.     return 0;
  470. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement