Guest User

Untitled

a guest
Feb 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. # ===============================
  2. # IMPORTS RANDOM NUMBER GENERATOR
  3. # ===============================
  4. import random
  5.  
  6. # ================================================================
  7. # GENERATES RANDOMLY THE SUM OF TWO INTEGERS IN THE [1,6] INTERVAL
  8. # ================================================================
  9. def rollDices():
  10. return int(random.randint(1,6) + random.randint(1,6))
  11.  
  12. # ================================================
  13. # RETURN THE OUTCOME STRING GIVEN AN INTEGER STATE
  14. # ================================================
  15. def getOutcome(outcome):
  16. if(outcome == 0):
  17. result = "Lost at first roll."
  18. elif(outcome == 1):
  19. result = "Won at first roll."
  20. elif(outcome == 2):
  21. result = "Won on [4,5,6,8,9,10]"
  22. elif(outcome == 3):
  23. result = "Lost on [4,5,6,8,9,10]"
  24. return result
  25.  
  26. # ==============
  27. # PLAYS THE GAME
  28. # ==============
  29. def playGame():
  30.  
  31. # Define Variables
  32. gameFinished = False
  33. wonGame = False
  34. totRolls = 0
  35.  
  36. while (not(gameFinished)):
  37.  
  38. # ROLL DICES
  39. totScore = rollDices()
  40. totRolls += 1;
  41.  
  42. # GAME IS LOST
  43. if(totScore in [2,3,12]):
  44. gameFinished = True
  45. wonGame = False
  46. return 0,wonGame,totRolls,totScore
  47.  
  48. # GAME IS WON
  49. if(totScore in [7,11]):
  50. gameFinished = True
  51. wonGame = True
  52. return 1,wonGame,totRolls,totScore
  53.  
  54. # JUST CONTINUE PLAYING
  55. if(totScore in [4,5,6,8,9,10]):
  56.  
  57. # REPEAT UNTIL YOU FIND THE SCORE AGAIN OR 7
  58. newScore = 0
  59. while((newScore != totScore)and(newScore != 7)):
  60.  
  61. newScore = rollDices()
  62. totRolls += 1;
  63.  
  64. # CHECK IF YOU WIN OR LOOSE
  65. if(newScore == totScore):
  66. gameFinished = True
  67. wonGame = True
  68. return 2,wonGame,totRolls,totScore
  69.  
  70. if(newScore == 7):
  71. gameFinished = True
  72. wonGame = False
  73. return 3,wonGame,totRolls,totScore
  74.  
  75. # ============
  76. # MAIN PROGRAM
  77. # ============
  78. if __name__ == "__main__":
  79.  
  80. intVal,wonGame,numRolls,lastScore = playGame()
  81.  
  82. print("Outcome: %s" % (getOutcome(intVal)))
  83. if(wonGame):
  84. print("You have won the game.")
  85. else:
  86. print("You have lost the game.")
  87. print("Total rolls needed for this game: %d" % (numRolls))
  88. print("Last outcome: %d" % (lastScore))
Add Comment
Please, Sign In to add comment