from MiniGame import *

class RockPaperScissorsException(Exception):
	pass

class RockPaperScissors(MiniGame):
	states = [
		#r  p  s
		[0, 2, 1], # rock
		[1, 0, 2], # paper
		[2, 1, 0]] # scissors
	def __init__(self):
		self.Reset()
	
	def GetName(self, id):
		try:
			return ["rock", "paper", "scissors"][id]
		except:
			return ""
	
	def Reset(self):
		self.board = [-1, -1]
	
	def Play(self, move):
		"""
		Make a move.
		"""
		player, state = move
		
		# if nobody has won
		if not self.Notify(self.GetStatus()):
			# check if a particular move is legal
			if not self.LegalMove(move):
				raise RockPaperScissorsException, "That move is not legal"
			else:
				# the move is legal
				
				# fill the slot
				self.board[player - 1] = state
				
				if self.Notify(self.GetStatus()):
					self.Reset()
	
	def GetStatus(self):
		"""
		See if either player has won.
		Return -1 if the game is a draw.
		Return 0 if the game is in progress.
		Return 1 if player 1 has won.
		Return 2 if player 2 has won.
		"""
		# if there are still slots to play
		if self.IsPlayable():
			status = None
		else: # get our status if the game is won
			status = (len([b for b in range(2) if self.board[b] == -1]) == 0 and self.states[self.board[0]][self.board[1]] or -1)
		
		return status
	
	def GetBoard(self):
		"""
		Get the current layout of the board.
		"""
		return self.board
	
	def LegalMove(self, move):
		player, state = move
		return player in [1, 2] and state in [0, 1, 2] and self.board[player - 1] == -1
	
	def IsPlayable(self):
		return len([b for b in self.board if b == -1])


