2048小游戏

#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<time.h>

char ch;
unsigned short a[4][4];
unsigned int score;

void in();
void out();
void creater();
bool gameover();

void up();
void down();
void left();
void right();

void color(int _color) {
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),_color);
	return;
}

void gotoxy(int xx,int yy) {
	COORD position;
	position.X=yy;
	position.Y=xx;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
	return;
}

int main() {
	SetWindowText(NULL,"2048");
	srand(time(NULL));
	CONSOLE_CURSOR_INFO cursor_info= {1,0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
	while(true) {
		creater();
		out();
		in();
		if(gameover()) {
			system("color 7F");
			color(12);
			gotoxy(4,5);
			printf("Game over!");
			Sleep(3000);
			system("cls");
			FILE* read=fopen("s.txt","r");
			if(read!=NULL) {
				unsigned int ts;
				fscanf(read,"%d",&ts);
				fclose(read);
				if(ts<score) {
					FILE* write=fopen("s.txt","w");
					fprintf(write,"%d",score);
					fclose(write);
				}
			} else {
				FILE* write=fopen("s.txt","w");
				fprintf(write,"%d",score);
				fclose(write);
			}
			return 0;
		}
		score++;
	}
	return 0;
}

void in() {
	ch=getch();
	switch(ch) {
		case 'w':
		case 'W': {
			up();
			break;
		}
		case 's':
		case 'S': {
			down();
			break;
		}
		case 'a':
		case 'A': {
			left();
			break;
		}
		case 'd':
		case 'D': {
			right();
			break;
		}
	}
	return;
}

void out() {
	gotoxy(0,0);
	for(unsigned char i=0; i<4; i++) {
		for(unsigned char j=0; j<4; j++) {
			printf("|----");
		}
		printf("|\n");
		for(unsigned char j=0; j<4; j++) {
			printf("|");
			if(a[i][j]!=0) {
				switch(a[i][j]) {
					case 2: {
						color(2);
						break;
					}
					case 4: {
						color(3);
						break;
					}
					case 8: {
						color(4);
						break;
					}
					case 16: {
						color(5);
						break;
					}
					case 32: {
						color(6);
						break;
					}
					case 64: {
						color(9);
						break;
					}
					case 128: {
						color(10);
						break;
					}
					case 256: {
						color(11);
						break;
					}
					case 512: {
						color(12);
						break;
					}
					case 1024: {
						color(13);
						break;
					}
					case 2048: {
						color(14);
						break;
					}
				}
				printf("%4d",a[i][j]);
				color(7);
			} else {
				printf("    ");
			}
		}
		printf("|\n");
	}
	for(unsigned char j=0; j<4; j++) {
		printf("|----");
	}
	printf("|\n");
	printf("score:%d",score);
	return;
}

void creater() {
	unsigned char tx;
	unsigned char ty;
	do {
		tx=rand()%4;
		ty=rand()%4;
	} while(a[tx][ty]!=0);
	if(rand()%2) {
		a[tx][ty]=2;
	} else {
		a[tx][ty]=4;
	}
	return;
}

bool gameover() {
	for(unsigned char i=0; i<4; i++) {
		for(unsigned char j=0; j<4; j++) {
			if(!a[i][j]) {
				return false;
			}
		}
	}
	return true;
}

void up() {
	for(unsigned char ty=0; ty<4; ty++) {
		for(unsigned char tx=0; tx<4; tx++) {
			for(unsigned char cnt=0; a[tx][ty]==0&&cnt<8; cnt++) {
				for(unsigned l=tx; l<3&&a[tx][ty]==0; l++) {
					a[l][ty]=a[l+1][ty];
					a[l+1][ty]=0;
				}
			}
		}
	}
	for(unsigned char ty=0; ty<4; ty++) {
		for(unsigned char tx=0; tx<4; tx++) {
			if(a[tx][ty]==a[tx+1][ty]&&a[tx][ty]!=0) {
				a[tx][ty]+=a[tx+1][ty];
				a[tx+1][ty]=0;
				for(unsigned l=tx+1; l<3; l++) {
					a[l][ty]=a[l+1][ty];
					a[l+1][ty]=0;
				}
				score+=16;
			}
		}
	}
	return;
}

void down() {
	for(unsigned char ty=0; ty<4; ty++) {
		for(unsigned char tx=3; tx>0; tx--) {
			for(unsigned char cnt=0; a[tx][ty]==0&&cnt<8; cnt++) {
				for(unsigned l=tx; l>0&&a[tx][ty]==0; l--) {
					a[l][ty]=a[l-1][ty];
					a[l-1][ty]=0;
				}
			}
		}
	}
	for(unsigned char ty=0; ty<4; ty++) {
		for(unsigned char tx=3; tx>0; tx--) {
			if(a[tx][ty]==a[tx-1][ty]&&a[tx][ty]!=0) {
				a[tx][ty]+=a[tx-1][ty];
				a[tx-1][ty]=0;
				for(unsigned l=tx-1; l>0; l--) {
					a[l][ty]=a[l-1][ty];
					a[l-1][ty]=0;
				}
				score+=16;
			}
		}
	}
	return;
}

void left() {
	for(unsigned char tx=0; tx<4; tx++) {
		for(unsigned char ty=0; ty<4; ty++) {
			for(unsigned char cnt=0; a[tx][ty]==0&&cnt<8; cnt++) {
				for(unsigned l=ty; l<3&&a[tx][ty]==0; l++) {
					a[tx][l]=a[tx][l+1];
					a[tx][l+1]=0;
				}
			}
		}
	}
	for(unsigned char tx=0; tx<4; tx++) {
		for(unsigned char ty=0; ty<3; ty++) {
			if(a[tx][ty]==a[tx][ty+1]&&a[tx][ty]!=0) {
				a[tx][ty]+=a[tx][ty+1];
				a[tx][ty+1]=0;
				for(unsigned l=ty+1; l<3; l++) {
					a[tx][l]=a[tx][l+1];
					a[tx][l+1]=0;
				}
				score+=16;
			}
		}
	}
	return;
}

void right() {
	for(unsigned char tx=0; tx<4; tx++) {
		for(unsigned char ty=3; ty>0; ty--) {
			for(unsigned char cnt=0; a[tx][ty]==0&&cnt<8; cnt++) {
				for(unsigned l=ty; l>0&&a[tx][ty]==0; l--) {
					a[tx][l]=a[tx][l-1];
					a[tx][l-1]=0;
				}
			}
		}
	}
	for(unsigned char tx=0; tx<4; tx++) {
		for(unsigned char ty=3; ty>0; ty--) {
			if(a[tx][ty]==a[tx][ty-1]&&a[tx][ty]!=0) {
				a[tx][ty]+=a[tx][ty-1];
				a[tx][ty-1]=0;
				for(unsigned l=ty-1; l>0; l--) {
					a[tx][l]=a[tx][l-1];
					a[tx][l-1]=0;
				}
				score+=16;
			}
		}
	}
	return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值