用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c++代码库

2048

2014-10-06 作者: 云代码会员举报

[c++]代码库

/*
 1.0
 */
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<stdbool.h>
int i,j;
int arr[7][9]={0};
int grade=0;
//地图输出
int map(){
    for(i=1;i<=5;i+=2){
	    for(j=1;j<=7;j+=2)
		    arr[i][j]=5;
    }
    for(i=0;i<=6;i++){
	    for(j=0;j<=8;j+=2)
	        arr[i][j]=7;
    }
    for(i=1;i<=5;i+=2){
	    for(j=2;j<=6;j+=2)
		    arr[i][j]=9;
    }
	printf("*********************\n");
	printf("*****Grade:%5d*****\n",grade);
	printf("*********************\n");
	for(i=0;i<=6;i++){
	    for(j=0;j<=8;j++){
			if(arr[i][j]==7)
				printf("|");
			else if(arr[i][j]==5)
				printf(" -- ");
			else if(arr[i][j]==9)
				printf("+");
			else if(arr[i][j]==0)
				printf("    ");
			else 
				printf("%4d",arr[i][j]);
		}
		printf("\n");
	}
	printf("*********************\n");
	
}
int move(){
	char direction;
	int n=1,m=0;
    printf("请输入方向:w为上,s为下,a为左,d为右\n");
	scanf("%c",&direction);
	scanf("%*c");
	switch(direction){
	case 'w':{
	    /*			 
		移动
		*/
		//每一列的移动    
        for(j=1;j<=7;j+=2){
		    if(arr[0][j]==0){
				n=0;
			    for(i=0;i<=6;i+=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[2][j]==0){
				n=2;
			    for(i=2;i<=6;i+=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[4][j]==0){
				n=4;
			    for(i=4;i<=6;i+=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
		}
        //相加相邻的数
        for(j=1;j<=7;j+=2){
		    for(i=0;i<=6;i+=2){
			    if(arr[i][j]==arr[i+2][j] && arr[i][j]!=0){
				    arr[i][j]=2*arr[i][j];
					arr[i+2][j]=0;
				}
			}
		}
		//每一列的移动    
        for(j=1;j<=7;j+=2){
		    if(arr[0][j]==0){
				n=0;
			    for(i=0;i<=6;i+=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[2][j]==0){
				n=2;
			    for(i=2;i<=6;i+=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[4][j]==0){
				n=4;
			    for(i=4;i<=6;i+=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
		}
        //最后一行随机分配一个数
		if(arr[6][1]!=0 && arr[6][3]!=0 && arr[6][5]!=0 && arr[6][7]!=0){
			break;	
		}
		else{
	    for(;;){
		    m=(rand()%4)*2+1;
		    if(arr[6][m]==0){
			    arr[6][m]=(rand()%2+1)*2;
				break;
			}
		}
		}
		//结束'w'
	    break;
	}
	

	case 's':{
	    //移动
		//每一列的移动    
        for(j=1;j<=7;j+=2){
		    if(arr[6][j]==0){
				n=6;
			    for(i=6;i>=0;i-=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[4][j]==0){
				n=4;
			    for(i=4;i>=0;i-=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[2][j]==0){
				n=2;
			    for(i=2;i>=0;i-=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
		}
        //相加相邻的数
        for(j=1;j<=7;j+=2){
		    for(i=0;i<=6;i+=2){
			    if(arr[i][j]==arr[i+2][j] && arr[i][j]!=0){
				    arr[i][j]=2*arr[i][j];
					arr[i+2][j]=0;
				}
			}
		}
		//每一列的移动    
        for(j=1;j<=7;j+=2){
		    if(arr[6][j]==0){
				n=6;
			    for(i=6;i>=0;i-=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[4][j]==0){
				n=4;
			    for(i=4;i>=0;i-=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[2][j]==0){
				n=2;
			    for(i=2;i>=0;i-=2){
					if(arr[i][j]!=0){
				        arr[n][j]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
		}
        //最后一行随机分配一个数
		if(arr[0][1]!=0 && arr[0][3]!=0 && arr[0][5]!=0 && arr[0][7]!=0){
			break;	
		}
		else{
	    for(;;){
		    m=(rand()%4)*2+1;
		    if(arr[0][m]==0){
			    arr[0][m]=(rand()%2+1)*2;
				break;
			}
		}
		}

        //结束's'
	    break;
	}

	
	case 'a':{
	    //移动
		//每一行的移动    
        for(i=0;i<=6;i+=2){
		    if(arr[i][1]==0){
				n=1;
			    for(j=1;j<=7;j+=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[i][3]==0){
				n=3;
			    for(j=3;j<=7;j+=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[i][5]==0){
				n=5;
			    for(j=5;j<=7;j+=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
		}
        //相加相邻的数
        for(i=0;i<=6;i+=2){
		    for(j=1;j<=7;j+=2){
			    if(arr[i][j]==arr[i][j+2] && arr[i][j]!=0){
				    arr[i][j]=2*arr[i][j];
					arr[i][j+2]=0;
				}
			}
		}
		//每一行的移动    
        for(i=0;i<=6;i+=2){
		    if(arr[i][1]==0){
				n=1;
			    for(j=1;j<=7;j+=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[i][3]==0){
				n=3;
			    for(j=3;j<=7;j+=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
			else if(arr[i][5]==0){
				n=5;
			    for(j=5;j<=7;j+=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n+=2;
					}
				}
				continue;
			}
		}
        //最后一行随机分配一个数
		if(arr[0][7]!=0 && arr[2][7]!=0 && arr[4][7]!=0 && arr[6][7]!=0){
			break;	
		}
		else{
	    for(;;){
		    m=(rand()%4)*2;
		    if(arr[m][7]==0){
			    arr[m][7]=(rand()%2+1)*2;
				break;
			}
		}
		}

        //结束'a'
	    break;
	}
    
	case 'd':{
	    //移动
		//每一行的移动    
        for(i=0;i<=6;i+=2){
		    if(arr[i][7]==0){
				n=7;
			    for(j=7;j>=1;j-=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[i][5]==0){
				n=5;
			    for(j=5;j>=1;j-=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[i][3]==0){
				n=3;
			    for(j=3;j>=1;j-=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
		}
        //相加相邻的数
        for(i=0;i<=6;i+=2){
		    for(j=1;j<=7;j+=2){
			    if(arr[i][j]==arr[i][j+2] && arr[i][j]!=0){
				    arr[i][j]=2*arr[i][j];
					arr[i][j+2]=0;
				}
			}
		}
		//每一行的移动    
        for(i=0;i<=6;i+=2){
		    if(arr[i][7]==0){
				n=7;
			    for(j=7;j>=1;j-=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[i][5]==0){
				n=5;
			    for(j=5;j>=1;j-=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
			else if(arr[i][3]==0){
				n=3;
			    for(j=3;j>=1;j-=2){
					if(arr[i][j]!=0){
				        arr[i][n]=arr[i][j];
	                    arr[i][j]=0;
						n-=2;
					}
				}
				continue;
			}
		}
        //最后一行随机分配一个数
		if(arr[0][1]!=0 && arr[2][1]!=0 && arr[4][1]!=0 && arr[6][1]!=0){
			break;	
		}
		else{
	    for(;;){
		    m=(rand()%4)*2;
		    if(arr[m][1]==0){
			    arr[m][1]=(rand()%2+1)*2;
				break;
			}
		}
		}
        //结束'd'
	    break;
	}
	default:
		printf("输入不正确请重新输入\n");
}
}

int mark(){
	grade=0;
    for(i=0;i<=6;i+=2){
	    for(j=1;j<=7;j+=2){
		    grade=grade+arr[i][j];
		}
	}
}


bool over(){
	int num=0;
    for(j=1;j<=7;j+=2){
	    for(i=2;i<=6;i+=2){
			if(arr[i][j]!=arr[i-2][j]&&arr[i][j]!=0){
			    num++;
			}
		}
	}
    for(j=1;j<=7;j+=2){
	    for(i=0;i<=4;i+=2){
			if(arr[i][j]!=arr[i+2][j]&&arr[i][j]!=0){
			    num++;
			}
		}
	}
    for(i=0;i<=6;i+=2){
	    for(j=1;j<=5;j+=2){
			if(arr[i][j]!=arr[i][j+2]&&arr[i][j]!=0){
			    num++;
			}
		}
	}
    for(i=0;i<=6;i+=2){
	    for(j=3;j<=7;j+=2){
			if(arr[i][j]!=arr[j-2][j]&&arr[i][j]!=0){
			    num++;
			}
		}
	}
	if(num==48){
	    return true;
	}
	else if(num<48){
		return false;
	}

}

int file(int new_grade){
	int old_grade=0;
	FILE *fp=fopen("grade.bin","wb+");
	if(fp){
		fread(&old_grade,sizeof(int),1,fp);
	}
}

int main(){
    srand(time(0));
	//随机赋值
    for(i=1;i<=4;i++){
	    arr[(rand()%4)*2][(rand()%4)*2+1]=(rand()%2+1)*2;
    }
    map();
	while(1){
	    move();
		mark();
		system("clear");
	    map();
		if(over()){
	        printf("******Game Over******\n");
	        printf("*********************\n");
		    break;
		}
    }
	printf("**你的分数是:%5d***\n",grade);
	printf("*********************\n");
	return 0;
	
}


网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...