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 tien = 0;
- char left( int toado_x , int toado_y);
- char right( int toado_x, int toado_y);
- char top( int toado_x,int toado_y);
- char bottom( int toado_x,int toado_y);
- void intoado(int toado_x,int toado_y);
- bool ngoaimap(int vitri_x,int vitri_y);
- int ktmap (int &x, int &y, int &tien);
- void fall (int &x, int &y );
- void LEFT_ ( int &x, int &y );
- void RIGHT ( int &x, int &y );
- void JUMP_ (int &x, int &y );
- void LEFTJUMP_ (int &x, int &y);
- void RIGHTJUMP_ (int &x, int &y);
- 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_d.txt" ;
- readFiles(filename);
- printCurrentMap(map);
- /////////////////////////////////////////////
- //TODO: Complete Your code after this line
- int a,b;
- int i;
- int x,y;
- for( a=0; a<=9; a++)
- {
- for( b=0; b<=19; b++)
- {
- if( map[9-a][b]=='@')
- {
- x=b;
- y=a;
- }
- }
- }
- cout << "(" << x << "," << y <<")";
- for (i = 0; i < step.size(); i++)
- {
- cout << step.at(i);
- if ( step.at(i) == "LEFT_" ) LEFT_(x,y);
- if ( step.at(i) == "RIGHT" ) RIGHT(x,y);
- if ( step.at(i) == "JUMP_" ) JUMP_(x,y);
- if ( step.at(i) == "RJUMP" ) RIGHTJUMP_(x,y);
- if ( step.at(i) == "LJUMP" ) LEFTJUMP_(x,y);
- if ( ktmap(x,y,tien) == 1 ) break;
- cout << endl;
- }
- cout << endl;
- if ( ktmap(x,y,tien) != 1 ) cout << -1;
- else cout << tien;
- system("pause");
- return 0;
- }
- //end main()
- ////////////////////////////////////
- char left( int toado_x , int toado_y)
- {
- char vitri;
- vitri=map[9-toado_y][toado_x-1];
- return vitri;
- }
- ////////////////////////////////////
- char right( int toado_x, int toado_y)
- {
- char vitri;
- vitri= map[9-toado_y][toado_x+1];
- return vitri;
- }
- ////////////////////////////////////
- char top( int toado_x,int toado_y)
- {
- char vitri;
- vitri=map[8-toado_y][toado_x];
- return vitri;
- }
- ////////////////////////////////////
- char bottom( int toado_x,int toado_y)
- {
- char vitri;
- vitri=map[10-toado_y][toado_x];
- return vitri;
- }
- ////////////////////////////////////
- void intoado(int toado_x,int toado_y)
- {
- cout<<" ("<<toado_x<<","<<toado_y<<")";
- }
- ////////////////////////////////////
- bool ngoaimap(int vitri_x,int vitri_y)
- {
- bool outside;
- outside=( vitri_x == -1 || vitri_x == 20 || vitri_y == -1 || vitri_y == 10 );
- return outside;
- }
- ////////////////////////////////////
- // int tien=0;
- int ktmap (int &x, int &y, int &tien)
- {
- switch (map[9-y][x])
- {
- case '$' :
- tien += 1;
- map[9-y][x] = ' ';
- break;
- case '!' :
- return 1;
- break;
- default :
- return 0;
- }
- /*{
- if (map[9-y][x]=='$')
- {
- tien += 1;
- map[9-y][x] = ' ';
- }
- if (map[9-y][x] == '!')
- return 1;
- else return 0;
- }*/
- }
- ////////////////////////////////////
- void fall (int &x, int &y )
- {
- while (y > 0)
- {
- if (bottom(x,y) != '#' && !ngoaimap(x,y-1))
- {
- y -= 1;
- intoado(x,y);
- if (ktmap(x,y,tien) == 1)
- return;
- }
- else return;
- }
- }
- ////////////////////////////////////
- void LEFT_ ( int &x, int &y )
- {
- if (ngoaimap(x-1,y))
- intoado(x,y);
- else
- {
- switch (left(x,y))
- {
- case '#' :
- intoado(x,y);
- break;
- default :
- x -= 1;
- intoado(x,y);
- if (ktmap(x,y,tien) == 1)
- return;
- fall(x,y);
- break;
- }
- }
- }
- ////////////////////////////////////
- void RIGHT ( int &x, int &y )
- {
- if(ngoaimap(x+1,y))
- intoado(x,y);
- else
- {
- switch (right(x,y))
- {
- case '#':
- intoado(x,y);
- break;
- default :
- {
- x += 1;
- intoado(x,y);
- if (ktmap(x,y,tien) == 1)
- return;
- fall(x,y);
- }
- break;
- }
- }
- }
- ////////////////////////////////////
- void JUMP_ (int &x, int &y )
- {
- if (top(x,y) != '#' && !ngoaimap(x,y+1))
- {
- y += 1;
- intoado(x,y);
- if (ktmap(x,y,tien) == 1)
- return;
- }
- else intoado(x,y);
- if (top(x,y) != '#' && !ngoaimap(x,y+1))
- {
- y += 1;
- intoado(x,y);
- if (ktmap(x,y,tien) == 1)
- return;
- }
- fall(x,y);
- }
- ////////////////////////////////////
- void LEFTJUMP_ (int &x, int &y)
- {
- if(top(x,y)!='#' && !ngoaimap(x,y+1))
- {
- y+=1;
- intoado(x,y);
- if (ktmap(x,y,tien)==1)
- return;
- if (top(x,y) !='#' && !ngoaimap(x,y+1))
- {
- y+=1;
- intoado(x,y);
- if(ktmap(x,y,tien)==1)
- return;
- if ( left(x,y) != '#' && !ngoaimap(x-1,y))
- {
- x-=1;
- intoado(x,y);
- if( ktmap(x,y,tien)==1)
- return;
- if( left(x,y) !='#' && !ngoaimap(x-1,y))
- {
- x-=1;
- intoado(x,y);
- if( ktmap(x,y,tien)==1)
- return;
- fall (x,y);
- }
- else fall(x,y);
- }
- else fall(x,y);
- }
- else
- if( left(x,y)!='#' && !ngoaimap(x-1,y))
- {
- x-=1;
- intoado(x,y);
- if(ktmap(x,y,tien)==1)
- return;
- fall(x,y);
- }
- else fall(x,y);
- }
- else intoado(x,y);
- return;
- }
- ////////////////////////////////////
- void RIGHTJUMP_ (int &x, int &y)
- {
- if(top(x,y)!='#'&&!ngoaimap(x,y+1))
- {
- y+=1;
- intoado(x,y);
- if(ktmap(x,y,tien)==1)
- return;
- if(top(x,y)!='#'&&!ngoaimap(x,y+1))
- {
- y+=1;
- intoado(x,y);
- if(ktmap(x,y,tien)==1)
- return;
- if( right(x,y)!='#'&&!ngoaimap(x+1,y))
- {
- x+=1;
- intoado(x,y);
- if( ktmap(x,y,tien)==1)
- return;
- if( right(x,y)!='#'&& !ngoaimap(x+1,y))
- {
- x+=1;
- intoado(x,y);
- if(ktmap(x,y,tien)==1)
- return;
- fall(x,y);
- }
- else fall(x,y);
- }
- else fall(x,y);
- }
- else
- if(right(x,y)!='#'&&!ngoaimap(x+1,y))
- {
- x+=1;
- intoado(x,y);
- if( ktmap(x,y,tien)==1)
- return;
- fall(x,y);
- }
- else fall(x,y);
- }
- else intoado(x,y);
- return;
- }
- //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)
- //END TODO
- /////////////////////////////////////////////
- //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