コード例 #1
0
ファイル: main.cpp プロジェクト: hainh/GL-AI-Contest-2015
// This function is called automatically when it's your turn.
// Remember to call AI_Move() with a valid move before the time is run out.
// See <ai/Game.h> and <ai/AI.h> for supported APIs.
void AI_Update()
{
	FILETIME ft_now;
	GetSystemTimeAsFileTime(&ft_now);

	AI *p_ai = AI::GetInstance();
	int * _board = p_ai->GetBoard();	// Access block at (x, y) by using board[CONVERT_COORD(x,y)]
	Position myPos = p_ai->GetMyPosition();
	Position enemyPos = p_ai->GetEnemyPosition();

	int direction = AiMove(_board, myPos, enemyPos);

	if(direction)
	{
		printf("Move: %d", direction);

		//Remember to call AI_Move() within allowed time
		Game::GetInstance()->AI_Move(direction);
	}
	else
	{
		Game::GetInstance()->AI_Move(0);
		printf("Damn, I was trapped!\n");
	}

	FILETIME ft_now2;
	GetSystemTimeAsFileTime(&ft_now2);

	LONGLONG ll_now = (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);
	LONGLONG ll_now2 = (LONGLONG)ft_now2.dwLowDateTime + ((LONGLONG)(ft_now2.dwHighDateTime) << 32LL);

	printf("time = %lld\n", (ll_now2 - ll_now) / 10000);
}
//Human move     MIN    O=1
int HumanMove(int b[9])									//returns MINIMUM of the scores for a move 
{
	int bestValue=100;                        
	int stateValue=CheckStatus(b);

	
	if(stateValue!=20)       			 		
	{
		if(stateValue==1)
		{stateValue=(10);}
		else if(stateValue==(-1))
		{stateValue=(-10);}

		return stateValue;
	}
	

	for(int k=0;k<9;k++)
	{
		if(CheckEmpty(b,k))
		{
			b[k]=1;
			//delay(500);
			int val=AiMove(b);

			if (val <= bestValue)  				
			{
				bestValue = val;
			}

			b[k]=0;
		}
	}

	return bestValue;
}
コード例 #3
0
ファイル: main.cpp プロジェクト: hainh/GL-AI-Contest-2015
void test()
{
	//void main()
	

	int board_state[] = {
		1, 0, 0, 0, 5, 0, 0, 5, 0, 5, 5,
		0, 0, 0, 0, 5, 0, 5, 0, 0, 5, 5,
		0, 0, 0, 0, 0, 5, 0, 0, 5, 5, 5,
		0, 0, 5, 0, 0, 5, 5, 5, 5, 5, 5,
		0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 5, 5, 5, 5, 0, 5,
		5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 5, 5, 0, 0, 0, 0, 5,
		0, 0, 0, 0, 0, 0, 5, 5, 3, 5, 5,
		5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
		5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
	};//               |

	auto b = copyFrom(board_state);
	unsigned int* rBoard = b->cells;

	float total = 0;
	system("pause");

	for (int i = 0; i < 45; ++i)
	{

		FILETIME now;
		GetSystemTimeAsFileTime(&now);
		LONGLONG now1 = (LONGLONG)now.dwLowDateTime + ((LONGLONG)(now.dwHighDateTime) << 32LL);

		int maxDepth = deepMove_ia(rBoard, 8, 8, 0, 100, false);

		GetSystemTimeAsFileTime(&now);
		LONGLONG now2 = (LONGLONG)now.dwLowDateTime + ((LONGLONG)(now.dwHighDateTime) << 32LL);

		int maxDepth2 = deepMove(b, 8, 8, 0, 100, false);

		GetSystemTimeAsFileTime(&now);
		LONGLONG now3 = (LONGLONG)now.dwLowDateTime + ((LONGLONG)(now.dwHighDateTime) << 32LL);

		//printf("Max Depth iterative = %d, time = %lld\n", maxDepth, now2 - now1);
		//printf("Max Depth recursive = %d, time = %lld\n", maxDepth2, now3 - now2);
		float rate = (now2 - now1) / (float)(now3 - now2);
		//printf("Ratio iterative/recursive = %f\n", rate);
		total += rate;
	}

	printf("end test, rate = %f", total / 45);
	getchar();

	//extern int evaluateBoard(board* b, const Position& myPos, const Position& opPos);
	Position myPos = Position(0, 0);
	Position opPos = Position(8, 8);
	
	for (int y = 0; y < MAP_SIZE; ++y)
	{
		for (int x = 0; x < MAP_SIZE; ++x)
		{
			printf("%d  ", board_state[CONVERT_COORD(x, y)]);
		}
		printf("\n\n");
	}

	while (true)
	{
		FILETIME ft_now;
		GetSystemTimeAsFileTime(&ft_now);
		LONGLONG ll_now = (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);

		int dir = AiMove(board_state, myPos, opPos);

		GetSystemTimeAsFileTime(&ft_now);
		LONGLONG ll_now2 = (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);
		printf("Player1: time = %lld, dir = %d\n", (ll_now2 - ll_now) / 10000, dir);
		board_state[CONVERT_COORD(myPos.x, myPos.y)] = BLOCK_PLAYER_1_TRAIL;
		switch (dir)
		{
		case 1:
			myPos.x--;
			break;
		case 2:
			myPos.y--;
			break;
		case 3:
			myPos.x++;
			break;
		case 4:
			myPos.y++;
			break;
		default:
			break;
		}
		board_state[CONVERT_COORD(myPos.x, myPos.y)] = BLOCK_PLAYER_1;
		for (int y = 0; y < MAP_SIZE; ++y)
		{
			for (int x = 0; x < MAP_SIZE; ++x)
			{
				printf("%d  ", board_state[CONVERT_COORD(x, y)]);
			}
			printf("\n\n");
		}

		//int charCode = 0;
		//do
		//{
		//	printf("Your move: ");
		//	charCode = getchar();
		//	while (getchar() != '\n'){}
		//}
		//while (charCode != 'a' && charCode != 's' && charCode != 'd' && charCode != 'w');
		//board_state[CONVERT_COORD(opPos.x, opPos.y)] = BLOCK_PLAYER_2_TRAIL;
		//switch (charCode)
		//{
		//case 'a':
		//	opPos.x--;
		//	break;
		//case 'w':
		//	opPos.y--;
		//	break;
		//case 'd':
		//	opPos.x++;
		//	break;
		//case 's':
		//	opPos.y++;
		//	break;
		//default:
		//	break;
		//}
		//board_state[CONVERT_COORD(opPos.x, opPos.y)] = BLOCK_PLAYER_2;

		printf("Press Enter to move next ");
		while (getchar() != '\n');

		GetSystemTimeAsFileTime(&ft_now);
		ll_now = (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);

		dir = AiMove(board_state, opPos, myPos);

		GetSystemTimeAsFileTime(&ft_now);
		ll_now2 = (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);
		printf("Player2: time = %lld, dir = %d\n", (ll_now2 - ll_now) / 10000, dir);
		board_state[CONVERT_COORD(opPos.x, opPos.y)] = BLOCK_PLAYER_2_TRAIL;
		switch (dir)
		{
		case 1:
			opPos.x--;
			break;
		case 2:
			opPos.y--;
			break;
		case 3:
			opPos.x++;
			break;
		case 4:
			opPos.y++;
			break;
		default:
			break;
		}
		board_state[CONVERT_COORD(opPos.x, opPos.y)] = BLOCK_PLAYER_2;

		for (int y = 0; y < MAP_SIZE; ++y)
		{
			for (int x = 0; x < MAP_SIZE; ++x)
			{
				printf("%d  ", board_state[CONVERT_COORD(x, y)]);
			}
			printf("\n\n");
		}
	}

}
コード例 #4
0
AiMove AIShell::getBestMove(int** board, int player, int depth, int col, int row) {

    // Base case, check for end state
	int score = 0;
	if(col != -1)
	{
		score = getScore(col, row, player *	-1); // getting score of the current state
	}
    if (depth == depth_limit || isFull(board) || (score <= -996) || (score >= 996))	//if depth or terminal node has been reached
    {


    	AiMove move = AiMove(score);
    	//std::cout<<move.score<<std::endl;
    	//if (move.score == 2){std::cout<<"got 2!!!!" << std::endl;}
    	move.x = col;
    	move.y = row;
    	move.level = depth;

    	if(player == AI_PIECE)
    	{
    		//std::cout<<"Node value for AI move at "<<col<< ", " <<row<<": "<<score<<std::endl;
    	}
    	else
    	{
    		//std::cout<<"Node value for Opponent move at " <<col<< ", "<<row<<": "<<score<<std::endl;
     	}
    	//if(score <= -996 || score >= 996)
    		//std::cout<<score<<std::endl;
    	return move;

    }

    std::vector<AiMove> moves;

    // Do the recursive function calls and construct the moves vector
    for (int x = 0; x < numCols; x++) {
        for (int y = 0; y < numRows; y++) {
        	//std::cout<<"Col:"<<x<<std::endl;
        	//std::cout<<"Row:"<<y<<std::endl;
        	//std::cout<<"Val:"<<board[x][y]<<std::endl;
            if (board[x][y] == 0) {
            	AiMove move;
                //move.x = x;
                //move.y = y;
                board[x][y] = player;
                if (player == 1) {
                    move = getBestMove(board, -1, depth+1,x,y);
                } else {
                    move = getBestMove(board, 1, depth+1,x,y);
                }
                move.x = x;
                move.y = y;
                moves.push_back(move);
                //for (int i=0; i < moves.size(); i++){
                	//std::cout << i << ": " << moves[i].score << std::endl;
                //}
                board[x][y] = 0;
                if(gravityOn)
                	break;
            }
        }
    }

    // Pick the best move for the current player
    //for (int i =0; i < moves.size();i++)
    //{
    	//std::cout<< "m: " << moves[i].score<< std::endl;
    //}

/*
    if(depth == 1 || depth == 0)
    {
    	std::cout<<player<<std::endl;
    	if(depth == 1)
    		std::cout<<"first print"<<std::endl;
    	else
    		std::cout<<"final choices"<<std::endl;
    	for(int i = 0; i < moves.size(); i++)
    	{
    		std::cout<<moves[i].x<<" "<<moves[i].y<<" "<<moves[i].score<<" "<<moves[i].level<<std::endl;
    	}
    	std::cout<<"second print"<<std::endl;
    }
*/
    int bestMove = 0;
    int lowestDepth = 999999999;
   // std::cout<<"Prints start here"<<std::endl;
    if (player == 1) {
        int bestScore = -1000000;
        for (int i = 0; i < moves.size(); i++) {
        	//std::cout<<moves[i].x<<" "<<moves[i].y<<" "<<moves[i].score<<" "<<moves[i].level<<std::endl;
            if (moves[i].score > bestScore && moves[i].level <= lowestDepth) {
                bestMove = i;
                bestScore = moves[i].score;
                lowestDepth = moves[i].level;
                //if(lowestDepth == 2)
                	//std::cout<<lowestDepth<<std::endl;
            }
        }
    } else {
        int bestScore = 1000000;
        for (int i = 0; i < moves.size(); i++) {
        	//std`::cout<<moves[i].x<<" "<<moves[i].y<<" "<<moves[i].score<<" "<<moves[i].level<<std::endl;
            if (moves[i].score < bestScore && moves[i].level <= lowestDepth) {
            	//std::cout<< "m: " <<moves[i].score<< std::endl;
                bestMove = i;
                bestScore = moves[i].score;
                lowestDepth = moves[i].level;
                //if(lowestDepth == 2)
                	//std::cout<<lowestDepth<<std::endl;
            }
        }
    }
    //if(depth <= 1)
    	//std::cout<<"Move chosen"<<moves[bestMove].x<<" "<<moves[bestMove].y<<" "<<moves[bestMove].score<<" "<<moves[bestMove].level<<std::endl;
    // Return the best move
    //std::cout<<"Prints end here"<<std::endl;

    return moves[bestMove];
}