Advertisement
PhanNguyen

BTL-KTLT by Phan Nguyen 1412807

Apr 10th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.84 KB | None | 0 0
  1. //Copyright: Nguyen Duc Minh Khoi (email: ducminhkhoi@gmail.com) - March 2015 NEW
  2. //REMEMBER: Do not include any other library. Otherwise you will get 0 mark
  3. #include <iostream>
  4. #include <string>
  5. #include <fstream>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. char map[10][20]; //Variable stores map
  10. vector<string> step; //Variable stores steps
  11. int numberOfSteps = 0;
  12.  
  13. void readFiles(char *);
  14. void printCurrentMap(char [10][20]); //convenience for checking your map
  15.  
  16. //VARIABLES
  17. int x,y;
  18. int i,j;
  19. int m=0;
  20. //################################################################################
  21. //Start functions
  22. //################################################################################
  23. //bentrai function (return the value of map on the LEFT of the current position)
  24. char bentrai ( int td_x, int td_y)
  25. {
  26.     char kitu;
  27.     kitu = map[9-td_y][td_x-1];
  28.     return kitu;
  29. }
  30. //End of bentrai function
  31. //--------------------------------------------------------------------------------
  32. //benphai function (return the value of map on the RIGHT of the current position)
  33. char benphai (int td_x, int td_y)
  34. {
  35.     char kitu;
  36.     kitu = map[9-td_y][td_x+1];
  37.     return kitu;
  38. }
  39. //End of benphai functions
  40. //--------------------------------------------------------------------------------
  41. //bentren function (return the value on the map ABOVE the current position)
  42. char bentren (int td_x, int td_y)
  43. {
  44.     char kitu;
  45.     kitu = map[8-td_y][td_x];
  46.     return kitu;
  47. }
  48. //End of bentren function
  49. //--------------------------------------------------------------------------------
  50. //benduoi function (return the value on the map UNDER the current position
  51. char benduoi (int td_x, int td_y)
  52. {
  53.     char kitu;
  54.     kitu = map[10-td_y][td_x];
  55.     return kitu;
  56. }
  57. //End of benduoi function
  58. //--------------------------------------------------------------------------------
  59. //intoado function
  60. void intoado (int td_x, int td_y)
  61. {
  62.     cout << "(" << td_x << "," << td_y <<") ";
  63. }
  64. //ngoaimap function (check if the destination is out of map)
  65. bool ngoaimap ( int vitri_x, int vitri_y )
  66. {
  67.     bool b;
  68.     b= ( vitri_x == -1 || vitri_x == 20 || vitri_y == -1 || vitri_y == 10 );
  69.     return b;
  70. }
  71. //End of ngoaimap function
  72. //--------------------------------------------------------------------------------
  73. //checkmap function
  74. int checkmap (int x, int y, int &m)
  75. {
  76.     if (map[9-y][x] == '$')
  77.         {
  78.             m += 1;
  79.             map[9-y][x] = ' ';
  80.         }
  81.     if (map[9-y][x] == '!')
  82.         return 1;
  83.     else return 0;
  84. }
  85. //--------------------------------------------------------------------------------
  86. //Fall function
  87. void fall (int &x, int &y )
  88. {
  89.     while (y > 0)
  90.     {
  91.         if (benduoi(x,y) != '#' && !ngoaimap(x,y-1))
  92.         {
  93.             y -= 1;
  94.             intoado(x,y);
  95.             if (checkmap(x,y,m) == 1)
  96.                 return;
  97.         }
  98.         else return;
  99.     }
  100.  
  101. }
  102. //End of Fall function
  103. //--------------------------------------------------------------------------------
  104. //LEFT function
  105. void left ( int &x, int &y )
  106. {
  107.     if (bentrai(x,y) != '#' && !ngoaimap(x-1,y))
  108.     {
  109.         x -= 1;
  110.         intoado(x,y);
  111.         if (checkmap(x,y,m) == 1)
  112.             return;
  113.         fall(x,y);
  114.     }
  115.     else intoado(x,y);
  116. }
  117. //End of LEFT function
  118. //--------------------------------------------------------------------------------
  119. //RIGHT function
  120. void right ( int &x, int &y )
  121. {
  122.     if (benphai(x,y) != '#' && !ngoaimap(x+1,y))
  123.     {
  124.         x += 1;
  125.         intoado(x,y);
  126.         if (checkmap(x,y,m) == 1)
  127.             return;
  128.         fall(x,y);
  129.     }
  130.     else intoado(x,y);
  131. }
  132. //End of RIGHT function
  133. //--------------------------------------------------------------------------------
  134. //JUMP_ function
  135. void jump (int &x, int &y )
  136. {
  137.     if (bentren(x,y) != '#' && !ngoaimap(x,y+1))
  138.     {
  139.         y += 1;
  140.         intoado(x,y);
  141.         if (checkmap(x,y,m) == 1)
  142.             return;
  143.     }
  144.     else intoado(x,y);
  145.     if (bentren(x,y) != '#' && !ngoaimap(x,y+1))
  146.     {
  147.         y += 1;
  148.         intoado(x,y);
  149.         if (checkmap(x,y,m) == 1)
  150.             return;
  151.     }
  152.     fall(x,y);
  153.    
  154. }
  155. //End of  jump function
  156. //--------------------------------------------------------------------------------
  157. //RJUMP function
  158. void rjump (int &x, int &y)
  159. {
  160.     if (bentren(x,y) != '#' && !ngoaimap(x,y+1))        //check the above block, if ok then move up 1
  161.     {
  162.         y += 1;
  163.         intoado(x,y);
  164.         if (checkmap(x,y,m) == 1)
  165.             return;
  166.         if (bentren(x,y) != '#' && !ngoaimap(x,y+1))    //check the next above block, if ok then move up 1 (2 up so far)
  167.         {
  168.             y += 1;
  169.             intoado(x,y);
  170.             if (checkmap(x,y,m) == 1)
  171.                 return;
  172.             if (benphai(x,y) != '#' && !ngoaimap(x+1,y))    //check the next block on the right, if ok then move right 1 (2 up and 1 right so far)
  173.             {
  174.                 x += 1;
  175.                 intoado(x,y);
  176.                 if (checkmap(x,y,m) == 1)
  177.                     return;
  178.                 if (benphai(x,y) != '#' && !ngoaimap(x+1,y))    //check the next block on the right, if ok then move right 1 (2 up and 2 right so far)
  179.                 {
  180.                     x += 1;
  181.                     intoado(x,y);
  182.                     if (checkmap(x,y,m) == 1)
  183.                         return;
  184.                     fall(x,y);
  185.                 }
  186.                 else
  187.                     fall(x,y);      // move up 2, move right 1 then fall
  188.             }
  189.             else
  190.                 fall(x,y);          // moved up 2 then fall
  191.         }
  192.         else
  193.             if (benphai(x,y) != '#' && !ngoaimap(x+1,y))    //move up 1 and move right 1
  194.                 {
  195.                     x += 1;
  196.                     intoado(x,y);
  197.                     if (checkmap(x,y,m) == 1)
  198.                         return;
  199.                     fall(x,y);
  200.                 }
  201.             else
  202.             fall(x,y);              // moved up 1 then fall
  203.     }
  204.     else
  205.         intoado(x,y);       //if cant move up 1 then stay, print the current coordiate
  206.     return;
  207. }
  208. int main(int argc, char* argv[]){
  209.     //for convenience just let filename = "testcase/testcase_1_X.txt" with X from 1 to 5: the order of your testcases.
  210.     //REMEMBER: change filename = argv[1] when submit !!!!!! Otherwise you will get 0 mark
  211.     char * filename = "testcase/testcase_1_2.txt";
  212.    
  213.     readFiles(filename);
  214.    
  215.     /////////////////////////////////////////////
  216.     //TODO: Complete Your code after this line
  217.     //Hint: You may find it easier to use functions to remove duplicate codes.
  218.     //      However, you can code anything else. Your input is read in 2 variables
  219.     //      char map[10][20] and vector<string> step (just like 1 dimensional array for convenience)
  220.    
  221.     printCurrentMap(map);
  222.     cout << step.at(1) << endl << step.size() << endl;
  223.     // Search for initial coordinate
  224.     for (i = 0 ; i <= 9 ; i++ )
  225.     {
  226.         for ( j = 0 ; j <= 19 ; j++ )
  227.         {
  228.             if (map[9-i][j] == '@')
  229.             {
  230.                 x=j;
  231.                 y=i;
  232.             }
  233.         }
  234.     }
  235.     //End search
  236.     //some comments here blah blah blah blah
  237.     x=12;y=8;
  238.     rjump(x,y);
  239.    
  240.     cout << map[4][3];
  241.    
  242.     system("pause");
  243.    
  244.     //END TODO
  245.     /////////////////////////////////////////////
  246.  
  247.     return 0;
  248. }
  249.  
  250. //Description: print the current map for easier debugging
  251. //INPUT: the current map variable
  252. //OUTPUT: no output, but it will print the current map on screen
  253. void printCurrentMap(char current_map[10][20]){
  254.     cout << endl;
  255.     cout << "Current Map:" << endl;
  256.     cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
  257.     for (int i = 0; i < 10; i++){
  258.         cout << 9 - i << "| ";
  259.         for (int j = 0; j < 20; j++){
  260.             cout << current_map[i][j] << " ";
  261.         }
  262.  
  263.         cout << "|" << endl;
  264.     }
  265.  
  266.     cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
  267.     cout << "   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9   " << endl;
  268. }
  269.  
  270. //Description: read char map[10][20] and vector<string> step from file
  271. //INPUT: file name in char *
  272. //OUTPUT: no output, but it will change the global variables map[10][20] and vector step
  273. //--------Given to Students----------//
  274. void readFiles(char* filename){
  275.     string line;
  276.     ifstream myfile (filename);
  277.  
  278.     int i = 0;
  279.  
  280.     if (myfile.is_open()){
  281.         while ( getline (myfile, line) ){
  282.             if (i < 10){
  283.                 strcpy(map[i++], line.c_str());
  284.             } else if (i >= 10){
  285.                 for (int j = 0; j < line.length(); j+=6){
  286.                     numberOfSteps++;
  287.                     string word = line.substr(j, 5);
  288.                     step.push_back(word);
  289.                 }
  290.             }
  291.         }
  292.  
  293.         myfile.close();
  294.     } else {
  295.         cout << "Unable to open file";
  296.     }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement