黑白迭代c++源代码

这篇博客详细介绍了如何用C++实现黑白迭代游戏,包括随机取数、隐藏光标、定位及输出函数等功能模块的实现。文章还强调了初始化随机数种子的重要性,以及在输出棋盘状态和题目时的注意事项。此外,博客还提供了重置棋盘、检查答案、撤销操作等功能的实现,并附带了完整的源代码。
摘要由CSDN通过智能技术生成

功能模块

随机取数

一定要注意这里必须要初始化随机数种子,否则你每一次运行随机取的数会相同。

/***随机数***/
int rand_srand(int x){
   
	srand((unsigned)time(NULL));
	return rand() % x; 
} 

隐藏光标

可以优化游戏的显示功能

/***隐藏光标***/
void hide_cursor(){
   
	HANDLE hout=GetStdHandle(STD_OUTPUT_HANDLE);
	COORD coord;
    CONSOLE_CURSOR_INFO cursor_info={
   1,0};
    SetConsoleCursorInfo(hout, &cursor_info);
}

定位函数(光标移动函数)

可以用来移动光标(也可以自己算

/***定位到第y行的第x列***/
void gotoxy(int y, int x) {
   
    int xx = 0x0b;
    HANDLE hOutput;
    COORD loc;
    loc.X = x;
    loc.Y = y;
    hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleCursorPosition(hOutput, loc);
    return;
}

输出函数

输出‘要求’

不做过多解释(就是想写啥就写啥

/***输出要求***/
void print_equirement(){
   
	/***输出顶部***/
	gotoxy(0, 1);
	cout << "────────────────────── equirement───────────────────────" << endl;
	/***输出所有equirement***/
	gotoxy(1, 1);
	cout << "          type '0 0' if you want to end to game." << endl;
	gotoxy(2, 1);
	cout << "      type '11 11' if you want to check the answer." << endl;
	gotoxy(3, 1);
	cout << "      type '12 12' if you want to reset your answer." << endl;
	gotoxy(4, 1);
	cout << "       type '13 13' if you want to change question." << endl;
	gotoxy(5, 1);
	cout << "      type '14 14' if you want to hide the question." << endl;
	gotoxy(6, 1);
	cout << "  type '15 15' if you want to revoke the previous step." << endl;
	gotoxy(7, 1);
	cout << "      type '16 16' if you want to look the answer." << endl;
	/***输出分界线***/
	gotoxy(8, 1);
	cout << "───────────────────── answer place─────────────────────" << endl;
}

输出目前的棋盘状态

这里有一个细节,就是输出的是判断1还是0输出,如果直接存储方格会出现输出错误,达不到想要的结果

/***输出目前的棋盘状态***/ 
void print_your_answer(){
   
	for(int i = 1;i <= 10;i++){
   
		gotoxy(10 + i, 35);
		for(int j = 1;j <= 10;j++){
   
			if(c[i][j] == 1)
				cout << "■";
			if(c[i][j] == 0)
				cout << "□";
		}
	}
}

输出题目

这里和上面一样,不要直接存储方格

/***输出题目***/ 
void print_question(int l){
   
	for(int i = 1;i <= 10;i++){
   
		gotoxy(10 + i, 4);
		for(int j = 1;j <= 10;j++){
   
			if(question[l][i][j] == 1)
				cout << "■";
			if(question[l][i][j] == 0)
				cout << "□";
		}
	}
}

输出边界

智,啊,不。聪明的输出上下左右

/***输出边界***/ 
void print_boundary(){
   
	/***输出左上角***/ 
	gotoxy(0, 0); 
	cout << "┌"; 
	/***输出左边***/ 
	for(int i = 1;i <= 26;i++){
   
		gotoxy(i, 0);
		if(i != 8 && i != 23) 
			cout << "│";
		else 
			cout << "┝";
	}
	/***输出左下角***/ 
	gotoxy(27, 0);
	cout << "└";
	
	/***输出右上角***/ 
	gotoxy(0, 57); 
	cout << "┐"; 
	/***输出右边***/ 
	for(int i = 1;i <= 26;i++){
   
		gotoxy(i, 57);
		if(i != 8 && i != 23) 
			cout << "│";
		else 
			cout << "┤";
	}
	/***输出右下角***/ 
	gotoxy(27, 57);
	cout << "┘";
	
	/***输出caution行***/ 
	gotoxy(23, 1);
	cout << "──────────────────────── caution───────────────────────";
	/***输出最下面***/ 
	gotoxy(27, 1); 
	for(int i = 1;i <= 55;i++)
		cout << "─"; 
}

输出行列的位置

这里没什么好说的

/***输出行列位置***/ 
void print_xyplaces(){
   
	gotoxy(15, 26);
	cout << "row:";
	gotoxy(16, 25);
	cout << "column:";
}

输出全部棋盘

这里就是一个综合,把之前所有游戏输出写到了一起

/***输出全部棋盘***/ 
void print_all(int l){
   
	print_equirement();
	print_boundary();
	print_xyplaces();
	gotoxy(10, 7);
	cout << "   question" << endl;
	print_question(l);
	cout << endl;
	gotoxy(10, 28);
	cout << "           your answer" << endl;
	print_your_answer();
}

输出结束语

这里我只输出了边框,在使用的位置我加入了提示语一季reply的输入位置

/***输出结束语***/ 
void print_finish_reply(){
   
	for(int i = 1;i <= 5;i++){
   
		gotoxy(11 + i, 15);
		cout << "                            ";
	}
	
	/***输出边框***/ 
	gotoxy(12, 15);
	cout << "┌─────────────────────────┐";
	for(int i = 1;i <= 3;i++){
   
		gotoxy(12 + i, 15);
		cout << "│";
		gotoxy(12 + i, 41);
		cout << "│";
	}
	gotoxy(16, 15);
	cout << "└─────────────────────────┘";
}

输出菜单

这里包括菜单的边框一季菜单的选择难度模块,在程序中有很细致的解说(看不懂我也没办法了

/***输出菜单***/ 
short ShowMenu() {
   
	system("cls");
	//返回值显示用户所选难度级别
	gotoxy(3, 11);
	cout << "Which level do you want to try?";
	
	/***输出顶部***/ 
	gotoxy(2, 4); 
	cout << "┌──────────────────────────────────────────┐";
	/***输出左侧***/ 
	for(int i = 3;i <= 9;i++){
   
		gotoxy(i, 4);
		cout << "│"; 
	} 
	/***输出右侧***/ 
	for(int i = 3;i <= 9;i++){
   
		gotoxy(i, 47);
		cout << "│"; 
	} 
	/***输出隔层***/ 
	gotoxy(8, 4);
	cout << "┝──────────────────────────────────────────┤";
	/***输出选择规则***/ 
	gotoxy(9, 7);
	cout << "Use '←' or '→' to choose difficulties.";
	/***输出底部***/
	gotoxy(10, 4);
	cout << "└──────────────────────────────────────────┘";
	
	/***定义result和输入的VK变量***/ 
	short result = 0;
	char ch = 0;
	
	do {
   
		/***左方向键***/ 
		if (ch == 75)
			result = (result + 4) % 5;
			
		/***右方向键***/
		if (ch == 77)  
			result = (result + 1) % 5;   
		
		/***被选中时绿底黑字,未被选中时黑底白字***/
		/***容易***/
		gotoxy(5, 12); 
		if (result == 0)  SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
		else  SetConsoleTextAttribute(handle, FOREGROUND_GREEN| FOREGROUND_BLUE| FOREGROUND_RED);
		cout << "EASILY";
		
		/***简单***/
		gotoxy(5, 22);
		if (result == 1)  SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
		else  SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
		cout << "SIMPLE";
		
		/***中等***/
		gotoxy(5, 32);
		if (result == 2)  SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
		else  SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
		cout << "MODERATE";
		
		/***困难***/
		gotoxy(7, 15);
		if (result == 3)  SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
		else  SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
		cout << "DIFFICULTY";
		
		/***炼狱***/
		gotoxy(7, 29);
		if (result == 4)  SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
		else  SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
		cout << "PURGATORY";
		
		ch = _getch();
		if (ch == 0)
			ch = _getch();
	} while (ch != VK_RETURN);/***只有按下回车键,结束循环***/ 
	
	/***恢复成默认文字及背景色***/
	SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
	system("cls");
	return result;
}

输出答案

这里只是为了可以在游戏内看答案而已

/***输出答案***/ 
void show_ans(int l){
   
	/***将提示框的背景清空***/ 
	for(int i = 1;i <= 12;i++){
   
		gotoxy(9 + i, 15);
		cout << "                           ";
	}
	
	/***输出提示框***/
	gotoxy(10, 15);
	/***输出顶部***/ 
	cout << "┌─────────────────────────┐";
	gotoxy(21, 15);
	/***输出底部***/ 
	cout << "└─────────────────────────┘";
	/***输出两边***/ 
	for(int i = 1;i <= 10;i++){
   
		gotoxy(10 + i, 15);
		cout << "│";
		gotoxy(10 + i, 41);
		cout << "│";
	}
	
	/***输出答案部分***/
	for(int i = 1;i <= 10;i++){
   
		gotoxy(10 + i, 17);
		cout << "row" << i << ":";
		for(int j = 1;j <= 10;j++)
			if(answer_group[l][i][j] == 1)
				cout << j << " ";
	}
	
}

输出欢迎界面

这里我是一个一个字母输出的(就是为了有输入感)

/***欢迎界面***/
void welcome(){
   
	/***设置输出框大小***/ 
	system("mode con cols=54 lines=13");
	
	/***cout << "WELCOME TO \"BLACK AND WHITE ITERATION\"";***/ 
	gotoxy(6, 8);
	cout << "W";Sleep(100);
	cout << "E";Sleep(100);
	cout << "L";Sleep(100);
	cout << "C";Sleep(100);
	cout << "O";Sleep(100);
	cout << "M";Sleep(100);
	cout << "E";Sleep(100);
	cout << " ";Sleep(100);
	cout << "T";Sleep(100);
	cout << "O";Sleep(100);
	cout << " ";Sleep(100);
	cout << "\"";Sleep(100);
	cout << "B";Sleep(100);
	cout << "L";Sleep(100);
	cout << "A";Sleep(100);
	cout << "C";Sleep(100);
	cout << "K";Sleep(100);
	cout << " ";Sleep(100);
	cout << "A";Sleep(100);
	cout << "N";Sleep(100);
	cout << "D";Sleep(100);
	cout << " ";Sleep(100);
	cout << "W";Sleep(100);
	cout << "H";Sleep(100);
	cout << "I";Sleep(100);
	cout << "T";Sleep(100);
	cout << "E";Sleep(100);
	cout << " ";Sleep(100);
	cout << "I";Sleep(100);
	cout << "T";Sleep(100);
	cout << "E";Sleep(100);
	cout << "R";Sleep(100);
	cout << "A";Sleep(100);
	cout << "T";Sleep(100);
	cout << "I";Sleep(100);
	cout << "O";Sleep(100);
	cout << "N";Sleep(100);
	cout << "\"";Sleep(100);
	
	/***cout << "press \"Enter\" to continue"***/ 
	gotoxy(7, 15);
	cout << "p";Sleep(100);
	cout << "r";Sleep(100);
	cout << "e";Sleep(100);
	cout << "s";Sleep(100);
	cout << "s";Sleep(100);
	cout << " ";Sleep(100);
	cout << "\"";Sleep(100);
	cout << "E";Sleep(100);
	cout << "n";Sleep(100);
	cout << "t";Sleep(100);
	cout << "e";Sleep(100);
	cout << "r";Sleep(100);
	cout << "\"";Sleep(100);
	cout << " ";Sleep(100);
	cout << "t";Sleep(100);
	cout << "o";Sleep(100);
	cout << " ";Sleep(100);
	cout << "c";Sleep(100);
	cout << "o";Sleep(100);
	cout << "n";Sleep(100);
	cout << "t";Sleep(100);
	cout << "i";Sleep(100);
	cout << "n";Sleep(100);
	cout << "u";Sleep(100);
	cout << "e";Sleep(100);
	
	/***判断是否敲击回车***/ 
	char ch = 0;
	do{
   
		ch = _getch();
	}while (ch != VK_RETURN);
}

输出结束语

没啥好说的,和上面的一样,挨个字母输出

/***输出结束框***/
void thank(){
   
	/***设置输出框大小***/ 
	system("mode con cols=54 lines=13");
	
	/***cout << "THANK YOU FOR PLAYING";***/ 
	gotoxy(6, 15);
	cout << "T";Sleep(100);
	cout << "H";Sleep(100);
	cout << "A";Sleep(100);
	cout << "N";Sleep(100);
	cout << "K";Sleep(100);
	cout << " ";Sleep(100);
	cout << "Y";Sleep(100);
	cout << "O";Sleep(100);
	cout << "U";Sleep(100);
	cout << " ";Sleep(100);
	cout << "F";Sleep(100);
	cout << "O";Sleep(100);
	cout << "R";Sleep(100);
	cout << " ";Sleep(100);
	cout << "P";Sleep(100);
	cout << "L";Sleep(100);
	cout << "A";Sleep(100);
	cout << "Y";Sleep(100);
	cout << "I";Sleep(100);
	cout << "N";Sleep(100);
	cout << "G";Sleep(100);
	
	/***cout << "press \"Enter\" to continue"***/ 
	gotoxy(7, 14);
	cout << "p";Sleep(100);
	cout << "r";Sleep(100);
	cout << "e";Sleep(100);
	cout << "s";Sleep(100);
	cout << "s";Sleep(100);
	cout << " ";Sleep(100);
	cout << "\"";Sleep(100);
	cout << "E";Sleep(100);
	cout << "n";Sleep(100);
	cout << "t";Sleep(100);
	cout << "e";Sleep(100);
	cout << "r";Sleep(100);
	cout << "\"";Sleep(100);
	cout << " ";Sleep(100);
	cout << "t";Sleep(100);
	cout << "o";Sleep(100);
	cout << " ";Sleep(100);
	cout << "c";Sleep(100);
	cout << "o";Sleep(100);
	cout << "n";Sleep(100);
	cout << "t";Sleep(100);
	cout << "i";Sleep(100);
	cout << "n";Sleep(100);
	cout << "u";Sleep(100);
	cout << "e";Sleep(100);
	
	/***判断是否敲击回车***/ 
	char ch = 0;
	do{
   
		ch = _getch();
	}while (ch != VK_RETURN);
} 

输出规则界面

/***规则界面***/ 
void rule(){
   
	/***设置输出框大小***/ 
	system("mode con cols=54 lines=13");
	
	/***输出规则***/ 
	gotoxy(4, 0);
	cout << "          input the row and column each time";
	gotoxy(5, 0);
	cout << "   This position and the top, bottom, left and right";
	gotoxy(6, 0);
	cout << "    of this position will change to opposite color";
	gotoxy(7, 0);
	cout << "    Finally, we need to reach the image on the left";
	gotoxy(8, 0);
	cout << "             press \"Enter\" to continue";
	
	/***输出外框***/ 
	/***输出顶部***/ 
	gotoxy(3, 0); 
	cout << " ┌────────────────────── rule──────────────────────┐";
	/***输出底部***/ 
	gotoxy(9, 0);
	cout << " └─────────────────────────────────────────────────┘";	 
	/***输出两侧***/ 
	for(int i = 1;i <= 5;i++){
   
		gotoxy(3 + i, 0);
		cout << " │";
		gotoxy(3 + i, 51);
		cout << "│";
	} 
	
	/***判断是否敲击回车***/ 
	char ch = 0;
	do{
   
		ch = _getch();
	}while (ch != VK_RETURN);
}

隐藏问题

这个功能在节目中的试炼间又出现

/***隐藏问题***/ 
void hide_question(){
   
	/***隐藏问题***/ 
	for(int i = 1;i <= 11;i++){
   
		gotoxy(i + 9, 4);
		cout << "                   ";
	}
	
	/***输出提示框***/ 
	gotoxy(15, 8);
	cout << "The question";
	gotoxy(16, 10);
	cout << "is hide!";
	gotoxy(14, 6);
	cout << "┌─────────────┐"; 
	for(int i = 1;i <= 2;i++){
   
		gotoxy(14 + i, 6);
		cout << "│";
		gotoxy(14 + i, 20);
		cout << "│";
	}
	gotoxy(17, 6); 
	cout << "└─────────────┘";
}

重置棋盘

重置自己的答案

/***重置棋盘***/ 
void reset(int l){
   
	/***棋盘数值归零***/ 
	for(int i = 0;i <= l;i++)
		for(int j = 0;j <= l;j++)
			c[i][j] = 0;
}

检查答案

/***检查答案***/ 
bool chk(int l){
   
	/***逐个判断是否正确***/ 
	for(int i = 1;i <= 10;i++)
		for(int j = 1;j <= 10;j++)
			if(c[i][j] != question[l][i][j])
				return false;
	return true;		
}

撤销

/***撤销***/
void revoke(int sstep){
   
	c[xysteps[sstep].Cx][xysteps[sstep].Cy] = (c[xysteps[sstep].Cx][xysteps[sstep].Cy] + 1) % 2;
	c[xysteps[sstep].Cx + 1][xysteps[sstep].Cy] = (c[xysteps[sstep].Cx + 1][xysteps[sstep].Cy] + 1) % 2;
	c[xysteps[sstep].Cx - 1][xysteps[sstep].Cy] = (c[xysteps[sstep].Cx - 1][xysteps[sstep].Cy] + 1) % 2;
	c[xysteps[sstep].Cx][xysteps[sstep].Cy + 1] = (c[xysteps[sstep].Cx][xysteps[sstep].Cy + 1] + 1) % 2;
	c[xysteps[sstep].Cx][xysteps[sstep].Cy - 1] = (c[xysteps[sstep].Cx][xysteps[sstep].Cy - 1] + 1) % 2;
}

主函数

/***主函数***/ 
int main(){
   
	/***隐藏鼠标光标***/ 
	hide_cursor();
	
	/***输出欢迎***/ 
	welcome();
	system("cls");
	Sleep(200);
	
	/***输出规则***/ 
	rule(); 
	system("cls");
	Sleep(200);
	
	/***设置menu输出框的大小***/ 
	GAME_START:
	system("mode con cols=54 lines=13");
	handle = GetStdHandle(STD_OUTPUT_HANDLE);
	
	/***确定难度***/ 
	level = ShowMenu();
	
	/***设置game输出框的大小***/ 
	system("mode con cols=59 lines=28");
	question_number = rand_srand(5) + level * 5;
	print_all(question_number);
	
	/***多次输入行列数***/
	while(true){
   
		/***输入行列数***/ 
		gotoxy(15, 30);
		cin >> x;
		if(hide_question_time == 0){
   
			gotoxy(16, 32);
			cin >> y;
		}
		else if(hide_question_time == 1){
   
			gotoxy(16, 31);
			cin >> y;
		}
		/***记录每一步和步数***/ 
		if(x >= 1 && x <= 10 && y >= 1 && y <= 10){
   
			steps++;
			xysteps[steps].Cx = x;
			xysteps[steps].Cy = y;
		}
		system("cls");
		
		/***判断放弃***/ 
		if(x == 0 && y == 0){
   
			int gi;
			print_all(question_number);
			/***输出caution***/ 
			gotoxy(24, 2);
			cout << "                 Do you wanna give up?" << endl;
			gotoxy(25, 2);
			cout << "               (answer '1' if you want)";
			gotoxy(26, 2);
			cout << "            (answer '0' if you don't want)";
			
			/***询问reply***/ 
			gotoxy(15, 25);
			cout << "reply:";
			gotoxy(16, 25);
			cout << "       ";
			gotoxy(15, 31);
			cin >> gi;
			
			/***判断是否确定放弃***/ 
			if(gi == 1)
				break;
				
			/***盖住reply***/ 
			gotoxy(15, 25);
			cout << "        ";
			gotoxy(14, 19);
			cout << "       ";
		}
		
		/***判断检查***/ 
		if(x == 10 + 1 && y == 10 + 1)
			/***正确***/ 
			if(chk(question_number)){
   
				print_all(question_number);
				/***输出caution***/ 
				gotoxy(25, 2);
				cout << "       congratulation(press \"Enter\" to continue)" << endl << endl;
				char ch = 0;
				do{
   
					ch = _getch();
				}while (ch != VK_RETURN);
				
				/***判断是否再来一局***/ 
				print_finish_reply();
				int rep;
				/***输出caution***/ 
				gotoxy(13, 16);
				cout << " Do you wanna play again?";
				gotoxy(14, 16);
				cout << "  press '1' if you want. ";
				gotoxy(15, 16);
				cout << "         reply:";
				/***输入reply***/
				gotoxy(15, 31);
				cin >> rep;
				/***判断是否为重新开始游戏***/ 
				if(rep == 1){
   
					reset(10);
					goto GAME_START;
				}
				else{
   
					gotoxy(28, 0);
					thank();
					system("cls");
					return 0;
				}
			}
			/***错误***/ 
			else{
   
				print_all(question_number);
				/***输出caution***/
				gotoxy(25, 2);
				cout << "       Your answer is not correct,please continue!" << endl;
			}
			
		/***重置答案***/ 
		if(x == 10 + 2 && y == 10 + 2){
   
			/***重置棋盘***/ 
			reset(10);
			
			/***输出caution***/
			gotoxy(25, 2); 
			cout << "                  You answer is reset!" << endl;
			
			/***部署清零***/
			steps = 0; 
		}
		
		/***重置问题***/ 
		if(x == 10 + 3 && y == 10 + 3){
   
			question_number = rand_srand(5) + level * 5;
			/***输出caution***/
			gotoxy(25, 2);
			cout << "                 The quetion is change!" << endl;
		}
		
		/***判断输入是否为改变棋盘状态***/ 
		if(x == 10 + 4 && y == 10 + 4){
   
			hide_question_time = (hide_question_time + 1) % 2;
			if(hide_question_time == 1){
   
				/***输出caution***/ 
				gotoxy(24, 2);
				cout << "                  The question is hide!";
				gotoxy(25, 2);
				cout << "          Please contniue to solve the problem!";
				gotoxy(26, 2);
				cout << "        type '14 14' again to show the question!";
			} 
			if(hide_question_time == 0){
   
				gotoxy(25, 2);
				cout << "                  T
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值