Jump to content
8 minutes ago, Wictorian said:

Is there a way to do this without using 2 billion if else statements?

There's realistically only 4 possible combinations of a rock paper scissors game.

 

Someone can choose rock and be beaten by paper

Someone can choose paper and be beaten by scissors

Someone can choose scissors and be beaten by rock

Someone can choose X and be matched by X

 

So realistically you'll only need 4 if statements. 

 

If you really want to steer away from if statements then you can probably use a dictionary to define winning pairs (not incl draws). Some variation on this could be good.

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to post
Share on other sites

That's the fun of programming by using neat ways to do things. 

 

You could use 4~ if else statements. 

You could use numbers and a bit of math. 

You can probably write a data structure to do it. 

 

Don't open till you figure a numbers way

Spoiler

Rock is 0

Paper is 1

Scissors is 2.

 

Player number - cpu number

If difference = 1 player won

If difference = 0 tie

Else cpu won 

 

Don't open till you think of data structures solution

Spoiler

You can use a first in last out data structure

 

Player make choice 1,2,3

Rock paper scissors

Fill data structure up to player choice.

 

Fill other data structure up to cpu choice.

While

If player structure length > cpu structure length and (cpu size > 0 and player size < MAX SIZE)

player won

 

If player size = cpu size

Draw

 

Else

Cpu won

 

 

 

I was going to use push and pop but...

Yeah actually you can do that too. 

You need a set and then check the size of the set since sets don't allow duplicates. 

 

Link to post
Share on other sites

10 minutes ago, fpo said:

That's the fun of programming by using neat ways to do things. 

 

You could use 4~ if else statements. 

You could use numbers and a bit of math. 

You can probably write a data structure to do it. 

 

Don't open till you figure a numbers way

  Reveal hidden contents

Rock is 0

Paper is 1

Scissors is 2.

 

Player number - cpu number

If difference = 1 player won

If difference = 0 tie

Else cpu won 

 

Don't open till you think of data structures solution

  Reveal hidden contents

You can use a first in last out data structure

 

Player make choice 1,2,3

Rock paper scissors

Fill data structure up to player choice.

 

Fill other data structure up to cpu choice.

While

If player structure length > cpu structure length and (cpu size > 0 and player size < MAX SIZE)

player won

 

If player size = cpu size

Draw

 

Else

Cpu won

 

 

 

I was going to use push and pop but...

Yeah actually you can do that too. 

You need a set and then check the size of the set since sets don't allow duplicates. 

 

That numbers method is smart. I like that a lot.

Main PC [ CPU AMD Ryzen 9 7900X3D with H150i ELITE CAPPELIX  GPU Nvidia 3090 FE  MBD ASUS ROG STRIX X670E-A  RAM Corsair Dominator Platinum 64GB@5600MHz  PSU HX1000i  Case Lian Li PC-O11 Dynamic  Monitor LG UltraGear 1440p 32" Nano IPS@180Hz  Keyboard Keychron Q6 with Kailh Box Switch Jade  Mouse Logitech G Pro Superlight  Microphone Shure SM7B with Cloudlifter & GoXLR ]

 

Server [ CPU AMD Ryzen 5 5600G  GPU Intel ARC A380  RAM Corsair VEGEANCE LPX 64GB  Storage 16TB EXOS ]

 

Phone [ Google Pixel 8 Pro, 256GB, Snow ]

Link to post
Share on other sites

1 hour ago, fpo said:

That's the fun of programming by using neat ways to do things. 

 

You could use 4~ if else statements. 

You could use numbers and a bit of math. 

You can probably write a data structure to do it. 

 

Don't open till you figure a numbers way

  Hide contents

Rock is 0

Paper is 1

Scissors is 2.

 

Player number - cpu number

If difference = 1 player won

If difference = 0 tie

Else cpu won 

 

Don't open till you think of data structures solution

  Reveal hidden contents

You can use a first in last out data structure

 

Player make choice 1,2,3

Rock paper scissors

Fill data structure up to player choice.

 

Fill other data structure up to cpu choice.

While

If player structure length > cpu structure length and (cpu size > 0 and player size < MAX SIZE)

player won

 

If player size = cpu size

Draw

 

Else

Cpu won

 

 

 

I was going to use push and pop but...

Yeah actually you can do that too. 

You need a set and then check the size of the set since sets don't allow duplicates. 

 

I had thought about the numbers way but the problem is if you choose rock and cpu chooses scissors you should win but cpu wins according to this algorithm.

Link to post
Share on other sites

8 minutes ago, Wictorian said:

ok so the difference is 2 which means cpu won. 

 

however as far as i am concerned rock beats scissors.

Yeah maybe there's another check that needs doing if player is rock and cpu is scissors

Maybe start at 1 then negatives are counted or something.

Link to post
Share on other sites

The numbers method is the way to go.

 

Assign each item in the game a value.

 

Rock = 0

Paper = 1

Scissors = 2

 

The calculation is:

 

result = P1 - P2

If result < 0 add 3

 

If result = 0 its a draw

If result = 1 P1 wins

If result = 2 P2 wins

 

The math works, and its simple code the last 3 should be a switch/case statement.

 

Link to post
Share on other sites

Well I mean there aren't too many combinations of it.  Flappy's is actually a pretty good one, but I mean even if you wanted a more hardcoded solution, that is still only 3 combinations plus a 4th to detect a tie.  And I mean those 3 choices can just be put into a single if statement

if person_a_choice == person_b_choice:
    return "tie"

if (person_a_choice == "rock" and person_b_choice == "scissors") or
    (person_a_choice == "paper" and person_b_choice == "rock") or
    (person_a_choice == "scissors" and person_b_choice == "paper"):
    return "person a wins"
return "person b wins"


Depending on the project though, I'd take a lot more of a complex approach to generalize it out to allow multiple players or additional items (which have it's own tree)

 

