Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Copyright: Nguyen Duc Minh Khoi (email: ducminhkhoi@gmail.com) - March 2015 NEW
- //REMEMBER: Do not include any other library. Otherwise you will get 0 mark
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <vector>
- using namespace std;
- char map[10][20]; //Variable stores map
- vector<string> step; //Variable stores steps
- int numberOfSteps = 0;
- void readFiles(char *);
- void printCurrentMap(char[10][20]); //convenience for checking your map
- int main(int argc, char* argv[]){
- //for convenience just let filename = "testcase/testcase_1_X.txt" with X from 1 to 5: the order of your testcases.
- //REMEMBER: change filename = argv[1] when submit !!!!!! Otherwise you will get 0 mark
- char * testcase = "testcase/testcase_1_1.txt";
- readFiles(testcase);
- printCurrentMap(map);
- /////////////////////////////////////////////
- //TODO: Complete Your code after this line
- int x = 9;
- int y = 0;
- int coin = 0;
- cout << "(" << y << 9 - x << ") ";
- for (int i = 0; i < numberOfSteps; i++)
- {
- ////////////////////////////////////////////////////////////////////////////////////////////
- if (step[i] == "RIGHT")
- {
- switch (map[x][y+1])
- {
- case ' ' :
- y++;
- cout << "(" << y << 9 - x << ") ";
- break;
- case '$' :
- y++;
- cout << "(" << y << 9 - x << ") ";
- coin++;
- map[x][y] = ' ';
- break;
- }
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- if (step[i] == "LEFT_")
- {
- switch (map[x][y - 1])
- {
- case ' ':
- y--;
- cout << "(" << y << 9 - x << ") ";
- break;
- case '$':
- y--;
- cout << "(" << y << 9 - x << ") ";
- coin++;
- map[x][y] = ' ';
- break;
- }
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- if (step[i] == "JUMP_")
- {
- if (map[x - 1][y] != '#')
- {
- x--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x - 1][y] != '#')
- {
- x--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '!')
- break;
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- }
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- if (step[i] == "RJUMP")
- {
- if (map[x - 1][y] != '#')
- {
- x--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x - 1][y] != '#')
- {
- x--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x][y] == '!')
- break;
- if (map[x][y + 1] != '#')
- {
- y++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x][y] == '!')
- break;
- if (map[x][y + 1] != '#')
- {
- y++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x][y] == '!')
- break;
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$' || map[x + 1][y] == '!')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '!')
- break;
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- else
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$' || map[x + 1][y] == '!')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '!')
- break;
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- else
- while (map[x + 1][y] == ' ')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- }
- }
- else
- if (map[x][y + 1] != '#')
- {
- y++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- else
- cout << "(" << y << 9 - x << ") ";
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////
- if (step[i] == "LJUMP")
- {
- if (map[x - 1][y] != '#')
- {
- x--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x - 1][y] != '#')
- {
- x--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x][y - 1] != '#')
- {
- y--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- if (map[x][y - 1] != '#')
- {
- y--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- else
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- else
- while (map[x + 1][y] == ' ')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- }
- }
- else
- if (map[x][y - 1] != '#')
- {
- y--;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- while (map[x + 1][y] == ' ' || map[x + 1][y] == '$')
- {
- x++;
- cout << "(" << y << 9 - x << ") ";
- if (map[x][y] == '$')
- {
- coin++;
- map[x][y] = ' ';
- }
- }
- }
- else
- cout << "(" << y << 9 - x << ") ";
- }
- }
- }
- cout << endl << coin << endl ;
- //Hint: You may find it easier to use functions to remove duplicate codes.
- // However, you can code anything else. Your input is read in 2 variables
- // char map[10][20] and vector<string> step (just like 1 dimensional array for convenience)
- system(" pause");
- //END TODO
- /////////////////////////////////////////////
- return 0;
- }
- //Description: print the current map for easier debugging
- //INPUT: the current map variable
- //OUTPUT: no output, but it will print the current map on screen
- void printCurrentMap(char current_map[10][20]){
- cout << endl;
- cout << "Current Map:" << endl;
- cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
- for (int i = 0; i < 10; i++){
- cout << 9 - i << "| ";
- for (int j = 0; j < 20; j++){
- cout << current_map[i][j] << " ";
- }
- cout << "|" << endl;
- }
- cout << " - - - - - - - - - - - - - - - - - - - - - - " << endl;
- cout << " 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 " << endl;
- }
- //Description: read char map[10][20] and vector<string> step from file
- //INPUT: file name in char *
- //OUTPUT: no output, but it will change the global variables map[10][20] and vector step
- //--------Given to Students----------//
- void readFiles(char* filename){
- string line;
- ifstream myfile(filename);
- int i = 0;
- if (myfile.is_open()){
- while (getline(myfile, line)){
- if (i < 10){
- strcpy(map[i++], line.c_str());
- }
- else if (i >= 10){
- for (int j = 0; j < line.length(); j += 6){
- numberOfSteps++;
- string word = line.substr(j, 5);
- step.push_back(word);
- }
- }
- }
- myfile.close();
- }
- else {
- cout << "Unable to open file";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement