Advertisement
timber101

Dice Game 4 - Two Players

Nov 27th, 2022
964
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. #  Dice game extended for two players
  2. #  Again please DON'T just copy this, understand each step as I walk through the video
  3. #  You may find other ways of achieving the same objective
  4.  
  5. import random, time
  6.  
  7. def roll():
  8.     d1 = random.randint(1,6)
  9.     d2 = random.randint(1,6)
  10.     #d1 = int(input("Enter d1 >>"))
  11.     #d2 = int(input("Enter d2 >>"))
  12.     d3 = 0
  13.     tot = d1 + d2
  14.     if tot % 2 ==0:  # testing there for an even number
  15.         tot +=10
  16.     else:
  17.         tot -= 5 # tot = tot -5
  18.         if tot < 0:
  19.             tot = 0
  20.     if d1 == d2: # checking for both the same
  21.         d3 = random.randint(1,6)
  22.         tot += d3
  23.     #print("die1=",d1,"die2=", d2, "die3=",d3,"total=", tot) # for testing
  24.     return tot
  25.  
  26. p1score = 0 # initalising total variables
  27. p2score = 0
  28. for i in range(5):
  29.     print("round", i+1)
  30.     p1roll = roll()
  31.     p2roll = roll()
  32.     time.sleep(1)
  33.     print("P1 scored", p1roll)
  34.     time.sleep(1)
  35.     print("P2 scored", p2roll)
  36.     p1score +=p1roll
  37.     p2score +=p2roll # p2score = p2score +p2roll
  38.     time.sleep(1)
  39.     print("Total scores P1 -", p1score,"P2 -", p2score)
  40.     #input()
  41. #forcing the score to be the same
  42. #p1score = 100
  43. #p2score = 100
  44.  
  45. while p1score == p2score:
  46.     print("Draw round")
  47.     p1roll = roll()
  48.     p2roll = roll()
  49.     time.sleep(1)
  50.     print("P1 Draw roll scored", p1roll)
  51.     time.sleep(1)
  52.     print("P2 Draw roll scored", p2roll)
  53.     p1score +=p1roll
  54.     p2score +=p2roll # p2score = p2score +p2roll
  55.     time.sleep(1)
  56.     print("Total scores P1 -", p1score,"P2 -", p2score)
  57.    
  58. if p1score > p2score:
  59.    print("Player 1 wins score", p1score)
  60. else:
  61.    print("Player 2 wins score", p2score)  
  62. #print("die1=",d1,"die2=", d2, "die3=",d3,"total=", tot)
  63. #print("die1= " + str(d1) +" die2= " +str(d2) + " tot= " +str(tot)) # concatenation + casting
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement