This is a finished project where Two people roll a dice twice and if the total of the two dice are even it adds 10 points to the two dice rolls and if the total is negative it takes away 5 points from the two dice rolls.
import random
import time
total_score2 = 0
total_score1 = 0
rounds = 0
playerOnePoints = 0
playerTwoPoints = 0
while rounds < 5:
total_score2 = total_score2 + playerTwoPoints
total_score1 = total_score1 + playerOnePoints
rounds = rounds + 1
number = random.randint(1,6)
number2 = random.randint(1,6)
playerOnePoints = number + number2
print("Round",rounds)
print("-------------------------------------------")
print("Player 1's turn Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
time.sleep(1)
print("Player 1's first roll is", number)
print("Player 1's second roll Type 'roll' to roll the dice")
userOneInput = input(">>> ")
if userOneInput == "roll":
time.sleep(1)
print("player 1's second roll is", number2)
if playerOnePoints % 2 == 0:
playerOnePoints = playerOnePoints + 10
print("Player 1's total is even so + 10 points")
print("-------------------------------------------")
print("Player 1 has",playerOnePoints, "points")
else:
playerOnePoints = playerOnePoints - 5
print("player 1's total is odd so -5 points")
print("-------------------------------------------")
print("Player 1 has",playerOnePoints, "points")
number = random.randint(1,6)
number2 = random.randint(1,6)
playerTwoPoints = number + number2
print("-------------------------------------------")
print("Player 2's turn Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
time.sleep(1)
print("Player 2's first roll is", number)
print("Player 2's second roll Type 'roll' to roll the dice")
userTwoInput = input(">>> ")
if userTwoInput == "roll":
time.sleep(1)
print("player 2's second roll is", number2)
if playerTwoPoints <= 0:
playerTwoPoints = 0
if playerTwoPoints % 2 == 0:
playerTwoPoints = playerTwoPoints + 10
print("Player 2's total is even so + 10 points")
print("-------------------------------------------")
print("Player 2 has",playerTwoPoints, "points")
else:
playerTwoPoints = playerTwoPoints - 5
print("player 2's total is odd so -5 points")
print("-------------------------------------------")
print("Player 2 has",playerTwoPoints, "points")
print("-------------------------------------------")
print("Total score for player 1 is", total_score1)
print("-------------------------------------------")
print("Total score for player 2 is", total_score2)
print("-------------------------------------------")
As I mentioned this is a finished project but I would like suggestions on how I could improve this project to make it better also I would like it if someone could explain to me on how to shorten the code. All suggestions will be greatly appreciated
Thanks :-)
-
\$\begingroup\$ Every contribution to this will help me alot so please make suggestions and as I said every contribution will be greatly appreciated \$\endgroup\$colkat406– colkat4062018年12月03日 16:54:57 +00:00Commented Dec 3, 2018 at 16:54
1 Answer 1
Some suggestions:
Pulling out functions or classes w/methods will allow you to reduce the amount of nesting in the code, and at the same time will package the code into easily understood functionally and semantically separate parts. This is incredibly important in larger programs, but can also highlight ways in which existing shorter programs can be structured for maintainability. For example, a
Player
class could have ascore
field and aGame
class could have arounds
field and aroll
method:MAX_ROUNDS = 5 class Player: def __init__(self): self.score = 0 def win(self): self.score += 10 def lose(self): self.score -= 5 class Game: rounds = 0 player_1 = Player() player_2 = Player() def play(): if self.rounds >= MAX_ROUNDS: ... else: ... self.rounds += 1
Dependency injection of
randint
andinput
would allow this code to be unit tested.- If someone wants to play this a lot they would probably not appreciate the "dramatic pause" before showing the result of the rolls. I would simply get rid of the
sleep
s.
-
\$\begingroup\$ Could you show me what you mean by when you said (a Player class could have a score field and a Game class could have a rounds field and a roll method.) \$\endgroup\$colkat406– colkat4062018年12月03日 16:32:01 +00:00Commented Dec 3, 2018 at 16:32
-
\$\begingroup\$ if I was to put the code you wrote into my code where would I put it ? \$\endgroup\$colkat406– colkat4062018年12月04日 17:28:08 +00:00Commented Dec 4, 2018 at 17:28
-
\$\begingroup\$ I'm afraid that's a bit more coaching than you can expect from this site. If you want to learn the basics of object orientation I'm afraid you'll have to look for a course or tutorial. \$\endgroup\$l0b0– l0b02018年12月04日 18:46:10 +00:00Commented Dec 4, 2018 at 18:46
-
1\$\begingroup\$ Isn't that code declaring
rounds
,score
, etc as class variables, and not instance variables? Wouldn't all instances ofPlayer
be sharing the same score? \$\endgroup\$eric.m– eric.m2019年07月05日 08:18:02 +00:00Commented Jul 5, 2019 at 8:18 -
\$\begingroup\$ @eric.m Why would all instances of
Player
share the same score. If you instantiate multiple players for a multi-player game, then they should have different score. Or maybe I misunderstand something. \$\endgroup\$cezar– cezar2020年02月27日 17:41:50 +00:00Commented Feb 27, 2020 at 17:41