Advertisement
TroubleMaker84

Untitled

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