
#pragma once
#include "System.h"
#include <fstream>
#include <iostream>

using namespace std;
// 

// ナンバーワンストーンの取得
int GetNumberStone(int Num, SHOTPOS *pShotPos)
{
	int iRet = -1;
	float Shot[16][2] = {};
	float TeeX = 2.375;
	float TeeY = 4.88;

	for (unsigned int i = 0; i < 16; i++){
		for (unsigned int j = 0; j < i; j++){
			if ((Shot[j][0] == 0 && Shot[j][1] == 0 ) || ((pow(TeeX - Shot[j][0], 2) + pow(TeeY - Shot[j][1], 2)) >(pow(TeeX - GameState.body[i][0], 2) + pow(TeeY - GameState.body[i][1], 2))))
			{
				memcpy(Shot[j + 1], Shot[j], sizeof(float)*(15 - j));
				Shot[j][0] = GameState.body[i][0];
				Shot[j][1] = GameState.body[i][1];
				break;
			}
		}
	}

	for (unsigned int i = 0; i < 16; i++){
		if (!(Shot[i][0] == 0 && Shot[i][1] == 0) && Shot[Num][0] == GameState.body[i][0] && Shot[Num][1] == GameState.body[i][1]){
			iRet = i % 2;
		}
	}

	pShotPos->x = Shot[Num][0];
	pShotPos->y = Shot[Num][1];

	return iRet;
}

// 改行の削除
void DeleteNL(char *Message)
{
	char *p;
	p = Message;

	while (*p != 0x00){
		if (*p == '\n' || *p == '\r'){
			*p = 0x00;
			break;
		}
		p++;
	}
	return;
}

// 引数の取得
BOOL GetArgument(char *lpResult, size_t numberOfElements, char *Message, int n)
{
	BOOL bRet = FALSE;
	char *p, *q;

	if (Message != NULL){
		p = Message;
		while (*p == ' '){
			p++;
		}

		// ポインタを取得したい引数の先頭に合わせる
		for (int i = 0; i<n; i++){
			while (*p != ' '){
				if (*p == 0x00){
					return FALSE;
				}
				p++;
			}
			while (*p == ' '){
				p++;
			}
		}

		// 取得したい引数をlpResultにコピーする
		q = strstr(p, " ");
		if (q == NULL){
			strcpy(lpResult, p);
			bRet = TRUE;
		}
		else{
			strncpy_s(lpResult, numberOfElements, p, q - p);
			if ((unsigned int)(q - p) < numberOfElements){
				lpResult[q - p] = 0x00;
			}
			bRet = TRUE;
		}
	}

	return bRet;
}

