⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree.cpp

📁 EVC 环境下的五子棋, 用模块器5.0可以跑一下!
💻 CPP
字号:
#include "StdAfx.h"
/**
 *
 * 文件名:	Tree.cpp
 * 描述:	经验树函数实现
 * 作者:	魏罡
 * 时间:	2005.7.10 - 2005.10.6
 *
 * file name:	Tree.cpp
 * description: 
 * author:		WEI-Gang
 * time:		2005.7.10 - 2005.10.6
 *
 */

#include "Tree.h"
//#include <fstream.h>

//iostream


/////////////////////////////////////////////////

CTree::CTree(){
	play = &head;
	head.parent = NULL;
	head.left = NULL;
	head.right = NULL;
	head.lay[0] = '\0';
	dir = 1;
	//ifstream inf("data/tree.tid");
	//inf>>dir;
	//inf.close();
	FILE *f;
	f = fopen("data/tree.tid", "r");
}

CTree::~CTree(){
	play = head.left;
	node * temp = play;
	while(play && play != &head){
		if(play->left){
			play = play->left;
		}
		else{
			temp = play;
			temp->parent->left = temp->right;
			play = play->right?play->right:play->parent;
			delete temp;
		}
	}
}

void CTree::Play(int x,int y,char layout[]){
	//行棋(x,y)。
	node * tempnode = play->left;
	while(tempnode){
		if(tempnode->x == x && tempnode->y == y){//找到结点,赋值
			play = tempnode;
			strcpy(play->lay,layout);
			break;
		}
		tempnode = tempnode->right;
	}
	if(!tempnode){//找不到,生成新结点。
		tempnode = new node;
		tempnode->parent = play;
		tempnode->right = play->left;
		play->left = tempnode;
		play = tempnode;
		play->left = NULL;
		strcpy(play->lay,layout);
		play->x = x;
		play->y = y;
		play->win = 0;
		play->lose = 0;
	}
}

void CTree::ReadData(int time){
	//读入当前第time手的下一手数据。
	int x = play->x;
	int y = play->y;
	char * layout = play->lay;
	char filename[2][30] = {"data/td0/XXX/XXXX.trg","data/td0/XXX/XXXX.trg"};
	filename[0][9] = '0' + time/100;
	filename[0][10] = '0' + (time%100)/10;
	filename[0][11] = '0' + time%10;
	filename[0][13] = '0' + x/10;
	filename[0][14] = '0' + x%10;
	filename[0][15] = '0' + y/10;
	filename[0][16] = '0' + y%10;
	strcpy(filename[1],filename[0]);
	filename[dir][7] = '1';

//	ifstream inf(filename[0]);
//	ofstream outf(filename[1]);

	FILE *fi, *fo;
	fi = fopen(filename[0], "r");
	fo = fopen(filename[1], "r");

	/*
	char templayout[450];
	int  finded = 0;
	
	//找到棋形一样的记录
	while(!inf.eof()){
		inf.getline(templayout,449);
		if(strlen(templayout)<1){
			continue;
		}
		if(finded != 0||strcmp(templayout,layout)){
			outf<<templayout<<endl;
			inf.getline(templayout,449);
			outf<<templayout<<endl;
		}
		else{
			//找到,读入并生成儿子结点
			node * temp = NULL;
			inf>>x;
			while(x != -1){
				temp = new node;
				temp->x = x;
				inf>>temp->y>>temp->win>>temp->lose;
				temp->right = play->left;
				play->left = temp;
				temp->parent = play;
				temp->left = NULL;
				inf>>x;
			}
			inf.getline(templayout,499);
			finded = 1;
		}
	}
	outf.close();
	inf.close();
	*/
}

void CTree::ChangeValue(int val){
	//改变胜负得分。
	node * temp = play;
	while(temp != &head && temp){
		if(val > 0){
			temp->win ++;
		}
		else{
			temp->lose ++;
		}
		temp = temp->parent;
		val = -val;
	}
}

void CTree::Save(int time){
	//保存树中数据到文件。
	/*
	int x,y;
	char filename[30] = "data/td0/XXX/XXXX.trg";
	if(dir){
		filename[7] = '1';
	}
	node * temp;
	while(play != &head){
		x = play->x;
		y = play->y;
		filename[9] = '0' + time/100;
		filename[10] = '0' + (time%100)/10;
		filename[11] = '0' + time%10;
		filename[13] = '0' + x/10;
		filename[14] = '0' + x%10;
		filename[15] = '0' + y/10;
		filename[16] = '0' + y%10;
		ofstream outf(filename,ios::app);
		outf<<play->lay<<endl;
		//写入儿子结点数据
		temp = play->left;
		while(temp){
			outf<<temp->x<<" "<<temp->y<<" "<<temp->win<<" "<<temp->lose<<" ";
			temp = temp->right;
		}
		outf<<-1<<endl;
		outf.close();
		play = play->parent;
		time --;
	}
	dir = !dir;
	ofstream outf("data/tree.tid");
	outf<<dir;
	outf.close();
	*/
}

long CTree::GetMax(int &x,int &y,int sx[],int sy[]){
	//获得最大胜负值结点的x,y值并记录下所有儿子结点的xy,返回结点胜负值。
	node * temp = play->left;
	int i = 0;
	long max = -99999999;
	while(temp){
		sx[i] = temp->x;
		sy[i] = temp->y;
		if(max < GetValue(temp->win,temp->lose)){
			max = GetValue(temp->win,temp->lose);
			x = sx[i];
			y = sy[i];
		}
		temp = temp->right;
		i ++;
	}
	sx[i] = -1;
	return max;
}

long inline CTree::GetValue(long win,long lost){
	//根据胜负次数,计算胜负值。
	return win*win - lost*lost - win*lost;//暂定
}

int CTree::Back(int &x,int &y){
	//返回到树中上一步行棋并带回此次行棋的坐标(x,y),
	//如树中无上一步记录,则返回1,否则返回0。
	x = play->x;
	y = play->y;
	play = play->parent;

	//清除儿子结点
	node * temp = play->left;
	while(temp){
		play->left = temp->right;
		delete temp;
		temp = play->left;
	}

	return play == &head?1:0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -