ShipHull* GameControl::createShip(Grid3D* grid, std::vector<size_t> coords, std::string type)
{
	//
	auto player = getActivePlayer();
	auto ship_index = mListSelections[player];
	if (mShipNumbers[ship_index] == 0 && type == "")
		return NULL;
	if (type == "")
		mShipNumbers[ship_index]--;
	// Get which type need to be made
	if (type == "")
		type = mShipyard.getNameOfShipType(ship_index);
	auto ship = mShipyard.createShip(type, grid);
	if (ship)
	{
		// Model
		if (getGamePhase() == GP_SET)
		{
			auto result = AstrOWar::GameModelSingleton.addShipToModel(type, coords[0], coords[1], coords[2]);
			addShip(result.resume, ship);
			cout << "Add Ship ErrCode: " << result.errorCode << endl;
		}
		moveShipTo(ship, coords);
		onShipCreated();
	}
	return ship;
}
Beispiel #2
0
Cell MainPlayer::takeTurn(const GameBoard &gameBoard) {
    gamePhase = getGamePhase(gameBoard);
    int maxDepth = 5;
    if (gamePhase == GAME_END) {
        maxDepth = 13;
    }
    startWorking = std::chrono::system_clock::now();
    return recursion(gameBoard, myTile, 0, maxDepth).cell;
}
Beispiel #3
0
int get_Heuristic (Board *board , int depth){

	if ( board == NULL ) {
		logChess(WARN, "The board is null.");
		return NULL_BOARD;
	}

	if ( depth < 0 ) {
		logChess(WARN, "The depth is not correct. Minor than 0.");
		return BAD_DEPTH;
	}

	if(evaluateOurCheck(board)!=KING_IN_CHECK)
	{

		Board tmpBoard;
		memcpy(&tmpBoard, board, sizeof(Board));

		int material = 		getTotalTableMaterial(&tmpBoard);

		int mineMaterial=	getMaterial(board,board->myColor);					 //Calculating mine material to evaluate the game phase
		int movements=		getCurrentMovement(depth);							 //Calculating number of movements to calculate game phase
		int phase	 =		getGamePhase(board,movements,mineMaterial); 		 //Calculating phase of the game to be used in position
		int position = 		getTotalPiecePositionValue(&tmpBoard,depth,phase);	// Calculating game phase according to the position

		int menaced  =		getMenacedValueTotal(tmpBoard.myColor,&tmpBoard);

		int side = 			sideToMove(depth);
		int evaluation = 	0;
		if ( phase == END) {
			evaluation=2*material+ 0.5*position+ 0.1*menaced;
		} else {
			evaluation=1.6*material + 0.9*position+ 0.2*menaced;
		}


		//printf("material:%d + position:%d + menaced:%d = evaluation:%d\n", material, position, menaced, evaluation); //ONLY FOR TEST

		return evaluation * side;
	}

	else 	//we are in check, it's not useful to check with the heuristic
	{
		return OUR_CHECK_VALUE;
	}
}
Beispiel #4
0
/*
 * Helper function performing the login procedure.
 */
static void loginHandleSocket(int socket) {
    // Try to receive the first packet from the client
    rfc response;
    int receive = receivePacket(socket, &response);
    if (receive == -1) {
        errnoPrint("receive");
        return;
    } else if (receive == 0) {
        errorPrint("Remote host closed connection");
        return;
    }

    // Check if we received a login request
    if (equalLiteral(response.main, "LRQ")) {
        // Check RFC version
        if (response.loginRequest.version != RFC_VERSION_NUMBER) {
            sendErrorMessage(socket, "Login Error: Wrong RFC version used");
            infoPrint("Login attempt with wrong RFC version: %d", response.loginRequest.version);
            return;
        }

        // Store username string
        int length = ntohs(response.main.length) - 1;
        char s[length + 1];
        s[length] = '\0';
        memcpy(s, response.loginRequest.name, length);

        // Check Game Phase
        if (getGamePhase() != PHASE_PREPARATION) {
            sendErrorMessage(socket, "Login Error: Game has already started");
            infoPrint("Login attempt while game has already started: \"%s\"", s);
            return;
        }

        // Detect empty name string
        if (length == 0) {
            sendErrorMessage(socket, "Login Error: A username is required");
            infoPrint("Login attempt without a name");
            return;
        }

        // Detect duplicate names
        for (int i = 0; i < MAX_PLAYERS; i++) {
            if (userGetPresent(i)) {
                if (strcmp(userGetName(i), s) == 0) {
                    sendErrorMessage(socket, "Login error: Name already in use");
                    infoPrint("Login attempt with duplicate name: \"%s\"", s);
                    return;
                }
            }
        }

        // Detect too many players
        int pos = userFirstFreeSlot();
        if (pos == -1) {
            sendErrorMessage(socket, "Login Error: Server is full");
            infoPrint("Login attempt while server is full: \"%s\"", s);
            return;
        }

        // Write new user data into "database"
        userSetPresent(pos, 1);
        userSetName(pos, s);
        userSetSocket(pos, socket);
        scoreMarkForUpdate();

        // Start client thread for new user
        int thread = -1;
        for (int i = 0; i < (1 + MAX_PLAYERS); i++) {
            if (threadsFree[i] == -1) {
                thread = i;
            }
        }
        if (pthread_create(&threads[thread], NULL, clientThread, (void*)pos) != 0) {
            threadsFree[thread] = pos;
            errnoPrint("pthread_create");
            return;
        }

        // Send LOK message
        response.main.type[0] = 'L';
        response.main.type[1] = 'O';
        response.main.type[2] = 'K';
        response.main.length = htons(2);
        response.loginResponseOK.version = RFC_VERSION_NUMBER;
        response.loginResponseOK.clientID = (uint8_t)pos;
        if (send(socket, &response, RFC_LOK_SIZE, 0) == -1) {
            errnoPrint("send");
            return;
        }
    } else {
        errorPrint("Unexpected response: %c%c%c", response.main.type[0],
                response.main.type[1], response.main.type[2]);
        return;
    }
}
int Evaluation::evaluate(Board* board, int* moves, int movesSize) {
	int movesCount = board -> genAllLegalMoves(moves, movesSize);
	if (movesCount == 0) {
		if (board -> isInCheck(board -> toMove))
			return MATE_VALUE; // checkmate
		else
			return 0; // stalemate
	}

	int gamePhase = getGamePhase(board);

	int eval = material(board, WHITE) - material(board, BLACK);

	bool useEndingTables = true;
	if (gamePhase <= PHASE_OPENING)
		useEndingTables = false;

	// pawns
	for (int i = 0; i < board -> wPawns.count; i++) {
		if (useEndingTables)
			eval += W_PAWN_POS_ENDING[board -> wPawns.pieces[i]];
		else
			eval += W_PAWN_POS[board -> wPawns.pieces[i]];
	}
	for (int i = 0; i < board -> bPawns.count; i++) {
		if (useEndingTables)
			eval -= B_PAWN_POS_ENDING[board -> bPawns.pieces[i]];
		else
			eval -= B_PAWN_POS[board -> bPawns.pieces[i]];
	}

	// knights
	for (int i = 0; i < board -> wKnights.count; i++) {
		if (useEndingTables)
			eval += KNIGHT_POS_ENDING[board -> wKnights.pieces[i]];
		else
			eval += W_KNIGHT_POS[board -> wKnights.pieces[i]];
	}
	for (int i = 0; i < board -> bKnights.count; i++) {
		if (useEndingTables)
			eval -= KNIGHT_POS_ENDING[board -> bKnights.pieces[i]];
		else
			eval -= B_KNIGHT_POS[board -> bKnights.pieces[i]];
	}

	// bishops
	for (int i = 0; i < board -> wBishops.count; i++) {
		if (useEndingTables)
			eval += BISHOP_POS_ENDING[board -> wBishops.pieces[i]];
		else
			eval += W_BISHOP_POS[board -> wBishops.pieces[i]];
	}
	for (int i = 0; i < board -> bBishops.count; i++) {
		if (useEndingTables)
			eval -= BISHOP_POS_ENDING[board -> bBishops.pieces[i]];
		else
			eval -= B_BISHOP_POS[board -> bBishops.pieces[i]];
	}

	// rooks
	for (int i = 0; i < board -> wRooks.count; i++) {
		if (useEndingTables)
			eval += ROOK_POS_ENDING[board -> wRooks.pieces[i]];
		else
			eval += W_ROOK_POS[board -> wRooks.pieces[i]];
	}
	for (int i = 0; i < board -> bRooks.count; i++) {
		if (useEndingTables)
			eval -= ROOK_POS_ENDING[board -> bRooks.pieces[i]];
		else
			eval -= B_ROOK_POS[board -> bRooks.pieces[i]];
	}

	// queens
	for (int i = 0; i < board -> wQueens.count; i++) {
		if (useEndingTables)
			eval += QUEEN_POS_ENDING[board -> wQueens.pieces[i]];
		else
			eval += W_QUEEN_POS[board -> wQueens.pieces[i]];
	}
	for (int i = 0; i < board -> bQueens.count; i++) {
		if (useEndingTables)
			eval -= QUEEN_POS_ENDING[board -> bQueens.pieces[i]];
		else
			eval -= B_QUEEN_POS[board -> bQueens.pieces[i]];
	}

	// kings
	if (useEndingTables) {
		eval += KING_POS_ENDING[board -> wKing.pieces[0]];
		eval -= KING_POS_ENDING[board -> bKing.pieces[0]];
	} else {
		eval += W_KING_POS[board -> wKing.pieces[0]];
		eval -= B_KING_POS[board -> bKing.pieces[0]];
	}

	return eval * board -> toMove;
}