Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <iostream>
- #include <sstream>
- #include <vector>
- #include <list>
- #include <map>
- #include <cmath>
- #include <cstdio>
- #include <algorithm>
- #include <istream>
- #include <ostream>
- using namespace std;
- class point {
- int x_, y_;
- public:
- point(int x=0, int y=0) : x_(x), y_(y) {}
- int get_x() const {
- return x_;
- }
- int get_y() const {
- return y_;
- }
- void set_x(int x) {
- x_ = x;
- }
- void set_y(int y) {
- y_ = y;
- }
- };
- istream& operator>>(istream& in, point& p) {
- int x, y;
- char c;
- in >> x >> c;
- if(c != ',') {
- in.clear(ios_base::badbit);
- return in;
- }
- in >> y;
- p.set_x(x);
- p.set_y(y);
- return in;
- }
- class minesweeper {
- int width_;
- int height_;
- vector<point> bombs_;
- istream& in_;
- ostream& out_;
- public:
- minesweeper(int width, int height, const vector<point>& bombs, istream& in, ostream& out):
- width_(width),height_(height),bombs_(bombs),in_(in),out_(out){}
- void run() {
- SetAlive();
- CreatePlayBoardAndSet(height_,width_);
- CreatePlayGroundAndSet(height_,width_);
- for(vector<point>::iterator it=bombs_.begin();it!=bombs_.end();it++){
- point p;
- p=*it;
- SetBombs(play_ground_,p,height_,width_);
- }
- SetPlayGround(play_ground_,height_,width_);
- string text;
- vector<string> comands;
- //PrintPlayGround(play_ground_,height_,width_);
- int counting=0; //oprawi imeto
- do{
- if(cin.eof())
- break;
- else if(AreOnlyBombsLeft(play_board_,play_ground_,height_,width_)){//||win_from_start(play_board_,play_ground_,height_,width_)useles
- FinalSetOfTheGame(play_board_,play_ground_,height_,width_);
- if(alive_)
- cout<<"game win"<<endl;
- PrintGameBoard(play_board_,height_,width_);
- break;
- }
- else{
- PrintGameBoard(play_board_,height_,width_);
- }
- cout<<"> ";
- getline(cin,text);
- comands.push_back(text);
- SetCommand(play_ground_,comands,counting,play_board_,height_,width_,bombs_);
- counting++;
- }while(!cin.eof());
- }
- private:
- int **play_ground_;
- char **play_board_;
- bool alive_;
- void SetAlive(){
- alive_=1;
- }
- void SetNotAlive(){
- alive_=0;
- }
- bool AreOnlyBombsLeft(char **arr_char,int **arr_int,const int height,const int width){
- for(int i=0;i<height;i++){
- for(int k=0;k<width;k++){
- if((arr_char[i][k]=='_'||arr_char[i][k]=='!')&&arr_int[i][k]!=-1)
- return 0;
- }
- }
- return 1;
- }
- bool win_from_start(char **arr_char,int **arr_int, const int height, const int width){ // do we need that ?
- int count=0;
- for(int i=0;i<height;i++)
- for(int k=0;k<width;k++)
- if(arr_char[i][k]=='_' && arr_int[i][k]!=-1)
- count++;
- if(count==(height*width))
- return 1;
- return 0;
- }
- void SetBombs(int **arr,point p,const int height,const int width)
- {
- if(IsInTheGame(height,width,p.get_x(),p.get_y()))
- arr[p.get_x()][p.get_y()]=-1;
- }
- void PrintPlayGround(int **arr,const int height,const int width){
- for(int i=0;i<height;i++){
- for(int k=0;k<width;k++)
- cout<<arr[i][k]<<" ";
- cout<<endl;
- }
- }
- void CreatePlayBoardAndSet(const int height,const int width)
- {
- play_board_= new char*[height];
- for(int i=0;i<height;i++)
- play_board_[i] = new char[width];
- for(int i=0;i<height;i++)
- for(int k=0;k<width;k++)
- play_board_[i][k]='_';
- }
- void CreatePlayGroundAndSet(const int height,const int width)
- {
- play_ground_=new int*[height];
- for(int i=0;i<height;i++)
- play_ground_[i]=new int[width];
- for(int i=0;i<height;i++)
- for(int k=0;k<width;k++)
- play_ground_[i][k]=0;
- }
- void PrintGameBoard(char **arr,const int height,const int width){
- for(int i=0;i<height;i++){
- for(int k=0;k<width;k++)
- cout<<arr[i][k];
- cout<<endl;
- }
- }
- void ExecuteWhenClickIsZero(int **arr_int,char **arr_char,point p,const int height,const int width){
- if(height==1||width==1){
- if(height==1&&width==1){
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- return;
- }
- if(width==1)
- {
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- if(p.get_x()==0){
- arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
- }
- else if(p.get_x()==height-1){
- arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
- }
- else{
- arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
- arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
- }
- }
- else{
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- if(p.get_y()==0){
- arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
- }
- else if(p.get_y()==width-1){
- arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
- }
- else{
- arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
- arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
- }
- }
- return;
- }
- //nagore e test case 2
- if(p.get_x()==0&&(p.get_y()==0||p.get_y()+1==width)){
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
- if(p.get_y()==0){
- arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
- arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
- }
- else{
- arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
- arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
- }
- }
- else if(p.get_x()+1==height && (p.get_y()==0||p.get_y()+1==width)){
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
- if(p.get_y()==0){
- arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
- arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
- }
- else{
- arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
- arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
- }
- }
- else if((p.get_y()==0||p.get_y()+1==width)&& p.get_x()!=0 && p.get_x()+1!=height){
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
- arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
- if(p.get_y()==0){
- arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
- arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
- arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
- }
- else{
- arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
- arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
- arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
- }
- }
- else if((p.get_y()!=0&&p.get_y()+1!=width)&&(p.get_x()==0||p.get_x()+1==height)){
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
- arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
- if(p.get_x()==0){
- arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
- arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
- arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
- }
- else{
- arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
- arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
- arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
- }
- }
- else{
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- arr_char[p.get_x()+1][p.get_y()]=arr_int[p.get_x()+1][p.get_y()]+'0';
- arr_char[p.get_x()][p.get_y()+1]=arr_int[p.get_x()][p.get_y()+1]+'0';
- arr_char[p.get_x()-1][p.get_y()]=arr_int[p.get_x()-1][p.get_y()]+'0';
- arr_char[p.get_x()][p.get_y()-1]=arr_int[p.get_x()][p.get_y()-1]+'0';
- arr_char[p.get_x()+1][p.get_y()-1]=arr_int[p.get_x()+1][p.get_y()-1]+'0';
- arr_char[p.get_x()+1][p.get_y()+1]=arr_int[p.get_x()+1][p.get_y()+1]+'0';
- arr_char[p.get_x()-1][p.get_y()-1]=arr_int[p.get_x()-1][p.get_y()-1]+'0';
- arr_char[p.get_x()-1][p.get_y()+1]=arr_int[p.get_x()-1][p.get_y()+1]+'0';
- }
- }
- void ExecuteCommandClick(int **arr_int,char **arr_char,point p,const int height,const int width){
- if(!IsInTheGame(height,width,p.get_x(),p.get_y()))
- return;
- if(arr_char[p.get_x()][p.get_y()]=='!'){
- arr_char[p.get_x()][p.get_y()]='_';
- return;
- }
- if(arr_int[p.get_x()][p.get_y()]==0){
- //cout<<height<<" "<<width<<endl;
- ExecuteWhenClickIsZero(arr_int,arr_char,p,height,width);
- //PrintGameBoard(arr_char,height,width);
- int count=0; //i tuk
- int count2=0;//tuk imenowane
- do{
- for(int i=0;i<height;i++){
- for(int k=0;k<width;k++){
- //cout<<i<<" "<<k<<" "<<arr_char[i][k]<<endl;
- if(arr_char[i][k]=='0'){
- if(IsInTheGame(height,width,i+1,k)){
- if(arr_char[i+1][k]=='_'){
- count++;
- point p1(i+1,k);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- if(IsInTheGame(height,width,i,k+1)){
- if(arr_char[i][k+1]=='_'){
- count++;
- point p1(i,k+1);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- if(IsInTheGame(height,width,i-1,k)){
- if(arr_char[i-1][k]=='_'){
- count++;
- point p1(i-1,k);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- if(IsInTheGame(height,width,i,k-1)){
- if(arr_char[i][k-1]=='_'){
- count++;
- point p1(i,k-1);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- if(IsInTheGame(height,width,i+1,k+1)){
- if(arr_char[i+1][k+1]=='_'){
- count++;
- point p1(i+1,k+1);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- if(IsInTheGame(height,width,i-1,k-1)){
- if(arr_char[i-1][k-1]=='_'){
- count++;
- point p1(i-1,k-1);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- if(IsInTheGame(height,width,i+1,k-1)){
- if(arr_char[i+1][k-1]=='_'){
- count++;
- point p1(i+1,k-1);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- if(IsInTheGame(height,width,i-1,k+1)){
- if(arr_char[i-1][k+1]=='_'){
- count++;
- point p1(i-1,k+1);
- //cout<<p1.get_x()<<" "<<p1.get_y()<<endl;
- ExecuteCommandClick(arr_int,arr_char,p1,height,width);
- }
- }
- }
- }
- }
- if(count2==count)
- break;
- count2=count;
- }while(1);
- }
- if(arr_int[p.get_x()][p.get_y()]>0)
- arr_char[p.get_x()][p.get_y()]=arr_int[p.get_x()][p.get_y()]+'0';
- if(arr_int[p.get_x()][p.get_y()]<0){
- cout<<"game over"<<endl;
- SetNotAlive();
- FinalSetOfTheGame(arr_char,arr_int,height,width);
- return;//trybwa li ni ?
- }
- }
- void FinalSetOfTheGame(char **arr_char,int **arr_int,const int height,const int width){
- for(int i=0;i<height;i++)
- for(int k=0;k<width;k++){
- if(arr_int[i][k]==-1)
- arr_char[i][k]='*';
- else{
- arr_char[i][k]=arr_int[i][k]+'0';
- }
- }
- }
- void ExecuteCommandFlag(char **arr_char,point p){
- if(arr_char[p.get_x()][p.get_y()]=='_'){
- arr_char[p.get_x()][p.get_y()]='!';
- return;
- }
- else if(arr_char[p.get_x()][p.get_y()]=='!')
- arr_char[p.get_x()][p.get_y()]='_';
- }
- void ExecuteCommandHint(int **arr_int,point p){
- if(arr_int[p.get_x()][p.get_y()]==-1)
- cout<<"bomb"<<endl;
- else{
- cout<<"not a bomb"<<endl;
- }
- }
- bool IsInTheGame(const int height,const int width,int row,int colon){
- return row>=0 && colon>=0 && height>row && width>colon;
- }
- void SetPlayGround(int **arr,const int height,const int width){// oprawi go s else i if
- for(int i=0;i<height;i++){
- for(int k=0;k<width;k++){
- if(arr[i][k]==-1){
- if(IsInTheGame(height,width,i,k-1))
- if(arr[i][k-1]!=-1)
- arr[i][k-1]=arr[i][k-1]+1;
- if(IsInTheGame(height,width,i,k+1))
- if(arr[i][k+1]!=-1)
- arr[i][k+1]=arr[i][k+1]+1;
- if(IsInTheGame(height,width,i-1,k))
- if(arr[i-1][k]!=-1)
- arr[i-1][k]=arr[i-1][k]+1;
- if(IsInTheGame(height,width,i+1,k))
- if(arr[i+1][k]!=-1)
- arr[i+1][k]=arr[i+1][k]+1;
- if(IsInTheGame(height,width,i+1,k-1))
- if(arr[i+1][k-1]!=-1)
- arr[i+1][k-1]=arr[i+1][k-1]+1;
- if(IsInTheGame(height,width,i+1,k+1))
- if(arr[i+1][k+1]!=-1)
- arr[i+1][k+1]=arr[i+1][k+1]+1;
- if(IsInTheGame(height,width,i-1,k-1))
- if(arr[i-1][k-1]!=-1)
- arr[i-1][k-1]=arr[i-1][k-1]+1;
- if(IsInTheGame(height,width,i-1,k+1))
- if(arr[i-1][k+1]!=-1)
- arr[i-1][k+1]=arr[i-1][k+1]+1;
- }
- }
- }
- }
- void SetCommand(int **arr_int,vector<string> str,int begin_from,char **arr,const int height,const int width,vector <point> bomb){
- string command;
- point p1;
- vector<string>::iterator it=str.begin()+begin_from;
- command=*it;
- int finding_space=command.find(" ");
- int find_flag=command.find("flag");
- int find_click=command.find("click");
- int find_hint=command.find("hint");
- if(!find_hint||!find_flag||!find_click){
- command.erase(0,finding_space+1);
- int finding_coma=command.find(",");
- string first_num(finding_coma,' ');
- int x;
- for(int i=0;i<finding_coma;i++)
- first_num[i]=command[i];
- x=atoi(first_num.c_str());
- command.erase(0,finding_coma+1);
- string sec_num(command.length(),' ');
- int y;
- for(int i=0;i<command.length();i++)
- sec_num[i]=command[i];
- y=atoi(sec_num.c_str());
- p1.set_x(x);
- p1.set_y(y);
- if(find_flag==0)
- ExecuteCommandFlag(arr,p1);
- else if(find_click==0)
- ExecuteCommandClick(arr_int,arr,p1,height,width);
- else if(find_hint==0)
- ExecuteCommandHint(arr_int,p1);
- }
- }
- };
- //6e razdelim programata na zada4i na 2
- class playground: public minesweeper{
- int **arr_;
- public:
- playground(int width, int height, const vector<point>& bombs, istream& in, ostream& out,int **arr):
- minesweeper(width,height,bombs,in,out),
- arr_(arr)
- {}
- };
- class playboard:public minesweeper{
- char **arr_;
- public:
- playboard(int width, int height, const vector<point>& bombs, istream& in, ostream& out,char **arr):
- minesweeper(width,height,bombs,in,out),
- arr_(arr)
- {}
- };
- int main() {
- string line;
- getline(cin, line);
- istringstream iss(line);
- int width, height;
- iss >> width >> height;
- getline(cin, line);
- iss.str(line);
- iss.clear();
- int bombs_count;
- iss >> bombs_count;
- vector<point> bombs;
- for (int i = 0; i < bombs_count; i++) {
- point p;
- getline(cin, line);
- iss.str(line);
- iss.clear();
- iss >> p;
- bombs.push_back(p);
- }
- minesweeper game(width, height, bombs, cin, cout);
- game.run();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement