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
- //VARIABLES
- int x,y;
- int i,j;
- int m=0;
- //################################################################################
- //Start functions
- //################################################################################
- //bentrai function (return the value of map on the LEFT of the current position)
- char bentrai ( int td_x, int td_y)
- {
- char kitu;
- kitu = map[9-td_y][td_x-1];
- return kitu;
- }
- //End of bentrai function
- //--------------------------------------------------------------------------------
- //benphai function (return the value of map on the RIGHT of the current position)
- char benphai (int td_x, int td_y)
- {
- char kitu;
- kitu = map[9-td_y][td_x+1];
- return kitu;
- }
- //End of benphai functions
- //--------------------------------------------------------------------------------
- //bentren function (return the value on the map ABOVE the current position)
- char bentren (int td_x, int td_y)
- {
- char kitu;
- kitu = map[8-td_y][td_x];
- return kitu;
- }
- //End of bentren function
- //--------------------------------------------------------------------------------
- //benduoi function (return the value on the map UNDER the current position
- char benduoi (int td_x, int td_y)
- {
- char kitu;
- kitu = map[10-td_y][td_x];
- return kitu;
- }
- //End of benduoi function
- //--------------------------------------------------------------------------------
- //intoado function
- void intoado (int td_x, int td_y)
- {
- cout << "(" << td_x << "," << td_y <<") ";
- }
- //ngoaimap function (check if the destination is out of map)
- bool ngoaimap ( int vitri_x, int vitri_y )
- {
- bool b;
- b= ( vitri_x == -1 || vitri_x == 20 || vitri_y == -1 || vitri_y == 10 );
- return b;
- }
- //End of ngoaimap function
- //--------------------------------------------------------------------------------
- //checkmap function
- int checkmap (int x, int y, int &m)
- {
- if (map[9-y][x] == '$')
- {
- m += 1;
- map[9-y][x] = ' ';
- }
- if (map[9-y][x] == '!')
- return 1;
- else return 0;
- }
- //--------------------------------------------------------------------------------
- //Fall function
- void fall (int &x, int &y )
- {
- while (y > 0)
- {
- if (benduoi(x,y) != '#' && !ngoaimap(x,y-1))
- {
- y -= 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- }
- else return;
- }
- }
- //End of Fall function
- //--------------------------------------------------------------------------------
- //LEFT function
- void left ( int &x, int &y )
- {
- if (bentrai(x,y) != '#' && !ngoaimap(x-1,y))
- {
- x -= 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- fall(x,y);
- }
- else intoado(x,y);
- }
- //End of LEFT function
- //--------------------------------------------------------------------------------
- //RIGHT function
- void right ( int &x, int &y )
- {
- if (benphai(x,y) != '#' && !ngoaimap(x+1,y))
- {
- x += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- fall(x,y);
- }
- else intoado(x,y);
- }
- //End of RIGHT function
- //--------------------------------------------------------------------------------
- //JUMP_ function
- void jump (int &x, int &y )
- {
- if (bentren(x,y) != '#' && !ngoaimap(x,y+1))
- {
- y += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- }
- else intoado(x,y);
- if (bentren(x,y) != '#' && !ngoaimap(x,y+1))
- {
- y += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- }
- fall(x,y);
- }
- //End of jump function
- //--------------------------------------------------------------------------------
- //RJUMP function
- void rjump (int &x, int &y)
- {
- if (bentren(x,y) != '#' && !ngoaimap(x,y+1)) //check the above block, if ok then move up 1
- {
- y += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- if (bentren(x,y) != '#' && !ngoaimap(x,y+1)) //check the next above block, if ok then move up 1 (2 up so far)
- {
- y += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- 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)
- {
- x += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- 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)
- {
- x += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- fall(x,y);
- }
- else
- fall(x,y); // move up 2, move right 1 then fall
- }
- else
- fall(x,y); // moved up 2 then fall
- }
- else
- if (benphai(x,y) != '#' && !ngoaimap(x+1,y)) //move up 1 and move right 1
- {
- x += 1;
- intoado(x,y);
- if (checkmap(x,y,m) == 1)
- return;
- fall(x,y);
- }
- else
- fall(x,y); // moved up 1 then fall
- }
- else
- intoado(x,y); //if cant move up 1 then stay, print the current coordiate
- return;
- }
- 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 * filename = "testcase/testcase_1_2.txt";
- readFiles(filename);
- /////////////////////////////////////////////
- //TODO: Complete Your code after this line
- //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)
- printCurrentMap(map);
- cout << step.at(1) << endl << step.size() << endl;
- // Search for initial coordinate
- for (i = 0 ; i <= 9 ; i++ )
- {
- for ( j = 0 ; j <= 19 ; j++ )
- {
- if (map[9-i][j] == '@')
- {
- x=j;
- y=i;
- }
- }
- }
- //End search
- //some comments here blah blah blah blah
- x=12;y=8;
- rjump(x,y);
- cout << map[4][3];
- 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