C language simple game of 2048

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43669941/article/details/86760005

main.c

#include <stdio.h>
#include "2048.h"

int main(void) {
	//初始化
	int arr[ROW][COL] = {0};
	srand((unsigned int)time(NULL));
	//欢迎界面
	startInterface();
	//运行游戏
	runingGame(arr);
	
	return 0;
}

2048.h

#ifndef _2048_H_
#define _2048_H_
#define ROW 4
#define COL 4

void startInterface(void);//欢迎界面
void showArr(int arr[ROW][COL]);//打印游戏界面
int randNum(int arr[ROW][COL]);//生成随机数
void upGo(int arr[ROW][COL]);//上移
void upCombine(int arr[ROW][COL]);//上结合
void downGo(int arr[ROW][COL]);//下移
void downCombine(int arr[ROW][COL]);//下结合
void leftGo(int arr[ROW][COL]);//左移
void leftCombine(int arr[ROW][COL]);//左结合
void rightGo(int arr[ROW][COL]);//右移
void rightCombine(int arr[ROW][COL]);//右结合
void moveArr(int arr[ROW][COL]);//总移动
void runingGame(int arr[ROW][COL]);//运行游戏
int winJudge(int arr[ROW][COL]);//判断胜利

#endif

2048.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include "2048.h"

