jeudi 25 août 2016

No repeat randomizing in objective C (Xcode)

i am lost. So i need any help. I wrote this code for a simple game. It is randomizing a number and then do something with SWITCH. It is simple but i want to have a task only once in one round. In my case round is 3 task. After a round is done, i want to reset my variables and start a new round again. So it is random, but task wonn't happen twice in one round.

I wrote this code but it didn't work. If i click on button which will start rand(), it will do 1st task, then i click again, it will do 2nd task, but when i click 3rd times nothing is happening. 3rd task works for 4th click. Thank's for any help and sorry for my english :)

int text;
int x=3; //this is number of tasks in one round, so in switch i have 3 tasks
int z=9999;
int x0=1; //this mean's that task 0 can happen only once in one round
int x1=1;
int x2=1;



-(void)reset{  // this is rof checking if x is <1, then it will reset all variables (start a new round)

if (x<1) {
    x=3;
    x0=1;
    x1=1;
    x2=1;
    }
     }

-(void)randomize{

  text = rand() % 3; //this wil get me a random number for switch
while(text==z) //this is because i don't want to have same number  consecutively
{text = rand() % 3;}

}

- (IBAction)random:(id)sender {


   [self randomize];

    switch (text) {


        case 0:
            if (x0!=0){

                //something will happen

                z=0;
                x0--;
                x--;
                [self reset];
                break;

            }

            else {

                [self randomize];

            }


        case 1:
            if (x1!=0) {

                //something will happen

                z=1;
                x1--;
                x--;
                [self reset];
                break;

            }
            else {

                [self randomize];
                break;
            }

        case 2:


            if (x2!=0) {

                //something will happen

                z=2;
                x2--;
                x--;
                [self reset];


            }
            else {

               [self randomize];

            }


    }

}




Aucun commentaire:

Enregistrer un commentaire