Jump to content



Photo
- - - - -

If Statement Problem?


  • Please log in to reply
15 replies to this topic

#1 Hyuji

Hyuji

    Newbie

  • Members
  • Pip
  • 5 posts

  • Calculators:
    Casio fx-9750GII

Posted 05 February 2013 - 01:24 AM

I'm trying a paper scissors rock game on my calculator, just for fun, but I can't seem to get it to work properly. Whenever I try to execute it, I can get it to give me the numbers that the calculator randomly chooses, but I can't get it to say whether I win, lose, or get a tie.
This is my code:

Lbl 0
"Paper=1, Scissors=2, Rock=3"?->A
Fix 0
2Ran# +1->B
If A/B=1/2
Then "LOSE"
Else If A/B=1/3
Then "WIN"
Else If A/B=2
Then "WIN"
Else If A/B=2/3
Then "LOSE"
Else If A/B=3
Then "LOSE"
Else If A/B=3/2
Then "WIN"
Else If A=B
Then "TIE"⊿
Goto 0

Here I'm saying that paper is 1, scissors is 2 and rock is 3, and if you pick 1 and it randomly picks 2, then you lose because A/B=1/2, and you chose paper while it chose scissors, so its supposed to say LOSE. I read on another thread about someone who had a whole bunch of if statements, too, and someone solved the problem by using strings. I took what he did and tried to use strings here, but I don't really understand them that much and so I'm not sure how to apply it here.

#2 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 05 February 2013 - 02:47 AM

I think your problem is that the if statement should end with an IfEnd command.

So like this:

Lbl 0
"Paper=1, Scissors=2, Rock=3"?->A
Fix 0
2Ran# +1->B
If A/B=1/2
Then "LOSE"
Else If A/B=1/3
Then "WIN"
Else If A/B=2
Then "WIN"
Else If A/B=2/3
Then "LOSE"
Else If A/B=3
Then "LOSE"
Else If A/B=3/2
Then "WIN"
Else If A=B
Then "TIE"⊿
'My added line below (BTW, this is a comment in casio basic)
IfEnd
Goto 0


#3 Hyuji

Hyuji

    Newbie

  • Members
  • Pip
  • 5 posts

  • Calculators:
    Casio fx-9750GII

Posted 05 February 2013 - 08:08 AM

I knew I would've forgotten something, but when I tried it didn't change anything.

#4 naib864

naib864

    Casio Technician

  • Members
  • PipPipPipPipPipPip
  • 316 posts
  • Gender:Male
  • Location:Behind the Moon :D
  • Interests:Programming, Minecraft

  • Calculators:
    fx-9860GII

Posted 05 February 2013 - 12:21 PM

Try this:

Lbl 0
"Paper=1, Scissors=2, Rock=3"?->A
Fix 0
2Ran# +1->B
If A/B=1/2
Then "LOSE"
Else If A/B=1/3
Then "WIN"
Else If A/B=2
Then "WIN"
Else If A/B=2/3
Then "LOSE"
Else If A/B=3
Then "LOSE"
Else If A/B=3/2
Then "WIN"
Else If A=B
Then "TIE"
IfEnd
IfEnd
IfEnd
IfEnd
IfEnd
IfEnd
IfEnd
Goto 0
Every If-command needs an IfEnd.

#5 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 05 February 2013 - 04:40 PM

Every If-command needs an IfEnd.

Ah, yes, I forgot that casio-BASIC doesn't have an ElseIf command. :)

#6 MicroPro

MicroPro

    Casio Overlord

  • Deputy
  • PipPipPipPipPipPipPip
  • 640 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    Casio ClassPad 300

Posted 05 February 2013 - 04:51 PM

Ah, yes, I forgot that casio-BASIC doesn't have an ElseIf command. :)

How limiting... It's strange because CP does have it.

Switch seems a better alternative here.

Edited by MicroPro, 05 February 2013 - 04:56 PM.


#7 Casimo

Casimo

    Casio Overlord

  • Members
  • PipPipPipPipPipPipPip
  • 641 posts
  • Gender:Not Telling

Posted 05 February 2013 - 04:58 PM

There is no switch on the fx9860 ;)

#8 MicroPro

MicroPro

    Casio Overlord

  • Deputy
  • PipPipPipPipPipPipPip
  • 640 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    Casio ClassPad 300

Posted 05 February 2013 - 05:06 PM

There is no switch on the fx9860 ;)

How more limiting... At least in this specific program look-up tables can be used. (luckily ;) )

#9 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 05 February 2013 - 05:10 PM

just for the record, lua doesn't have a switch-case function either...

#10 MicroPro

MicroPro

    Casio Overlord

  • Deputy
  • PipPipPipPipPipPipPip
  • 640 posts
  • Gender:Male
  • Location:Iran

  • Calculators:
    Casio ClassPad 300

Posted 05 February 2013 - 05:18 PM

Because it doesn't require multiple ends. Some variations even add the elif keyword (instead of the lengthy elseif keyword) to make if-then-else ladders as short as switch statements.

#11 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 05 February 2013 - 06:12 PM

also, to optimize this program, the use of the => command may help.

usage:

statement => command if statement is true

so, this code

If A/B=1/2
Then "LOSE"


can be changed to
A/B=1/2 => "LOSE"

Hope that helps ;)
  • MicroPro likes this

#12 Hyuji

Hyuji

    Newbie

  • Members
  • Pip
  • 5 posts

  • Calculators:
    Casio fx-9750GII

Posted 06 February 2013 - 12:36 AM

Adding in the rest of the IfEnd's did manage to get the Goto 0 to work, but it still won't say whether I win, lose, or get a tie. I tried using the double arrow instead of if statements but it didn't work, just kept returning 0.

