Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- struct location{
- int H;
- int W;
- int G;
- };
- vector<string> date;
- void fast_input(char *name);
- void output();
- void move(int move_direction);
- location now_human;
- void where_human(){
- for(int i=0;i<date.size();i++){
- for(int j=0;j<date[i].size();j++){
- if(date[i][j]=='V'||date[i][j]=='v'){
- now_human.H=i;
- now_human.W=j;
- if(date[i][j]=='V'){
- now_human.G=1;
- }else{
- now_human.G=0;
- }
- if(date[i][j]=='V'){
- date[i][j]='O';
- }else{
- date[i][j]='.';
- }
- }
- }
- }
- }
- vector<location> now_pack;
- void where_pack(){
- for(int i=0;i<date.size();i++){
- for(int j=0;j<date[i].size();j++){
- if(date[i][j]=='X'||date[i][j]=='&'){
- location now_pack_date;
- now_pack_date.H=i;
- now_pack_date.W=j;
- if(date[i][j]=='&'){
- now_pack_date.G=1;
- }else{
- now_pack_date.G=0;
- }
- now_pack.push_back(now_pack_date);
- if(date[i][j]=='&'){
- date[i][j]='O';
- }else{
- date[i][j]='.';
- }
- }
- }
- }
- }
- int input(){
- char c;
- cin >> c;
- switch(c){
- case 'd':
- return 0;
- case 'w':
- return 1;
- case 'a':
- return 2;
- case 's':
- return 3;
- case 'q':
- return 4;
- case 'r':
- return 5;
- default:
- return -1;
- }
- }
- int clear(){
- for(int i=0;i<now_pack.size();i++){
- if(now_pack[i].G == 0){
- return -1;
- }
- }
- return 0;
- }
- int main(int I,char *fname[]){
- fast_input(fname[I-1]);
- where_human();
- where_pack();
- for(;;){
- int move_direction;
- output();
- if(clear()==0){
- break;
- }
- move_direction=input();
- if(0<=move_direction && move_direction<=3){
- move(move_direction);
- }else if(move_direction == 4){
- cout << "end" << endl;
- return 0;
- }else if(move_direction == 5){
- date.erase(date.begin(),date.end());
- now_pack.erase(now_pack.begin(),now_pack.end());
- fast_input(fname[I-1]);
- where_human();
- where_pack();
- }
- }
- cout << "Game Clear!" << endl;
- return 0;
- }
- void fast_input(char *name){
- int i;
- int R,C;
- ifstream ifs( name );
- ifs >> R >> C;
- for(i=0;i<R;i++){
- string str;
- ifs >> str;
- date.push_back(str);
- }
- }
- void output(){
- #ifdef _WIN32
- system("cls"); // for windows
- #elif __APPLE_
- system("clear"); // for linux, mac
- #elif __linux__
- system("clear"); // for linux, mac
- #endif
- for(int i=0;i<date.size();i++){
- for(int j=0;j<date[i].size();j++){
- int put_pack=0;
- for(int k=0;k<now_pack.size();k++){
- if(i==now_pack[k].H&&j==now_pack[k].W){
- if(date[i][j]=='O'){
- cout << '&' ;
- }else{
- cout << 'X';
- }
- put_pack=1;
- break;
- }
- }
- if(i==now_human.H&&j==now_human.W){
- if(now_human.G==1){
- cout << 'V' ;
- }else{
- cout << 'v' ;
- }
- }else if(put_pack==0){
- cout << date[i][j];
- }
- }
- cout << endl;
- }
- }
- void move(int move_direction){
- int move_date[2][4]={{0,-1,0,1},{1,0,-1,0}};
- for(int i=0;i<now_pack.size();i++){
- if(move_date[0][move_direction]+now_human.H==now_pack[i].H&&move_date[1][move_direction]+now_human.W==now_pack[i].W){
- if(date[move_date[0][move_direction]*2+now_human.H][move_date[1][move_direction]*2+now_human.W]!='#'){
- for(int j=0;j<now_pack.size();j++){
- if(move_date[0][move_direction]*2+now_human.H==now_pack[j].H&&move_date[1][move_direction]*2+now_human.W==now_pack[j].W){
- return;
- }
- }
- now_pack[i].H += move_date[0][move_direction];
- now_pack[i].W += move_date[1][move_direction];
- if(date[now_pack[i].H][now_pack[i].W]=='O'){
- now_pack[i].G = 1;
- }else{
- now_pack[i].G = 0;
- }
- }else{
- return;
- }
- }
- }
- switch(date[move_date[0][move_direction]+now_human.H][move_date[1][move_direction]+now_human.W]){
- case '.':
- now_human.H += move_date[0][move_direction];
- now_human.W += move_date[1][move_direction];
- now_human.G = 0;
- break;
- case 'O':
- now_human.H += move_date[0][move_direction];
- now_human.W += move_date[1][move_direction];
- now_human.G = 1;
- break;
- case '#':
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement