控制上实现的连连看

连连看游戏,主要的分几个部分:1,数据的生成。2,数据的显示。3,判断是否消除。4,判断游戏结束。


1,数据的生成:按一定的规律产生数,然后随机生成两个位置,然后交换两个数之间的位置。

2,数据的显示:根据数组位置的数显示相应的字符。

3,判断是否消除:用BFS判断是否存在超过的两个拐弯,超过则不能消除,数组中的数变成0,否则能消除。

4,判断游戏结束:判断数组中是否存在数,存在则游戏继续,否则游戏结束。

游戏说明:

鼠标点击两个小字符,相同则消除,不相同,则不消除。点击鼠标右键,实现重排。


头文件:Game.h

#ifndef _GAME_H
#define _GAME_H
//int sence[6][6];			   
const int C = 8; //游戏行数 
const int R = 8; //游戏列数 
/*********************************************************
*作用:初始化数据 
*参数:无 
*返回值: 无 
**********************************************************/
void initData();
/*********************************************************
*作用:显示数据 
*参数:无
*返回值: 无
**********************************************************/
void display();
/*********************************************************
*作用:判断两张图片是否可以消除
*参数:int x1 第一张图的横坐标 
*	   int y1 第一张图的纵坐标
*      int x2 第二张图的横坐标
*	   int y2 第二张图的纵坐标
*返回值: 如果可以消除,返回 1,否则返回 0 
**********************************************************/
int canClear(int x1, int y1, int x2, int y2);
/*********************************************************
*作用:判断是否全部消除 
*参数:无 
*返回值: 如果全部消除,返回 1,否则返回 0 
**********************************************************/
int allClear();
/*********************************************************
*作用:搜索两张图直接的路径,判断是否可以消除 
*参数:int x1 第一张图的横坐标 
*	   int y1 第一张图的纵坐标
*      int x2 第二张图的横坐标
*	   int y2 第二张图的纵坐标
*返回值: 如果可以消除,返回1,否则返回0 
**********************************************************/
int bfs(int x1, int y1, int x2, int y2);
/**********************************************************
*作用:清屏 
*参数:无
*返回值: 无
***********************************************************/
void clean();
/**********************************************************
*作用:重置,避免死局 
*参数:无
*返回值: 无
***********************************************************/
void change();

#endif

cpp文件:Game.cpp

#include "Game.h"
#include <iostream>
#include <ctime>
#include <windows.h>
#include <cstring>
#include <cstdlib>
#include <queue>
using namespace std;

int sence[C][R];
int xy[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
int ch[10] = {1,2,3,4,5,6,11,12,14,15};
class Data
{
	public:
		int x, y;
		int c, dir;
};
void initData()
{
	memset(sence, 0, sizeof(sence));
	for(int i = 1; i <= C - 2; i++)
		for(int j = 1; j <= R - 2; j++)
		{
			sence[i][j] = j;
		}
	srand(time(NULL));
	for(int i = 0; i < (C - 2) * (C - 2) * 2; i++)
	{
		int x1 = (rand() % (C - 2)) + 1;
		int y1 = (rand() % (R - 2)) + 1;
		int x2 = (rand() % (C - 2)) + 1;
		int y2 = (rand() % (R - 2)) + 1;
		int temp = sence[x1][y1];
		sence[x1][y1] = sence[x2][y2];
		sence[x2][y2] = temp;
	}
}
void clean()
{
	for(int i = 0; i < 5 * C; i++)
	{
		for(int j = 0; j < 5 * R; j++)
			cout << "  ";
	}
}
void change()
{
	srand(time(NULL));
	for(int i = 0; i < (C - 2) * 4; i++)
	{
		int x1 = (rand() % (C - 2)) + 1;
		int y1 = (rand() % (R - 2)) + 1;
		int x2 = (rand() % (C - 2)) + 1;
		int y2 = (rand() % (R - 2)) + 1;
		int temp = sence[x1][y1];
		sence[x1][y1] = sence[x2][y2];
		sence[x2][y2] = temp;
	}
} 
void display()
{
	int i, j;
	HANDLE  hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);         //获取输出句柄 
	SetConsoleTextAttribute(hConsole, FOREGROUND_RED);  //改变颜色 
	for(i = 0; i < R / 2; i++)
		cout << ' ';
	cout << "  ~ 连连看游戏 ~" << endl;
	SetConsoleTextAttribute(hConsole, 15);              //设置回原来的颜色 
	cout << "     ";
	for(i = 1; i <= R - 2; i++)
	{
		cout << i << "   ";
	}
	for(i = 0; i < C; i++)
	{
		if(i != 0 && i != C - 1)
			cout << i;
		for(j = 0; j < R; j++)
		{
			if(sence[i][j])
				cout << char(ch[sence[i][j] - 1]) << " | ";
			else
			{
				if(i != 0 && i != C - 1 && j != R - 1)
					cout << "  | ";
			}
		}
		cout << endl;
		if(i != C - 1)
		{
			for(int k = 0; k < 4 * R - 4; k++)
				cout << "-";
			cout << endl;
		}
	}
}
int bfs(int x1, int y1, int x2, int y2)
{
	queue <Data> Que;
	Data tem, D;
	tem.x = x1; tem.y = y1; tem.c = 0; tem.dir = -1;
	Que.push(tem);
	while(!Que.empty())
	{
		D = Que.front();
		Que.pop();
		if(D.x == x2 && D.y == y2)
			return 1;
		for(int i = 0; i < 4; i++)
		{
			tem.x = D.x + xy[i][0];
			tem.y = D.y + xy[i][1];
			if(D.dir < 0)
			{
				tem.dir = i;
				tem.c = 0;
			}
			else if(D.dir == i)
			{
				tem.dir = D.dir;
				tem.c = D.c;
			}
			else
			{
				tem.dir = i;
				tem.c = D.c + 1;
			}
			if(tem.x >= 0 && tem.x < C && tem.y >= 0 && tem.y < R && tem.c <= 2)
			{
				if(sence[tem.x][tem.y] == 0 || (tem.x == x2 && tem.y == y2))
				{
				//	b[tem.x][tem.y] = 1;
					Que.push(tem);
				}
			}
		}
	}
	return 0;
}
int canClear(int x1, int y1, int x2, int y2)
{
	if((x1 == x2 && y1 == y2) || sence[x1][y1] != sence[x2][y2])
		return 0;
/*	if((x1 == x2 && abs(y1 - y2) == 1) || (y1 == y2 && abs(x1 - x2) == 1))
		return 1;*/
	if(bfs(x1, y1, x2, y2))
		return 1;
	else
		return 0;
}
int allClear()
{
	int flag = 0;
	for(int i = 1; i <= C - 2; i++)
	{
		for(int j = 1; j <= R - 2; j++)
		{
			if(sence[i][j])
			{
				flag = 1;
				break;
			}
		}
		if(flag)
			break;
	}
	if(flag)
		return 1;
	else
		return 0;
}

