#!/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'