-2

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.

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];
            }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

0

If you hit the else branch in any of your cases you call randomize, which does not attempt to do a task based on your new random numbers, and then the call to random will return ending the response to the click. This means when you hit one of the else branches no task is performed for the current click - the dead clicks you are seeing.

So in your else branches you need to both obtain a new random number and use it to determine which task to do, which just happens to be exactly what you need to do in response to a click and you already have a method defined to do it... Enough of a hint I hope.

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Thank you very much for your response ! :) i dit it like this docs.google.com/document/d/… , but i have still dead clicks. – Ondřej Veverka Aug 26 '16 at 20:26
  • That link is not complete. You should either add an addendum to your question showing what you've now tried, or better ask a new question. But before doing either have you used the debugger and stepped through the calls? You should quickly find your mistake that way. – CRD Aug 26 '16 at 21:33
0

Take your set of alternatives and shuffle it. Then on each task iterate to the next value in the shuffled set. Voila, no repeats. This negates the need for if/else logic in your switch cases.

At the end of each round, re-shuffle and reset your iteration index. If you want repeats of some cases but not others, add multiple instances of those cases to the set. If repeats are allowed, but not sequentially, check the shuffled set for adjacent pairs and reshuffle if needed.

pjs
  • 18,696
  • 4
  • 27
  • 56
  • thank you a lot for your answer, but i can't imagine it well.. I am new in Objective C coding – Ondřej Veverka Aug 26 '16 at 19:28
  • @OndřejVeverka Okay, [here's how to shuffle in ObjC](http://stackoverflow.com/a/56656/2166798). The rest should be straightforward. Out of curiosity, if you're new to ObjC is there some reason you're not learning Swift instead? – pjs Aug 26 '16 at 20:04