二叉树实现猜动物小游戏

该游戏玩法如下:

描述:自己脑海里想一只动物,然后根据计算机的提示输入yes/no,让计算机它猜你脑海中的动物,如果它猜不对就将自己的动物输入到计算机,让它记住能保证下次它能猜出来。

二叉树实现猜动物小游戏_第1张图片

源文件main.cpp

#include
#include
#include
#include"bintree.h"
#include"useful.h"
using namespace std;

void ask_and_move(binary_tree_node*& current_ptr);
binary_tree_node* beginning_tree();
void instruct();
void learn(binary_tree_node*& leaf_ptr);
void play(binary_tree_node* current_ptr);

int main() {
	binary_tree_node* animal_root_ptr;
	instruct();
	animal_root_ptr = beginning_tree();
	do
		play(animal_root_ptr);
	while (inquire("还要不要再玩一次?"));
	cout << "欢迎下次来玩~" << endl;
	return EXIT_SUCCESS;
	while (1);
}
void instruct() {
	cout << "现在开始游戏吧" << endl;
	cout << "你脑海中想象一只动物,让我来猜一猜" << endl;
}
void ask_and_move(binary_tree_node*& current_ptr) {
	cout << current_ptr->data();//输出问题
	if (inquire("请回答:")) {
		current_ptr = current_ptr->left();
	}
	else
		current_ptr = current_ptr->right();
}
//创建二叉树
binary_tree_node* beginning_tree(){
	binary_tree_node* root_ptr;//头节点指针
	binary_tree_node* child_ptr;//孩子节点指针
	const string root_question("你是哺乳动物么?");
	const string left_question("你的体型比猫大,对么?");
	const string right_question("这个动物是生活在水里么?");
	const string animal_1("袋鼠");
	const string animal_2("老鼠");
	const string animal_3("罗非鱼");
	const string animal_4("喜鹊");

	//根节点
	root_ptr = new binary_tree_node(root_question);//头节点指针指向新创建的节点问题

	//左子树
	child_ptr = new binary_tree_node(left_question);//左孩子问题
	child_ptr->set_left(new binary_tree_node(animal_1));
	child_ptr->set_right(new binary_tree_node(animal_2));

	root_ptr->set_left(child_ptr);//将根节点的左节点设置为子节点的左节点

	//右子树
	child_ptr = new binary_tree_node(right_question);
	child_ptr->set_left(new binary_tree_node(animal_3));
	child_ptr->set_right(new binary_tree_node(animal_4));

	root_ptr->set_right(child_ptr);//将根节点的右节点设置为子节点的右节点
	return root_ptr;
}
void learn(binary_tree_node*& leaf_ptr) {
	string guess_animal;
	string correct_animal;
	string new_question;
	guess_animal = leaf_ptr->data();
	cout << "好吧,我不知道是什么,还是你告诉我吧?" << endl;
	getline(cin, correct_animal);
	cout << "现在请输入一个新的问题来区分 ";
	cout <<'['<set_data(new_question);
	cout<set_left(new binary_tree_node(correct_animal));
		leaf_ptr->set_right(new binary_tree_node(guess_animal));
	}
	else {
		leaf_ptr->set_left(new binary_tree_node(guess_animal));
		leaf_ptr->set_right(new binary_tree_node(correct_animal));
	}
}
void play(binary_tree_node* current_ptr) {
	cout << "你现在脑海里想象一只动物,想好了然后按下回车.";
	eat_line();//等待,直到按下回车
	while (!current_ptr->is_leaf())//如果不是叶子节点就执行ask_and_remove()函数,因为是叶子节点就直接输出最后猜测的结果
		ask_and_move(current_ptr);
	cout << ("是" + current_ptr->data()+"吧");
	if (!inquire(",我猜对了吗?"))//询问是否猜对,否,将该动物添加到二叉树中
		learn(current_ptr);
	else
		cout << "我猜对了,哈哈哈哈" << endl;

}

 bintree.h头文件

template
class binary_tree_node {
public:
	typedef Item value_type;
	binary_tree_node(const Item& init_data = Item(), binary_tree_node* init_left = NULL, binary_tree_node* init_right = NULL) {
		data_field = init_data;
		left_field = init_left;
		right_field = init_right;
	}
	Item data() { return data_field; }
	binary_tree_node* left() { return left_field; }
	binary_tree_node* right() { return right_field; }
	void set_data(Item new_data) { data_field = new_data; }
	void set_left(binary_tree_node* new_left) { left_field = new_left; }
	void set_right(binary_tree_node* new_right) { right_field = new_right; }
	bool is_leaf() {
		return (left_field == NULL) && (right_field == NULL);
	}

private:
	Item data_field;
	binary_tree_node* left_field;
	binary_tree_node* right_field;
};

useful.h

#ifndef __USEFUL__
#define __USEFUL__
double random_fraction();
double random_real(double low, double high);
void display(double x);
void eat_line();
bool inquire(const char query[]);
#endif

useful.cpp

#include
#include
#include
#include
#include"useful.h"
using namespace std;

void display(double x) {
	const char STAR = '*';
	const char BLANK = ' ';
	const char VERTICAL_BAR = '|';
	const int LIMIT = 39;
	int i;
	if (x < -LIMIT)
		x = -LIMIT;
	else if (x > LIMIT)
		x = LIMIT;
	for (int i = -LIMIT; i < 0; i++) {
		if (i > x)
			cout << STAR;
		else
			cout << BLANK;
	}
	cout << VERTICAL_BAR;
	for (int i = 1; i <= LIMIT; i++) {
		if (i <= x)
			cout << STAR;
		else
			cout << BLANK;
	}
}
//返回浮点0-1的随机数
double random_fraction() {
	//cstdlib
	return rand() / double(RAND_MAX);
}
double random_real(double low, double high) {
	assert(low <= high);
	return low + random_fraction()*(high - low);
}
void eat_line() {
	char next;
	do
		cin.get(next);//一直等待,直到按下回车
	while (next != '\n');
}
//传入一个数组		
bool inquire(const char query[]) {
	char answer;
	do {
		cout << query << "[Yes or No]" << endl;
		cin >> answer;
		answer = toupper(answer);//这个表示变成大写
		eat_line();
	}
	while ((answer != 'Y') && (answer != 'N'));//如果输入的不是yes/no就继续询问
	return (answer == 'Y');//返回输入的是否为yes,是返回true否则返回false
}

完整代码获取:https://gitee.com/lihiyu/guess_animal

你可能感兴趣的:(数据结构基础)