mercredi 20 mai 2020

C++ Accessing if statements if they are inside for loop

Im writing snake in c++, and i came across some problems. My printBoard method just prints the map for the game. It has access to array where the clean map is sitting. But the most important part is that it checks if coordinates of current field are the same as Food coordinats or one of snake's body coordinates. Obviously if they arent it just prints clean field. And here goes the question, can i somehow connect if statement from inside of my last loop to if and else outside. now its saying there is no previous if statement?

void Board::printBoard(Player player1)
{
    system("cls");
    for(unsigned int i=0; i< BOARD_HEIGHT ; i++)
    {
        for(unsigned int j=0; j< BOARD_WIDTH; j++)
        {
            for(unsigned int z=0; z< player1._snakeBody.size(); z++)
            {
                if(j == player1._snakeBody.at(z).first && i == player1._snakeBody.at(z).second)
                {
                    std::cout<< "S";
                }
            }
            if(j ==_foodXcord && i == _foodYcord)
            {
                std::cout<< "X";
            }
            else
            {
                std::cout << this->_board[i][j];
            }
        }
        std::cout << std::endl;
    }
}

this what i have here isnt right because it prints the blank field even if SnakeBody is found on it.

Aucun commentaire:

Enregistrer un commentaire