Is there an easy way to compare a “Rock Paper Scissors” scenario? I know I could use a bunch of If…Then statements but I was wondering if there was something simpler built-into Xojo.
Xojo 2017R3
Is there an easy way to compare a “Rock Paper Scissors” scenario? I know I could use a bunch of If…Then statements but I was wondering if there was something simpler built-into Xojo.
Xojo 2017R3
Select Case Object
Case Rock
....
Case Paper
...
End Select
Dont forget Lizard Spock
Huh?
I don’t watch that show.
Can we please get back to my original question?
Sorry I was under the impression that the use of Select Case is the option to use instead of if then.
That didn’t work?
[quote=438460:@Charles Fasano]I don’t watch that show.
Can we please get back to my original question?[/quote]
at least a few ways
lets assume 2 players
with 2 players and 3 choices there are several cases
player1 player2
rock rock
rock paper
rock scissors
paper rock
paper paper
paper scissors
scissors rock
scissors paper
scissors scissors
so you might write a method that takes the player1 choice and player2 choice and returns which one wins
lets assume there’s an enum like
enum RockPaperScissorsChoice
rock
paper
scissors
and the method is like
Function Winner(player1Choice as RockPaperScissorsChoice, player1Choice as RockPaperScissorsChoice) as Integer
const tie = 0
const player1Wins = 1
const player2Wins = 2
// returns 1 if player 1 wins
// returns 2 if player2 wins
// return 0 if a tie
// handle Ties
if player1choice = player2Choice then
return Tie
end if
// handle the cases where player 2 beats player 1
select case player1Choice
case rock
select case player2Choice
case paper
return player2Wins
case paper
select case player2Choice
case scissors
return player2Wins
case scissors
select case player2Choice
case rock
return player2Wins
end select
// if it wasnt a tie and player 2 didnt win then player 1won
return player1Wins
end function
Another idea.
Create a class with three subclasses and implement Operator_,Compare in each.
here is another short solution
Private Function Winner(player1 as string, player2 as string) as integer
// Player1 and Player2 will be one of [R,P,S]
If player1=player2 Then Return 0 // its a tie
Dim s As String=Min(player1,player2)+Max(player1,player2)
Select Case s
Case "PR"
w="P"
Case "RS"
w="R"
Case "PS"
w="S"
End Select
If w=player1 Then Return 1
return 2
End Function
Rock = 0
Paper = 1
Scissors = 2
Returns 0 if is a tie
Returns 1 if player1wins
Returns 2 if player2 wins
Here is a shorter solution:
Function Winner(P1 as Integer, P2 as Integer) as Integer
If P1 = P2 Then Return 0
If (P1 = 0 And P2 = 1) Or (P1 = 1 And P2 = 2) Or (P1 = 2 And P2 = 0) Then
Return 2
Else
Return 1
End If
End Function
Or using Select And If
Function Winner(P1 as Integer, P2 as Integer) as Integer
If P1 = P2 Then Return 0
Select Case P1
Case 0
If P2 = 1 Then Return 2
Case 1
If P2 = 2 Then Return 2
Case 2
If P2 = 0 Then Return 2
End Select
Return 1
End Function
Or perhaps something like this…
// Set up a who-kills-who array..
const ROCK as integer = 0
const PAPER as integer = 1
const SCISSORS as integer = 2
// Paper kills ROCK etc...
dim kill() as integer = array(PAPER,SCISSORS,ROCK)
...
Function winner(kill() as integer, p1 as integer, p2 as integer) as integer
if p1 = kill(p2) then return 1 // P1 wins
if p2 = kill(p1) then return 2 // P2 wins
return 0 // Draw
...
Call like this...
dim result as integer
result = winner(kill(),ROCK,PAPER)
or
result = winner(kill,ROCK,PAPER)
Error, expected (Double, Double), but these arguments are (String, String).
Dim s As String=Min(player1,player2)+Max(player1,player2)
Edit: don’t know if this is the best ‘fix’ for this error:
Dim s As String=chr(Min(player1.asc,player2.asc))+chr(Max(player1.asc,player2.asc))
Call me Sheldon or Thomas A. Anderson 'cause I used The Matrix
https://jakobssystems.net/downloads/RPLS.xojo_xml_project.zip
… and just uploaded the Web-Version of RPSLS
https://jakobssystems.net/downloads/RPSLS-web.xojo_xml_project.zip
Fun-Fact: The ZIP of the XML Projectfile for Web file is 92x bigger than the original one.
And of course there is plenty of room for optimizations.
Did I mention that I love Xojo as RAD-Tool (Rapid Application Development)?