2048 控制台版(c实现)

这是效果图

用c++写的,其实就是用了c++的输入输出,剩下全是C的内容

代码中有详解,很简单,欢迎借鉴和交流

 

#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include <conio.h>   //为了读取方向键
#include <iomanip>	//设置控制台填充字符等
using namespace std;
int score = 0;
int map[4][4];    //棋盘

void showMap()    //显示棋盘
{
	cout << setw(46) << "2048 by DoubleCake" << endl;
	cout << setw(50) << " |-----------------------|" << endl;
	for (int i = 0; i <= 3; i++)
	{
		cout << setw(24) << "";
		for (int j = 0; j <= 3; j++)
		{
			if (map[i][j] == 0)
				cout << setw(2) << "|" << setw(4) << " ";
			else
				cout << setw(2) << "|" << setw(4) << map[i][j];

			if (j == 3)
			{
				cout << setw(2) << "|" << endl;
				cout << setw(50) << " |-----------------------|" << endl;
			}
		}
	}
}

void randNum()	//从随机位置产生数
{
	int m = rand() % 4;
	int n = rand() % 4;
	while (map[m][n]){
		m = rand() % 4;
		n = rand() % 4;
	}
	map[m][n] = 2;
}
void startGame() //开始游戏
{
	memset(map, 0, sizeof(map));
	randNum();
	showMap();
}
int moveUp()	//上移
{
	int i, j;
	int res = 0;//用来记录是否发生移动
	for (j = 0; j < 4; j++){
		//先补一次空位
		for (i = 1; i < 4; i++){
			if (map[i][j] && !map[i - 1][j]){
				map[i - 1][j] = map[i][j];
				map[i][j] = 0;
				//把空位补到底
				if (i > 1)
					i -= 2;
				res = 1;
			}
		}
		//从根部开始,相同的合并
		for (i = 1; i < 4; i++){
			if (map[i][j] && map[i][j] == map[i - 1][j]){
				map[i - 1][j] *= 2;
				score += map[i][j];
				map[i][j] = 0;
				res = 1;
			}
		}
		//再补一次空位
		for (i = 1; i < 4; i++){
			if (map[i][j] && !map[i - 1][j]){
				map[i - 1][j] = map[i][j];
				map[i][j] = 0;
				//把空位补到底
				if (i > 1)
					i -= 2;
			}
		}
	}
	return res;
}

int moveDown()	//下移
{
	int i, j;
	int res = 0;
	for (j = 0; j < 4; j++){
		for (i = 2; i >= 0; i--){
			if (map[i][j] && !map[i + 1][j]){
				map[i + 1][j] = map[i][j];
				map[i][j] = 0;
				if (i < 2)
					i += 2;
				res = 1;
			}
		}
		for (i = 2; i >= 0; i--){
			if (map[i][j] && map[i][j] == map[i + 1][j]){
				map[i + 1][j] *= 2;
				score += map[i][j];
				map[i][j] = 0;
				res = 1;
			}
		}
		for (i = 2; i >= 0; i--){
			if (map[i][j] && !map[i + 1][j]){
				map[i + 1][j] = map[i][j];
				map[i][j] = 0;
				if (i < 2)
					i += 2;
			}
		}
	}
	return res;
}

int moveLeft()
{
	int i, j;
	int res = 0;
	for (i = 0; i < 4; i++){
		for (j = 1; j < 4; j++){
			if (map[i][j] && !map[i][j - 1]){
				map[i][j - 1] = map[i][j];
				map[i][j] = 0;
				if (j > 1)
					j -= 2;
				res = 1;
			}
		}
		for (j = 1; j < 4; j++){
			if (map[i][j] && map[i][j] == map[i][j - 1]){
				map[i][j - 1] *= 2;
				score += map[i][j];
				map[i][j] = 0;
				res = 1;
			}
		}
		for (j = 1; j < 4; j++){
			if (map[i][j] && !map[i][j - 1]){
				map[i][j - 1] = map[i][j];
				map[i][j] = 0;
				if (j > 1)
					j -= 2;
			}
		}
	}
	return res;
}

int moveRight()
{
	int i, j;
	int res = 0;
	for (i = 0; i < 4; i++){
		for (j = 2; j >= 0; j--){
			if (map[i][j] && !map[i][j + 1]){
				map[i][j + 1] = map[i][j];
				map[i][j] = 0;
				if (j < 2)
					j += 2;
				res = 1;
			}
		}
		for (j = 2; j >= 0; j--){
			if (map[i][j] && map[i][j] == map[i][j + 1]){
				map[i][j + 1] *= 2;
				score += map[i][j];
				map[i][j] = 0;
				res = 1;
			}
		}
		for (j = 2; j >= 0; j--){
			if (map[i][j] && !map[i][j + 1]){
				map[i][j + 1] = map[i][j];
				map[i][j] = 0;
				if (j < 2)
					j += 2;
			}
		}
	}
		return res;
}

int maxNum()  //棋盘最大数
{
	int max = map[0][0];
	for (int i = 0; i <= 3; i++)
	for (int j = 0; j <= 3; j++)
	if (map[i][j]>max)
		max = map[i][j];
	return max;
}
int Win()    //判断是否胜利
{
	int flag = 0;
	if (maxNum() == 2048)
	{
		cout << setw(45) << "You Win!" << endl;
		flag = 1;
	}
	return flag;
}
int GameOver()       //判断是否游戏结束
{
	int flag = 1;
	int i, j;
	//如果有空位就可以继续
	for (i = 0; i < 4; i++)
	for (j = 0; j < 4; j++)
	if (!map[i][j])
		flag = 0;
	//如果没有空位但是有相邻相同的数也可以继续
	if (flag == 1){
		for (i = 0; i < 4; i++){
			for (j = 0; j < 4; j++){
				if (i != 0 && map[i][j] == map[i - 1][j])
					flag = 0;
				if (i != 3 && map[i][j] == map[i + 1][j])
					flag = 0;
				if (j != 0 && map[i][j] == map[i][j - 1])
					flag = 0;
				if (j != 3 && map[i][j] == map[i][j + 1])
					flag = 0;
			}
		}
	}
	if (flag == 1)
		cout << setw(43) << "Game Over!" << endl;
	return flag;
}
void keydown() //读取方向
{
		int ch = _getch();
		srand((unsigned)time(NULL));
		switch (ch)
		{
		case 72:	// top
			if (moveUp()){
				randNum();
				system("cls");
				showMap();
			} 
			break;
		case 75:	 // left
			if (moveLeft()){
				randNum();
				system("cls");
				showMap();
			} 
			break;
		case 77:	// right
			if (moveRight()){
				randNum();
				system("cls");
				showMap();
			} 
			break;
		case 80:	// down
			if (moveDown()){
				randNum();
				system("cls");
				showMap();
			} 
			break;
		default:
			break;
		}
}
int main() //主函数
{
	system("color f9");
	int makesure = 1;	//游戏结束后是否继续游戏
	while (makesure)
	{
		system("cls");
		startGame();
		while (Win() + GameOver() == 0)
		{
			keydown();
		}
		cout << setw(43) << "你的最后成绩为:" << score << endl;
		cout << setw(60) << "若要重新开始游戏请输入1,若要结束请输入0。" << endl;
		cin >> makesure;
		while (makesure != 1 && makesure != 0)
		{
			cout << "输入不正确,请重新输入!" << endl;
			cin >> makesure;
		}
	}
	cout << "再见!" << endl;
	system("pause");
	return 0;
}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值