C
C#4mo ago
GregTheEgg

Need help compressing an RPS system

// I really couldn't think of anything better minus a load of if conditionals, mb.
if (inter.value == random){
Debug.Log("tie");
}
if (inter.value == 1 && random == 3){
Debug.Log("win");
}
if (inter.value == 2 && random == 1){
Debug.Log("win");
}
if (inter.value == 3 && random == 2){
Debug.Log("win");
}
if (inter.value == 1 && random == 2){
Debug.Log("lose");
}
if (inter.value == 2 && random == 3){
Debug.Log("lose");
}
if (inter.value == 3 && random == 1){
Debug.Log("lose");
}
// I really couldn't think of anything better minus a load of if conditionals, mb.
if (inter.value == random){
Debug.Log("tie");
}
if (inter.value == 1 && random == 3){
Debug.Log("win");
}
if (inter.value == 2 && random == 1){
Debug.Log("win");
}
if (inter.value == 3 && random == 2){
Debug.Log("win");
}
if (inter.value == 1 && random == 2){
Debug.Log("lose");
}
if (inter.value == 2 && random == 3){
Debug.Log("lose");
}
if (inter.value == 3 && random == 1){
Debug.Log("lose");
}
5 Replies
GregTheEgg
GregTheEgg4mo ago
I'm really struggling to think of ways to compress the code :Sadge:
Angius
Angius4mo ago
I guess you could use tuples, pattern matching, and a switch expression
GregTheEgg
GregTheEgg4mo ago
what's tuples?
Angius
Angius4mo ago
var result = (inter.Value, random) switch {
(1, 3) or (2, 1) or (3, 2) => "win",
(1, 2) or (2, 3) or (3, 1) => "lose",
_ => "tie"
}
Debug.log(result);
var result = (inter.Value, random) switch {
(1, 3) or (2, 1) or (3, 2) => "win",
(1, 2) or (2, 3) or (3, 1) => "lose",
_ => "tie"
}
Debug.log(result);
GregTheEgg
GregTheEgg4mo ago
ooh that works
Want results from more Discord servers?
Add your server