连连看算法(控制台实现)



#include"stdafx.h"
#include"iostream"
#include"string"
#include"ctime"
#include <windows.h>

using namespace std;

int set_X(){
	//srand(time(NULL));//调用系统时间
	//Sleep(1000);
	int x = rand()%5 + 1;//产生1-5间的随机数
	return x;
}

void show_animals(int x, int &tiger, int &bunny, int &birds, int &panda, int & goose, int &m, int &n, string (&LinkGame)[6][6]){
	switch (x){//输出对应的字符
	case 1:

		tiger++;
		if(tiger > 6){
			n--;
		}
		else{
			cout<<" tiger "<<" | ";
			LinkGame[m][n] = "tiger";//对相应的位置进行赋值
		};
		break;
	case 2:
		bunny++;
		if(bunny > 6){
			n--;
		}
		else{
			cout<<" bunny "<<" | ";
			LinkGame[m][n] = "bunny";
		};
		break;
	case 3:
		birds++;
		if(birds > 6){
			n--;
		}
		else{
			cout<<" birds "<<" | ";
			LinkGame[m][n] = "birds";
		};
		break;
	case 4:
		panda++;
		if(panda > 6){
			n--;
		}
		else{
			cout<<" panda "<<" | ";
			LinkGame[m][n] = "panda";
		};
		break;
	case 5:
		goose++;
		if(goose > 6){
			n--;
		}
		else{
			cout<<" goose "<<" | ";
			LinkGame[m][n] = "goose";
		};
		break;
	default:
		break;
	};
}

void refresh(string (&LinkGame)[6][6]){//更新
	int m = 0, n = 0, i = 0;
	cout<<" |   0    |    1    |    2    |    3    |    4    |    5    |"<<endl;
	for(m = 0; m < 5; m++){//嵌套循环进行初始化
		cout<<i<<"|";
		i++;
		for(n = 0; n <6; n++){
			cout<<" "<<LinkGame[m][n]<<" "<<" | ";
		};
		cout<<endl;//换行
	};
}

bool check_no_conner(int first_r, int first_c, int second_r, int second_c, string (&LinkGame)[6][6]){//判断没有拐角的选择
	int a = 0;
	if(LinkGame[first_r][first_c] == LinkGame[second_r][second_c]){
		if(first_r == second_r){
			if(first_c > second_c){
				for(a = first_c-1; a > second_c; a--){//两点间为通路返回ture
					if(LinkGame[first_r][a] != "     "){
						return false;
					};
				};
				return true;
			}
			else if(first_c < second_c){
				for(a = first_c+1; a < second_c; a++){
					if(LinkGame[first_r][a] != "     "){
						return false;
					};
				};
				return true;
			};
		}
		else if(first_c == second_c){
			if(first_r > second_r){
				for(a = first_r-1; a > second_r; a--){
					if(LinkGame[first_c][a] != "     "){
						return false;
					};
				};
				return true;
			}
			else if(first_r < second_r){
				for(a = first_r+1; a < second_r; a--){
					if(LinkGame[first_c][a] != "     "){
						return false;
					};
				};
				return true;
			};
		}
		else{
			return false;
		};
	}
	else{
		return false;
	};
}

bool check_one_conner(int first_r, int first_c, int second_r, int second_c, string (&LinkGame)[6][6]){
	if(LinkGame[first_r][first_c] == LinkGame[second_r][second_c]){
		if(LinkGame[first_r][second_c] == "     "){//选中的两点组成的矩形定点为空
			LinkGame[first_r][second_c] = LinkGame[first_r][first_c];//对其进行赋值
			if((check_no_conner(first_r, second_c,first_r, first_c, LinkGame)) && (check_no_conner(first_r, second_c,second_r,second_c, LinkGame))){
				LinkGame[first_r][second_c] = "     ";
				return true;
			};
			LinkGame[first_r][second_c] = "     ";
		}
		else if(LinkGame[first_c][second_r] == "     "){
			LinkGame[first_c][second_r] = LinkGame[first_r][first_c];
			if((check_no_conner(first_c, second_r,first_r, first_c, LinkGame)) && (check_no_conner(first_c, second_r,second_r,second_c, LinkGame))){
				LinkGame[first_c][second_r] = "     ";
				return true;
			};
			LinkGame[first_c][second_r] = "    ";
		}
		else{
			return false;
		};
	}
	else{
		return false;
	};
}

bool check_two_conner_right(int a, int first_r, int first_c, int second_r, int second_c, string (&LinkGame)[6][6]){
	for(a = second_r+1; a < 5; a++){
		if(LinkGame[a][second_c] == "     "){
			LinkGame[a][second_c] = LinkGame[second_r][second_c];
			if(check_one_conner(a, second_c, first_r, first_c, LinkGame)){
				LinkGame[a][second_c] = "     ";
				return true;
			};
		};
	};
	return false;
}

