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