2

I have just initialized an array of random values ​​but I would like that in the case where 3 values ​​are aligned, my array is regenerated in such a way that, as of its initialization my array never comprises 3 consecutive similar values.

I am a beginner in C ++, and I don't know what it is the best way to go about it, is it better to create functions that will check if my previous box in my array is similar? or do it directly in my array set up function? Below here is the creation of my table. Many thanks for your help.

void CPlateau::CreatePlateau()
{
    m_arrPlateau = new int*[m_iLignes];
    for (int ligne = 0; ligne < m_iLignes; ligne++)
    {
        m_arrPlateau[ligne] = new int[m_iColonnes];
            
        for (int col = 0; col < m_iColonnes; col++)
            m_arrPlateau[ligne][col] = 0;
    }
}

void CPlateau::SetupPlateau()
{ 
    if (m_arrPlateau == NULL)
        CreatePlateau();
    
    for (int ligne = 0; ligne < m_iLignes; ligne++)
        for (int col = 0; col < m_iColonnes; col++)
            m_arrPlateau[ligne][col] = (rand() % 7);
}
Priyanshul Govil
  • 518
  • 5
  • 20
SimonW.
  • 33
  • 6
  • Here, "similar values" are "equal values", right? – JohnFilleau Feb 26 '20 at 18:49
  • The question is unclear to me. You want random values, but not three same in a row. Fine. Randomiize, keep track of the last two values and when they are the same randomize the third until it is different. What confuses me about your question is, what is your problem? – Yunnosch Feb 26 '20 at 19:59
  • Don't use raw pointers, use `std::vector` (or `std::array`). Don't use `rand()`, use `std::random_device`. You have a matrix, not an array, so what do you mean with "three consecutive values"? – Werner Henze Feb 26 '20 at 20:17
  • @John Yes I mean Equal value. – SimonW. Feb 26 '20 at 20:37
  • @Yunnosch It might be more clear if I explain you why i'm doing this: I want to create a match-3 game like bejeweled, so it's necessary for me to prohibit that 3 consecutive values, horizontal or vertical are equal during initialization – SimonW. Feb 26 '20 at 20:37
  • @Werner Henze thank you for your advice, I will try to take it into account. I'm not familiar with programming – SimonW. Feb 26 '20 at 20:40

1 Answers1

0

Tried it in an online compiler:

#include <iostream>
#include <vector>
#include <random>

int main()
{
    const int CANDY_TYPES = 7;
    const int ROWS = 8;
    const int COLUMNS = 8;
    std::vector<std::vector<int>> table(ROWS, std::vector<int>(COLUMNS, -1)); // -1 means uninitialized
    
    std::random_device rd;  //Will be used to obtain a seed for the random number engine
    std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
    std::uniform_int_distribution<int> dist(0, CANDY_TYPES - 1);
    
    for (int i = 0; i < ROWS; ++i)
        for (int j = 0; j < COLUMNS; ++j) {
            do {
                const int candy = dist(gen);
                
                if (!(i > 1 && table[i - 1][j] == candy && table[i - 2][j] == candy) // vertical check
                    && !(j > 1 && table[i][j - 1] == candy && table[i][j - 2] == candy) // horizontal check
                    /*&& !(i > 1 && j > 1 && table[i - 1][j - 1] == candy && table[i - 2][j - 2] == candy)*/) // diagonal check
                {
                    table[i][j] = candy;
                }
            } while (table[i][j] == -1);
        }
        
    // print console
    for (int i = 0; i < ROWS; ++i) {
        for (int j = 0; j < COLUMNS; ++j)
            std::cout << table[i][j] << " ";
            
        std::cout << std::endl;
    }
    
    return 0;
}

Random number generation copied from std::uniform_int_distribution.

Optionally there is a diagonal check commented out.

Kao
  • 537
  • 5
  • 12
  • note that `std::random_device` might return constant values if the system doesn't have a true random number source, see https://stackoverflow.com/questions/18880654/ – M.M Jan 28 '21 at 21:13