C++有趣程序(5)

Hello, 欢迎登录 or 注册!

/ 0评 / 0

本文作者:  本文分类:C++小游戏 编程学习笔记  浏览:1090
阅读时间:2421字, 约2.5-4分钟

作者提醒:请注意,本文写作时间为 2020 年,作者经验尚不成熟,会含有非常多错误或主观的内容,本系列内容留作纪念,基本无参考价值。

这个程序有一个源代码,有一个头文件(完全可以放在cpp文件里,但是我就是要把它弄成头文件hhh),代码较长。实现了随机华容道。唯一的缺点就是随机的时候不支持判断这个华容道可不可行,因为123 456 87这样的华容道是无解的。

huarongdao.cpp:

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#include "MakeRandom.h"

using namespace std;

char mapp[3][3]; //因为map在bits\stdc++.h中有重名,换一个名字
char omapp[3][3] = { //目标的最终地图 
//x:  0   1   2   // y:
	{'1','2','3'},// 0
	{'4','5','6'},// 1
	{'7','8',' '},// 2
}; 

struct pos {
	int x, y;
} location[9];



void mapout(void ){
	printf("\n  数字华容道\n  ____________________\n   Developed By 23786\n\n  先输入要移动的数字,再输入要移动的方向(l,r,u,d)\n\n");
	
	for (int i = 0; i <= 2; i ++) {
		printf("  %c %c %c\n",mapp[i][0], mapp[i][1], mapp[i][2]);
	}
}



// 从MakeRandom.h中获取随机华容道并保存 
void randomm(void) {
	int a[10];
	int *p = random();
	for (int i = 0; i < 8; i ++) {
		a[i] = *(p + i);
	}
	for (int i = 0; i < 3; i ++) {
		for (int j = 0; j < 3; j ++) {
			if (i == 2 && j == 2) mapp[i][j] = ' '; //如果是华容道最后一个坐标,设为空格 
			else {
				int randresult = a[i * 3 + j];
				mapp[i][j] = char(randresult + 48);
				location[randresult].x = j;
				location[randresult].y = i;
			}
		}
	}
	return;
}




// 移动功能 
void movee(int a, char b) {
	const int x = location[a].x;
	const int y = location[a].y;
	switch(b){
		case 'r':{
			//右:把x坐标+1 
			printf("方向:右\n");
			Sleep(750);
			if (x + 1 != 3 && mapp[y][x + 1] == ' ') {
				location[a].x ++;
				mapp[y][x + 1] = mapp[y][x];
				mapp[y][x] = ' ';
			}
			break;
		}
		case 'l':{
			//左:把x坐标-1 
			printf("方向:左\n");
			Sleep(750);
			if (x - 1 != -1 && mapp[y][x - 1] == ' ') {
				location[a].x --;
				mapp[y][x - 1] = mapp[y][x];
				mapp[y][x] = ' ';
			}
			break;
		}
		case 'u':{
			//上:把y坐标-1 
			printf("方向:上\n");
			Sleep(750);
			if (y - 1 != -1 && mapp[y - 1][x] == ' ') {
				location[a].y --;
				mapp[y - 1][x] = mapp[y][x];
				mapp[y][x] = ' ';
			}
			break;
		}
		case 'd':{
			//下:把y坐标+1 
			printf("方向:下\n");
			Sleep(750);
			if (y + 1 != 3 && mapp[y + 1][x] == ' ') {
				location[a].y ++;
				mapp[y + 1][x] = mapp[y][x];
				mapp[y][x] = ' ';
			}
			break;
		}
	}
}


// 判断是否成功(因为我从来没成功过所以不知道这个对不对www) 
bool ifSuccess() {
	int collector = 0;
	for (int i = 0; i < 3; i ++) {
		if (strcmp(mapp[i], omapp[i]) == 0) collector ++;
	}
	return collector == 3? true:false;
}


// 主程序 
int main() {
	system("color f0");
	randomm();
	char num, dir;
	while(1) {
		mapout();
		printf("  ");
		num = getch();
		printf("移动:%c   ", num);
		dir = getch();
		const int number = int(num) - 48;
		movee(number, dir);
//		if(ifSuccess) break;
		system("cls");
	}
	printf("  ");
	MessageBox(NULL,"你赢了!!!!!","祝贺你",MB_OK);
	printf("\n  ");
	system("pause"); //虽然不想加这个...但是如果不加程序就直接推出了。。。 
	return 0; 
}

MakeRandom.h:

#include <bits/stdc++.h>

void insort(int a[], int b[]) {
	for(int i = 0; i < 8; i ++)
		for(int j = i + 1; j < 8; j ++)
			if(b[j] < b[i]) {
				std::swap(b[i], b[j]);
				std::swap(a[i], a[j]);
			}
}

void randnumber(int a[]) {
	int i, b[8];
	srand((unsigned int)time(NULL));
	for(i = 0; i < 8; i ++) {
		b[i] = rand() % 8 + 1;
		a[i] = i + 1;
	}
	insort(a, b);
} 

int *random() {
	int a[8];
	randnumber(a);
	int *p = &a[0];
	return p;
}

==The End==

关于作者

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注