#include "Player.h"

class BetterPlayer:public Player{//長さ３専用
public:
	char*GetName(){ return "BetterThinkPlayer.";}
	void Play(MBGame& in){
		if(in.HasEmpty() && in.Length()>3) {
			while(in.SetStone(rand()%in.Length(),rand()%in.Length(),Mark_));
			return;
		}
		//マスの重み
		short Point[]={	43,27 ,41,
						28,100,26,
						42,29 ,44,
		};
		int op=0;
		do{
			if(!in.HasEmpty()) return;
			think(in,Point,sizeof(Point));
			for(int i=0;i<sizeof(Point);i++){
				if(Point[i] >Point[op]){
					op=i;
				}
			}
			Point[op]=-10000;
		}while(!in.SetStone(op%3,op/3,Mark_));
		
	}
protected:
	
	void think(MBGame& in,short Point[],int N){
		const short Posi = 10;
		const short Nega = -5;
		const short denger =9999;
		char Lines[8][3]={
						{0,1,2},{3,4,5},{6,7,8},//横
						{0,3,6},{1,4,7},{2,5,8},//縦
						{0,4,8},{2,4,6},//斜め
		};
		char Side[9][9]={	
						{1,3,-1},//-1は番兵
						{0,2,4,-1},
						{1,5,-1},
						{0,4,6,-1},
						{0,1,2,3,5,6,7,8,-1},
						{2,4,8,-1},
						{3,7,-1},
						{6,4,8,-1},	
						{5,7,-1},
		};
		for(int i=0;i<9;i++){//こまの配置による判定
			for(int j=0;Side[i][j]!= -1;j++){
				int x=0,y=0;
				int N = Side[i][j];
				x = N%3;
				y = N/3;
				Mark M= in.At(x,y);
				if(M == Mark_) Point[Side[i][j]]+= Posi;
				if(M != Mark_ && M != None ) Point[Side[i][j]]+= Nega;
			}
		}
		for(int i=0;i<8;i++){//超危ない時の判定
			int P=0;
			int x=0,y=0;
			for(int j=0;j<3;j++){
				int N= Lines[i][j];
				x = N%3;
				y = N/3;
				Mark M = in.At(x,y);
				if(M == Mark_) P++;
				if(M != None && M != Mark_) P--;
			}
			for(int j=0;j<3;j++){
				if(P == 2) Point[Lines[i][j]]+=denger;
				if(P == -2) Point[Lines[i][j]]+=denger;
			}
		}
	}

};