void startInterface(void) {
	system("cls");
	printf("\n\n");
	printf("*----------------------------*\n");
	printf("|             2048           |\n");
	printf("|                            |\n");
	printf("|      用wasd控制上下左右    |\n");
	printf("*----------------------------*\n");
	printf("\n\n");
	printf("按任意键开始游戏...");
	//getchar();
	getch();
}
int randNum(int arr[ROW][COL]) {
	int row=0, col=0, flag=0, temp=0;
	//判断是否填满
	while(1) {
		for(row=0; row<ROW; row++) {
			for(col=0; col<COL; col++) {
				if(arr[row][col]==0) {
					temp=1;
					break;
				}
			}
			if(temp) break;
		}
		if(temp) break;
		//没有一个数为0,即没有空位置
		return 1;
	}
	//产生随机位置
	while(1){
		row = rand()%4;
		col = rand()%4;
		if(arr[row][col]==0) {
			flag = rand()%10;
			if(flag) {
				arr[row][col] = 2;
			} else {
				arr[row][col] = 4;
			}
			break;
		}
	}
	return 0;
}
void runingGame(int arr[ROW][COL]) {
	while(1) {
		//判断是否占满
		if(randNum(arr)) {
			if(winJudge(arr)==1) {
				showArr(arr);
				printf("游戏胜利");
				getch();
				return;
			} else if(winJudge(arr)==2) {
				showArr(arr);
				printf("游戏结束");
				getch();
				return;
			}
		}
		showArr(arr);
		moveArr(arr);
	}
}
void showArr(int arr[ROW][COL]) {
	int row=0, col=0;
	system("cls");
	printf("\n\n ____ ____ ____ ____\n");
	for(row=0; row<ROW; row++) {
			//第一排(竖线和空格)
			printf("|    |    |    |    |\n");
			//第二排(竖线和数字)
			for(col=0; col<COL; col++) {
				if(arr[row][col]!=0) {
					printf("|%4d", arr[row][col]);
				} else {
					printf("|    ");
				}
			}
			printf("|\n");
			//第三排(竖线和横线)
			printf("|____|____|____|____|\n");
	}
	printf("\n\n");
}
void upGo(int arr[ROW][COL]) {
	int row=0, col=0, count=0;
	for(row=1; row<ROW; row++) {
		for(col=0; col<COL; col++) {
			while(1) {
				if(row!=0&&arr[row-1][col]==0) {
					arr[row-1][col] = arr[row][col];
					arr[row][col] = 0;
					row--;
					count++;
				} else {
					row += count;	
					count = 0;
					break;
				}
			}
		}
		
	}
}
void upCombine(int arr[ROW][COL]) {
	int row=0, col=0;
	for(row=1; row<ROW; row++) {
		for(col=0; col<COL; col++) {
			if(arr[row][col]==arr[row-1][col]) {
				arr[row-1][col] = arr[row][col]*2;
				arr[row][col] = 0;
			}
		}
	}
}
void downGo(int arr[ROW][COL]) {
	int row=0, col=0, count=0;
	for(row=2; row>=0; row--) {
		for(col=0; col<COL; col++) {
			while(1) {
				if(row!=3&&arr[row+1][col]==0) {
					arr[row+1][col] = arr[row][col];
					arr[row][col] = 0;
					row++;
					count++;
				} else {
					row -= count;	
					count = 0;
					break;
				}
			}
		}
		
	}
}
void downCombine(int arr[ROW][COL]) {
	int row=0, col=0;
	for(row=2; row>=0; row--) {
		for(col=0; col<COL; col++) {
			if(arr[row][col]==arr[row+1][col]) {
				arr[row+1][col] = arr[row][col]*2;
				arr[row][col] = 0;
			}
		}
	}
}
void leftGo(int arr[ROW][COL]) {
	int row=0, col=0, count=0;
	for(col=1; col<COL; col++) {
		for(row=0; row<ROW; row++) {
			while(1) {
				if(col!=0&&arr[row][col-1]==0) {
					arr[row][col-1] = arr[row][col];
					arr[row][col] = 0;
					col--;
					count++;
				} else {
					col += count;	
					count = 0;
					break;
				}
			}
		}
		
	}
}
void leftCombine(int arr[ROW][COL]) {
	int row=0, col=0;
	for(col=1; col<COL; col++) {
		for(row=0; row<ROW; row++) {
			if(arr[row][col]==arr[row][col-1]) {
				arr[row][col-1] = arr[row][col]*2;
				arr[row][col] = 0;
			}
		}
	}
}
void rightGo(int arr[ROW][COL]) {
	int row=0, col=0, count=0;
	for(col=2; col>=0; col--) {
		for(row=0; row<ROW; row++) {
			while(1) {
				if(col!=3&&arr[row][col+1]==0) {
					arr[row][col+1] = arr[row][col];
					arr[row][col] = 0;
					col++;
					count++;
				} else {
					col -= count;	
					count = 0;
					break;
				}
			}
		}
		
	}
}
void rightCombine(int arr[ROW][COL]) {
	int row=0, col=0;
	for(col=2; col>=0; col--) {
		for(row=0; row<ROW; row++) {
			if(arr[row][col]==arr[row][col+1]) {
				arr[row][col+1] = arr[row][col]*2;
				arr[row][col] = 0;
			}
		}
	}
}
void moveArr(int arr[ROW][COL]) {
	char ord;
	int flag=0;
	while(!flag) {
		ord=getch();
		switch(ord) {
		case 'w':
			//前进
			upGo(arr);
			//合并
			upCombine(arr);
			//前进
			upGo(arr);
			flag=1;
			break;
		case 's':
			//前进
			downGo(arr);
			//合并
			downCombine(arr);
			//前进
			downGo(arr);
			flag=1;
			break;
		case 'a':
			//前进
			leftGo(arr);
			//合并
			leftCombine(arr);
			//前进
			leftGo(arr);
			flag=1;
			break;
		case 'd':
			//前进
			rightGo(arr);
			//合并
			rightCombine(arr);
			//前进
			rightGo(arr);
			flag=1;
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	}
}
int winJudge(int arr[ROW][COL]) {
	int row=0, col=0;
	//判断2048
	for(row=0; row<ROW; row++) {
		for(col=0; col<COL; col++) {
			if(arr[row][col]==2048) return 1;
		}
	}
	//判断游戏结束(界面占满且四个方向移动后都不能合并)
	//判断向上
	for(row=1; row<ROW; row++) {
		for(col=0; col<COL; col++) {
			if(arr[row][col]==arr[row-1][col]) return 0;
		}
	}
	//判断向下
	for(row=2; row>=0; row--) {
		for(col=0; col<COL; col++) {
			if(arr[row][col]==arr[row+1][col]) return 0;
		}
	}
	//判断向左
	for(col=2; col>=0; col--) {
		for(row=0; row<ROW; row++) {
			if(arr[row][col]==arr[row][col+1]) return 0;
		}
	}
	//判断向右
	for(col=2; col>=0; col--) {
		for(row=0; row<ROW; row++) {
			if(arr[row][col]==arr[row][col+1]) return 0;
		}
	}
	
	//既不能胜利又不能结合则游戏结束
	return 2;
}

 

Guess you like

Origin blog.csdn.net/weixin_43669941/article/details/86760005