// コマンド処理
int DoCommand(char *Message)
{
	int	iRet = TRUE;
	char CMD[1024];
	char Buffer[1024];

	// 改行の削除
	DeleteNL(Message);

	// コマンドの取得
	if (GetArgument(CMD, sizeof(CMD), Message, 0) == FALSE){
		iRet = FALSE;
		return iRet;
	}

	// コマンド処理
	if (_stricmp(CMD, "NEWGAME") == 0){
		// GAMESTATE の初期化
		memset(GameState.body, 0x00, sizeof(float)* 32);
		memset(GameState.Score, 0x00, sizeof(int)* 10);
		GameState.ShotNum = 0;
		GameState.CurEnd = 0;
		GameState.LastEnd = 0;
		GameState.WhiteToMove = 0;
	}
	else if (_stricmp(CMD, "ISREADY") == 0){
		Send("READYOK");
	}
	else if (_stricmp(CMD, "POSITION") == 0){
		for (unsigned int i = 0; i<16; i++){
			// X 座標
			if (GetArgument(Buffer, sizeof(Buffer), Message, 2 * i + 1) == FALSE){
				iRet = FALSE;
				return iRet;
			}
			GameState.body[i][0] = (float)atof(Buffer);

			// Y 座標
			if (GetArgument(Buffer, sizeof(Buffer), Message, 2 * i + 2) == FALSE){
				iRet = FALSE;
				return iRet;
			}
			GameState.body[i][1] = (float)atof(Buffer);
		}
	}
	else if (_stricmp(CMD, "SETSTATE") == 0){
		// 現在のショット数
		if (GetArgument(Buffer, sizeof(Buffer), Message, 1) == FALSE){
			iRet = FALSE;
			return iRet;
		}
		GameState.ShotNum = atoi(Buffer);

		// 現在のエンド数
		if (GetArgument(Buffer, sizeof(Buffer), Message, 2) == FALSE){
			iRet = FALSE;
			return iRet;
		}
		GameState.CurEnd = atoi(Buffer);

		// 最終エンド
		if (GetArgument(Buffer, sizeof(Buffer), Message, 3) == FALSE){
			iRet = FALSE;
			return iRet;
		}
		GameState.LastEnd = atoi(Buffer);

		// 手番
		if (GetArgument(Buffer, sizeof(Buffer), Message, 4) == FALSE){
			iRet = FALSE;
			return iRet;
		}
		GameState.WhiteToMove = atoi(Buffer);
	}
	else if (_stricmp(CMD, "GO") == 0){
		SHOTPOS ShotPos;
		SHOTVEC ShotVec;
		int i;
		int ShotNum;

		if (GameState.ShotNum == 0){
			// 最善ショットの送信
			ShotPos.x = 2.375;
			ShotPos.y = 8.0;
			ShotPos.angle = 0;
			CreateShot(ShotPos, &ShotVec);
			sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
			Send(Buffer);
		}
		else if (GameState.ShotNum == 1){
			// 最善ショットの送信
			ShotPos.x = 2.375;
			ShotPos.y = 8.0;
			ShotPos.angle = 0;
			CreateShot(ShotPos, &ShotVec);
			sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
			Send(Buffer);
		}
		else if (GameState.ShotNum == 2){
			// 最善ショットの送信
			ShotPos.x = 2.375;
			ShotPos.y = 10.0;
			ShotPos.angle = 0;
			CreateShot(ShotPos, &ShotVec);
			sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
			Send(Buffer);
		}
		else if (GameState.ShotNum == 3){
			// 最善ショットの送信
			ShotPos.x = 2.375;
			ShotPos.y = 10.0;
			ShotPos.angle = 0;
			CreateShot(ShotPos, &ShotVec);
			sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
			Send(Buffer);
		}
		else if (GameState.ShotNum == 4){
			//最善ショットの送信
			if (GameState.body[3][1] >= 4.88){
				ShotPos.x = GameState.body[3][0];
				ShotPos.y = GameState.body[3][1];
				ShotPos.angle = 0;
				CreateHitShot(ShotPos, 14, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}

		}
		else if (GameState.ShotNum == 5){
			// 最善ショットの送信
			if (GameState.body[4][1] >= 4.88){
				ShotPos.x = GameState.body[4][0];
				ShotPos.y = GameState.body[4][1] - 0.5;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}

		}
		else if (GameState.ShotNum == 6){
			if (GameState.body[3][1] >= 4.88){
				ShotPos.x = GameState.body[5][0];
				ShotPos.y = GameState.body[5][1];
				ShotPos.angle = 0;
				CreateHitShot(ShotPos, 14, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}

		}
		else if (GameState.ShotNum == 7){
			// 最善ショットの送信
			if (GameState.body[4][1] >= 4.88){
				ShotPos.x = GameState.body[6][0];
				ShotPos.y = GameState.body[6][1] - 0.5;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
		}
		else if (GameState.ShotNum == 8){
			if (GameState.body[3][1] >= 4.88){
				ShotPos.x = GameState.body[7][0];
				ShotPos.y = GameState.body[7][1];
				ShotPos.angle = 0;
				CreateHitShot(ShotPos, 14, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}

		}
		else if (GameState.ShotNum == 9){
			// 最善ショットの送信
			if (GameState.body[4][1] >= 4.88){
				ShotPos.x = GameState.body[8][0];
				ShotPos.y = GameState.body[8][1] - 0.5;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
		}
		else if (GameState.ShotNum == 10){
			if (GameState.body[3][1] >= 4.88){
				ShotPos.x = GameState.body[9][0];
				ShotPos.y = GameState.body[9][1];
				ShotPos.angle = 0;
				CreateHitShot(ShotPos, 14, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}

		}
		else if (GameState.ShotNum == 11){
			// 最善ショットの送信
			if (GameState.body[4][1] >= 4.88){
				ShotPos.x = GameState.body[10][0];
				ShotPos.y = GameState.body[10][1] - 0.5;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
		}
		else if (GameState.ShotNum == 12){
			if (GameState.body[3][1] >= 4.88){
				ShotPos.x = GameState.body[11][0];
				ShotPos.y = GameState.body[11][1];
				ShotPos.angle = 0;
				CreateHitShot(ShotPos, 14, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}

		}
		else if (GameState.ShotNum == 13){
			// 最善ショットの送信
			if (GameState.body[4][1] >= 4.88){
				ShotPos.x = GameState.body[12][0];
				ShotPos.y = GameState.body[12][1] - 0.5;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
			else{
				ShotPos.x = 2.375;
				ShotPos.y = 4.88;
				ShotPos.angle = 0;
				CreateShot(ShotPos, &ShotVec);
				sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
				Send(Buffer);
			}
		}
		else if (GameState.ShotNum == 14){
			ShotPos.x = 2.375;
			ShotPos.y = 4.88;
			ShotPos.angle = 0;
			CreateShot(ShotPos, &ShotVec);
			sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
			Send(Buffer);

		}
		else if (GameState.ShotNum == 15){
			// 最善ショットの送信
			ShotPos.x = 2.375;
			ShotPos.y = 4.88;
			ShotPos.angle = 0;
			CreateShot(ShotPos, &ShotVec);
			sprintf_s(Buffer, sizeof(Buffer), "BESTSHOT %f %f %d", ShotVec.x, ShotVec.y, ShotVec.angle);
			Send(Buffer);
		}
		
		//ofstream fout("zahyo.csv");
		//for (int a = 0; a < 16; a++)
		//	fout << GameState.body[a][0] << " " << GameState.body[a][1] << endl;
		//fout.close();
	}



	return iRet;
}
