r/programminghorror • • Sep 26 '26

Rock Paper Scissors

Surely there's a more better and efficient way of coding this...

97 Upvotes

13 comments sorted by

23

u/fakehalo Sep 26 '26

The nested while()s with a break label instead of just one while() using 'continue' is the real crime here

18

u/Odd-Painting2596 Sep 26 '26

Neat part about programming, there is better AND worse.

8

u/Bright-Historian-216 Sep 26 '26

my python implementation from two years ago:
while True: print(f"Your opponent chose: {(d:=['rock','scissors','paper'])[r:=__import__('random').randint(0,2)]}. {'You lost!' if r==((p := d.index((f:=lambda: inp if (inp:=input('Enter rock, paper or scissors: ')) in d else f())()))+2)%3 else ('Draw!' if r==p else 'You win!')}\n")

yes, that is all one line of code

27

u/glovmpop Sep 26 '26

You don't technically even need to choose for the computer! We can keep the input prompt to give the illusion of choice

import random
while True:
  input("choose rock, paper or scissors!")
  print(random.choice(("You win!", "You lose..", "Draw.")))

4

u/FooeyBar Sep 26 '26

I really dislike all the multiple calls to println

4

u/Merry-Lane Sep 26 '26

4th "look at my shifumi code" this week.

3

u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 26 '26

Does seem to be a trend. Though I only see three. Was one deleted?

3

u/letmehaveanameyoudum Sep 28 '26

i once at 9 tried to make a rock paper scissors bot in C, it ended being 500 lines and the bot being over powered

2

u/MMORPGnews Sep 27 '26

In 2021 I worked with few experienced devs, for unknown reason, they decided to create most ineffective code for our free project. 

Why? Maybe to sell better solutions, maybe they just didn't understood. 

2

u/KodeBarista Sep 27 '26

Use code formatting.

0

u/0xt0bi03 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Sep 27 '26

blud needs to know, trees, or hashtables, or atleast bitwise and logical operators

2

u/gsweats Sep 28 '26

I've never fully understood bitwise operators, or really needed them for webdev but could you explain or show how bitwise would help with rock, paper, scissors? Please?