#create a shuffled list and get the next index everytime

38 messages · Page 1 of 1 (latest)

jaunty nebulaBOT
#

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.

ancient adder
#

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

lusty rampart
#

!howto ask

jaunty nebulaBOT
# lusty rampart !howto ask
How to Ask a Programming Question

Anyone can ask a question in our programming channels. Following the guide Writing The Perfect Question is recommended.

What to Post

State your problem clearly and provide all necessary details:

  • the relevant portion of your code, or all of it
  • the expected output
  • the actual output (or the full error)
    :trophy: Gold Standard: Minimal Reproducible Example
Where to Post

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:

  • Compiler Explorer for most C/C++ snippets
  • OnlineGDB for interaction, debugging
    :no_entry: Do not post screenshots, let alone photos of your screen!
lusty rampart
#

!sc

jaunty nebulaBOT
# lusty rampart !sc
Slacking, Herald of Monke
Please Do Not Send Screenshots!

They're hard to read and prevent copying and pasting.

ancient adder
#
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();
    }
}
ancient adder
#

@lusty rampart But you can't try to help me ?

lusty rampart
#

Sorry, didnt see

#

Could you state your question more clearly?

ancient adder
#

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...

lusty rampart
#

So what is your question?

ancient adder
#

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

lusty rampart
ancient adder
#

what I just wrote above

lusty rampart
#

Which part are you having issues with?

ancient adder
#

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

lusty rampart
#

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++

ancient adder
#

yeah this is a vector

lusty rampart
#

The easiest way to sort a vector is to use the standard library’s std::sort

#

Which is in <algorithm>

ancient adder
#

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

lusty rampart
#

Oh, sorry

#

There is a shuffle function

#

It was introduced in C++20 so you might have to change a setting in your IDE

lusty rampart
ancient adder
#

Okay I'll try to see how it works

lusty rampart
#

Which IDE do you use?

ancient adder
#

Clion

lusty rampart
#

Let me know how it went afterwards 🙂

ancient adder
#

no problem