C语言-实现2048控制台版和图形界面版

2017.1 远古demo
控制台版
效果
EasyX图形界面版
在这里插入图片描述

代码段 https://github.com/PorYoung/PublicRepository/blob/master/2048_console.cpp

/************************************************************************************************************************
文件名称:main.c
文件描述:实现控制台的2048逻辑代码
编译环境:vs2015
最后修改:
<2017.1.8> <最后一次修改23:38 依然存在算法上的问题,在上下左右当中> <修改者:PorYoung>
<最近修改:2017.1.9 算法问题待定,另发现生成2和4时存在bug,整体有待优化><修改者:PorYoung>
<优化思路:①代码优化,减少函数个数;②功能优化:增加撤销功能,自动走一步功能;③算法优化:生成算法优化、移动、加法优化>
<唯一遗憾的是算法没有模块化,代码没有精简,先把存货放出来,之后有时间再慢慢修改完善吧>
*************************************************************************************************************************/

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

void Add(int dir);
void Print();
int map[4][4] = { { 0,0,0,0 }, };
void MoveRight();
void MoveLeft();
void MoveUp();
void MoveDown();

void Print()
{
	printf("\t\t\t\t*Welcome to 2048*\n");
	printf("\t\t*use     to play; press \"esc\" to reset*\n");
	printf("\t\t\t┏━━━┳━━━┳━━━┳━━━┓\n");
	printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[0][0], map[0][1], map[0][2], map[0][3]);
	printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
	printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
	printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
	printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[1][0], map[1][1], map[1][2], map[1][3]);
	printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
	printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
	printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[2][0], map[2][1], map[2][2], map[2][3]);
	printf("\t\t\t┣━━━╋━━━╋━━━╋━━━┫\n");
	printf("\t\t\t┃\t┃\t┃\t┃\t┃\n");
	printf("\t\t\t┃%4d\t┃%4d\t┃%4d\t┃%4d\t┃\n", map[3][0], map[3][1], map[3][2], map[3][3]);
	printf("\t\t\t┗━━━┻━━━┻━━━┻━━━┛\n");

}

void Add(int dir) //第一次进入界面 dir=0; 上 dir=1; 下 dir=2; 左 dir=3; 右 dir=4
{
	srand((unsigned int)time(NULL));	//随机数
	int num, flag;	//2,4
	int row, col;
	row = rand() % 4;	//0-3
	col = rand() % 4;	//0-3
	flag = rand() % 2;	//flag 0 1
	if (flag == 0)
		num = 2;
	else
		num = 4;
	switch (dir)
	{
	case 0:
		map[row][col] = num; break;
	case 1://if (map[3][col] == 0)
		{
			map[3][col] = num;
		}
		else
		{
			for (col = 0; col < 4; col)
				if (map[3][col] == 0) break;
		}
		break;
	case 2://if (map[0][col] == 0)
		{
			map[0][col] = num;
		}
		else
		{
			for (col = 0; col < 4; col++)
			{
				if (map[0][col] == 0) break;
			}
		}
		break;
	case 3://if (map[row][14] == 0)
		{
			map[row][15] = num;
		}
		else
		{
			for (row = 0; row < 4; row++)
			{
				if (map[row][16] == 0) break;         /*有疑问!!!*/
			}
		}
		break;
	case 4://if (map[row][0] == 0)
		{
			map[row][0] = num;
		}
		else
		{
			for (row = 0; row < 4; row++)
			{
				if (map[row][0] == 0) break;
			}
		}
		break;
	}
}

//控制函数
void Move()
{
	char ch;
	int row, col;
	ch = _getch();
	switch (ch)
	{
	case 72://MoveUp();
		break;
	case 80://MoveDown();
		break;
	case 75://MoveLeft();
		break;
	case 77://MoveRight();
		break;
	case 27://重置
	{
		for (row = 0; row < 4; row++)
		{
			for (col = 0; col < 4; col++)
			{
				map[row][col] = 0;
			}
		}
		Add(0);
	}
	}
	system("cls");
}

void MoveUp()
{
	int temp, row, col;
	for (col = 0; col < 4; col++)
	{
		int n = 4;
		while (n--)
		{
			for (row = 0; row < 3; row++)
			{
				if (map[row][col] == 0)
				{
					for (temp = row; temp < 3; temp++)
					{
						map[temp][col] = map[temp + 1][col];
						map[temp + 1][col] = 0;
					}
				}
			}
		}
		//实现加法
		for (row = 0; row < 3; row++)
		{
			if (map[row][col] == map[row + 1][col])
			{
				map[row][col] = map[row][col] * 2;
				map[row + 1][col] = 0;
				for (temp = row + 1; temp < 3; temp++)
				{
					map[temp][col] = map[temp + 1][col];
					map[temp + 1][col] = 0;
				}
			}
		}
	}
	Add(1);
}

