Reputation:
I'm just starting Python and writing a simple rock paper scissors game. Here's what I have so far. I can play a game with another player perfectly. What I want to know is how to keep score of the wins, so if Player 1 wins the first game it would say:
Player 1 = 1 Player 2 = 0
And so on. Also if there's any tips to make my code more efficient (with a nice explanation), that would be nice.
Thanks!
Upvotes: 0
Views: 13772
Reputation: 4557
Well there are several ways to make it more efficient, but I have to ask how you are going to play Rock Paper Scissors if you have to type them in one at a time :-P
The scoring could be boiled down to this:
if choice1 == choice2 :
print "Its's a tie."
elif choice1 - choice2 == 1 or choice2 - choice1 == 2 :
print "%s wins." % player1
else:
print "%s wins." % player2
If you want to do multiple games, just put the whole thing inside a while(1):
loop, and add a variable for each score. Then it would become:
score1 = 0
score2 = 0
while(1):
<lines 6-18>
if choice1 == choice2 :
print "Its's a tie."
elif choice1 - choice2 == 1 or choice2 - choice1 == 2 :
print "%s wins." % player1
score1 = score1 + 1
else:
print "%s wins." % player2
score2 = score2 + 1
print "%s: %d points. %s: %d points." % (player1, score1, player2, score2)
Upvotes: 0
Reputation:
Consider this pseudo-code for suggestions (some parts need to be implemented):
# a Player class. it should have a Name and a Score
class Player():
def __init__(name):
self.name = name
self.score = 0
# displays a prompt and reads in the players name returning a string
def getPlayerName():
# needs code here, see next function for idea of what
pass
# ask the player to make a choice, takes a Player object
# and returns a string
def getPlayerAttack(player):
print "%s, what do you choose?" % player.name
return raw_input("> ")
# determines who wins and updates score accordingly
# takes in the player objects and their attack choices
def attack(player1, choice1, player2, choice2):
if choice1 == choice2:
print "Its's a tie."
elif choice1 == "1" and choice2 == "2":
print "%s wins." % player2
player2.score = player2.score + 1
elif ... # other attacks
# display the scores of the two players
def displayScores(player1, player2):
print "%s vs %s" % (player1.score, player2.score)
player1 = Player(getPlayerName())
player2 = Player(getPlayerName())
while true:
choice1 = getPlayerAttack(player1)
choice2 = getPlayerAttack(player2)
attack(player1, choice1, player2, choice2)
displayScores(player1, player2)
This will need some work, and it's not super-optimal, but it should be a start and should show some more concepts. Use Ctrl-C to stop or add a stop condition (e.g. either player enters "0") -- bugs included for free. :)
Happy coding.
Upvotes: 3
Reputation: 870
Try this:
player1 = raw_input("Player 1 name: ")
player2 = raw_input("Player 2 name: ")
while(1)
player1score = 0
player2score = 0
print "%s, what do you choose?" % player1
print "1. Rock"
print "2. Paper"
print "3. Scissors"
choice1 = raw_input("> ")
print "%s, what do you choose?" % player2
print "1. Rock"
print "2. Paper"
print "3. Scissors"
choice2 = raw_input("> ")
if choice1 == "1" and choice2 == "1":
print "Its's a tie."
if choice1 == "1" and choice2 == "2":
print "%s wins." % player2
player2score=player2score+1
if choice1 == "2" and choice2 == "1":
print "Player 1 wins." % player1
player1score=player1score+1
if choice1 == "1" and choice2 == "3":
print "Player 1 wins." % player1
player1score=player1score+1
if choice1 == "3" and choice2 == "1":
print "%s2 wins." % player2
player2score=player2score+1
if choice1 == "2" and choice2 == "3":
print "%s wins." % player2
player2score=player2score+1
if choice1 == "3" and choice2 == "2":
print "Player 1 wins." % player1
player1score=player1score+1
print "Player1: %s" % player1score
print "Player2: %s" % player2score
Upvotes: 0