Python Rock Paper Scissors program

Hey guys,
Im starting to learn python for blender applications, and made this,
it works up until I define Rand (as in random choice for rock paper or scissors,)

can anyone help?

P.S. Sorry, its very disorganized! :o

import random
Name = raw_input ("Enter Name here: ") 
print "Hello " + Name + "! I am a python script specialy designed for a game called rock paper scissors!"
answer = raw_input ("Do you want to play? : ")
if answer == ("yes"):
    print "Okay! Lets play!"
    PC = raw_input ("Choose rock, paper, or scissors: ")
    rand = random.randint(1, 3) #1 means rock, 2 means paper, 3 means scissors
    if PC == ("rock"):
        if rand == ("1"):
            print "Tie game!"
        elif rand == ("2"):
            print "Game lost!"
        elif rand == ("3"):
            print "Game Win!"
    if PC == ("paper"):
        if rand == ("2"):
            print "Tie game!"
        elif rand == ("3"):
            print "Game lost!"
        elif rand == ("1"):
            print "Game Win!"
    if PC == ("scissors"):
        if rand == ("3"):
            print "Tie game!"
        elif rand == ("1"):
            print "Game lost!"
        elif rand == ("2"):
            print "Game Win!"
else :
    print "aww!"

Hi Mcrich114,
variable rand is an integer, not string, so write:

if PC == "rock":
     rand == 1:

fixing using migius’ help

import random
Name = raw_input ("Enter Name here: ") 
print "Hello " + Name + "! I am a python script specialy designed for a game called rock paper scissors!"
answer = raw_input ("Do you want to play? : ")
if answer == ("yes"):
    print "Okay! Lets play!"
    PC = raw_input ("Choose rock, paper, or scissors: ")
    rand = random.randint(1, 3) #1 means rock, 2 means paper, 3 means scissors
    if PC == ("rock"):
        if rand == (1):
            print "Tie game!"
        elif rand == 2:
            print "Game lost!"
        elif rand == 3:
            print "Game Win!"
    if PC == ("paper"):
        if rand == 2:
            print "Tie game!"
        elif rand == 3:
            print "Game lost!"
        elif rand == 1:
            print "Game Win!"
    if PC == ("scissors"):
        if rand == 3:
            print "Tie game!"
        elif rand == 1:
            print "Game lost!"
        elif rand == 2:
            print "Game Win!"
else :
    print "aww!"

also, I’d use a switch statement instead of all those ifs , but that’s juts me :smiley:

it works in pywin32, but not blender 2.5 python window.
how do you get the input to work properly from blender ?

Blender 2.5 uses Python 3.

In Python 3, it is “input” not “raw_input”, also, it is print(“Game Win!”) not print.

-Sunjay03

Oh! hey, looks like we have a missunderstanding here,

Im using 2.6 for one,

and too, this has not thing to do with blender, its just basic python skills practice

SunJay03 :
yes, the print statement works okay with the parenthesis but when I tried

input("input here")

I got an error:

Traceback (most recent call last):
File “<console>”, line 1, in <module>
RuntimeError: input(): lost sys.stdin

odd, the raw_input statement worked fine,
its just the random generater that gets screwy,

Yes! that worked!

import random
Name = raw_input ("Enter Name here: ") 
print "Hello " + Name + "! I am a python script specialy designed for a game called rock paper scissors!"
answer = raw_input ("Do you want to play? : ")
if answer == ("yes"):
    print "Okay! Lets play!"
    PC = raw_input ("Choose rock, paper, or scissors: ")
    rand = random.randint(1, 3) #1 means rock, 2 means paper, 3 means scissors
    if rand == 1:
        print "I choose rock!"
    if rand == 2:
        print "I choose paper!"
    if rand == 3:
        print "I choose scissors!"
    if PC == "rock":
        if rand == 1:
            print "Tie game!"
        elif rand == 2:
            print "Game lost!"
        elif rand == 3:
            print "Game Win!"
    if PC == "paper":
        if rand == 2:
            print "Tie game!"
        elif rand == 3:
            print "Game lost!"
        elif rand == 1:
            print "Game Win!"
    if PC == "scissors":
        if rand == "3":
            print "Tie game!"
        elif rand == "1":
            print "Game lost!"
        elif rand == "2":
            print "Game Win!"
else :
    print "aww!"

Now, How do I restart the code, I can’t find any good recursion explanations?

Here is my rock paper scissors program. A bit more optimized, but it is just to give you an idea of whats out there:


############################################
#			Rock paper scissors			   #
# a game (not created, but programed)	   #
# by Sunjay Varma - www.sunjay-varma.com   #
############################################
import random
def RockPaperScissors():
	# just some initialization stuff
	print "Welcome to rock paper scissors.
Please type your choice when you are given your turn.
There are three choices:
	1) Rock -or- r
	2) Paper -or- p
	3) Scissors -or- s

To quit, just press enter on your turn without typing anything (or type quit)

Start!

Score: CPU=0 Player=0"

	playerscore, cpuscore = 0,0
		
	playing = 1
	while playing:
		try:playermove = {'r':'rock', 'p':'paper', 's':'scissors'}[raw_input('
Human\'s turn: ').strip().lower()[0]]
		except: playing = 0; continue

		cpumove = random.choice(['rock', 'paper', 'scissors'])
		print 'CPU\'s turn: '+cpumove
		if playermove == cpumove:
			print 'Tie!'
		
		else:
			judge = {('rock','paper'):False,('paper', 'scissors'):False,('scissors','rock'):False}
			if not judge.get((playermove, cpumove), True):
				cpuscore += 1
				print 'Player lost!'
				print 'CPU='+str(cpuscore)+' Player='+str(playerscore)
			else:
				playerscore += 1
				print 'CPU lost!'
				print 'CPU='+str(cpuscore)+' Player='+str(playerscore)

	print 'Final scoring: '+'CPU='+str(cpuscore)+' Player='+str(playerscore)
	print "
Thanks for playing!"
	raw_input("Press &lt;enter&gt; to quit!")

RockPaperScissors()


Mine is about 33 lines.

-Sunjay03

cause i didn’t know .get construct, i would write it this way:


            judge = (('rock','paper'),('paper', 'scissors'),('scissors','rock'))
            if (playermove, cpumove) in judge:

edit: alternatively:

if not judge.get((playermove, cpumove), True):

can be replaced with:

if (playermove, cpumove) in judge.keys():

Good idea.

I hadn’t thought of that. Well I did, when I was thinking about it in my room…but then I forgot. :wink:

Oh well!
-Sunjay03

okay , so how could I apply this to a blender game? any ideas?

That would take a little more effort. These scripts are no good for a real 3 dimensional program like Blender. Depending on how you do it, it can be very complicated or not.

It really depends.

Do you want it to be strictly 2D? Or do you want something fancy 3D?

I guess we could start 2D.

Just get a picture of all three parts and load (UV map) them onto planes.

Then we start making the basic logic. How much do you know about logic bricks and Game Engine Python?

The first step would be to show the mouse.

import Rasterizer; Rasterizer.showMouse(1)

Place that line in a new text document in the Blender Text Editor. Name the file showMouse.py and place it in the text field of a Python script controller.

Connect an Always sensor, with True level triggering off, to the controller.

Press “p” while hovering the mouse over the 3D view to play the game engine and see your mouse show!

-Sunjay03

(Wow. I could wright a good tutorial on this. :wink: )

[EDIT:] I found this thread on a real rock paper scissors game made in Blender:
http://blenderartists.org/forum/showthread.php?t=181082

Mouse script worked perfectly!

Logic bricks I know alot about, PyBge? Not so much,

2d, because its a first attempt, lets see were this goes,