bool check_two_conner_left(int a, int first_r, int first_c, int second_r, int second_c, string (&LinkGame)[6][6]){
	for(a = second_r-1; a >= 0; a--){
		if(LinkGame[a][second_c] == "     "){
			LinkGame[a][second_c] = LinkGame[second_r][second_c];
			if(check_one_conner(a, second_c, first_r, first_c, LinkGame)){
				LinkGame[a][second_c] = "     ";
			};	
		};
	}; 
	return false;
}

bool check_two_conner_down(int a, int first_r, int first_c, int second_r, int second_c, string (&LinkGame)[6][6]){
	for(a = second_c+1; a < 6; a++){
		if(LinkGame[second_r][a] == "     "){
			LinkGame[second_r][a] = LinkGame[second_r][second_c];
			if(check_one_conner(second_r, a, first_r, first_c, LinkGame)){
				LinkGame[second_r][a] = "     ";
				return true;
			};				
		};
	};
	return false;
}

bool check_two_conner_up(int a, int first_r, int first_c, int second_r, int second_c, string (&LinkGame)[6][6]){
	for(a = second_c-1; a >= 0; a--){
		if(LinkGame[second_r][a] == "     "){
			LinkGame[second_r][a] = LinkGame[second_r][second_c];
			if(check_one_conner(second_r, a, first_r, first_c, LinkGame)){
				LinkGame[second_r][a] = "     ";
				return true;
			};
		};
	};
	return false;
}

bool check_two_conner(int first_r, int first_c, int second_r, int second_c, string (&LinkGame)[6][6]){
	if(LinkGame[first_r][first_c] == LinkGame[second_r][second_c]){
		int a  = 0;
		if(check_two_conner_right(a, first_r, first_c, second_r, second_c,LinkGame)){
			return true;
		}
		else if(check_two_conner_left(a, first_r, first_c, second_r, second_c,LinkGame)){
			return true;
		}
		else if(check_two_conner_up(a, first_r, first_c, second_r, second_c,LinkGame)){
			return true;
		}
		else if(check_two_conner_down(a, first_r, first_c, second_r, second_c,LinkGame)){
			return true;
		}
		else{
			return false;
		};
	}
	else{
		return false;
	};
}

bool check_end(string (&LinkGame)[6][6]){
	int m = 0, n = 0;
	for(m = 0; m < 5; m++){//嵌套循环进行初始化
		for(n = 0; n <6; n++){
			if(LinkGame[m][n] !="     "){
				return true;
			};
		};
	};
	return false;
}

void get_in(int &first_r, int &first_c, int &second_r, int &second_c){
	cin>>first_r>>first_c>>second_r>>second_c;
	if(first_r == second_r && first_c == second_c){
		cout<<endl;
		cout<<"非法操作,请重新输入!"<<endl<<endl;
		cout<<"输入需要消掉的坐标:(r1 c1 r2 c2)"<<"   ";
		get_in(first_r, first_c, second_r, second_c);
	};
}

void choice_delete(int first_r, int first_c, int second_r, int second_c,string (&LinkGame)[6][6]){
	LinkGame[first_r][first_c] = "     ";
	LinkGame[second_r][second_c] = "     ";
	refresh(LinkGame);
}

int main(){
	const int c = 6;//定义二维数组
	const int r = 6;//同上
	int choice_c = 0;//函数中定义数组中某个位置
	int choice_r = 0;//同上
	int tiger = 0, bunny = 0, birds = 0, panda = 0, goose = 0;
	string LinkGame[r][c];
	int m = 0, n = 0, i = 0;
	cout<<" |   0    |    1    |    2    |    3    |    4    |    5    |"<<endl;
	for(m = 0; m < 5; m++){//嵌套循环进行初始化
		cout<<i<<"|";
		i++;
		for(n = 0; n <6; n++){
			Sleep(10);
			int x = set_X();//调用srand产生一个1-5之间的随机数
			show_animals(x,tiger, bunny, birds, panda, goose, m, n, LinkGame);//判断生成正确性
		};
		cout<<endl;//换行
	};
	int first_r = 0;
	int first_c = 0;
	int second_r = 0;
	int second_c = 0;
	do{
		cout<<endl;
		cout<<"输入需要消掉的坐标:(r1 c1 r2 c2)"<<"   ";
		get_in(first_r, first_c, second_r, second_c);
		if(check_no_conner(first_r, first_c,second_r,second_c, LinkGame)){
			choice_delete(first_r, first_c, second_r, second_c,LinkGame);
		}
		else if(check_one_conner(first_r, first_c, second_r, second_c, LinkGame)){
			choice_delete(first_r, first_c, second_r, second_c,LinkGame);
		}
		else if(check_two_conner(first_r, first_c, second_r, second_c, LinkGame)){
			choice_delete(first_r, first_c, second_r, second_c,LinkGame);
		}
		else{
			cout<<endl;
			cout<<"无法消去,请重新输入!"<<endl;
		};
	}
	while(check_end(LinkGame));
	cout<<"**********************YOU WIN!!!*************************";
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值