Advertisement
TroubleMaker84

Untitled

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