void MoveDown()
{
	int row, col, temp;
	for (col = 0; col < 4; col++)
	{
		int n = 4;
		while (n--)
		{
			for (row = 3; row > 0; row--)
			{
				if (map[row][col] == 0)
				{
					for (temp = row; temp > 0; temp--)
					{
						map[temp][col] = map[temp - 1][col];
						map[temp - 1][col] = 0;
					}

				}
			}
		}
		//加法操作
		for (row = 3; row > 0; row--)
		{
			if (map[row][col] == map[row - 1][col])
			{
				map[row][col] = map[row][col] * 2;
				map[row - 1][col] = 0;
				for (temp = row - 1; temp > 0; temp--)
				{
					map[temp][col] = map[temp - 1][col];
					map[temp - 1][col] = 0;
				}
			}
		}
	}

	Add(2);
}

void MoveLeft()
{
	int row, col, temp;
	for (row = 0; row < 4; row++)
	{
		int n = 4;
		while (n--)
		{
			for (col = 0; col < 4; col++)
			{
				if (map[row][col] == 0)
				{
					for (temp = col; temp < 3; temp++)
					{
						map[row][temp] = map[row][temp + 1];
						map[row][temp + 1] = 0;
					}
				}
			}
		}
		//加法操作
		for (col = 0; col < 3; col++)
		{
			if (map[row][col] == map[row][col + 1])
			{
				map[row][col] = map[row][col] * 2;
				map[row][col + 1] = 0;
				for (temp = col + 1; temp < 3; temp++)
				{
					map[row][temp] = map[row][temp + 1];
					map[row][temp + 1] = 0;
				}
			}
		}
	}
	Add(3);
}

void MoveRight()
{
	int row, col, temp;
	for (row = 0; row < 4; row++)
	{
		int n = 4;
		while (n--)
		{
			for (col = 3; col > 0; col--)
			{
				if (map[row][col] == 0)
				{
					for (temp = col; temp > 0; temp--)
					{
						map[row][temp] = map[row][temp - 1];
						map[row][temp - 1] = 0;
					}
				}
			}
		}
		//加法操作
		for (col = 3; col > 0; col--)
		{
			if (map[row][col] == map[row][col - 1])
			{
				map[row][col] = map[row][col] * 2;
				map[row][col - 1] = 0;
				for (temp = col - 1; temp > 0; temp--)
				{
					map[row][temp] = map[row][temp - 1];
					map[row][temp - 1] = 0;
				}
			}
		}
	}
	Add(4);
}

int main(void)
{
	printf("程序正在启动,请稍后......\n");
	Add(0);
	printf("按任意键开始游戏\n");
	_getch();
	Print();
	while (1)
	{
		Move();
		Print();
	}
	return 0;
}

/*************************************
工程名称:2048图形界面版
创建时间:2017.1.19
编译环境:VS2015
创建者:PorYoung
**************************************/


#include "graphics.h"
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "windows.h"
#include "resource.h" 
#include "stdio.h"

IMAGE bkImg;
int maps[4][4] = { 0 };
char str[4];
int temp = 0;		//游戏状态	

void gameShow();
int numShow();	//随机生成2或4
void gamePlay();
//void judge();

//4*4界面
void gameShow()
{
	if (temp == 0)
	{
		for (int i = 0; i < 4; i++)
			for (int j = 0; j < 4; j++)
				maps[i][j] = 0;
		int x, y;
		x = rand() % 10 % 4;
		y = rand() % 10 % 4;
		maps[x][y] = numShow();
		temp = 1;
	}
	BeginBatchDraw();
	//putimage(0, 0, &bkImg);
	for (int i = 1; i < 4; i++)
	{
		setlinecolor(RGB(125, 125, 125));
		line(86, 40 + 80 * i, 415, 40 + 80 * i);
		line(88 + 82 * i, 40, 88 + 82 * i, 358);
	}
	for (int i = 0, x = 106, y = 60; i < 4; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			sprintf(str, "%d", maps[i][j]);
			setbkmode(TRANSPARENT);
			settextcolor(RGB(25, 65, 125));
			settextstyle(40, 10, "微软雅黑");
			outtextxy(x, y, str);
			x += 80;
			if (x >= 400)
			{
				y += 82;
				x = 106;
			}
		}
	}
	EndBatchDraw();
}

int numShow()
{
	int r = rand() % 10, num;
	if (r < 5)	num = 2;
	else num = 4;
	return num;
}

void gamePlay()
{
	void upPlay();
	void downPlay();
	void leftPlay();
	void rightPlay();
	Sleep(100);
	char ch = _getch();
	switch (ch)
	{
	case 72:
		upPlay();
		break;	//上
	case 80:
		downPlay();
		break;	//下
	case 75:
		leftPlay();
		break;	//左
	case 77:
		rightPlay();
		break;	//右
	case 'r':
	case 'R':
		temp = 0;
		break;
	}
}