#13 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 06 February 2013 - 01:14 AM

Ran# produces a number from .0 to 1.0.

Maybe RanInt() is better?

#14 Hyuji

Hyuji

    Newbie

  • Members
  • Pip
  • 5 posts

  • Calculators:
    Casio fx-9750GII

Posted 06 February 2013 - 01:21 AM

Of course! The Fix 0 only displays it as an integer, but it's still technically a decimal. I should've thought of that. It finally works now, thank you!

#15 flyingfisch

flyingfisch

    Casio Maniac

  • Deputy
  • PipPipPipPipPipPipPipPip
  • 1891 posts
  • Gender:Male
  • Location:OH,USA
  • Interests:Aviation, Skiing, Programming, Mountain Biking.

  • Calculators:
    fx-9860GII
    fx-CG10 PRIZM

Posted 06 February 2013 - 01:25 AM

no problem! :)

#16 2MB26

2MB26

    Newbie

  • Members
  • Pip
  • 2 posts

  • Calculators:
    fx9750gii Casio Graphical Calculator

Posted 18 November 2016 - 11:03 AM

I'm trying a paper scissors rock game on my calculator, just for fun, but I can't seem to get it to work properly. Whenever I try to execute it, I can get it to give me the numbers that the calculator randomly chooses, but I can't get it to say whether I win, lose, or get a tie.
This is my code:
 

Lbl 0
"Paper=1, Scissors=2, Rock=3"?->A
Fix 0
2Ran# +1->B
If A/B=1/2
Then "LOSE"
Else If A/B=1/3
Then "WIN"
Else If A/B=2
Then "WIN"
Else If A/B=2/3
Then "LOSE"
Else If A/B=3
Then "LOSE"
Else If A/B=3/2
Then "WIN"
Else If A=B
Then "TIE"⊿
Goto 0
Here I'm saying that paper is 1, scissors is 2 and rock is 3, and if you pick 1 and it randomly picks 2, then you lose because A/B=1/2, and you chose paper while it chose scissors, so its supposed to say LOSE. I read on another thread about someone who had a whole bunch of if statements, too, and someone solved the problem by using strings. I took what he did and tried to use strings here, but I don't really understand them that much and so I'm not sure how to apply it here.

 

Hi, I used this code (plus all the fixes you got) and I've added a lot to it so now it offers single/multi player and asks if you want to play again amongst other things

I've attached the skeleton code if you anyone wants to use it

Any advice on what I can improve etc. would be welcome!

 

Lbl 0

"SINGLE PLAYER = 1"

"MULTIPLAYER = 2"

? -> Y:

If (Y≠1 And Y≠2)

Then "ERROR"

GoTo 0

IfENd

If(Y=1)

Then Lbl 1

"ROCK = 1"

"PAPER = 2"

"SCISSORS = 3"

? -> A:

If (A>3 Or A<1)

Then ""

"ERROR"

""

GoTo 1

IfEnd

If (A=1)

Then "ROCK"

IfEnd

If (A=2)

Then "PAPER"

IfEnd

If (A=3)

Then "SCISSORS"

IfEnd

"VS"

Int (Ran# x3) +1 -> B

If(B=1)

Then "ROCK"

IfEnd

If (B=2)

Then  "PAPER"

IfEnd

If (B=3)

Then "SCISSORS"

IfEnd

""

IF (A÷B=1/2 Or A÷B=2/3 Or A÷B=3)

Then "LOSE":

IfEnd

If (A÷B=1/3 Or A÷B=2 Or A÷B=3/2)

Then "WIN":

IfEnd

If (A=B )

Then "TIE"

IfEnd

""

Lbl 2

"PLAY AGAIN?"

"Y=1 N=2”

? -> X:

If (X=1)

Then GoTo1

IfEnd

If (X=2)

Then Break

IfEnd

If (X≠ 1 And X ≠2)

Then "ERROR"

""

GoTo2

IfEnd

IfEnd

 

If (Y=2)

Then Lbl 3

"PLAYER ONE"

"ROCK = 1"

"PAPER = 2"

"SCISSORS = 3"

? -> A:

If (A>3 Or A<1)

Then ""

"ERROR"

""

GoTo3

IfEnd

"PRESS ENTER TO HIDE YOUR SELECTION"

""

""

""

Lbl 4

"PLAYER TWO"

"ROCK = 1"

"PAPER = 2"

"SCISSORS = 3"

? -> B:

If (A>3 Or A<1)

Then ""

"ERROR"

""

GoTo4

IfEnd

"PRESS ENTER TO HIDE YOUR SELECTION"

""

""

""

If (A=1)

Then "ROCK"

IfEnd

If (A=2)

Then "PAPER"

IfEnd

If (A=3)

Then "SCISSORS"

IfEnd

"VS"

If (B=1)

Then "ROCK"

IfEnd

If (B=2)

Then "PAPER"

IfEnd

If (B=3)

Then "SCISSORS"

IfEnd

""

If (A÷B=1 /2 Or A÷B=2/3 Or A÷B=3)

Then "PLAYER TWO WINS!":

IfEnd

If (A÷B=1/3 Or A÷B=2 Or A÷B=3/2)

Then "PLAYER ONE WINS":

IfEnd

If (A=B )

Then "TIE"

IfEnd

""

Lbl 5

"PLAY AGAIN?"

"Y=1 N=2"

? -> X:

If (X=1)

Then GoTo 3

IfEnd

If (X=2)

Then Break

IfEnd

If (X≠1 And X≠2)

Then "ERROR"

""

GoTo5

IfEnd

IfEnd


Edited by 2MB26, 18 November 2016 - 11:05 AM.



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users