0

I am creating a pong game and am attempting to end the game when the last digit of the score ends with 5 but I'm not sure how to accomplish this. This is my code so far:

if score >= 50 {show_message('ObiWan Wins'); game_end();}
if score <= 50 && score(ENDS IN DIGIT 5 NOT SURE WHAT CODE TO PLACE HERE) {show_message('Vader Wins'); game_end();}
Tanay Karnik
  • 424
  • 6
  • 19
Alex Chapp
  • 137
  • 4
  • 16

2 Answers2

1

For "repetitions" you have the modulo operation. What you actually want is the code to be executed at:

score = 5
score = 15
score = 25
....

So at a period size of "10", when you do the modulo operation with "10" as second argument you do get such a period.

0 % 10 = 0
1 % 10 = 1
2 % 10 = 2
3 % 10 = 3
4 % 10 = 4
5 % 10 = 5
6 % 10 = 6
7 % 10 = 7
8 % 10 = 8
9 % 10 = 9
10 % 10 = 0
11 % 10 = 1
12 % 10 = 2
...

From this is ought to be obvious.

if score >= 50 {
    show_message('ObiWan Wins'); 
    game_end();
} else if ( score % 10 = 5) {
    show_message('Vader Wins'); 
    game_end();
}

As a side node I changed the if score <= 50 to else if - while making no difference in this case, when you choose between two option you don't want them both to be executed at the same time

paul23
  • 8,799
  • 12
  • 66
  • 149
1

Just another approach:

if score >= 50 {
    show_message('ObiWan Wins'); 
    game_end();
} else if ( score mod 5 = 0) && ( score mod 10 != 0 ) {
    show_message('Vader Wins'); 
    game_end();
}
Tanay Karnik
  • 424
  • 6
  • 19