C语言抓狐狸问题

//#include "pch.h"
#include 
#include 
#include 
#include 
int a[11];
int count = 0;
int i, j;

void creat_hole1(){
	srand(time(NULL));
	int hole_fox = rand() % 9 ;
	j = hole_fox % 3 + 1;
	i = (hole_fox - j + 1) / 3 + 1;
	a[0] = hole_fox + 1;
	printf("最初狐狸在%d\n", a[0]);
}

void move() {

	for (;;) {
		srand(time(NULL));
		int reaction = rand() % 4;
		if (reaction == 0 && i < 3) {
			i++;
			break;
		}
		if (reaction == 1 && i > 1) {
			i--;
			break;
		}
		if (reaction == 2 && j < 3) {
			j++;
			break;
		}
		if (reaction == 3 && j > 1) {
			j--;
			break;
		}

	}
}

int grasp() {
	count++;
	int hole;
	printf("请输入数字");
	scanf("%d", &hole);

	if (3 * (i - 1) + j == hole) {
		printf("抓捕成功");
		return 1;//抓捕结束
	}
	int m, n;
	n = (hole - 1) % 3 + 1;
	m = (hole - n) / 3 + 1;
	if ((m == i && fabs(n - j) == 1) || ((n == j) && fabs(m - i) == 1)) 
		move();
	 //未受惊
	a[count] = 3 * (i - 1) + j;
	printf("现在狐狸在洞%d\n", a[count]);//最终把这行注释掉
	return 0;
}

void print() {
	printf("狐狸运动轨迹");
	printf("%d", a[0]);
	for (int b = 1; b < count; b++)
		printf("->%d", a[b]);
}

int creat_hole2() {
	srand(time(NULL));
	a[0] = rand() % 4;
	return a[0];
}

int move2(int hole_fox, int mode) { //模式1和模式2的移动函数
	srand(time(NULL));
	int reaction = rand() % 2;
	if ((hole_fox == 1 || hole_fox == 4) && mode == 1)
		hole_fox = (hole_fox + 3) / 2;
	else
		hole_fox = (2 * reaction - 1) + hole_fox; //狐狸逃跑

	if (hole_fox == 0 || hole_fox == 5)  //环形的情况下判断是否越界
		hole_fox = fabs(hole_fox - 4);
	return hole_fox;
}

int grasp2(int hole_fox, int mode) {//模式1和模式2下的抓捕
	count++;
	printf("请输入要抓的洞");
	int hole;
	scanf("%d", &hole);
	if (hole == hole_fox) {
		printf("抓捕成功!");
		return 5;
	}
	if (fabs(hole - hole_fox) == 1) 
		hole_fox = move2(hole_fox, mode);

	printf("现在狐狸在洞%d",hole_fox);
	a[count] = hole_fox;
	return hole_fox;
}

void chose_mode() {//选择游戏模式
	int mode;
	int flag = 0;
	printf("请选择一种模式");
fflush(stdin);
rewind(stdin);
	scanf("%d", &mode);
	if (mode == 3) { //九宫格模式
		creat_hole1();
		for (; flag == 0 && count < 10;)
			flag = grasp();
		if (flag == 0)
			count = 11;
	}
	else if (mode == 1 || mode == 2) {//模式1和模式2
		int hole_fox = grasp2(creat_hole2(), mode);
		for (; hole_fox != 5 && count < 10;)
			hole_fox = grasp2(hole_fox, mode);
		if (hole_fox != 5)
			count = 11;
	}
	else {
		printf("输入错误,");
		chose_mode();
	}
}

void main()
{
	chose_mode();
	print();
}

你可能感兴趣的:(C语言抓狐狸问题)