C语言实现2048小游戏
众所周知,推箱子、2048、贪吃蛇被称为大学编程课三大经典小项目。很多刚学编程的小伙伴都想写出一个自己的小游戏,这次介绍其中的2048。 游戏规则:WASD移动,V撤回,P主动结束游戏。 数字会显示颜色,不同数值颜色不尽相同。 可以设置游戏行列大小以及难度。 可在windows下运行,根据注释提示修改部分函数名之后也能在linux下运行。
移动算法思路如图:
源代码如下:欢迎讨论。
#include"tzfe.h"
int ttafe[X][Y] = {0};
int efatt[X][Y] = {0};
int condition = 0;
int allcond = 0;
char congame ;
#define NODE "\033[0m"
#define WRITE "\033[1;36;40m"
#define YELLOW "\033[1;33;40m"
#define BULE "\033[1;34;40m"
#define GREEN "\033[1;32;40m"
#define YRED "\033[1;35;40m"
#define RED "\033[1;31;40m"
void random_num_posi(int a[X][Y]){
int m,n,flag = 0;
int k;
for(m=0;m<X;m++){
for(n=0;n<Y;n++){
if(a[m][n] == 0)
flag ++;
}
}
if(flag == 0)
return ;
srand((unsigned int)time(NULL));
do{
m = rand()%X;
n = rand()%Y;
if(a[m][n] == 0){
while(1){
k = rand()%3*2;
if(k != 0){
a[m][n] = k;
return ;
}
}
}
}
while(1);
}
void boundary_display(int x){
for(int i=0; i<x; i++){
printf("--------");
}
printf("\n");
}
void printf_myarray(int a[X][Y]){
int i,j;
for(i=0;i<X;i++){
boundary_display(X);
for(j=0;j<Y;j++){
printf("|");
if(a[i][j] == 0)
printf("%d\t",a[i][j]);
else if(a[i][j] == 2 || a[i][j] == 4)
printf("%s%d%s\t",WRITE,a[i][j],NODE);
else if(a[i][j] == 8 || a[i][j] == 16)
printf("%s%d%s\t",YELLOW,a[i][j],NODE);
else if(a[i][j] == 32 || a[i][j] == 64)
printf("%s%d%s\t",BULE,a[i][j],NODE);
else if(a[i][j] == 128 || a[i][j] == 256)
printf("%s%d%s\t",GREEN,a[i][j],NODE);
else if(a[i][j] == 512 || a[i][j] == 1024)
printf("%s%d%s\t",YRED,a[i][j],NODE);
else
printf("%s%d%s\t",RED,a[i][j],NODE);
}
printf("|\n");
}
boundary_display(X);
}
void move_left_array(int a[X][Y]){
int doub[Y] = {0};
int i,j;
for(i=0;i<X;i++){
for(j=0;j<Y;j++){
if(a[i][j] != 0) {
int jx;
for(jx=j+1;jx<Y;jx++){
if(a[i][jx] != 0){
if(a[i][j] == a[i][jx]){
allcond = 1;
a[i][j] = a[i][j] + a[i][jx];
a[i][jx] = 0;
}
break;
}
}
}
}
int jy = 0;
for(j=0;j<Y;j++){
if(a[i][j] != 0){
if(jy != j)
allcond = 1;
doub[jy] = a[i][j];
a[i][j] = 0;
a[i][jy] = doub[jy];
jy ++;
}
}
}
}
void move_right_array(int a[X][Y]){
int doub[Y] = {};
int i,j;
for(i=0;i<X;i++){
for(j=Y-1;j>=0;j--){
if(a[i][j] != 0){
int jx;
for(jx=j-1;jx>=0;jx--){
if(a[i][jx] != 0){
if(a[i][j] == a[i][jx]){
allcond = 1;
a[i][j] = a[i][j] + a[i][jx];
a[i][jx] = 0;
}
break;
}
}
}
}
int jy = Y-1;
for(j=Y-1;j>=0;j--){
if(a[i][j] != 0){
if(jy != j)
allcond = 1;
doub[jy] = a[i][j];
a[i][j] = 0;
a[i][jy] = doub[jy];
jy --;
}
}
}
}
void move_up_array(int a[X][Y]){
int doub[X] = {};
int i,j;
for(j=0;j<Y;j++){
for(i=0;i<X;i++){
if(a[i][j] != 0){
int ix;
for(ix=i+1;ix<X;ix++){
if(a[ix][j] != 0){
if(a[ix][j] == a[i][j]){
allcond = 1;
a[i][j] = a[i][j] + a[ix][j];
a[ix][j] = 0;
}
break;
}
}
}
}
int iy = 0;
for(i=0;i<X;i++){
if(a[i][j] != 0){
if(iy != i)
allcond = 1;
doub[iy] = a[i][j];
a[i][j] = 0;
a[iy][j] = doub[iy];
iy ++;
}
}
}
}
void move_down_array(int a[X][Y]){
int doub[X] = {};
int i,j;
for(j=0;j<Y;j++){
for(i=X-1;i>=0;i--){
if(a[i][j] != 0){
int ix;
for(ix=i-1;ix>=0;ix--){
if(a[ix][j] != 0){
if(a[ix][j] == a[i][j]){
allcond = 1;
a[i][j] = a[i][j] + a[ix][j];
a[ix][j] = 0;
}
break;
}
}
}
}
int iy = X-1;
for(i=X-1;i>=0;i--){
if(a[i][j] != 0){
if(iy != i)
allcond = 1;
doub[iy] = a[i][j];
a[i][j] = 0;
a[iy][j] = doub[iy];
iy --;
}
}
}
}
void move_all_direction(int a[X][Y],int b[X][Y]){
while(1){
char c = getch();
if(c == 'w' || c == 'W'){
copy_two_array(a,b);
move_up_array(a);
return;
}
else if(c == 's' || c == 'S'){
copy_two_array(a,b);
move_down_array(a);
return;
}
else if(c == 'a' || c == 'A'){
copy_two_array(a,b);
move_left_array(a);
return;
}
else if(c == 'd' || c == 'D'){
copy_two_array(a,b);
move_right_array(a);
return;
}
else if(c == 'v' || c == 'V'){
copy_two_array(b,a);
return;
}
else if(c == 'p' || c == 'P')
exit(-1);
else
continue;
}
}
int init_game_diffic(){
int a;
system("cls");
printf(" =========================================================\n");
printf(" |----------------------欢迎来到2048----------------------|\n");
printf(" |-----------游戏难度分为适中、困难、地狱三种难度-----------|\n");
printf(" |------通过输入WASD来移动,撤回按V,退出按P(支持大小写)----|\n");
printf(" |------------------输入1、2、3对应三种难度----------------|\n");
printf(" |-----------------输入其他数字则是无尽模式----------------|\n");
printf(" =========================================================\n");
scanf("%d",&a);
system("cls");
if(a >= 1 && a <= 2)
a = a * 2048;
else if(3 == a)
a = 8192;
else
a = 9999;
return a;
}
void success_or_file(int a[X][Y],int max){
int m,n,flag = 0;
for(m=0;m<X;m++){
for(n=0;n<Y;n++){
if(a[m][n] == 0)
flag ++;
if(a[m][n] == max){
condition ++;
switch (max){
case 2048:printf("2048!经典的一次胜利!\n");break;
case 4096:printf("继续向前!\n");break;
case 8192:printf("登峰造极!\n");break;
default :break;
}
return ;
}
}
}
if(flag == 0){
for(m=0;m<X-1;m++){
for(n=0;n<Y-1;n++){
if(a[m][n] == a[m][n+1])
return ;
else if(a[m][n] == a[m+1][n])
return ;
}
}
if(a[X-1][Y-1] == a[X-2][Y-1] || a[X-1][Y-1] == a[X-1][Y-2])
return ;
condition ++ ;
printf("你输了,再接再厉!\n");
}
}
void crdit_game_all(int a[X][Y]){
int i,j,sum=0;
for(i=0;i<X;i++){
for(j=0;j<Y;j++)
sum = sum + a[i][j];
}
printf("您的得分是:%d\n",sum);
}
void copy_two_array(int a[X][Y],int b[X][Y]){
int i,j;
for(i=0;i<X;i++){
for(j=0;j<Y;j++)
b[i][j] = a[i][j];
}
}
void ttafe_game(){
int max = init_game_diffic();
random_num_posi(ttafe);
copy_two_array(ttafe,efatt);
printf_myarray(ttafe);
crdit_game_all(ttafe);
while(1){
move_all_direction(ttafe,efatt);
if(allcond == 1){
random_num_posi(ttafe);
allcond = 0;
}
system("cls");
printf_myarray(ttafe);
crdit_game_all(ttafe);
success_or_file(ttafe,max);
if(condition != 0)
return ;
}
}
#ifndef __TZFE_H__
#define __TZFE_H__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#define X 4
#define Y 4
void random_num_posi(int a[X][Y]);
void boundary_display(int x);
void printf_myarray(int a[X][Y]);
void move_left_array(int a[X][Y]);
void move_right_array(int a[X][Y]);
void move_up_array(int a[X][Y]);
void move_down_array(int a[X][Y]);
void move_all_direction(int a[X][Y],int b[X][Y]);
int init_game_diffic();
void success_or_file(int a[X][Y],int max);
void crdit_game_all(int a[X][Y]);
void copy_two_array(int a[X][Y],int b[X][Y]);
void ttafe_game();
#endif
#include <stdio.h>
#include <stdlib.h>
#include "tzfe.h"
#include <time.h>
#include <string.h>
int main()
{
ttafe_game();
return 0;
}
|