fork download
  1. #!/usr/bin/python2.6
  2.  
  3. #try exception checking imported modules are loaded
  4. try:
  5. #import pygame
  6. #from pygame.locals import *
  7. import random
  8. #import time
  9. except ImportError, err:
  10. print " The module %s failed to load" %(err)
  11. exit(1)
  12.  
  13. #pygame.init()
  14.  
  15. #frames/s setting
  16. #FPS = 1
  17.  
  18. #create object to track time
  19. #timer = pygame.time.Clock()
  20.  
  21. #set screen
  22. #screen = pygame.display.set_mode((500,300))
  23. #pygame.display.set_caption('The Game of Pig (2 dice)')
  24.  
  25. #background = pygame.Surface(screen.get_size())
  26. #background = background.convert()
  27. #background.fill((250,250,250))
  28. #clock = pygame.time.Clock()
  29.  
  30. #load images of dice faces (1-6)
  31. #DIE1 = [pygame.image.load('Face-1.png'),pygame.image.load('Face-2.png'),
  32. pygame.image.load('Face-3.png'),pygame.image.load('Face-4.png'),
  33. pygame.image.load('Face-5.png'),pygame.image.load('Face-6.png')]
  34.  
  35. #DIE2 = [pygame.image.load('Face-1.png'),pygame.image.load('Face-2.png'),
  36. pygame.image.load('Face-3.png'),pygame.image.load('Face-4.png'),
  37. pygame.image.load('Face-5.png'),pygame.image.load('Face-6.png')]
  38.  
  39. class game():
  40. #Initialize variables to 0 and true. Fresh (new) player
  41. def __init__(self):
  42. self.round_score = 0
  43. self.total_score = 0
  44. self.turn = True
  45.  
  46. #function to roll the 2 dice. set turn to true.
  47. #return dice as a tuple
  48. def roll_dice(self):
  49. self.turn = True
  50. return (random.randint(1,6), random.randint(1,6))
  51.  
  52. #function to hold. Add player's current round score to total score
  53. #set turn to false. indicating next player's turn
  54. def hold_dice(self):
  55. self.total_score += self.round_score
  56. self.round_score = 0
  57. self.turn = False
  58. print 'hold:\ntotal_score = %s' %(self.total_score)
  59.  
  60. #function to determine:
  61. #1.either die is 1 if so round score set to 0 and turn over
  62. #2.both dice are 1 total score set to 0 and turn over
  63. def check_dice_status(self, dice):
  64. if( dice[0] == 1 and dice[1] == 1):
  65. self.round_score, self.total_score = 0,0
  66. self.turn = False
  67. print 'total_score set to 0'
  68.  
  69. elif( dice[0] == 1 or dice[1] ==1):
  70. self.round_score = 0
  71. self.turn = False
  72. print 'round_score set to 0'
  73. else:
  74. self.round_score += (dice[0]+dice[1])
  75. self.turn = True
  76. print 'round_score = %s' %(self.round_score)
  77.  
  78. #function to return status on turn.
  79. #return turn (true/false)
  80. def check_turn(self):
  81. return self.turn
  82.  
  83. #function that returns the true if player is winner else false
  84. def winner(self):
  85. if(self.total_score >= 100):
  86. return True
  87. else:
  88. return False
  89.  
  90. #create objects of the class game, which is the rules
  91. player1, player2 = game(), game()
  92.  
  93. #pick a player to go first randomly
  94. player_turn =random.randint(1,2)
  95. print 'Player %s goes first' %(player_turn)
  96.  
  97. #way to stop loop
  98. game_state = True
  99.  
  100. while(game_state):#game loop
  101. #print which player's turn it is
  102. print '\nPlayer%s :' %(player_turn)
  103. option = raw_input('Roll or Hold? ')
  104.  
  105. #if player 1 do below commands, else player 2
  106. if(player_turn == 1):
  107.  
  108. #if a winner is found. break the loop
  109. if(player1.winner()):
  110. game_state = False
  111. break
  112. #1 of 2 commands, ROLL. Check dice status to determine if their score has diminished or added onto
  113. if(option.lower() == 'roll'):
  114.  
  115. #roll contains a tuple of the random number(dice roll)
  116. roll = player1.roll_dice()
  117.  
  118. #display their rolls
  119. print 'roll[0], roll[1] =', roll[0], ' ', roll[1]
  120.  
  121. #check status of dice, adjust score accordingly
  122. player1.check_dice_status(roll)
  123.  
  124. #determines when the player's next turn will be
  125. if (player1.check_turn()): #check status of current player's turn
  126. player_turn = 1
  127. else:
  128. player_turn = 2
  129. #2 of 2 commands, HOLD. Player holds, meaning round score is added to total score.
  130. #next player's turn
  131. elif(option.lower() == 'hold'):
  132. player1.hold_dice()
  133. player_turn = 2
  134.  
  135. #player 2 does below commands. Iterate commands from top
  136. elif(player_turn == 2):
  137.  
  138. if(player2.winner()):
  139. game_state = False
  140. break
  141.  
  142. if(option.lower() == 'roll'):
  143.  
  144. roll = player2.roll_dice()
  145. print 'roll[0], roll[1] =', roll[0], ' ', roll[1]
  146. player2.check_dice_status(roll)
  147.  
  148. if (player2.check_turn()):
  149. player_turn = 2
  150. else:
  151. player_turn = 1
  152.  
  153. elif(option.lower() == 'hold'):
  154. player2.hold_dice()
  155. player_turn = 1
  156.  
  157. #declare which one is the winner
  158. if(player1.winner()):
  159. print '\nplayer1 is the WINNER\n'
  160. else:
  161. print '\nplayer2 is the WINNER\n'
Runtime error #stdin #stdout 0.02s 4672KB
stdin
Standard input is empty
stdout
Standard output is empty