When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
38 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.
I don't know how to create a function to create a shuffled list and to get the next index everytime
create a function to create a shuffled list and to get the next index everytime
create a shuffled list and get the next index everytime
!howto ask
Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.
State your problem clearly and provide all necessary details:
Provide the relevant code in the message, and format it nicely with a code block*. If it's too much for one message, you can upload it:
!sc
They're hard to read and prevent copying and pasting.
Game::Game()
{
grid = Grid();
blocks = GetAllBlocks();
//currentBlock = blocks[0];
currentBlock = GetRandomBlock();
nextBlock = GetRandomBlock();
}
Block Game::GetRandomBlock() //A remplacer par GetNextBlock()
{
if(blocks.empty())
{
blocks = GetAllBlocks();
}
int randomIndex = rand() % blocks.size();
Block block = blocks[randomIndex];
blocks.erase(blocks.begin() + randomIndex);
return block;
}
std::vector<Block> Game::GetAllBlocks()
{
return {SmallLBlock(), TBlock(), SmallCrossBlock(), ZBlock(), SmallTBlock(), SquareBlock(), UBlock(), ThreeBlock(), LightningBlock(), SmallCornerBlock(), SmallStairsBlock()}; //Continuer de mettre le reste des blocs dont ceux qui se répètent plusieurs fois
}
void Game::LockBlock()
{
std::vector<Position> tiles = currentBlock.GetCellPositions();
if(BlockFits())
{
for(Position item: tiles)
{
currentBlock.id = currentPlayer;
grid.grid[item.row][item.column] = currentBlock.id;
}
if (currentPlayer < numberOfPlayers)
{
currentPlayer += 1;
} else {
currentPlayer = 1;
}
currentBlock = nextBlock;
nextBlock = GetRandomBlock();
}
}
@lusty rampart But you can't try to help me ?
So I need to create a list with all my blocks as you see is GetAllBlocks, I need to shuffle this list. After that I have a variable currentBlock which has to take the first block of the list and then nextBlock which is the next one.
After that, the currentBlock will become the nextBlock in the second screen LockBlock().
I need to do that bcs I have to print the 5 next block in the list in my game.
So we'll maybe need to delete the GetRandomBlock function or just to replace it by a GetNextBlock() function...
So what is your question?
how to do that xD
it's been like 3 or 4 days i'm trying but with no results, I'm bad at c++ and have to finish my project before next friday, and I have others things to do in this project
Which part?
what I just wrote above
Which part are you having issues with?
like everything about this, create a list which I think is a vector in C, shuffle it bcs everytime I try with things on internet it doesn't work and after I have that I'll have to replace the variable to get the nextBlock
If by list you mean a container of contiguously stored items that is an array
std::vector is a class from the standard library that can act as an array, but it can also change in size
Which arrays can not by default in C++
yeah this is a vector
The easiest way to sort a vector is to use the standard library’s std::sort
Which is in <algorithm>
but how can I shuffle it ? Bcs everything I found was like to create a function to shuffle bcs their is no native function to do it
Oh, sorry
There is a shuffle function
It was introduced in C++20 so you might have to change a setting in your IDE
This is also in <algorithm>
Okay I'll try to see how it works
Which IDE do you use?
Clion
Let me know how it went afterwards 🙂
no problem