# Path: toolbox.py
# def log(*args):
# 	global loglock
# 	loglock.acquire()
# 	encoding=sys.stdout.encoding
# 	for arg in args:
# 		try:
# 			if type(arg)==type(str('abc')):
# 				arg=arg.decode('utf-8',errors='replace')
# 			elif type(arg)!=type(u'abc'):
# 				try:
# 					arg=str(arg)
# 				except:
# 					arg=unicode(arg,errors='replace')
# 				arg=arg.decode('utf-8',errors='replace')
# 			arg=arg.encode(encoding, errors='replace')
# 			print arg,
# 		except:
# 			print "?"*len(arg),
# 	print
# 	loglock.release()
# 
# class GRPException(Exception):
# 	def __init__(self,msg):
# 		if type(msg)==type(u"abc"):
# 			self.utf_msg=msg
# 			self.str_msg=msg.encode("utf-8",errors='replace')
# 		else:
# 			self.str_msg=msg
# 			self.utf_msg=msg.decode("utf-8",errors='replace')
# 		log("===")
# 		log(format_exc())
# 		log("===")
# 		Exception.__init__(self,self.str_msg)
# 
# 	def __unicode__(self):
# 		return self.utf_msg


import subprocess,sys
import threading, Queue
from time import sleep
from toolbox import log,GRPException
	def name(self):
		self.write("name")
		answer=self.readline().strip()
		try:
			return " ".join(answer.split(" ")[1:])
		except Exception, e:
			raise GRPException("GRPException in name()\nanswer='"+answer+"'\n"+unicode(e))
	
	def version(self):
		self.write("version")
		answer=self.readline().strip()
		try:
			return answer.split(" ")[1]
		except Exception,e:
			raise GRPException("GRPException in version()\nanswer='"+answer+"'\n"+unicode(e))


	def set_free_handicap(self,positions):
		self.free_handicap_stones=positions[:]
		stones=""
		for p in positions:
			stones+=p+" "
		self.write("set_free_handicap "+stones.strip())
		answer=self.readline().strip()
		try:
			if answer[0]=="=":
				return True
			else:
				return False	
		except Exception, e:

# Path: toolbox.py
# def log(*args):
# 	global loglock
# 	loglock.acquire()
# 	encoding=sys.stdout.encoding
# 	for arg in args:
# 		try:
# 			if type(arg)==type(str('abc')):
# 				arg=arg.decode('utf-8',errors='replace')
# 			elif type(arg)!=type(u'abc'):
# 				try:
# 					arg=str(arg)
# 				except:
# 					arg=unicode(arg,errors='replace')
# 				arg=arg.decode('utf-8',errors='replace')
# 			arg=arg.encode(encoding, errors='replace')
# 			print arg,
# 		except:
# 			print "?"*len(arg),
# 	print
# 	loglock.release()
# 
# class GRPException(Exception):
# 	def __init__(self,msg):
# 		if type(msg)==type(u"abc"):
# 			self.utf_msg=msg
# 			self.str_msg=msg.encode("utf-8",errors='replace')
# 		else:
# 			self.str_msg=msg
# 			self.utf_msg=msg.decode("utf-8",errors='replace')
# 		log("===")
# 		log(format_exc())
# 		log("===")
# 		Exception.__init__(self,self.str_msg)
# 
# 	def __unicode__(self):
# 		return self.utf_msg


# Path: gtp.py
import subprocess,sys
import threading, Queue
from time import sleep
from toolbox import log,GRPException

	def name(self):
		self.write("name")
		answer=self.readline().strip()
		try:
			return " ".join(answer.split(" ")[1:])
		except Exception, e:
			raise GRPException("GRPException in name()\nanswer='"+answer+"'\n"+unicode(e))
	
	def version(self):
		self.write("version")
		answer=self.readline().strip()
		try:
			return answer.split(" ")[1]
		except Exception,e:
			raise GRPException("GRPException in version()\nanswer='"+answer+"'\n"+unicode(e))


	def set_free_handicap(self,positions):
		self.free_handicap_stones=positions[:]
		stones=""
		for p in positions:
			stones+=p+" "
		self.write("set_free_handicap "+stones.strip())
		answer=self.readline().strip()
		try:
			if answer[0]=="=":
				return True
			else:
				return False	
		except Exception, e:

			raise GRPException("GRPException in set_free_handicap()\nanswer='"+answer+"'\n"+unicode(e))