zoukankan      html  css  js  c++  java
  • 亮瞎眼的2048

    请不要沉迷于此游戏,此程序辣眼睛...

     1 #include<bits/stdc++.h>
     2 #include<conio.h>
     3 using namespace std;
     4 int a[50][50],b[50][50],n;
     5 void qs(int k){
     6     int b[6],w=0,p=k;
     7     while(k){
     8         b[++w]=k%10;
     9         k/=10;
    10     }
    11     cout<<p;
    12     w=5-w;
    13     if(p)w++;
    14     while(w--)cout<<' ';
    15 }
    16 void out(){
    17     for(int i=1;i<=n;i++){
    18         for(int j=1;j<=n;j++)qs(a[i][j]);
    19         cout<<"
    
    
    ";
    20     }
    21 }
    22 void cpy(){
    23     for(int i=1;i<=n;i++)
    24         for(int j=1;j<=n;j++)
    25         b[i][j]=a[i][j];
    26 }
    27 bool check(){
    28     for(int i=1;i<=n;i++)
    29         for(int j=1;j<=n;j++)
    30         if(a[i][j]!=b[i][j])return 1;
    31     return 0;
    32 }
    33 int main(){
    34     system("color a");
    35     cout<<"Size:";cin>>n;system("cls");
    36     srand((int)time(NULL));
    37     int i,j,x,y,score=0;
    38     char k;
    39     while(1){
    40         cout<<"Score:"<<score<<endl;
    41         x=rand()%n+1,y=rand()%n+1;
    42         int c=0;
    43         while(a[x][y]){
    44         x=rand()%n+1,y=rand()%n+1,c++;
    45         if(c==16){
    46         cout<<"GAME OVER"<<endl;
    47         cout<<"TRY AGAIN (y/n):";
    48         
    49         k=getch();
    50         if(k=='y')system("start 2048.exe");
    51         exit(0);
    52     }
    53     }
    54         a[x][y]=2;
    55         cpy();
    56         out();
    57     while(!check()){
    58         k=getch();
    59         if(k=='w'||k=='H'){
    60             for(j=1;j<=n;j++)
    61                 for(i=2;i<=n;i++){
    62                     int x=i;
    63                     while(a[x-1][j]==0&&x>1)a[x-1][j]=a[x][j],a[x--][j]=0;
    64                     if(a[x][j]==a[x-1][j])a[x-1][j]*=2,a[x][j]=0,score+=a[x-1][j];
    65                 }                
    66         }
    67         if(k=='s'||k=='P'){
    68             for(j=1;j<=n;j++)
    69                 for(i=n-1;i>=1;i--){
    70                     int x=i;
    71                     while(a[x+1][j]==0&&x<n)a[x+1][j]=a[x][j],a[x++][j]=0;
    72                     if(a[x][j]==a[x+1][j])a[x+1][j]*=2,a[x][j]=0,score+=a[x+1][j];
    73                 }                
    74         }
    75         if(k=='a'||k=='K'){
    76             for(j=1;j<=n;j++)
    77                 for(i=2;i<=n;i++){
    78                     int x=i;
    79                     while(a[j][x-1]==0&&x>1)a[j][x-1]=a[j][x],a[j][x--]=0;
    80                     if(a[j][x]==a[j][x-1])a[j][x-1]*=2,a[j][x]=0,score+=a[j][x-1];
    81                 }
    82         }
    83         if(k=='d'||k=='M'){
    84             for(j=1;j<=n;j++)
    85                 for(i=n-1;i>=1;i--){
    86                     int x=i;
    87                     while(a[j][x+1]==0&&x<n)a[j][x+1]=a[j][x],a[j][x++]=0;
    88                     if(a[j][x]==a[j][x+1])a[j][x+1]*=2,a[j][x]=0,score+=a[j][x+1];
    89                 }
    90         }
    91 }
    92         system("cls");
    93     }
    94 }
  • 相关阅读:
    2013面试C++小结
    Linux C 面试题总结 .
    [SCOI2011]糖果
    python——简单爬虫
    python——ADSL拨号程序
    python——处理xls表格
    Vsphere初试——架设Panabit行为管理
    Vsphere初试——使用Vsphere client
    Vsphere初试——基本安装
    Python2与Python3的不同点
  • 原文地址:https://www.cnblogs.com/mimiorz/p/7274403.html
Copyright © 2011-2022 走看看