main.cpp 2.0 KB
Newer Older
D
dev@dev.com 已提交
1 2 3 4 5 6 7 8
#include <cstdio>
#include <memory.h>
#include <vector>
#include <string>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <set>
9
#include <ctime>
D
dev@dev.com 已提交
10 11 12 13 14 15 16 17 18 19
#include <unordered_set>
#include "chesspi.h"

int main()
{
	//初始棋局
	int coordx[32]{5,4,6,3,7,2,8,1,9,2,8,1,3,5,7,9,5,4,6,3,7,2,8,1,9,2,8,1,3,5,7,9};
	int coordy[32]{1,1,1,1,1,1,1,1,1,3,3,4,4,4,4,4,10,10,10,10,10,10,10,10,10,8,8,7,7,7,7,7};
	int alive[32]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};

20
	srand(time(0));
D
dev@dev.com 已提交
21
	printf ("Side(0=RED,1=BLACK):");
D
dev@dev.com 已提交
22 23 24 25 26 27 28 29 30
	chess_node root;

	if (!build_node(coordx,coordy,alive,0,&root))
	{
		printf ("创建棋局失败!\n");
		return 0;
	}

	//回合
D
dev@dev.com 已提交
31 32 33 34 35
	int side = 0;
	scanf("%d",&side);
	side %=2;
	if (side <0)
		side = 1;
D
dev@dev.com 已提交
36 37
	size_t round = 0;
	bool finished = false;
38
	root.side = side % 2;
D
dev@dev.com 已提交
39 40 41
	std::vector<chess_node> history;
	while (!finished)
	{
42 43 44 45 46 47
		printf ("\nround %d:\n==========\n", round);
		if (history.size())
			print_node(root,*history.rbegin());
		else
			print_node(root,root);
		history.push_back(root);
D
dev@dev.com 已提交
48
		if ((round+side) % 2==0)
D
dev@dev.com 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
		{
			//找到棋子
			bool ok = false;
			chess_node new_node;
			while (!ok)
			{
				printf ("请输入四个整数,分别是棋子X,Y,目的X,Y,逗号分割:\n");
				int x1,y1,x2,y2;
				scanf("%d,%d,%d,%d",&x1,&y1,&x2,&y2);
				ok = move_node(root,&new_node,x1+1,y1+1,x2+1,y2+1,round % 2);
				if (!ok)
				{
					printf ("没有选中棋子.\n");
					getchar();
				}
				else {
					root = new_node;

				}
			}
		}
70 71
		else
		{
D
dev@dev.com 已提交
72
			std::vector<chess_node> tree =
D
dev@dev.com 已提交
73
					build_tree(root,round%2,history);
D
dev@dev.com 已提交
74 75 76 77
			size_t best = judge_tree(tree);
			if (best <1 || best + 1 > tree.size())
				break;
			root = tree[best];
78
			printf ("Best % ld Cost: %f, %f, Weight %f\n",best
D
dev@dev.com 已提交
79 80 81
					,root.jump_cost[0],root.jump_cost[1],root.weight
					);
		}
82
		root.side = side % 2;
D
dev@dev.com 已提交
83 84
		++round;
		if (!((root.alive & 0x00010001)==0x00010001))
85 86 87 88 89
		{
			printf ("\nround %lu:\n==========\n", round);
			print_node(root,*history.rbegin());
			finished = true;
		}
D
dev@dev.com 已提交
90 91 92 93 94 95 96
	}
	printf ("FINISHED!\n");

	return 0;
}