1. 程式人生 > >C語言實現最簡單的2048小遊戲

C語言實現最簡單的2048小遊戲

網上解釋很多了,直接上程式碼吧,這個功能很簡單,易於學習,後期有時間會完善功能

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>

#define Key_Up 0x4800 // 向上方向鍵   
#define Key_Down 0x5000 // 向下方向鍵
#define Key_Right 0x4d00 // 向右方向鍵
#define Key_Left 0x4b00 // 向左方向鍵


int map[4][4] = { 0 };
int check[4] = { 0 };  //判斷遊戲是否結束,如果都不為0,遊戲結束 
int i, j;

//
//顯示在螢幕上
// 
void print()
{
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			printf("%d\t", map[i][j]);
		}
		printf("\n");
	}
}


//
//向右移動
//
int moveRight() {
	int flag = 0;  //來標記是否整個map矩陣能否改變,以此判斷整個遊戲是否結束
	//這個迴圈的目的是為了做某一行的加減,如某一行為[2 2 4 4],經過以下運算就會變為[0 4 0 8];
	for (i = 0; i < 4; i++)
	{
		for (j = 4 - 1; j >= 0; j--)
		{
			int cell = map[i][j];
			if (cell != 0)
			{
				int k = j - 1;
				while (k >= 0)
				{
					int nextcell = map[i][k];
					if (nextcell != 0)
					{
						if (cell == nextcell)
						{
							flag = 1;
							map[i][j] += map[i][k];
							map[i][k] = 0;
						}
						k = -1;
						break;
					}
					k--;
				}
			}
		}
	}

	//上一步做的是資料加減,這一步對加減後的資料做資料處理
	//例如某一行經過上一步的資料為【0 4 0 8】,將會變為【0 0 4 8】
	for (i = 0; i < 4; i++)
	{
		for (j = 4 - 1; j > 0; j--)
		{
			int cell = map[i][j];
			if (cell == 0)
			{

				int k = j - 1;
				while (k >= 0)
				{
					int nextcell = map[i][k];
					if (nextcell != 0)
					{
						flag = 1;//當前元素為0,說明能移動,改變flag的值
						map[i][j] = nextcell;
						map[i][k] = 0;
						k = -1;
					}
					k--;
				}
			}
		}
	}
	if (flag)
		return 0;
	else
		return 4;  //遊戲結束 
}


//
//向左移動
//
int moveLeft() {
	int flag=0;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			int cell = map[i][j];//cell單詞用的不太恰當,表示當前元素,你可以採用更有意義的命名
			if (cell != 0)
			{
				int k = j + 1;
				while (k < 4)
				{
					int nextcell = map[i][k];
					if (nextcell != 0)
					{
						if (cell == nextcell)
						{
							flag = 1;//相鄰兩個元素相同,就說明能移動,所以改變flag的值
							map[i][j] += map[i][k];
							map[i][k] = 0;
						}
						k = 4;
						break;
					}
					k++;
				}
			}
		}
	}

	//修改部分:for迴圈中的i或者j的迴圈條件

	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4 - 1; j++)
		{
			int cell = map[i][j];
			if (cell == 0)
			{
				int k = j + 1;
				while (k < 4)
				{
					int nextcell = map[i][k];
					if (nextcell != 0)
					{
						flag = 1;
						map[i][j] = nextcell;
						map[i][k] = 0;
						k = 4;
					}
					k++;
				}
			}
		}
	}
	if (flag)
		return 0;
	else
		return 3;
}


//
//向下移動 
// 
int moveDown() {
	int flag=0;
	for (i = 4 - 1; i >= 0; i--)
	{
		for (j = 0; j < 4; j++)
		{
			int cell = map[i][j];

			if (cell != 0)
			{
				int k = i - 1;
				while (k >= 0)
				{
					int nextcell = map[k][j];
					if (nextcell != 0)
					{
						if (map[i][j] == map[k][j])
						{
							flag = 1;
							map[i][j] += map[k][j];
							map[k][j] = 0;
						}
						k = 0;
						break;
					}
					k--;
				}
			}
		}
	}

	//修改部分:for迴圈中的i或者j的迴圈條件
	for (i = 4 - 1; i > 0; i--)
	{
		for (j = 0; j < 4; j++)
		{
			int cell = map[i][j];
			if (cell == 0)
			{
				int k = i - 1;
				while (k >= 0)
				{
					int nextcell = map[k][j];
					if (nextcell != 0)
					{
						flag = 1;
						map[i][j] = nextcell;
						map[k][j] = 0;
						k = 0;
					}
					k--;
				}
			}
		}
	}
	if (flag)
		return 0;
	else
		return 2;
}

//
//向上移動
// 
int moveUp() {
	int flag=0;
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			int cell = map[i][j];

			if (cell != 0)
			{
				int k = i + 1;
				while (k < 4)
				{
					int nextcell = map[k][j];
					if (nextcell != 0)
					{
						if (cell == nextcell)
						{
							flag = 1;
							map[i][j] += map[k][j];
							map[k][j] = 0;
						}
						k = 4;
						break;
					}
					k++;
				}
			}
		}
	}

	//修改部分:for迴圈中的i或者j的迴圈條件
	for (i = 0; i < 4 - 1; i++)
	{
		for (j = 0; j < 4; j++)
		{
			int cell = map[i][j];
			if (cell == 0)
			{

				int k = i + 1;
				while (k < 4)
				{
					int nextcell = map[k][j];
					if (nextcell != 0)
					{
						flag = 1;
						map[i][j] = nextcell;
						map[k][j] = 0;
						k = 4;
					}
					k++;
				}
			}
		}
	}
	if (flag==0)
		return 0;
	else
		return 1;
}


//
//隨機生成在空格上生成2或者4,並且判斷是否遊戲結束 
//
int randInteger()  //有缺陷,【0 0 0 0】
{
	for (i = 0; i < 4; i++)
	{
		for (j = 0; j < 4; j++)
		{
			if (map[i][j] == 0)
			{
				map[i][j] = (rand() % 2 + 1) * 2;   //
				return 0;
			}
		}
	}
	return 0;
}



int main()
{
	print();
	char key;
	while (1)
	{
		printf("上:w;下:s;左:a;右:d;退出:q\n");
		printf("請輸入要執行的操作:");
		scanf("%c", &key);
		switch (key)
		{
		case 'w':
		{
				randInteger();
				moveUp();
				printf(" 向上方向鍵被按下\n");
				print();
				break;
		}
		case 's':
		{
			randInteger();
			moveDown();
			printf(" 向下方向鍵被按下\n");
			print();
			break;
		}
		case 'a':
		{
			randInteger();
			moveLeft();
			printf(" 向左方向鍵被按下\n");
			print();
			break;
		}
		case 'd':
		{
			randInteger();
			moveRight();
			printf(" 向右方向鍵被按下\n");
			print();
			break;
		}
		case 'q':
			return 0;
		}
	}
	return 0;
}