main函数的cpp文件:

#include <iostream>
#include <cstdio>
#include "Game.h"
#include <windows.h>
using namespace std;


extern int sence[C][R];
int main(void) 
{
	///
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);        
	HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); 

	CONSOLE_SCREEN_BUFFER_INFO bInfo;
	INPUT_RECORD	mouseRec;
	DWORD			res;
	COORD			crPos, crHome = {0, 0}, temp = {0, 0};
	// 
	//*/
	while(1)
	{
		cout << "游戏说明:用鼠标左键点击两个小图案来实现消除,点击右键重置布局" << endl;
		cout << "                  开始游戏!(Y/N)";
		char ch = getchar();
		if(ch == 'N' || ch == 'n')
			break;
		SetConsoleCursorPosition(hOut, temp);
		clean();
		initData();
		int x1, y1, x2, y2;
		x1 = x2 = y1 = y2 = -1;
		while(allClear())
		{
			SetConsoleCursorPosition(hOut, temp);
			display();
			ReadConsoleInput(hIn, &mouseRec, 1, &res);
			crPos = mouseRec.Event.MouseEvent.dwMousePosition;
			if(mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
			{
				if(mouseRec.Event.MouseEvent.dwEventFlags == DOUBLE_CLICK)
				{
					continue;
				}
			}
			if(mouseRec.Event.MouseEvent.dwButtonState == RIGHTMOST_BUTTON_PRESSED)
			{
				change();
				continue;
			}
			if(mouseRec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
			{
				if(x1 < 0)
				{
					x1 = (crPos.Y - 1) / 2;
					y1 = (crPos.X - 1) / 4;
				}
				else if(x2 < 0)
				{
					x2 = (crPos.Y - 1) / 2;
					y2 = (crPos.X - 1) / 4;
				}
			}
			if(x1 > 0 && x2 > 0)
			{
				if(x1 <= 0 || x1 > C || y1 <= 0 || y1 > R || x2 <= 0 || x2 > C || y2 <= 0 || y2 > R)
				{
					system("cls");
					continue;
				}
					
				if(canClear(x1, y1, x2, y2))
				{
					sence[x1][y1] = 0;
					sence[x2][y2] = 0;
					x1 = x2 = y1 = y2 = -1;
				}
				else
				{
					x1 = x2; 
					y1 = y2;
					x2 = y2 = -1;
				}	
			}
		}
		SetConsoleCursorPosition(hOut, temp);
		clean();
		SetConsoleCursorPosition(hOut, temp);
		getchar();
	}
	CloseHandle(hOut);  // 关闭标准输出设备句柄  
	CloseHandle(hIn);   // 关闭标准输入设备句柄  
	return 0;
}

游戏截图:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值