Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.23 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <vector>
  5. #include <list>
  6. #include <map>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <algorithm>
  10. #include <istream>
  11. #include <ostream>
  12. using namespace std;
  13. class point {
  14. int x_, y_;
  15. public:
  16.  
  17. point(int x=0, int y=0) : x_(x), y_(y) {}
  18.  
  19. int get_x() const {
  20. return x_;
  21. }
  22.  
  23. int get_y() const {
  24. return y_;
  25. }
  26.  
  27. void set_x(int x) {
  28. x_ = x;
  29. }
  30.  
  31. void set_y(int y) {
  32. y_ = y;
  33. }
  34. };
  35. istream& operator>>(istream& in, point& p) {
  36. int x, y;
  37. char c;
  38. in >> x >> c;
  39. if(c != ',') {
  40. in.clear(ios_base::badbit);
  41. return in;
  42. }
  43. in >> y;
  44. p.set_x(x);
  45. p.set_y(y);
  46. return in;
  47. }
  48. class minesweeper {
  49. int width_;
  50. int height_;
  51. vector<point> bombs_;
  52. istream& in_;
  53. ostream& out_;
  54. public:
  55. minesweeper(int width, int height, const vector<point>& bombs, istream& in, ostream& out):
  56. width_(width),height_(height),bombs_(bombs),in_(in),out_(out){}
  57. void run() {
  58. SetAlive();
  59. CreatePlayBoardAndSet(height_,width_);
  60. CreatePlayGroundAndSet(height_,width_);
  61. for(vector<point>::iterator it=bombs_.begin();it!=bombs_.end();it++){
  62. point p;
  63. p=*it;
  64. SetBombs(play_ground_,p,height_,width_);
  65. }
  66. SetPlayGround(play_ground_,height_,width_);
  67. string text;
  68. vector<string> comands;
  69. //PrintPlayGround(play_ground_,height_,width_);
  70. int counting=0; //oprawi imeto
  71. do{
  72. if(cin.eof())
  73. break;
  74. else if(AreOnlyBombsLeft(play_board_,play_ground_,height_,width_)){//||win_from_start(play_board_,play_ground_,height_,width_)useles
  75. FinalSetOfTheGame(play_board_,play_ground_,height_,width_);
  76. if(alive_)
  77. cout<<"game win"<<endl;
  78. PrintGameBoard(play_board_,height_,width_);
  79. break;
  80. }
  81. else{
  82. PrintGameBoard(play_board_,height_,width_);
  83. }
  84. cout<<"> ";
  85. getline(cin,text);
  86. comands.push_back(text);
  87. SetCommand(play_ground_,comands,counting,play_board_,height_,width_,bombs_);
  88. counting++;
  89. }while(!cin.eof());
  90. }
  91. private:
  92.  
  93. int **play_ground_;
  94. char **play_board_;
  95. bool alive_;
  96. void SetAlive(){
  97. alive_=1;
  98. }
  99. void SetNotAlive(){
  100. alive_=0;
  101. }
  102. bool AreOnlyBombsLeft(char **arr_char,int **arr_int,const int height,const int width){
  103. for(int i=0;i<height;i++){
  104. for(int k=0;k<width;k++){
  105. if((arr_char[i][k]=='_'||arr_char[i][k]=='!')&&arr_int[i][k]!=-1)
  106. return 0;
  107. }
  108. }
  109. return 1;
  110.  
  111. }
  112. bool win_from_start(char **arr_char,int **arr_int, const int height, const int width){ // do we need that ?
  113. int count=0;
  114. for(int i=0;i<height;i++)
  115. for(int k=0;k<width;k++)
  116. if(arr_char[i][k]=='_' && arr_int[i][k]!=-1)
  117. count++;
  118.  
  119. if(count==(height*width))
  120. return 1;
  121. return 0;
  122. }
  123. void SetBombs(int **arr,point p,const int height,const int width)
  124. {
  125. if(IsInTheGame(height,width,p.get_x(),p.get_y()))
  126. arr[p.get_x()][p.get_y()]=-1;
  127. }
  128. void PrintPlayGround(int **arr,const int height,const int width){
  129. for(int i=0;i<height;i++){
  130. for(int k=0;k<width;k++)
  131. cout<<arr[i][k]<<" ";
  132. cout<<endl;
  133. }
  134. }
  135. void CreatePlayBoardAndSet(const int height,const int width)
  136. {
  137. play_board_= new char*[height];
  138. for(int i=0;i<height;i++)
  139. play_board_[i] = new char[width];
  140. for(int i=0;i<height;i++)
  141. for(int k=0;k<width;k++)
  142. play_board_[i][k]='_';
  143. }
  144. void CreatePlayGroundAndSet(const int height,const int width)
  145. {
  146. play_ground_=new int*[height];
  147. for(int i=0;i<height;i++)
  148. play_ground_[i]=new int[width];
  149. for(int i=0;i<height;i++)
  150. for(int k=0;k<width;k++)
  151. play_ground_[i][k]=0;
  152. }
  153. void PrintGameBoard(char **arr,const int height,const int width){
  154. for(int i=0;i<height;i++){
  155. for(int k=0;k<width;k++)
  156. cout<<arr[i][k];
  157. cout<<endl;
  158. }
  159. }
  160.  
  161. void ExecuteWhenClickIsZero(int **arr_int,char **arr_char,point p,const int height,const int width){
  162. if(height==1||width==1){
  163. if(height==1&&width==1){
  164. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  165. return;
  166. }
  167. if(width==1)
  168. {
  169. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  170. if(p.get_x()==0){
  171. arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
  172. }
  173. else if(p.get_x()==height-1){
  174. arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
  175. }
  176. else{
  177. arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
  178. arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
  179. }
  180.  
  181. }
  182. else{
  183. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  184. if(p.get_y()==0){
  185. arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
  186. }
  187. else if(p.get_y()==width-1){
  188. arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
  189. }
  190. else{
  191. arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
  192. arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
  193. }
  194. }
  195. return;
  196.  
  197. }
  198. //nagore e test case 2
  199.  
  200. if(p.get_x()==0&&(p.get_y()==0||p.get_y()+1==width)){
  201. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  202. arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
  203. if(p.get_y()==0){
  204. arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
  205. arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
  206. }
  207. else{
  208. arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
  209. arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
  210. }
  211. }
  212. else if(p.get_x()+1==height && (p.get_y()==0||p.get_y()+1==width)){
  213. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  214. arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
  215. if(p.get_y()==0){
  216. arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
  217. arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
  218. }
  219. else{
  220. arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
  221. arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
  222. }
  223. }
  224. else if((p.get_y()==0||p.get_y()+1==width)&& p.get_x()!=0 && p.get_x()+1!=height){
  225. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  226. arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
  227. arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
  228. if(p.get_y()==0){
  229. arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
  230. arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
  231. arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
  232. }
  233. else{
  234. arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
  235. arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
  236. arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
  237. }
  238. }
  239. else if((p.get_y()!=0&&p.get_y()+1!=width)&&(p.get_x()==0||p.get_x()+1==height)){
  240. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  241. arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
  242. arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
  243. if(p.get_x()==0){
  244. arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
  245. arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
  246. arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
  247. }
  248. else{
  249. arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
  250. arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
  251. arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
  252. }
  253. }
  254. else{
  255. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  256. arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
  257. arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
  258. arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
  259. arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
  260. arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
  261. arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
  262. arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
  263. arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
  264. }
  265. }
  266. void ExecuteCommandClick(int **arr_int,char **arr_char,point p,const int height,const int width){
  267. if(!IsInTheGame(height,width,p.get_x(),p.get_y()))
  268. return;
  269. if(arr_char[p.get_x()][p.get_y()]=='!'){
  270. arr_char[p.get_x()][p.get_y()]='_';
  271. return;
  272. }
  273. if(arr_int[p.get_x()][p.get_y()]==0){
  274. //cout<<height<<" "<<width<<endl;
  275. ExecuteWhenClickIsZero(arr_int,arr_char,p,height,width);
  276. //PrintGameBoard(arr_char,height,width);
  277. int count=0; //i tuk
  278. int count2=0;//tuk imenowane
  279. do{
  280. for(int i=0;i<height;i++){
  281. for(int k=0;k<width;k++){
  282. //cout<<i<<" "<<k<<" "<<arr_char[i][k]<<endl;
  283. if(arr_char[i][k]=='0'){
  284. if(IsInTheGame(height,width,i+1,k)){
  285. if(arr_char[i+1][k]=='_'){
  286. count++;
  287. point p1(i+1,k);
  288. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  289. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  290. }
  291. }
  292. if(IsInTheGame(height,width,i,k+1)){
  293. if(arr_char[i][k+1]=='_'){
  294. count++;
  295. point p1(i,k+1);
  296. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  297. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  298. }
  299. }
  300. if(IsInTheGame(height,width,i-1,k)){
  301. if(arr_char[i-1][k]=='_'){
  302. count++;
  303. point p1(i-1,k);
  304. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  305. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  306. }
  307. }
  308. if(IsInTheGame(height,width,i,k-1)){
  309. if(arr_char[i][k-1]=='_'){
  310. count++;
  311. point p1(i,k-1);
  312. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  313. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  314. }
  315. }
  316. if(IsInTheGame(height,width,i+1,k+1)){
  317. if(arr_char[i+1][k+1]=='_'){
  318. count++;
  319. point p1(i+1,k+1);
  320. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  321. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  322. }
  323. }
  324. if(IsInTheGame(height,width,i-1,k-1)){
  325. if(arr_char[i-1][k-1]=='_'){
  326. count++;
  327. point p1(i-1,k-1);
  328. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  329. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  330. }
  331. }
  332. if(IsInTheGame(height,width,i+1,k-1)){
  333. if(arr_char[i+1][k-1]=='_'){
  334. count++;
  335. point p1(i+1,k-1);
  336. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  337. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  338. }
  339. }
  340. if(IsInTheGame(height,width,i-1,k+1)){
  341. if(arr_char[i-1][k+1]=='_'){
  342. count++;
  343. point p1(i-1,k+1);
  344. //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
  345. ExecuteCommandClick(arr_int,arr_char,p1,height,width);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. if(count2==count)
  352. break;
  353. count2=count;
  354. }while(1);
  355. }
  356. if(arr_int[p.get_x()][p.get_y()]>0)
  357. arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
  358. if(arr_int[p.get_x()][p.get_y()]<0){
  359. cout<<"game over"<<endl;
  360. SetNotAlive();
  361. FinalSetOfTheGame(arr_char,arr_int,height,width);
  362. return;//trybwa li ni ?
  363. }
  364. }
  365. void FinalSetOfTheGame(char **arr_char,int **arr_int,const int height,const int width){
  366. for(int i=0;i<height;i++)
  367. for(int k=0;k<width;k++){
  368. if(arr_int[i][k]==-1)
  369. arr_char[i][k]='*';
  370. else{
  371. arr_char[i][k]=arr_int[i][k]+'0';
  372. }
  373. }
  374. }
  375. void ExecuteCommandFlag(char **arr_char,point p){
  376. if(arr_char[p.get_x()][p.get_y()]=='_'){
  377. arr_char[p.get_x()][p.get_y()]='!';
  378. return;
  379. }
  380. else if(arr_char[p.get_x()][p.get_y()]=='!')
  381. arr_char[p.get_x()][p.get_y()]='_';
  382. }
  383. void ExecuteCommandHint(int **arr_int,point p){
  384. if(arr_int[p.get_x()][p.get_y()]==-1)
  385. cout<<"bomb"<<endl;
  386. else{
  387. cout<<"not a bomb"<<endl;
  388. }
  389. }
  390. bool IsInTheGame(const int height,const int width,int row,int colon){
  391. return row>=0 && colon>=0 && height>row && width>colon;
  392. }
  393. void SetPlayGround(int **arr,const int height,const int width){// oprawi go s else i if
  394. for(int i=0;i<height;i++){
  395. for(int k=0;k<width;k++){
  396. if(arr[i][k]==-1){
  397. if(IsInTheGame(height,width,i,k-1))
  398. if(arr[i][k-1]!=-1)
  399. arr[i][k-1]=arr[i][k-1]+1;
  400. if(IsInTheGame(height,width,i,k+1))
  401. if(arr[i][k+1]!=-1)
  402. arr[i][k+1]=arr[i][k+1]+1;
  403. if(IsInTheGame(height,width,i-1,k))
  404. if(arr[i-1][k]!=-1)
  405. arr[i-1][k]=arr[i-1][k]+1;
  406. if(IsInTheGame(height,width,i+1,k))
  407. if(arr[i+1][k]!=-1)
  408. arr[i+1][k]=arr[i+1][k]+1;
  409. if(IsInTheGame(height,width,i+1,k-1))
  410. if(arr[i+1][k-1]!=-1)
  411. arr[i+1][k-1]=arr[i+1][k-1]+1;
  412. if(IsInTheGame(height,width,i+1,k+1))
  413. if(arr[i+1][k+1]!=-1)
  414. arr[i+1][k+1]=arr[i+1][k+1]+1;
  415. if(IsInTheGame(height,width,i-1,k-1))
  416. if(arr[i-1][k-1]!=-1)
  417. arr[i-1][k-1]=arr[i-1][k-1]+1;
  418. if(IsInTheGame(height,width,i-1,k+1))
  419. if(arr[i-1][k+1]!=-1)
  420. arr[i-1][k+1]=arr[i-1][k+1]+1;
  421. }
  422. }
  423. }
  424. }
  425.  
  426.  
  427.  
  428.  
  429.  
  430.  
  431.  
  432. void SetCommand(int **arr_int,vector<string> str,int begin_from,char **arr,const int height,const int width,vector <point> bomb){
  433. string command;
  434. point p1;
  435. vector<string>::iterator it=str.begin()+begin_from;
  436. command=*it;
  437. int finding_space=command.find(" ");
  438. int find_flag=command.find("flag");
  439. int find_click=command.find("click");
  440. int find_hint=command.find("hint");
  441. if(!find_hint||!find_flag||!find_click){
  442. command.erase(0,finding_space+1);
  443. int finding_coma=command.find(",");
  444. string first_num(finding_coma,' ');
  445. int x;
  446. for(int i=0;i<finding_coma;i++)
  447. first_num[i]=command[i];
  448. x=atoi(first_num.c_str());
  449. command.erase(0,finding_coma+1);
  450. string sec_num(command.length(),' ');
  451. int y;
  452. for(int i=0;i<command.length();i++)
  453. sec_num[i]=command[i];
  454. y=atoi(sec_num.c_str());
  455. p1.set_x(x);
  456. p1.set_y(y);
  457. if(find_flag==0)
  458. ExecuteCommandFlag(arr,p1);
  459. else if(find_click==0)
  460. ExecuteCommandClick(arr_int,arr,p1,height,width);
  461. else if(find_hint==0)
  462. ExecuteCommandHint(arr_int,p1);
  463. }
  464. }
  465. };
  466.  
  467. //6e razdelim programata na zada4i na 2
  468.  
  469. class playground: public minesweeper{
  470. int **arr_;
  471.  
  472. public:
  473. playground(int width, int height, const vector<point>& bombs, istream& in, ostream& out,int **arr):
  474. minesweeper(width,height,bombs,in,out),
  475. arr_(arr)
  476. {}
  477. };
  478.  
  479. class playboard:public minesweeper{
  480. char **arr_;
  481. public:
  482. playboard(int width, int height, const vector<point>& bombs, istream& in, ostream& out,char **arr):
  483. minesweeper(width,height,bombs,in,out),
  484. arr_(arr)
  485. {}
  486.  
  487. };
  488. int main() {
  489. string line;
  490.  
  491. getline(cin, line);
  492. istringstream iss(line);
  493. int width, height;
  494. iss >> width >> height;
  495.  
  496. getline(cin, line);
  497. iss.str(line);
  498. iss.clear();
  499. int bombs_count;
  500. iss >> bombs_count;
  501. vector<point> bombs;
  502. for (int i = 0; i < bombs_count; i++) {
  503. point p;
  504. getline(cin, line);
  505. iss.str(line);
  506. iss.clear();
  507. iss >> p;
  508. bombs.push_back(p);
  509. }
  510. minesweeper game(width, height, bombs, cin, cout);
  511. game.run();
  512. return 0;
  513. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement