So I was inspired by @Chandler0Bing’s “Rock Paper Scissors Shoot” and decided to make my own. The goal is to have the smallest possible code while including all possible features. This program includes scores, looping, etc., while remaining only 20 lines. It is NOT meant to be readable, but rather to be small and OOP (object-oriented programming).
Repl Link
Code (so you don't have to click the link)
I,S,s,o,i,a=input,0,0,['rock','paper','scissors'],lambda:I(f"\033cPlease choose rock, paper, or scissors.\nThe computer will then make its choice and we'll see who wins!\n\nCurrent scores:\nYou: {S}\nComputer:{s}\n\nYour choice: ").lower()[0],[0,1,2]
while 1:
def k(g):
if g=='r':g=0
elif g=='p':g=1
elif g=='s':g=2
return g
while(x:=k(i())) not in a:
if (x:=k(i())) in a:break
l,z,y=['ties','beats','loses to'],0,__import__('random').randint(0,2)
if x==y:z=0
elif x==0 and y==1:z=1
elif x==0 and y==2:z=2
elif x==1 and y==0:z=2
elif x==1 and y==2:z=1
elif x==2 and y==0:z=1
elif x==2 and y==1:z=2
if z==1:s+=1
elif z==2:S+=1
if I(f'The computer chose {o[y]}, which {l[z]} your choice of {o[x]}.\nAgain?\n').lower() in ['n','no']:break
3 Likes
19 lines …
import random
choices = ['rock', 'paper', 'scissors']
scores = {'player': 0, 'computer': 0}
num_turns = int(input("How many turns do you want to play? "))
for i in range(num_turns):
player_choice = input("Enter your choice (rock/paper/scissors): ")
computer_choice = random.choice(choices)
print(f"Computer chose {computer_choice}")
if player_choice == computer_choice:
print("It's a tie!")
elif (player_choice == 'rock' and computer_choice == 'scissors') \
or (player_choice == 'paper' and computer_choice == 'rock') \
or (player_choice == 'scissors' and computer_choice == 'paper'):
print("You win!")
scores['player'] += 1
else:
print("Computer wins!")
scores['computer'] += 1
print(f"Final score: Player {scores['player']} - {scores['computer']} Computer")
PS: I cheated in the sense I did several of these to teach my kids python
long ago
4 Likes
Mine is 781 bytes and yours is 841 bytes. Mine is also less readable. Nice code though.
2 Likes
Why did I not use random.choice
? I was just telling my teacher to use it instead of a bunch of if
statements the other day and then I forgot to use it.
1 Like
Ok, so we go for less bytes not lines …
2 Likes
I think I have something somewhere (dunno what is the fixation of people with making this game … including for me) …need to check an old git. One sec
Tried to, post a link … no good. So this is the code I found, old stuff.
import random
c,s={'player':0,'computer':0},['rock','paper','scissors']
for i in range(int(input("How many turns? "))):
p=input("rock, paper or scissors? ")
o=random.choice(s)
print(f'{["tie","lose","win"][(s.index(p)-s.index(o)+1)%3]}! {o}')
c['player']+=s.index(p)<s.index(o);c['computer']+=s.index(p)>s.index(o)
print(f"Final score: Player {c['player']} - {c['computer']} Computer")
Let me add a note, this uses one of my favorite construct, but is slow
4 Likes
Can you do two input statements on the same line by doing this?
scores = {'player': 0, 'computer': 0}
Man is everyone a teacher in here? Lol 
1 Like
I’m not sure what that means. Please elaborate (I realize it wasn’t directly to me but I’d like to know).
2 Likes
Something like
a = {input('What is your name?\n\tName:\t') , input('How old are you?\n\tAge:\t')}
To maximize unreadability lol
That would work except that in that example you didn’t give any key name.
a = {'name':input('What is your name?\n\tName:\t') , 'age':input('How old are you?\n\tAge:\t')}
2 Likes
Ahhh, thank you, I will make people’s eyes suffer now! 
Pov you read my code from now on:

I am no teacher … I am even worse 
even better (less bytes):
name, age = [input('What is your name?\n\tName:\t') , input('How old are you?\n\tAge:\t')]
1 Like
Do you even need the brackets on that? (You might, I don’t typically do multiple definitions in one line)
No. I wonder if it will even work with the brackets.
1 Like
my fault as i first had the brackets on both sides and forgot to delete 
but i believe it still works …
1 Like
This code is not from me, but from somebody i know. Still I am sharing it to show that there are better languages to make weird implementations of this game 
This is the basic version (single game).
my @o=('rock','paper','scissors');print"Enter 0 for rock, 1 for paper, 2 for scissors: ";my$c=<STDIN>;chomp$c;if($c<0||$c>2){die"Invalid input"}my$x=int(rand(3));print"You chose: $o[$c]\n";print"Computer chose: $o[$x]\n";if($c==$x){print"It's a tie!\n"}elsif($c==($x+1)%3){print"You win!\n"}else{print"Computer wins!\n"}
Feel free to guess what language it might be. Somehow i feel we could use python to get the same
1 Like