jeudi 31 janvier 2019

A boolean function to return a change in a vector and true/false

I am learning c++ and have taken an online course with a project to create a 2048 game.

I am in the final stages and have come across a small problem. I am trying to create a function which takes 2 numbers as a range of values of a vector. It pushes all the numbers to the left and adds two adjacent numbers if they are equal. For example:

v (before):

[0 0 0 5 1 2 0 2 2 0 0 0 2 0 0 2 6 0 0 5 0 6 4]

bi: 1

ei: 4


v (after):

[0 5 0 0 1 2 0 2 2 0 0 0 2 0 0 2 6 0 0 5 0 6 4]

return: true

The function:

bool left_push(std::vector<int>& v, int bi, int ei){

    std::vector<int> tmp;
    bool check = false ;

    for(int i = bi+2 ; i <= ei ; i++){

        if(v[i]==v[i-1]){
            v[i-1] = v[i]+v[i];
            v[i] = 0;

            check = true;
        }

        if(v[i-1]==0){
            v[i-1] == v[i];
            v[i] = 0;

            check = true ;
        }

    return check;
}

The issue is that when I test this, nothing happens. At all.

I am not too sure what I am doing wrong. I would appreciate any pointers.

Aucun commentaire:

Enregistrer un commentaire