Here is an example that I just wrote up, showing a really generalized form.  I just very quickly wrote this up, it works but I mean is no way actually thought out or efficient...but shows how I might approach the problem if I wanted to lets say add more power to it.

# Just a note, I only experimented with Python before...so I really haven't likely used best practices within here
# This is the lose condition instead of win condition.  It helps when considering a 3 person game
# This shows how you can have multiple conditions.
lose_condition = {
    "rock": ["paper"],
    "paper": ["scissors", "water"],
    "scissors": ["rock"],
    "boat": ["scissors", "rock"],  # boat beats water, ties with paper, is beaten by scissors and rock
    "water": ["boat"],
    "autowin": [] # example showing autowin loses to nothing
}

# Classical one
classical_lose_condition = {
     "rock": ["paper"],
     "paper": ["scissors"],
     "scissors": ["rock"]
}


# returns a dictionary where the choice = 0 is a loss, choice = 1 is a win.
def which_choices_win(list_of_choices, conditions_for_loss):
    # everything is a winner to begin with
    winning_choices = dict.fromkeys(list_of_choices, 1)
    # winning_choices now only has singles...no duplicates
    for choice in winning_choices:
        if choice not in lose_condition:
            # well it's not in the list...so lets just assume that one wins
            continue

        for losing_condition in conditions_for_loss[choice]:
            if losing_condition in list_of_choices:  # Someone chose something that makes this fail
                winning_choices[choice] = 0 # no longer a winner
                break  # edit: added as this bothered me that I missed adding this...no need to needlessly miss cycles

    return winning_choices


def play_game_2_player(person_a_choice, person_b_choice):
    winning_choice = which_choices_win([person_a_choice, person_b_choice], lose_condition)
    if winning_choice[person_a_choice] == 1 and winning_choice[person_b_choice] == 1:
        return "Tie"
    elif winning_choice[person_a_choice] == 1:
        return "Person a won"
    else:
        return "Person b won"


def play_game_multiple_players(dictionary_player_with_choices):
    choice_list = list(dictionary_player_with_choices.values())
    winning_choices = which_choices_win(choice_list, lose_condition)
    winning_players = []

    for key, value in dictionary_player_with_choices.items():
        if winning_choices[value] == 1:
            winning_players.append(key)

    if len(winning_players) == 0:
        # no one won, so everyone wins
        winning_players = dictionary_player_with_choices.keys()

    return winning_players


print(play_game_2_player("rock", "scissors"))
print(play_game_2_player("paper", "scissors"))
print(play_game_2_player("boat", "scissors"))
print(play_game_2_player("boat", "paper"))


print(play_game_multiple_players(
    {"Player A": "rock",
     "Player B": "paper"}
))
print(play_game_multiple_players(
    {"Player A": "rock",
     "Player B": "paper",
     "Player C": "paper"}
))
print(play_game_multiple_players(
    {"Player A": "rock",
     "Player B": "paper",
     "Player C": "scissors"}
))
print(play_game_multiple_players(
    {"Player A": "rock",
     "Player B": "paper",
     "Player C": "scissors",
     "Cheater": "autowin"}
))

 

3735928559 - Beware of the dead beef

Link to post
Share on other sites

Mine is similar to @wanderingfool2s solution, it just less general.

import random

stronger_element = {
    "rock": "paper",
    "paper": "scissors",
    "scissors": "rock"
}

while(True):
    options = list(stronger_element.keys())
    machine_choice = random.choice(options)
    player_choice = input("choose from rock paper or scissors: ")
    if(player_choice not in options):
        print("not a valid option pls try again")
        continue
        
    print("machine's choice was: " + machine_choice);
        
    if(stronger_element[machine_choice] == player_choice):
        print("player won\n");
    elif(stronger_element[player_choice] == machine_choice):
        print("machine won\n")
    else:
        print("draw\n")

The hardcoded version is smaller  LOL

 

@wanderingfool2 please use syntax highlighting next time 🙂 it makes the code readable

ಠ_ಠ

Link to post
Share on other sites

1 hour ago, shadow_ray said:

The hardcoded version is smaller  LOL

 

@wanderingfool2 please use syntax highlighting next time 🙂 it makes the code readable

Yea, I added the formatting option now.  Also tweaked it by adding a break...it was bothering me that I still wasted a few CPU cycles

 

3735928559 - Beware of the dead beef

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×