Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.50 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <set>
  6. #include <windows.h>
  7. #include <conio.h>
  8. #include <cstdlib>
  9. #include <time.h>
  10. using namespace std;
  11. #if defined(_MSC_VER) || __cplusplus > 199711L
  12. #define aut(r,v) auto r = (v)
  13. #else
  14. #define aut(r,v) typeof(v) r = (v)
  15. #endif
  16. #define forA(V) for (aut(it,(V).begin());it!=(V).end();it++)
  17. #define pb push_back
  18. #define mp make_pair
  19. #define fi first
  20. #define se second
  21. #define ll long long
  22. #define ull unsigned ll
  23. #define MOD 1000000007
  24. #define INF (1<<31)-1
  25. #define vi vector <int>
  26. #define pii pair <int,int>
  27. #define pll pair <ll,ll>
  28. #define newl printf("\n")
  29. #define scanvi(v,N) for (int i=1;i<=N;i++) scanf("%d",&v[i]);
  30. #define printvi(v,N) for (int i=1;i<=N;i++) printf("%d ",v[i]);
  31. #define scanmi(v,N,M) for (int i=1;i<=N;i++) for (int j=1;j<=M;j++) scanf("%d",&v[i][j]);
  32. #define printmi(v,N,M) for (int i=1;i<=N;i++,newl) for (int j=1;j<=M;j++) printf("%d ",v[i][j]);
  33. char mat[1005][1005];
  34. int x,y,h=20,w=40,Score,nrE,Highscore,nrSuperTurns,cooldown;
  35. struct el
  36. {
  37.     int x,y;
  38. }Enemies[1005];
  39. bool PlayerAlive=true;
  40. void sp(int choosecolor)
  41. {
  42.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), choosecolor); //FUNCTION OF COLOR
  43. }
  44. int abs(int a){if (a<0) return -a; else return a;}
  45. void CreateRoom()
  46. {
  47.     for (int i=1;i<=w;i++) mat[1][i]='*';
  48.     for (int i=2;i<h;i++)
  49.     {
  50.         mat[i][1]=mat[i][w]='*';
  51.         for (int j=2;j<w;j++) mat[i][j]='-';
  52.     }
  53.     for (int i=1;i<=w;i++) mat[h][i]='*';
  54. }
  55. void Print()
  56. {
  57.     sp(7);
  58.     for (int i=1;i<=h;i++) puts(mat[i]+1);
  59.     printf("Score: %d\n",Score);
  60.     printf("Nr. of SPACE uses: %d\n",nrSuperTurns);
  61.     if (cooldown>0) printf("Cooldown: %d",cooldown);
  62. }
  63. bool Move()
  64. {
  65.     if (cooldown==0 && nrSuperTurns==0) nrSuperTurns=5;
  66.     int key=getch();
  67.     if (key==27) return false; // ESC / ABORT
  68.     else if (key==32)
  69.     {
  70.         if (cooldown==0 && nrSuperTurns==0) nrSuperTurns=5;
  71.         if (nrSuperTurns>0)
  72.         {
  73.             mat[x][y]='C';
  74.             nrSuperTurns--;
  75.             if (nrSuperTurns==0) cooldown=50;
  76.         }
  77.         else Move();
  78.         return true;
  79.     }
  80.     else if (key==0 || key==0xE0)
  81.     {
  82.         key=getch();
  83.         if (key==75 && y>2) // LEFT
  84.         {
  85.             if (cooldown>0) cooldown--;
  86.             mat[x][y]='-';
  87.             y--;
  88.             if (mat[x][y]=='E')
  89.             {
  90.                 int i;
  91.                 for (i=1;i<=nrE;i++)
  92.                     if (Enemies[i].x==x && Enemies[i].y==y) break;
  93.                 for (;i<nrE;i++)
  94.                     Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  95.                 nrE--;
  96.                 Score++;
  97.             }
  98.             mat[x][y]='M';
  99.             return true;
  100.         }
  101.         else if (key==77 && y<w-1) // RIGHT
  102.         {
  103.             if (cooldown>0) cooldown--;
  104.             mat[x][y]='-';
  105.             y++;
  106.             if (mat[x][y]=='E')
  107.             {
  108.                 int i;
  109.                 for (i=1;i<=nrE;i++)
  110.                     if (Enemies[i].x==x && Enemies[i].y==y) break;
  111.                 for (;i<nrE;i++)
  112.                     Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  113.                 nrE--;
  114.                 Score++;
  115.             }
  116.             mat[x][y]='M';
  117.             return true;
  118.         }
  119.         else if (key==72 && x>2) // UP
  120.         {
  121.             if (cooldown>0) cooldown--;
  122.             mat[x][y]='-';
  123.             x--;
  124.             if (mat[x][y]=='E')
  125.             {
  126.                 int i;
  127.                 for (i=1;i<=nrE;i++)
  128.                     if (Enemies[i].x==x && Enemies[i].y==y) break;
  129.                 for (;i<nrE;i++)
  130.                     Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  131.                 nrE--;
  132.                 Score++;
  133.             }
  134.             mat[x][y]='M';
  135.             return true;
  136.         }
  137.         else if (key==80 && x<h-1) // DOWN
  138.         {
  139.             if (cooldown>0) cooldown--;
  140.             mat[x][y]='-';
  141.             x++;
  142.             if (mat[x][y]=='E')
  143.             {
  144.                 int i;
  145.                 for (i=1;i<=nrE;i++)
  146.                     if (Enemies[i].x==x && Enemies[i].y==y) break;
  147.                 for (;i<nrE;i++)
  148.                     Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  149.                 nrE--;
  150.                 Score++;
  151.             }
  152.             mat[x][y]='M';
  153.             return true;
  154.         }
  155.         else Move();
  156.     }
  157.     else Move();
  158. }
  159. void createEnemy()
  160. {
  161.     int xe=rand()%h+1,ye=rand()%w+1;
  162.     bool ok=true;
  163.     while (ok)
  164.     {
  165.         ok=false;
  166.         if (abs(xe-x)<5 || abs(ye-y)<5) xe=rand()%h+1,ye=rand()%w+1,ok=true;
  167.         if (mat[xe][ye]!='-') xe=rand()%h+1,ye=rand()%w+1,ok=true;
  168.     }
  169.     nrE++;
  170.     Enemies[nrE].x=xe;
  171.     Enemies[nrE].y=ye;
  172.     mat[xe][ye]='E';
  173. }
  174. void createEnemies()
  175. {
  176.     while (nrE<max(1,h*w/100)) createEnemy();
  177. }
  178. void moveEnemy(int &xe,int &ye)
  179. {
  180.     if (mat[xe+1][ye]=='E' && mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E' && mat[xe][ye-1]=='E') return;
  181.     int cmd=-1;
  182.     if (mat[xe+1][ye]=='E' && mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E') cmd=0;
  183.     else if (mat[xe+1][ye]=='E' && mat[xe-1][ye]=='E' && mat[xe][ye-1]=='E') cmd=1;
  184.     else if (mat[xe+1][ye]=='E' && mat[xe][ye+1]=='E' && mat[xe][ye-1]=='E') cmd=2;
  185.     else if (mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E' && mat[xe][ye-1]=='E') cmd=3;
  186.     // cmd==0 LEFT
  187.     // cmd==1 RIGHT
  188.     // cmd==2 UP
  189.     // cmd==3 DOWN
  190.     if (xe<=x && ye<=y && cmd==-1)
  191.     {
  192.         if (mat[xe+1][ye]=='E' && mat[xe][ye+1]=='E')
  193.             while (cmd!=2 && cmd!=0) cmd=rand()%4;
  194.         else if (xe==x) cmd=1;
  195.         else if (ye==y) cmd=3;
  196.         else
  197.             while (cmd!=1 && cmd!=3) cmd=rand()%4;
  198.     }
  199.     else if (xe<=x && ye>=y && cmd==-1)
  200.     {
  201.         if (mat[xe+1][ye]=='E' && mat[xe][ye-1]=='E')
  202.             while (cmd!=2 && cmd!=1) cmd=rand()%4;
  203.         else if (xe==x) cmd=0;
  204.         else if (ye==y) cmd=3;
  205.         else
  206.             while (cmd!=0 && cmd!=3) cmd=rand()%4;
  207.     }
  208.     else if (xe>=x && ye<=y && cmd==-1)
  209.     {
  210.         if (mat[xe-1][ye]=='E' && mat[xe][ye+1]=='E')
  211.             while (cmd!=2 && cmd!=3) cmd=rand()%4;
  212.         else if (xe==x) cmd=1;
  213.         else if (ye==y) cmd=2;
  214.         else
  215.             while (cmd!=1 && cmd!=2) cmd=rand()%4;
  216.     }
  217.     else if (xe>=x && ye>=y && cmd==-1)
  218.     {
  219.         if (mat[xe-1][ye]=='E' && mat[xe][ye-1]=='E')
  220.             while (cmd!=1 && cmd!=3) cmd=rand()%4;
  221.         else if (xe==x) cmd=0;
  222.         else if (ye==y) cmd=2;
  223.         else
  224.             while (cmd!=0 && cmd!=2) cmd=rand()%4;
  225.     }
  226.     if (cmd==0 && (mat[xe][ye-1]=='*' || mat[xe][ye-1]=='E')) return;
  227.     else if (cmd==1 && (mat[xe][ye+1]=='*' || mat[xe][ye+1]=='E')) return;
  228.     else if (cmd==2 && (mat[xe-1][ye]=='*' || mat[xe-1][ye]=='E')) return;
  229.     else if (cmd==3 && (mat[xe+1][ye]=='*' || mat[xe+1][ye]=='E')) return;
  230.     else if (cmd==0)
  231.     {
  232.         mat[xe][ye]='-';
  233.         ye--;
  234.         if (mat[xe][ye]=='C')
  235.         {
  236.             Score++;
  237.             int i;
  238.             for (i=1;i<=nrE;i++)
  239.                 if (Enemies[i].x==xe && Enemies[i].y==ye) break;
  240.             for (;i<nrE;i++)
  241.                 Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  242.             nrE--;
  243.         }
  244.         else if (mat[xe][ye]=='M')
  245.         {
  246.             mat[xe][ye]='E';
  247.             PlayerAlive=false;
  248.             return;
  249.         }
  250.         else mat[xe][ye]='E';
  251.     }
  252.     else if (cmd==1)
  253.     {
  254.         mat[xe][ye]='-';
  255.         ye++;
  256.         if (mat[xe][ye]=='C')
  257.         {
  258.             Score++;
  259.             int i;
  260.             for (i=1;i<=nrE;i++)
  261.                 if (Enemies[i].x==xe && Enemies[i].y==ye) break;
  262.             for (;i<nrE;i++)
  263.                 Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  264.             nrE--;
  265.         }
  266.         else if (mat[xe][ye]=='M')
  267.         {
  268.             mat[xe][ye]='E';
  269.             PlayerAlive=false;
  270.             return;
  271.         }
  272.         else mat[xe][ye]='E';
  273.     }
  274.     else if (cmd==2)
  275.     {
  276.         mat[xe][ye]='-';
  277.         xe--;
  278.         if (mat[xe][ye]=='C')
  279.         {
  280.             Score++;
  281.             int i;
  282.             for (i=1;i<=nrE;i++)
  283.                 if (Enemies[i].x==xe && Enemies[i].y==ye) break;
  284.             for (;i<nrE;i++)
  285.                 Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  286.             nrE--;
  287.         }
  288.         else if (mat[xe][ye]=='M')
  289.         {
  290.             mat[xe][ye]='E';
  291.             PlayerAlive=false;
  292.             return;
  293.         }
  294.         else mat[xe][ye]='E';
  295.     }
  296.     else if (cmd==3)
  297.     {
  298.         mat[xe][ye]='-';
  299.         xe++;
  300.         if (mat[xe][ye]=='C')
  301.         {
  302.             Score++;
  303.             int i;
  304.             for (i=1;i<=nrE;i++)
  305.                 if (Enemies[i].x==xe && Enemies[i].y==ye) break;
  306.             for (;i<nrE;i++)
  307.                 Enemies[i].x=Enemies[i+1].x,Enemies[i].y=Enemies[i+1].y;
  308.             nrE--;
  309.         }
  310.         else if (mat[xe][ye]=='M')
  311.         {
  312.             mat[xe][ye]='E';
  313.             PlayerAlive=false;
  314.             return;
  315.         }
  316.         else mat[xe][ye]='E';
  317.     }
  318. }
  319. bool game()
  320. {
  321.     if (nrE==0) createEnemies();
  322.     if (!Move()) return false; // ESC / ABORT
  323.     else
  324.     {
  325.         if (nrE>0)
  326.             for (int i=1;i<=nrE;i++) moveEnemy(Enemies[i].x,Enemies[i].y);
  327.         if (PlayerAlive==false) return false;
  328.         system("cls");
  329.         Print();
  330.         game();
  331.     }
  332. }
  333. bool start()
  334. {
  335.     srand(time(NULL));
  336.     PlayerAlive=true;
  337.     nrSuperTurns=5;
  338.     cooldown=0;
  339.     Score=0;
  340.     x=2,y=2;
  341.     nrE=0;
  342.     CreateRoom();
  343.     mat[x][y]='M';
  344.     Print();
  345.     if (!game()) return false;
  346. }
  347. void Menu()
  348. {
  349.     system("cls");
  350.     printf("1.Play"); newl;
  351.     printf("2.Help"); newl;
  352.     printf("3.Highscore"); newl;
  353.     printf("4.Quit"); newl;
  354.     int cmd=getch();
  355.     while (cmd!='1' && cmd!='2' && cmd!='3' && cmd!='4') cmd=getch();
  356.     if (cmd=='1')
  357.     {
  358.         system("cls");
  359.         while (start());
  360.         system("cls");
  361.         printf("You have been killed or have quit the game.\n");
  362.         printf("Your final score is: %d\n",Score);
  363.         Sleep(2000);
  364.         if (Score>Highscore)
  365.         {
  366.             Highscore=Score;
  367.             printf("Congratulations! New highscore!\n");
  368.         }
  369.         printf("Press ESC to go back to menu.");
  370.         char cmd=getch();
  371.         while (cmd!=27) cmd=getch();
  372.         Menu();
  373.     }
  374.     else if (cmd=='2')
  375.     {
  376.         system("cls");
  377.         Sleep(1000);
  378.         printf("You are Michael (M) and you start in the top-left corner.\n");
  379.         Sleep(3000);
  380.         printf("Your goal is to touch as many Enemies (E) as possible before they touch you.\n");
  381.         Sleep(3000);
  382.         printf("You can move using the arrow keys.\n");
  383.         Sleep(3000);
  384.         printf("Space will transform you into C which makes enemies who touch you die.\n");
  385.         Sleep(3000);
  386.         printf("Space can be used 5 turns with a cooldown of 50 turns after using the 5 turns.\n");
  387.         Sleep(3000);
  388.         printf("Enemies will respawn at random locations when their number reaches zero.\n");
  389.         Sleep(3000);
  390.         printf("Press any button to go back to menu.\n");
  391.         Sleep(1000);
  392.         getch();
  393.         Menu();
  394.     }
  395.     else if (cmd=='3')
  396.     {
  397.         system("cls");
  398.         printf("Your highscore is %d\n",Highscore);
  399.         printf("Press anything to go back to menu.");
  400.         getch();
  401.         Menu();
  402.     }
  403.     else if (cmd=='4') return;
  404. }
  405. int main()
  406. {
  407.     Menu();
  408.     return 0;
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement