Advertisement
PhanNguyen

lĂ˝

Apr 14th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.40 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. int main(int argc, char* argv[]){
  17.     //for convenience just let filename = "testcase/testcase_1_X.txt" with X from 1 to 5: the order of your testcases.
  18.     //REMEMBER: change filename = argv[1] when submit !!!!!! Otherwise you will get 0 mark
  19.     char * testcase = "testcase/testcase_1_1.txt";
  20.  
  21.     readFiles(testcase);
  22.     printCurrentMap(map);
  23.    
  24.     /////////////////////////////////////////////
  25.     //TODO: Complete Your code after this line
  26.     int x = 9;
  27.     int y = 0;
  28.     int coin = 0;
  29.     cout << "(" << y << 9 - x << ") ";
  30.     for (int i = 0; i < numberOfSteps; i++)
  31.     {
  32.         ////////////////////////////////////////////////////////////////////////////////////////////
  33.         if (step[i] == "RIGHT")
  34.         {
  35.             switch (map[x][y+1])
  36.             {
  37.             case ' ' :
  38.                 y++;
  39.                 cout << "(" << y << 9 - x << ") ";
  40.                 break;
  41.             case '$' :
  42.                 y++;
  43.                 cout << "(" << y << 9 - x << ") ";
  44.                 coin++;
  45.                 map[x][y] = ' ';
  46.                 break;
  47.             }
  48.             while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
  49.             {
  50.                 x++;
  51.                 cout << "(" << y << 9 - x << ") ";
  52.                 if (map[x][y] == '$')
  53.                 {
  54.                     coin++;
  55.                     map[x][y] = ' ';
  56.                 }
  57.             }
  58.         }
  59.         ////////////////////////////////////////////////////////////////////////////////////////////
  60.         if (step[i] == "LEFT_")
  61.         {
  62.             switch (map[x][y - 1])
  63.             {
  64.             case ' ':
  65.                 y--;
  66.                 cout << "(" << y << 9 - x << ") ";
  67.                 break;
  68.             case '$':
  69.                 y--;
  70.                 cout << "(" << y << 9 - x << ") ";
  71.                 coin++;
  72.                 map[x][y] = ' ';
  73.                 break;
  74.             }
  75.             while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
  76.             {
  77.                 x++;
  78.                 cout << "(" << y << 9 - x << ") ";
  79.                 if (map[x][y] == '$')
  80.                 {
  81.                     coin++;
  82.                     map[x][y] = ' ';
  83.                 }
  84.             }
  85.         }
  86.         ////////////////////////////////////////////////////////////////////////////////////////////
  87.         if (step[i] == "JUMP_")
  88.         {
  89.             if (map[x - 1][y] != '#')
  90.             {
  91.                 x--;
  92.                 cout << "(" << y << 9 - x << ") ";
  93.                 if (map[x][y] == '$')
  94.                 {
  95.                     coin++;
  96.                     map[x][y] = ' ';
  97.                 }
  98.                 if (map[x - 1][y] != '#')
  99.                 {
  100.                     x--;
  101.                     cout << "(" << y << 9 - x << ") ";
  102.                     if (map[x][y] == '!')
  103.                         break;
  104.                     if (map[x][y] == '$')
  105.                     {
  106.                         coin++;
  107.                         map[x][y] = ' ';
  108.                     }
  109.                     while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
  110.                     {
  111.                         x++;
  112.                         cout << "(" << y << 9 - x << ") ";
  113.                     }
  114.                 }
  115.             }
  116.         }
  117.         ////////////////////////////////////////////////////////////////////////////////////////////
  118.         if (step[i] == "RJUMP")
  119.         {
  120.             if (map[x - 1][y] != '#')
  121.             {
  122.                 x--;
  123.                 cout << "(" << y << 9 - x << ") ";
  124.                 if (map[x][y] == '$')
  125.                 {
  126.                     coin++;
  127.                     map[x][y] = ' ';
  128.                 }
  129.                 if (map[x - 1][y] != '#')
  130.                 {
  131.                     x--;
  132.                     cout << "(" << y << 9 - x << ") ";
  133.                     if (map[x][y] == '$')
  134.                     {
  135.                         coin++;
  136.                         map[x][y] = ' ';
  137.                     }
  138.                     if (map[x][y] == '!')
  139.                         break;
  140.                     if (map[x][y + 1] != '#')
  141.                     {
  142.                         y++;
  143.                         cout << "(" << y << 9 - x << ") ";
  144.                         if (map[x][y] == '$')
  145.                         {
  146.                             coin++;
  147.                             map[x][y] = ' ';
  148.                         }
  149.                         if (map[x][y] == '!')
  150.                             break;
  151.                         if (map[x][y + 1] != '#')
  152.                         {
  153.                             y++;
  154.                             cout << "(" << y << 9 - x << ") ";
  155.                             if (map[x][y] == '$')
  156.                             {
  157.                                 coin++;
  158.                                 map[x][y] = ' ';
  159.                             }
  160.                             if (map[x][y] == '!')
  161.                                 break;
  162.                             while (map[x + 1][y] == ' ' || map[x + 1][y] == '$' || map[x + 1][y] == '!')
  163.                             {
  164.                                 x++;
  165.                                 cout << "(" << y << 9 - x << ") ";
  166.                                 if (map[x][y] == '!')
  167.                                     break;
  168.                                 if (map[x][y] == '$')
  169.                                 {
  170.                                     coin++;
  171.                                     map[x][y] = ' ';
  172.                                 }
  173.                             }
  174.                         }
  175.                         else
  176.                             while (map[x + 1][y] == ' ' || map[x + 1][y] == '$' || map[x + 1][y] == '!')
  177.                             {
  178.                             x++;
  179.                             cout << "(" << y << 9 - x << ") ";
  180.                             if (map[x][y] == '!')
  181.                                 break;
  182.                             if (map[x][y] == '$')
  183.                             {
  184.                                 coin++;
  185.                                 map[x][y] = ' ';
  186.                             }
  187.                             }
  188.                     }
  189.                     else
  190.                         while (map[x + 1][y] == ' ')
  191.                         {
  192.                         x++;
  193.                         cout << "(" << y << 9 - x << ") ";
  194.                         }
  195.                 }
  196.                 else
  197.                     if (map[x][y + 1] != '#')
  198.                     {
  199.                         y++;
  200.                         cout << "(" << y << 9 - x << ") ";
  201.                         if (map[x][y] == '$')
  202.                         {
  203.                             coin++;
  204.                             map[x][y] = ' ';
  205.                         }
  206.                         while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
  207.                         {
  208.                             x++;
  209.                             cout << "(" << y << 9 - x << ") ";
  210.                             if (map[x][y] == '$')
  211.                             {
  212.                                 coin++;
  213.                                 map[x][y] = ' ';
  214.                             }
  215.                         }
  216.                     }
  217.                     else
  218.                         cout << "(" << y << 9 - x << ") ";
  219.             }
  220.            
  221.         }
  222.     ////////////////////////////////////////////////////////////////////////////////////////////
  223.     if (step[i] == "LJUMP")
  224.     {
  225.         if (map[x - 1][y] != '#')
  226.         {
  227.             x--;
  228.             cout << "(" << y << 9 - x << ") ";
  229.             if (map[x][y] == '$')
  230.             {
  231.                 coin++;
  232.                 map[x][y] = ' ';
  233.             }
  234.             if (map[x - 1][y] != '#')
  235.             {
  236.                 x--;
  237.                 cout << "(" << y << 9 - x << ") ";
  238.                 if (map[x][y] == '$')
  239.                 {
  240.                     coin++;
  241.                     map[x][y] = ' ';
  242.                 }
  243.                 if (map[x][y - 1] != '#')
  244.                 {
  245.                     y--;
  246.                     cout << "(" << y << 9 - x << ") ";
  247.                     if (map[x][y] == '$')
  248.                     {
  249.                         coin++;
  250.                         map[x][y] = ' ';
  251.                     }
  252.                     if (map[x][y - 1] != '#')
  253.                     {
  254.                         y--;
  255.                         cout << "(" << y << 9 - x << ") ";
  256.                         if (map[x][y] == '$')
  257.                         {
  258.                             coin++;
  259.                             map[x][y] = ' ';
  260.                         }
  261.                         while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
  262.                         {
  263.                             x++;
  264.                             cout << "(" << y << 9 - x << ") ";
  265.                             if (map[x][y] == '$')
  266.                             {
  267.                                 coin++;
  268.                                 map[x][y] = ' ';
  269.                             }
  270.                         }
  271.                     }
  272.                     else
  273.                         while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
  274.                         {
  275.                         x++;
  276.                         cout << "(" << y << 9 - x << ") ";
  277.                         if (map[x][y] == '$')
  278.                         {
  279.                             coin++;
  280.                             map[x][y] = ' ';
  281.                         }
  282.                         }
  283.                 }
  284.                 else
  285.                     while (map[x + 1][y] == ' ')
  286.                     {
  287.                     x++;
  288.                     cout << "(" << y << 9 - x << ") ";
  289.                     }
  290.             }
  291.             else
  292.                 if (map[x][y - 1] != '#')
  293.                 {
  294.                 y--;
  295.                 cout << "(" << y << 9 - x << ") ";
  296.                 if (map[x][y] == '$')
  297.                     {
  298.                         coin++;
  299.                         map[x][y] = ' ';
  300.                     }
  301.                 while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
  302.                 {
  303.                     x++;
  304.                     cout << "(" << y << 9 - x << ") ";
  305.                     if (map[x][y] == '$')
  306.                     {
  307.                         coin++;
  308.                         map[x][y] = ' ';
  309.                     }
  310.                 }
  311.                 }
  312.                 else
  313.                     cout << "(" << y << 9 - x << ") ";
  314.         }
  315.  
  316.     }
  317.     }
  318.     cout << endl << coin << endl ;
  319.     //Hint: You may find it easier to use functions to remove duplicate codes.
  320.     //      However, you can code anything else. Your input is read in 2 variables
  321.     //      char map[10][20] and vector<string> step (just like 1 dimensional array for convenience)
  322.     system(" pause");
  323.     //END TODO
  324.     /////////////////////////////////////////////
  325.     return 0;
  326. }
  327.  
  328. //Description: print the current map for easier debugging
  329. //INPUT: the current map variable
  330. //OUTPUT: no output, but it will print the current map on screen
  331. void printCurrentMap(char current_map[10][20]){
  332.     cout << endl;
  333.     cout << "Current Map:" << endl;
  334.     cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
  335.     for (int i = 0; i < 10; i++){
  336.         cout << 9 - i << "| ";
  337.         for (int j = 0; j < 20; j++){
  338.             cout << current_map[i][j] << " ";
  339.         }
  340.  
  341.         cout << "|" << endl;
  342.     }
  343.  
  344.     cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
  345.     cout << "   0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9   " << endl;
  346. }
  347.  
  348. //Description: read char map[10][20] and vector<string> step from file
  349. //INPUT: file name in char *
  350. //OUTPUT: no output, but it will change the global variables map[10][20] and vector step
  351. //--------Given to Students----------//
  352. void readFiles(char* filename){
  353.     string line;
  354.     ifstream myfile(filename);
  355.  
  356.     int i = 0;
  357.  
  358.     if (myfile.is_open()){
  359.         while (getline(myfile, line)){
  360.             if (i < 10){
  361.                 strcpy(map[i++], line.c_str());
  362.             }
  363.             else if (i >= 10){
  364.                 for (int j = 0; j < line.length(); j += 6){
  365.                     numberOfSteps++;
  366.                     string word = line.substr(j, 5);
  367.                     step.push_back(word);
  368.                 }
  369.             }
  370.         }
  371.  
  372.         myfile.close();
  373.     }
  374.     else {
  375.         cout << "Unable to open file";
  376.     }
  377. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement