<commit_before># -*- coding: utf-8 -*-
import random, board
from wx.lib.pubsub import pub

class Game():
	"""
	Area game model
	"""
	def __init__(	self, height=28, width=40,\
	             	colors=["red","green","blue","yellow","magenta"],\
	             	color_enclosed=False, count_enclosed=True, win_enclosed=False):

		"""
		create game, assume 2 players hard coded
		"""

		# len(colors) > len(players) actually
		assert len(colors)	> 2, "colors list too short, length must be > 2"
		assert height     	> 4, "height too small, must be > 4"
		assert width      	> 4, "width too small, must be > 4"

		# set up board
		self.area = board.Board(height,width,colors)
		self.colors = range(len(colors)) # we only need the indices

		## handling of enclosed areas
		## NOTE: use exactly one for efficiency
		# color enclosed area immediately
		self.color_enclosed = color_enclosed
		# count enclosed area for score
		# NOTE: is implied by `color_enclosed`
		self.count_enclosed = count_enclosed
		# win condition based on enclosed area
		# NOTE: is implied by `count_enclosed`
		self.win_enclosed = win_enclosed

		# create players
		# TODO: generalize to distribute arbitrary number of players evenly
		self.players = []
		self.players.append(Player("Player 1",(0,0)))
		self.players.append(Player("Player 2",(height-1,width-1)))

		# set up players
		self.set_players()

		# one of the players starts at random
		self.turn = random.randrange(len(self.players))

	def __getitem__(self,key):
		return self.area[key]

	def __setitem__(self,key,value):
		self.area[key]	= value

	def __str__(self):
		p = map(str,self.players)
		# mark active player
		for i in range(len(self.players)):
			if self.turn == i:
				p[i] += ' *'

		return '\n'.join([str(self.area)] + p)

	def set_players(self):
		"""
		set up players' start positions
		"""

		assert len(self.players) < len(self.colors), "not enough colors available"

		for p in self.players:
			# initialize color
			# WARNING: do not remove, very important!
			p.color = self[p.pos]

			# get other players' colors in use
			used = self.colors_used()
			used.remove(p.color)
			# set different color if already used
			if p.color in used:
				# get all that is left
				rest = [x for x in self.colors if x not in used]
				# set to one of unused colors
				self.area.set_color({p.pos},random.choice(rest))
			# set up start position, assign player's area and color
			p.area,p.border = self.area.set_start(p.pos)
		for p in self.players:
			self.update_player(p)

	def command(self,p,c):
		"""
		try to fulfill player command
		"""

		assert p in xrange(len(self.players))
		assert c in self.colors

		# make sure it's player's turn and
		# chosen color is not used by other players
		if	p == self.turn and c in self.colors_available(p):
		  	p = self.players[p]
		  	# set new color to player's area
		  	self.area.set_color(p.area,c)

			# color enclosed area
			if self.color_enclosed:
				self.set_color_enclosed(p,c)

			# update player state
			self.update_player(p)
			# go to next turn
			self.next_turn()
			# check if game continues or ends
			if self.winner():
				pub.sendMessage("winner",winner=self.winner())
			else:
				pub.sendMessage("next turn")

	def colors_available(self,p):
		"""
		return list of colors available for a player, removes non-bordering as well
		"""
		if p == self.turn:
			p = self.players[p]
			used = set(self.colors_used())
			# exclude colors not bordering
			_,border = self.area.get_area(p.area,p.border)
			bordering = set([self[x] for x in border])
			available = set(self.colors) & bordering - used
			if available:
				return available
			else:
				# if none left bordering, return all that are not used
				return set(self.colors) - used
		else:
			return []

	def next_turn(self):
		self.turn = (self.turn + 1) % len(self.players)

	def colors_used(self):
		"""
		return list of colors used by players
		"""<commit_msg>allow picking non-bordering colors<commit_after># -*- coding: utf-8 -*-
import random, board
from wx.lib.pubsub import pub

class Game():
	"""
	Area game model
	"""
	def __init__(	self, height=28, width=40,\
	             	colors=["red","green","blue","yellow","magenta"],\
	             	color_enclosed=False, count_enclosed=True, win_enclosed=False):

		"""
		create game, assume 2 players hard coded
		"""

		# len(colors) > len(players) actually
		assert len(colors)	> 2, "colors list too short, length must be > 2"
		assert height     	> 4, "height too small, must be > 4"
		assert width      	> 4, "width too small, must be > 4"

		# set up board
		self.area = board.Board(height,width,colors)
		self.colors = range(len(colors)) # we only need the indices

		## handling of enclosed areas
		## NOTE: use exactly one for efficiency
		# color enclosed area immediately
		self.color_enclosed = color_enclosed
		# count enclosed area for score
		# NOTE: is implied by `color_enclosed`
		self.count_enclosed = count_enclosed
		# win condition based on enclosed area
		# NOTE: is implied by `count_enclosed`
		self.win_enclosed = win_enclosed

		# create players
		# TODO: generalize to distribute arbitrary number of players evenly
		self.players = []
		self.players.append(Player("Player 1",(0,0)))
		self.players.append(Player("Player 2",(height-1,width-1)))

		# set up players
		self.set_players()

		# one of the players starts at random
		self.turn = random.randrange(len(self.players))

	def __getitem__(self,key):
		return self.area[key]

	def __setitem__(self,key,value):
		self.area[key]	= value

	def __str__(self):
		p = map(str,self.players)
		# mark active player
		for i in range(len(self.players)):
			if self.turn == i:
				p[i] += ' *'

		return '\n'.join([str(self.area)] + p)

	def set_players(self):
		"""
		set up players' start positions
		"""

		assert len(self.players) < len(self.colors), "not enough colors available"

		for p in self.players:
			# initialize color
			# WARNING: do not remove, very important!
			p.color = self[p.pos]

			# get other players' colors in use
			used = self.colors_used()
			used.remove(p.color)
			# set different color if already used
			if p.color in used:
				# get all that is left
				rest = [x for x in self.colors if x not in used]
				# set to one of unused colors
				self.area.set_color({p.pos},random.choice(rest))
			# set up start position, assign player's area and color
			p.area,p.border = self.area.set_start(p.pos)
		for p in self.players:
			self.update_player(p)

	def command(self,p,c):
		"""
		try to fulfill player command
		"""

		assert p in xrange(len(self.players))
		assert c in self.colors

		# make sure it's player's turn and
		# chosen color is not used by other players
		if	p == self.turn and c in self.colors_available(p):
		  	p = self.players[p]
		  	# set new color to player's area
		  	self.area.set_color(p.area,c)

			# color enclosed area
			if self.color_enclosed:
				self.set_color_enclosed(p,c)

			# update player state
			self.update_player(p)
			# go to next turn
			self.next_turn()
			# check if game continues or ends
			if self.winner():
				pub.sendMessage("winner",winner=self.winner())
			else:
				pub.sendMessage("next turn")

	def colors_available(self,p):
		"""
		return set of colors available for a player
		"""
		if p == self.turn:
			return set(self.colors) - set(self.colors_used())
		else:
			return set()

	def next_turn(self):
		self.turn = (self.turn + 1) % len(self.players)

	def colors_used(self):
		"""
		return list of colors used by players
		"""
{'fraction_non_alphanumeric': 0.11933333333333333, 'fraction_numerical': 0.005066666666666666, 'mean_word_length': 3.2961053837342495, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6557578', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2479, 'n_words': 767}