Advertisement
TroubleMaker84

Untitled

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