If variable = Odd number...

GAGE HOLSTON
09 Dec 2017, 01:39

Hello, in my text adventure you can play mini-games to increase your total cash.
In one of them, the game sets a variable to a random number between 1 and 36.
I want to include a script where the game checks if the random number is an even or an odd number so that it can set another variable to the appropriate string.

The only problem is, I have no idea how to do that or if it's even possible. Does anyone know?


K.V.
09 Dec 2017, 02:29
<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Odds">
    <gameid>09aea278-999b-42cc-a36a-13efb9945e77</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <enter type="script">
      msg (IsOdd(9))
      msg (IsOdd(2))
      if (IsOdd(23)) msg ("YES")
    </enter>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
    </object>
  </object>
  <function name="IsOdd" parameters="int" type="boolean">
    bool = false
   if (int % 2 = 0) {
        bool = true
    }
    return (bool)
  </function>
</asl>

GAGE HOLSTON
09 Dec 2017, 03:45

Sorry, I'm completely loss when it comes to functions or scripts like this. I don't know where it goes or what I should change.


R2T1
09 Dec 2017, 05:27

Check if the number divided by 2 is equal to the integer of the same number divided by 2. If they are equal it is even else odd.

So... if variable / 2 = Int(variable /2) then even else odd.

Hopefully you can do this in the GUI without too much trouble.

Sorry I haven't got an actual image to show, but it has been a while since I have done anything in Quest.


K.V.
09 Dec 2017, 06:44

Good idea, R2T1!

image


GAGE HOLSTON
09 Dec 2017, 13:41

It works! Thanks KV and R2T1.


R2T1
09 Dec 2017, 21:03

@KV thanks for simplifying it further by using the MOD operator. I thought of it later but didn't update my post.


hegemonkhan
11 Dec 2017, 19:12

the modulus operator (%) / operation, does division, but it finds/gets the REMAINDER

the remainder is useful for odd/even (factoring/divisibility of specifically using '2'), factoring/divisibility (of using any number), and cyclic (example: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, ... Time especially: seconds, minutes, hours, etc), uses/needs

even (or odd) number: any even number divided by 2 will have no (zero: 0) remainder (and if it's not 0, then it will be a 1, and thus also is an odd number), (any number divided by 2 will either have no/zero/0 remainder or 1 as it's remainder), so:

get input {
  if (IsInt (result)) {
    number = ToInt (result)

    if (number % 2 = 0) {
      msg ("The number " + number + " is an even number")
    } else {
      msg ("The number " + number + " is an odd number")
    }

  } else {
    msg ("wrong input, please try again, typing in an integer (non-decimal number value: ..., -9999, -3, 0, 1, 6, 555, ...), please")
  }
}

// or:

get input {
  if (IsInt (result)) {
    number = ToInt (result)

    if (number % 2 = 1) {
      msg ("The number " + number + " is an odd number")
    } else {
      msg ("The number " + number + " is an even number")
    }

  } else {
    msg ("wrong input, please try again, typing in an integer (non-decimal number value: ..., -9999, -3, 0, 1, 6, 555, ...), please")
  }
}