Exemple #1
0
int main() {
    int matchResult = 0;
    int **board = createBoard();
    printBoard(board);
    char *currentPlayer = PLAYER_ONE;
    while(matchResult == 0) {
        int move[2];
        getPlayerMove(board, currentPlayer, move);
        matchResult = evalBoard(board);
        if (currentPlayer == PLAYER_ONE) {
            currentPlayer = PLAYER_TWO;
        }
        else {
            currentPlayer = PLAYER_ONE;
        }
    }
    if (matchResult == PLAYER_ONE_VAL) {
        printf("Player %s WON!\n", PLAYER_ONE);
    }
    if (matchResult == PLAYER_TWO_VAL) {
        printf("Player %s WON!\n", PLAYER_TWO);
    }
    if (matchResult == DRAW) {
        printf("DRAW!\n");
    }
    // clean up the memory
    freeBoard(board);

    return 0;
}
chessBoard::move myPlayer::calcMove()
{
	evals.clear();
	evals2.clear();
	looksGood.clear();
	chessBoard::move myMove;
	vector<int> plsno;
	int max = numeric_limits<int>::min();
	int min = numeric_limits<int>::max();
	//First move (ours)
	for(int x = 0; x < myBoard.numMoves(); x++)
	{
		chessBoard local = myBoard;
		local.makeMove(local.nthMove(x));
		//Second move (theirs)
		for(int y = 0; y < local.numMoves(); y++)
		{
			chessBoard localer = local;
			localer.makeMove(localer.nthMove(y));	
			//If the board is in a better or equal state after their move, then add the move that got us here and add the board state value (should have used std::pair)
			if(evalBoard(localer) >= evalBoard(local))
			{
				evals.push_back(evalBoard(localer));
				evals2.push_back(x);	
			}
			//If we can lose, add the potential loss move into a vector
			else if(evalBoard(localer) < -1000000)
			{
				plsno.push_back(x);
			}
		}	
	}	

	//Getting max score of moves
	for(int x = 0; x < evals.size(); x++)
	{
		if(evals[x] > max)
		{
			max = evals[x];
		}	
	}
	//Geting the best moves
	for(int x = 0; x < evals.size(); x++)
	{
		//If it is one of the best moves to make, it is stored in looksGood
		if(evals[x] == max)
		{
			looksGood.push_back(evals2[x]);
		}
	}
	//If something in the best move list can potentially cause us to lose, remove it
	for(int x = 0; x < plsno.size(); x++)
	{
		for(int y = 0; y < looksGood.size(); y++)
		{
			if(plsno[x] == looksGood[y])
			{
				looksGood.erase(looksGood.begin()+(y-1));
			}
		}
	}
	
	//Randomly picks a best move to use
	myMove = myBoard.nthMove(looksGood[rand() % looksGood.size()]);
	//If there are no good moves in the vector, do something random
	if(max == 0 || looksGood.size() == 0)
		myMove = myBoard.nthMove(rand() % myBoard.numMoves());
	
	//Increment the turn counter
	count++;
	
	return myMove;
}
Exemple #3
0
void evalBoardList(const Othello* othello, const BoardList* boards, int evals[]){
    int i;
    for(i = 0; i < boards->length; i++){
        evals[i] = evalBoard(&(boards->boards[i]), Othello_whosTurnIsNow(othello));
    }
}