void upPlay()
{
	int row, col;

	//移动	
	for (col = 0; col < 4; col++)
	{
		int i = 3;
		while (i--)
		{
			for (row = 0; row < 3; row++)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row + 1][col];
					maps[row + 1][col] = 0;
				}
			}
		}
	}
	//相加
	for (col = 0; col < 4; col++)
	{
		int i = 3;
		while (i--)
		{
			for (row = 0; row < 3; row++)
			{
				if (maps[row][col] == maps[row + 1][col])
				{
					maps[row][col] *= 2;
					maps[row + 1][col] = 0;
				}
			}
		}
	}
	//移动
	for (col = 0; col < 4; col++)
	{
		int i = 3;
		while (i--)
		{
			for (row = 0; row < 3; row++)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row + 1][col];
					maps[row + 1][col] = 0;
				}
			}
		}
	}
	//新数字
	row = 3;		//加不加无所谓
	do
	{
		col = rand() % 10 % 4;
	} while (maps[row][col] != 0);
	maps[row][col] = numShow();
}

void downPlay()
{
	int row, col;

	//移动	
	for (col = 0; col < 4; col++)
	{
		int i = 3;
		while (i--)
		{
			for (row = 3; row > 0; row--)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row - 1][col];
					maps[row - 1][col] = 0;
				}
			}
		}
	}
	//相加
	for (col = 0; col < 4; col++)
	{
		int i = 3;
		while (i--)
		{
			for (row = 3; row > 0; row--)
			{
				if (maps[row][col] == maps[row - 1][col])
				{
					maps[row][col] *= 2;
					maps[row - 1][col] = 0;
				}
			}
		}
	}
	//移动
	for (col = 0; col < 4; col++)
	{
		int i = 3;
		while (i--)
		{
			for (row = 3; row > 0; row--)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row - 1][col];
					maps[row - 1][col] = 0;
				}
			}
		}
	}
	//新数字
	row = 0;
	do
	{
		col = rand() % 10 % 4;
	} while (maps[row][col] != 0);
	maps[row][col] = numShow();
}

void leftPlay()
{
	int row, col;

	//移动	
	for (row = 0; row < 4; row++)
	{
		int i = 3;
		while (i--)
		{
			for (col = 0; col < 3; col++)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row][col + 1];
					maps[row][col + 1] = 0;
				}
			}
		}
	}
	//相加
	for (row = 0; row < 4; row++)
	{
		int i = 3;
		while (i--)
		{
			for (col = 0; col < 3; col++)
			{
				if (maps[row][col] == maps[row][col + 1])
				{
					maps[row][col] *= 2;
					maps[row][col + 1] = 0;
				}
			}
		}
	}
	//移动
	for (row = 0; row < 4; row++)
	{
		int i = 3;
		while (i--)
		{
			for (col = 0; col < 3; col++)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row][col + 1];
					maps[row][col + 1] = 0;
				}
			}
		}
		//新数字
		col = 3;		//加不加无所谓
		do
		{
			row = rand() % 10 % 4;
		} while (maps[row][col] != 0);
		maps[row][col] = numShow();
	}
}

void rightPlay()
{
	int row, col;

	//移动	
	for (row = 0; row < 4; row++)
	{
		int i = 3;
		while (i--)
		{
			for (col = 3; col > 0; col--)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row][col - 1];
					maps[row][col - 1] = 0;
				}
			}
		}
	}
	//相加
	for (row = 0; row < 4; row++)
	{
		int i = 3;
		while (i--)
		{
			for (col = 3; col > 0; col--)
			{
				if (maps[row][col] == maps[row][col - 1])
				{
					maps[row][col] *= 2;
					maps[row][col - 1] = 0;
				}
			}
		}
	}
	//移动
	for (row = 0; row < 4; row++)
	{
		int i = 3;
		while (i--)
		{
			for (col = 3; col > 0; col--)
			{
				if (maps[row][col] == 0)
				{
					maps[row][col] = maps[row][col - 1];
					maps[row][col - 1] = 0;
				}
			}
		}
	}
	//新数字
	col = 0;
	do
	{
		row = rand() % 10 % 4;
	} while (maps[row][col] != 0);
	maps[row][col] = numShow();
}

/*void judge()
{
	//判断是否结束
	int row, col, num = 0;
	for (row = 0; row < 4; row += 3)
		for (col = 0; col < 4; col++)
		{
			if (maps[row][col] != 0) num++;
		}
	for (col = 0; col < 4; col += 3)
		for (row = 0; row < 4; row++)
		{
			if (maps[row][col] != 0) num++;
		}
	if (num == 16)
	{
		setbkcolor(RGB(255, 255, 255));
		clearrectangle(88, 48, 412, 358);
		settextcolor(RGB(255, 100, 155));
		outtextxy(200, 170, "游戏结束!");
		Sleep(3000);
		temp = 0;
		clearrectangle(88, 48, 412, 358);
	}
}*/                //有bug,可不要

int main()
{
	initgraph(500, 400);
	loadimage(&bkImg, "IMAGE", MAKEINTRESOURCE(IDR_IMAGE1), 500, 400);
	putimage(0, 0, &bkImg);
	srand((unsigned int)time(NULL));
	while (1)
	{
		gameShow();
		gamePlay();
		setbkcolor(RGB(255, 255, 255));
		clearrectangle(88, 48, 412, 358);
		setlinecolor(RED);
		rectangle(235, 365, 260, 385);	//准备增加点击事件
	}
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值