[游戏]0F Game

· · 休闲·娱乐

#include<bits/stdc++.h>
#include<windows.h>
using namespace std;
int a[15][15];
//void lev_game(){
//
//}
struct str{
    int size;
    int a[15][15];
};
stack<str>his;
int lev;
void init(int size){
    int as=0;
    srand((unsigned)time(0));
    for(int i=0;;i++){
            if(i%100==0)srand((unsigned)time(0));
            int x=rand()%(size)+1,y=rand()%(size)+1;
//          cout<<x<<" "<<y<<" "<<a[x][y]<<endl;
            if(a[x][y]==0){
                as++; 
                a[x][y]=rand()%9;
//              cout<<x<<" "<<y<<" "<<a[x][y]<<endl;
                a[x-1][y]-=a[x][y];
                if(a[x-1][y]<0)a[x-1][y]=10+a[x-1][y];
                a[x][y-1]-=a[x][y];
                if(a[x][y-1]<0)a[x][y-1]=10+a[x][y-1];
                a[x][y+1]-=a[x][y];
                if(a[x][y+1]<0)a[x][y+1]=10+a[x][y+1];
                a[x+1][y]-=a[x][y];
                if(a[x+1][y]<0)a[x+1][y]=10+a[x+1][y];
            //rand()%10+50
//              for(int i=1;i<=size;i++){
//                  for(int j=1;j<=size;j++){
//                      cout<<a[i][j];
//                  }
//                  cout<<endl;
//              } 
//              cout<<endl;
                int f=1;
                for(int i=1;i<=size;i++){
                    for(int j=1;j<=size;j++){
                        if(a[i][j]==0)f=0;
                    }
                } 

                if(f){
                    cout<<"难度:"<<2*as<<endl;
                    return ;
                }
            }
    }
}
void inf_game(){
    cout<<"[无尽模式]"<<endl; 
    cout<<endl;
    cout<<"[游戏规则]:"<<endl;
    cout<<"[1]. 选择一个位置并让这个位置上的数字归0"<<endl;
    cout<<"[2]. 相邻位子会加上这个数字并保留个位"<<endl;
    cout<<"[3]. 全部为0就胜利啦~"<<endl;
    cout<<"[Tips]: 此游戏带有自动存档功能,最好把游戏文件放进您的U盘里,\n且发现一个叫\"save.zf\"的文件不要删除哟~那是游戏存档!"<<endl; 
    cout<<"[Tips_2]: 此游戏全部为随机生成,可能无解哦"<<endl;
    cout<<"输入游戏大小(n*n,推荐3 ~ 6,max=10,min=3)[Input](输入格式n)<<";
    int size;
    while(1){
        cin>>size; 
        if(size>10||size<=2){
            cout<<"[Error] - Size is too large/small.Please input agian."<<endl<<"[Input]<<"; 
        }
        else break;
    }
    cout<<"Loading..."<<endl;
    srand(time(0));
//  for(int i=1;i<=size;i++){
//      for(int j=1;j<=size;j++){
//          a[i][j]=(rand()%8)+1;
////            a[i][j]=0;
//      }
//  }
    init(size);
//  bool f=1;
//  for(int i=1;i<=size;i++){
//      for(int j=1;j<=size;j++)
//          f=a[i][j]?0:1; 
//  } 
//  if(f){
//      cout<<"你赢了!"<<endl<<endl<<endl;
//      return ;
//  }
    cout<<"Ready!"<<endl<<"   ";
    int s_t=time(0);
    if(size==10){
        cout<<" ";
    }
    for(int i=1;i<=size;i++){
        cout<<i<<"  ";
    }
    cout<<endl;
    for(int i=1;i<=size;i++){
        cout<<i<<" ";
        if(size==10){
                if(i<10)cout<<" ";
            }
        for(int j=1;j<=size;j++){
            cout<<"["<<a[i][j]<<"]";
        }
        cout<<endl;
    }
    freopen("save.zf","w",stdout);
    cout<<size<<endl;
    for(int i=1;i<=size;i++){
        for(int j=1;j<=size;j++){
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    freopen("CON","w",stdout);
    while(1){
        str st;
            st.size=size;
            for(int i=1;i<=size;i++){
                for(int j=1;j<=size;j++){
                    st.a[i][j]=a[i][j];
                }
            }
        his.push(st);
        int x,y;
        while(1){
            cout<<"[Input](输入格式\"x y\",\"-1 -1\"来撤回)<<";
            cin>>x>>y;
            if(((x<=0)||x>size||(y<=0)||y>size||a[x][y]==0)&&(x!=-1&&y!=-1))cout<<"数据不合法/超出或该位置已经为0,请重新输入"<<endl;
            else if(x==-1&&y==-1&&his.size()==1) cout<<"已经撤回到开始啦!"<<endl; 
            else break;
        }

        if(x==-1&&y==-1){
            his.pop();
            str strr=his.top();
            his.pop();
            for(int i=1;i<=size;i++){
                for(int j=1;j<=size;j++){
//                  cout<<strr.a[i][j];
                    a[i][j]=strr.a[i][j];
                }
//              cout<<endl;
            }
        }
        else{

            a[x-1][y]=(a[x][y]+a[x-1][y])%10;
            a[x][y-1]=(a[x][y]+a[x][y-1])%10;
            a[x][y+1]=(a[x][y]+a[x][y+1])%10;
            a[x+1][y]=(a[x][y]+a[x+1][y])%10;
            a[x][y]=0;
        }
        cout<<"   ";
        if(size==10){
            cout<<" ";
        }
        for(int i=1;i<=size;i++){
            cout<<i<<"  ";
        }
        cout<<endl;
        for(int i=1;i<=size;i++){
            cout<<i<<" ";
            if(size==10){
                if(i<10)cout<<" ";
            }
            for(int j=1;j<=size;j++){
                cout<<"["<<a[i][j]<<"]";
            }
            cout<<endl;
        }
        freopen("save.zf","w",stdout);
        cout<<size<<endl;
        for(int i=1;i<=size;i++){
            for(int j=1;j<=size;j++){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
        freopen("CON","w",stdout);
        bool f=1;
        for(int i=1;i<=size;i++){
            for(int j=1;j<=size;j++)
                if(a[i][j]!=0)f=0;
        } 
        if(f){
            cout<<"你赢了!"<<endl<<endl<<endl;
            int t=time(0)-s_t;
            if(t/3600){
                cout<<t/3600<<"hou";
                t%=3600;
            }
            if(t/60){
                cout<<t/60<<"min";
                t%=60;
            }
            if(t){
                cout<<t<<"sec"<<endl;
                t=0; 
            }
            freopen("save.zf","w",stdout);
            cout<<0;
            freopen("CON","w",stdout);
            break;
        }

    }
}
int main(){
//  ios::sync_with_stdio(false);
//  cin.tie(0);
//  cout.tie(0);
//  int n;
//  freopen("save.zf","r",stdin);
//  cin>>n;
//  if(n!=0){
//  int size=n;
//      for(int i=1;i<=size;i++){
//          for(int j=1;j<=size;j++){
//              cin>>a[i][j];
//  //          a[i][j]=0;
//          }
//      }
//      cout<<"   ";
//      for(int i=1;i<=size;i++){
//                      cout<<i<<"  ";
//                  }
//                  cout<<endl;
//                  for(int i=1;i<=size;i++){
//                      cout<<i<<" ";
//                      for(int j=1;j<=size;j++){
//                          cout<<"["<<a[i][j]<<"]";
//                      }
//                      cout<<endl;
//                  }
//      cout<<"您现在有未完成的存档!是否继续?[Input](Y/N)<<";
//      freopen("CON","r",stdin);
//      char s;
//      cin>>s;
//      if(s=='Y'){
//                  
//              while(1){
//                  int x,y;
//                  while(1){
//                      cout<<"[Input](输入格式\"x y\")<<";
//                      cin>>x>>y;
//                      if(x<=0||x>size||y<=0||y>size||a[x][y]==0)cout<<"数据不合法/超出或该位置已经为0,请重新输入"<<endl;
//                      else break;
//                  }
//                  a[x-1][y]=(a[x][y]+a[x-1][y])%10;
//                  a[x][y-1]=(a[x][y]+a[x][y-1])%10;
//                  a[x][y+1]=(a[x][y]+a[x][y+1])%10;
//                  a[x+1][y]=(a[x][y]+a[x+1][y])%10;
//                  a[x][y]=0;
//                  cout<<"  ";
//                  for(int i=1;i<=size;i++){
//                      cout<<i<<"  ";
//                  }
//                  cout<<endl;
//                  for(int i=1;i<=size;i++){
//                      cout<<i<<" ";
//                      for(int j=1;j<=size;j++){
//                          cout<<"["<<a[i][j]<<"]";
//                      }
//                      cout<<endl;
//                  }
//                  freopen("save.zf","w",stdout);
//                  cout<<size<<endl;
//                  for(int i=1;i<=size;i++){
//                      for(int j=1;j<=size;j++){
//                          cout<<a[i][j]<<" ";
//                      }
//                      cout<<endl;
//                  }
//                  freopen("CON","w",stdout);
//                  bool f=1;
//                  for(int i=1;i<=size;i++){
//                      for(int j=1;j<=size;j++)
//                          f=a[i][j]?0:1; 
//                  } 
//                  if(f){
//                      cout<<"你赢了!"<<endl<<endl<<endl;
//                      freopen("save.zf","w",stdout);
//                      cout<<0;
//                      freopen("CON","w",stdout);
//                      break;
//                  }
//              }
//      } 
//      else {
//          cout<<"存档已清除!"<<endl<<endl;
//          freopen("save.zf","w",stdout);
//          cout<<0;
//          freopen("CON","w",stdout);
//      }
//  }
//  freopen("CON","r",stdin);
    cout<<"0F Game"<<endl;
    cout<<"+=---------=[Alpha v.0.2.1 by Rootree & 30P7 ]=---------=+"<<endl;
    cout<<"更新功能[撤回]"<<endl;
    cout<<"[S]:开始游戏"<<endl;
    cout<<"[O]:设置"<<endl;
    cout<<"[Q]:离开"<<endl;
    freopen("CON","r",stdin);
    while(1){
        cout<<"[Input]<<";
        string option;
        cin>>option;
        if(option=="S"||option=="s"||option=="start"||option=="Start"||option=="START"){
            cout<<"[A]:关卡模式[未完成]"<<endl;
            cout<<"[B]:无限模式"<<endl;
            while(1){
                cout<<"[Input]<<";
                cin>>option;
                if(option=="A"||option=="B"||option=="b"||option=="a")break;
                else cout<<"Error"<<endl; 
            }
            if(option=="B"||option=="b")system("cls"),inf_game();
            else if(option=="A"||option=="a")system("cls");//lev_game();
            cout<<"0F Game"<<endl;
            cout<<"+=---------=[Alpha v.0.2.0 by Rootree & 30P7 ]=---------=+"<<endl;
            cout<<"[S]:开始游戏"<<endl;
            cout<<"[O]:设置"<<endl;
            cout<<"[Q]:离开"<<endl;
        }
        else if(option=="O"||option=="o"){
            cout<<"我懒得做等后面吧"<<endl;
        }
        else if(option=="Q"||option=="q"){
            freopen("save.zf","w",stdout);
            cout<<0;
            return 0;
        }
        else cout<<"Error"<<endl;
    }
}