
-----------------------------------
Zampano
Wed Sep 19, 2007 7:46 am

Even/Odd If Statements
-----------------------------------
Hey guys, I just started in Turing, and because I finished the class's assigned programming my teacher gave me a bonus question to solve which involves code I don't know about.
The assignment is to create a two-player game with alternating turns (the rest of the programming assignment is irrelevant) and I am not sure how I could reuse the game's code and still have alternating turns on that game.
My question eventually boils down to this:
How can I ask if a number is even in an if statement?
What I tried was:

tries:=tries+1

and later:

if tries=even then
     put "Player two, go."

However, nothing I tried worked. Is there a simpler way? Thanks guys.

-----------------------------------
neufelni
Wed Sep 19, 2007 7:59 am

RE:Even/Odd If Statements
-----------------------------------
You will need to use the mod command. It determines the remainder of a division. And since all even numbers have a remainder of 0 when divided by 2, it's as simple as this:
if num mod 2 = 0 then
    put "Even"
else
    put "Odd"
end if


-----------------------------------
Zampano
Wed Sep 19, 2007 8:31 am

Re: Even/Odd If Statements
-----------------------------------
I cannot thank you enough.
Lol, the class I had to do it in hadn't even finished and you responded so quickly.
Thanks.

-----------------------------------
Tony
Wed Sep 19, 2007 12:23 pm

RE:Even/Odd If Statements
-----------------------------------
Just no cheating, ok? ;)

Another really cool way of alternating turns has to do with the property that -1 * -1 = 1. So instead of incrementing the step and figuring out the remainder each time, you just flip the sign.

var turn : int := 1
loop
   if turn > 0 then
      put "Player 1"
   else
      put "Player 2"
   end if

   turn *= -1
end loop


-----------------------------------
Bored
Thu Sep 27, 2007 9:59 am

Re: Even/Odd If Statements
-----------------------------------
Or along the same idea, you could use a boolean value and change it each time.
var playerOne : boolean := true 
loop 
    if playerOne  then 
        put "Player 1" 
    else 
        put "Player 2" 
    end if 

    playerOne  := not playerOne %True becomes false or false becomes true
end loop

-----------------------------------
Zampano
Thu Sep 27, 2007 10:09 am

Re: Even/Odd If Statements
-----------------------------------
No cheating: goes without saying.
The assignment is not an inclass test, but rather the question was assigned in class, and I got a response before that same class ended, which meant I was able to make the whole program in one class.
Thanks again guys!
