Advertisement
Guest User

pacman

a guest
Nov 28th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.31 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8. char tmp_map[18][32];
  9.  
  10. char map[18][32] = {
  11.     "+#############################+",
  12.     "|                             |",
  13.     "|                             |",
  14.     "|## ########### ##   #########|",
  15.     "|   |                         |",
  16.     "| | |### |  |           |     |",
  17.     "| |      |  | |###  |   |  |  |",
  18.     "| | #####|  | |      ## |     |",
  19.     "| |           |###  |      |  |",
  20.     "| |##### ###         ##       |",
  21.     "|          ######  ####### ###|",
  22.     "|                             |",
  23.     "|# ### ####      ###   #######|",
  24.     "|                             |",
  25.     "|                             |",
  26.     "|                             |",
  27.     "|                             |",
  28.     "+#############################+"
  29.     };
  30.  
  31. void ShowMap()
  32. {
  33.     for(int i = 0; i < 18; i++) {
  34.         printf("%s\n",map[i] );
  35.     }
  36. }
  37.  
  38. void gotoxy( short x, short y )
  39. {
  40.     HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
  41.     COORD position = { x, y } ;
  42.  
  43.     SetConsoleCursorPosition( hStdout, position ) ;
  44. }
  45.  
  46. class entity {
  47. public:
  48.     entity( int x, int y ){
  49.         this ->x = x;
  50.         this ->y = y;
  51.     }
  52.  
  53.     void move_x( int p ){
  54.         if( map[y][x+p] == ' ' ) x += p;
  55.     }
  56.  
  57.     void move_y( int p ){
  58.         if( map[y+p][x] == ' ' ) y += p;
  59.     }
  60.  
  61.     void move( int p, int q ){
  62.         x += p;
  63.         y += q;
  64.     }
  65.  
  66.     int get_x(){ return x; }
  67.     int get_y(){ return y; }
  68.  
  69.     void draw( char p ){
  70.         map[x][y] = p;
  71.         gotoxy( x, y ); printf( "%c", p );
  72.     }
  73.  
  74. private:
  75.     int x;
  76.     int y;
  77. };
  78.  
  79. struct walk {
  80.     short walk_count;
  81.     short x;
  82.     short y;
  83.     short back;
  84. };
  85.  
  86. struct target {
  87.     short x;
  88.     short y;
  89. };
  90.  
  91. vector<target> walk_queue;
  92.  
  93. vector<walk> BFSArray;
  94.  
  95. void AddArray( int x, int y, int wc , int back ){
  96.     if( tmp_map[y][x] == ' ' || tmp_map[y][x] == '.' ){
  97.         tmp_map[y][x] = '#';
  98.         walk tmp;
  99.         tmp.x = x;
  100.         tmp.y = y;
  101.         tmp.walk_count = wc;
  102.         tmp.back = back;
  103.         BFSArray.push_back( tmp );
  104.     }
  105. }
  106.  
  107. void FindPath( int sx, int sy, int x, int y ){
  108.     memcpy( tmp_map, map, sizeof(map) );
  109.     BFSArray.clear();
  110.     walk tmp;
  111.     tmp.x = sx;
  112.     tmp.y = sy;
  113.     tmp.walk_count = 0;
  114.     tmp.back = -1;
  115.     BFSArray.push_back( tmp );
  116.  
  117.     int i = 0;
  118.     while( i < BFSArray.size() ){
  119.         if( BFSArray[i].x == x && BFSArray[i].y == y ){
  120.             walk_queue.clear();
  121.             target tmp2;
  122.             while( BFSArray[i].walk_count != 0 ){
  123.                 tmp2.x = BFSArray[i].x;
  124.                 tmp2.y = BFSArray[i].y;
  125.                 walk_queue.push_back( tmp2 );
  126.  
  127.                 i = BFSArray[i].back;
  128.             }
  129.  
  130.             break;
  131.         }
  132.  
  133.         AddArray( BFSArray[i].x+1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
  134.         AddArray( BFSArray[i].x-1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
  135.         AddArray( BFSArray[i].x, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
  136.         AddArray( BFSArray[i].x, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
  137.  
  138.         /*
  139.             AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
  140.             AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
  141.             AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
  142.             AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
  143.  
  144.             AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
  145.             AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
  146.             AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
  147.             AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
  148.         */
  149.         i++;
  150.     }
  151.  
  152.     BFSArray.clear();
  153. }
  154.  
  155.  
  156. int main()
  157. {
  158.     bool running = true;
  159.     int x = 15; // hero x
  160.     int y = 16; // hero y
  161.     int old_x;
  162.     int old_y;
  163.  
  164.     int ex = 1;
  165.     int ey = 1;
  166.  
  167.  
  168.     int pts = 0;
  169.  
  170.     printf("Instruction:\n1. Arrow Keys to move your hero\n2. Eat the dots produced by the Eater to gain poins\n3. Don't get caught by the Eater\n\n");
  171.     printf("H -> Hard\nN -> Normal\nE -> Easy\n\nInput : ");
  172.  
  173.     char diffi;
  174.     int speedmod = 3;
  175.  
  176.     cin >> diffi;
  177.  
  178.     if( diffi == 'N' ){
  179.         speedmod = 2;
  180.     }else if( diffi == 'H' ){
  181.         speedmod = 1;
  182.     }
  183.  
  184.     system("cls");
  185.     ShowMap();
  186.  
  187.     gotoxy( x, y ); cout << "H";
  188.  
  189.     int frame = 0;
  190.  
  191.     FindPath( ex,ey,x,y );
  192.  
  193.     while( running ){
  194.         gotoxy( x, y ); cout << " ";
  195.  
  196.         old_x = x;
  197.         old_y = y;
  198.  
  199.         if ( GetAsyncKeyState( VK_UP ) ){
  200.             if( map[y-1][x] == '.' ){ y--; pts++; } else
  201.             if( map[y-1][x] == ' ' ) y--;
  202.         }
  203.         if ( GetAsyncKeyState( VK_DOWN ) ){
  204.             if( map[y+1][x] == '.' ){ y++; pts++; } else
  205.             if( map[y+1][x] == ' ' ) y++;
  206.         }
  207.         if ( GetAsyncKeyState( VK_LEFT ) ){
  208.             if( map[y][x-1] == '.' ){ x--; pts++; } else
  209.             if( map[y][x-1] == ' ' ) x--;
  210.         }
  211.         if ( GetAsyncKeyState( VK_RIGHT ) ){
  212.             if( map[y][x+1] == '.' ){ x++; pts++; } else
  213.             if( map[y][x+1] == ' ' ) x++;
  214.         }
  215.  
  216.         if( old_x != x || old_y != y ){
  217.             FindPath( ex,ey,x,y );
  218.         }
  219.  
  220.         gotoxy( x,y ); cout << "H";
  221.  
  222.         map[ey][ex] = '.';
  223.         gotoxy( ex, ey ); cout << ".";
  224.  
  225.         if( frame%speedmod == 0 && walk_queue.size() != 0 ){
  226.             ex = walk_queue.back().x;
  227.             ey = walk_queue.back().y;
  228.             walk_queue.pop_back();
  229.         }
  230.  
  231.         gotoxy( ex, ey ); cout << "E";
  232.  
  233.         if( ex == x && ey == y ){
  234.             break;
  235.         }
  236.  
  237.  
  238.         gotoxy( 32, 18 );
  239.         gotoxy( 32, 1 ); cout << pts;
  240.         Sleep( 100 );
  241.         frame++;
  242.     }
  243.  
  244.     system("cls");
  245.     printf("You Lose and your score is : %i", pts );
  246.     cin.get();
  247.     cin.get();
  248.     cin.get();
  249.     cin.get();
  250.     cin.get();
  251.     cin.get();
  252.     cin.get();
  253.     cin.get();
  254.  
  255.     return 0;
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement