Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10.  
  11. struct location{
  12. int H;
  13. int W;
  14. int G;
  15. };
  16.  
  17.  
  18. vector<string> date;
  19. void fast_input(char *name);
  20.  
  21. void output();
  22.  
  23. void move(int move_direction);
  24.  
  25.  
  26.  
  27. location now_human;
  28. void where_human(){
  29. for(int i=0;i<date.size();i++){
  30. for(int j=0;j<date[i].size();j++){
  31. if(date[i][j]=='V'||date[i][j]=='v'){
  32. now_human.H=i;
  33. now_human.W=j;
  34. if(date[i][j]=='V'){
  35. now_human.G=1;
  36. }else{
  37. now_human.G=0;
  38. }
  39. if(date[i][j]=='V'){
  40. date[i][j]='O';
  41. }else{
  42. date[i][j]='.';
  43. }
  44. }
  45. }
  46. }
  47. }
  48.  
  49.  
  50.  
  51. vector<location> now_pack;
  52. void where_pack(){
  53. for(int i=0;i<date.size();i++){
  54. for(int j=0;j<date[i].size();j++){
  55. if(date[i][j]=='X'||date[i][j]=='&'){
  56.  
  57. location now_pack_date;
  58. now_pack_date.H=i;
  59. now_pack_date.W=j;
  60.  
  61. if(date[i][j]=='&'){
  62. now_pack_date.G=1;
  63. }else{
  64. now_pack_date.G=0;
  65. }
  66.  
  67. now_pack.push_back(now_pack_date);
  68.  
  69. if(date[i][j]=='&'){
  70. date[i][j]='O';
  71. }else{
  72. date[i][j]='.';
  73. }
  74. }
  75. }
  76. }
  77. }
  78.  
  79.  
  80.  
  81. int input(){
  82. char c;
  83. cin >> c;
  84. switch(c){
  85. case 'd':
  86. return 0;
  87. case 'w':
  88. return 1;
  89. case 'a':
  90. return 2;
  91. case 's':
  92. return 3;
  93. case 'q':
  94. return 4;
  95. case 'r':
  96. return 5;
  97. default:
  98. return -1;
  99. }
  100. }
  101.  
  102.  
  103.  
  104. int clear(){
  105. for(int i=0;i<now_pack.size();i++){
  106. if(now_pack[i].G == 0){
  107. return -1;
  108. }
  109. }
  110. return 0;
  111. }
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. int main(int I,char *fname[]){
  119.  
  120. fast_input(fname[I-1]);
  121.  
  122. where_human();
  123.  
  124. where_pack();
  125.  
  126.  
  127. for(;;){
  128.  
  129. int move_direction;
  130. output();
  131.  
  132. if(clear()==0){
  133. break;
  134. }
  135.  
  136. move_direction=input();
  137. if(0<=move_direction && move_direction<=3){
  138. move(move_direction);
  139. }else if(move_direction == 4){
  140. cout << "end" << endl;
  141. return 0;
  142. }else if(move_direction == 5){
  143.  
  144. date.erase(date.begin(),date.end());
  145. now_pack.erase(now_pack.begin(),now_pack.end());
  146.  
  147. fast_input(fname[I-1]);
  148. where_human();
  149. where_pack();
  150. }
  151.  
  152. }
  153. cout << "Game Clear!" << endl;
  154. return 0;
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162. void fast_input(char *name){
  163. int i;
  164. int R,C;
  165.  
  166. ifstream ifs( name );
  167.  
  168. ifs >> R >> C;
  169. for(i=0;i<R;i++){
  170. string str;
  171. ifs >> str;
  172. date.push_back(str);
  173. }
  174.  
  175. }
  176.  
  177.  
  178.  
  179. void output(){
  180. #ifdef _WIN32
  181. system("cls"); // for windows
  182. #elif __APPLE_
  183. system("clear"); // for linux, mac
  184. #elif __linux__
  185. system("clear"); // for linux, mac
  186. #endif
  187. for(int i=0;i<date.size();i++){
  188. for(int j=0;j<date[i].size();j++){
  189. int put_pack=0;
  190. for(int k=0;k<now_pack.size();k++){
  191. if(i==now_pack[k].H&&j==now_pack[k].W){
  192. if(date[i][j]=='O'){
  193. cout << '&' ;
  194. }else{
  195. cout << 'X';
  196. }
  197. put_pack=1;
  198. break;
  199. }
  200. }
  201. if(i==now_human.H&&j==now_human.W){
  202. if(now_human.G==1){
  203. cout << 'V' ;
  204. }else{
  205. cout << 'v' ;
  206. }
  207. }else if(put_pack==0){
  208. cout << date[i][j];
  209. }
  210. }
  211. cout << endl;
  212. }
  213. }
  214.  
  215.  
  216.  
  217. void move(int move_direction){
  218. int move_date[2][4]={{0,-1,0,1},{1,0,-1,0}};
  219.  
  220. for(int i=0;i<now_pack.size();i++){
  221. 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){
  222. if(date[move_date[0][move_direction]*2+now_human.H][move_date[1][move_direction]*2+now_human.W]!='#'){
  223. for(int j=0;j<now_pack.size();j++){
  224. 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){
  225. return;
  226. }
  227. }
  228. now_pack[i].H += move_date[0][move_direction];
  229. now_pack[i].W += move_date[1][move_direction];
  230. if(date[now_pack[i].H][now_pack[i].W]=='O'){
  231. now_pack[i].G = 1;
  232. }else{
  233. now_pack[i].G = 0;
  234. }
  235. }else{
  236. return;
  237. }
  238. }
  239. }
  240.  
  241. switch(date[move_date[0][move_direction]+now_human.H][move_date[1][move_direction]+now_human.W]){
  242. case '.':
  243. now_human.H += move_date[0][move_direction];
  244. now_human.W += move_date[1][move_direction];
  245. now_human.G = 0;
  246. break;
  247. case 'O':
  248. now_human.H += move_date[0][move_direction];
  249. now_human.W += move_date[1][move_direction];
  250. now_human.G = 1;
  251. break;
  252. case '#':
  253. break;
  254. }
  255.  
  256.  
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement