4 Komitmen 9f846996fc ... fa82e2d78e

Pembuat SHA1 Pesan Tanggal
  Bence DOMONKOS fa82e2d78e tmp: classful bots 1 tahun lalu
  GAcs 735d2d424e feat: stress test 1 tahun lalu
  GAcs 8f6f881524 feat: 3 bot strategies implemented 1 tahun lalu
  GAcs f691922a1f feat: factor out common parts from client and bot 1 tahun lalu
9 mengubah file dengan 394 tambahan dan 170 penghapusan
  1. 82 78
      bot.py
  2. 64 0
      bot_biggest_card.py
  3. 65 0
      bot_first_card.py
  4. 64 0
      bot_lowest_card.py
  5. 4 91
      client.py
  6. 86 0
      common_client.py
  7. 4 0
      game.py
  8. 1 1
      main.py
  9. 24 0
      minbot.py

+ 82 - 78
bot.py

@@ -1,96 +1,100 @@
 import json
-import select
 import socket
-import sys
-import time
 
 HOST = 'localhost'
 PORT = 6546
 
-ssock = None
+class Bot:
+	def __init__(self, name='BOT', host='localhost', port=PORT, initiate_start=False):
+		self.name = name
+		self.ssock = None
+		self.initiate_start = initiate_start
 
-def listen():
-	global ssock
-	ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-	#ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-	ssock.connect((HOST, PORT))
-	print('connected:', ssock)
-	send_msg(set_name('BOT_select_first'))
-	while True:
-		bmsg = ssock.recv(1024)
-		if len(bmsg) <= 0:
-			print('read 0; hangup')
-			exit(3)
-		msg = str(bmsg, 'utf-8').strip()
-		jmsg = json.loads(msg)
-		msg_type = parse_server_message(jmsg)
-		print('parsed msg_type:', msg_type)
-		if msg_type == 'query':
-			action = get_action(jmsg)
-			print('parsed action:', action)
-			if action == 'give_card':
-				bot_give_card(jmsg)
-			elif action == 'select_row':
-				bot_select_row(jmsg)
-			else:
-				print('unknown message action:', action)
-		else:
-			print('unknown message type:', msg_type)
+	@classmethod
+	def build_action(cls, action, rest = {}):
+		ret = { 'action': action }
+		ret = ret | rest
+		return ret
 
-def bot_give_card(msg):
-	print('about to give card')
-	hand = msg.get('hand')
-	print(hand, '->', hand[0])
-	msg = give_card(hand[0])
-	send_msg(msg)
-	return hand[0]
+	@classmethod
+	def choose_row(cls, row_index):
+		return cls.build_action('select_row', { 'i': row_index })
 
-def bot_select_row(msg):
-	print('about to select row')
-	msg = choose_row(0)
-	send_msg(msg)
-	return 0
+	@classmethod
+	def give_card(cls, card):
+		return cls.build_action('give_card', { 'card': card })
 
-def get_action(jmsg):
-	try:
-		action = jmsg.get('action')
-	except:
-		print('unable to get action')
-		exit(8)
-	return action
+	def bot_give_card(self, msg):
+		pass
 
-def parse_server_message(jmsg):
-	try:
-		typ = jmsg.get('type')
-	except:
-		print('unable to get type')
-		exit(8)
-	return typ
+	def bot_select_row(self, msg):
+		pass
 
-def send_msg(msg):
-	jmsg = json.dumps(msg)
-	print('msg to send:', jmsg)
-	ssock.send(bytes(jmsg, 'utf-8'))
+	def send_msg(self, msg):
+		jmsg = json.dumps(msg)
+		print('msg to send:', jmsg)
+		self.ssock.send(bytes(jmsg, 'utf-8'))
 
-def build_action(action, rest = {}):
-	ret = { 'action': action}
-	ret = ret | rest
-	return ret
+	def connect(self, host='localhost', port=6546):
+		def set_name(name):
+			return self.build_action('set_name', { 'name': name })
 
-def choose_row(row_index):
-	return build_action('select_row', { 'i': row_index })
+		self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+		self.ssock.connect((host, port))
+		self.send_msg(set_name(self.name))
 
-def give_card(card):
-	return build_action('give_card', { 'card': card })
+	def play(self, initiate_start=False):
+		def get_msg_type(msg):
+			try:
+				typ = msg.get('type')
+			except:
+				print('unable to get type')
+				exit(8)
+			return typ
 
-def set_name(name):
-	return build_action('set_name', { 'name': name })
+		def get_action(msg):
+			try:
+				action = msg.get('action')
+			except:
+				print('unable to get action')
+				exit(9)
+			return action
 
-if __name__ == '__main__':
-	try:
 		while True:
-			listen()
-	except KeyboardInterrupt:
-		print('interrupt - exiting')
-		#TODO: close
-		exit(9)
+			bmsg = self.ssock.recv(1024)
+			if len(bmsg) <= 0:
+				print('read 0; hangup')
+				exit(3)
+			jmsg = str(bmsg, 'utf-8').strip()
+			msg = json.loads(jmsg)
+			print('msg:', msg)
+			msg_type = get_msg_type(msg)
+			#print('parsed msg_type:', msg_type)
+			if msg_type == 'query':
+				action = get_action(msg)
+				print('parsed action:', action)
+				if action == 'give_card':
+					self.bot_give_card(msg)
+				elif action == 'select_row':
+					self.bot_select_row(msg)
+				else:
+					print('unknown message action:', action)
+			elif msg_type == 'info':
+				print('info type is not implemented')
+			else:
+				print('unknown message type:', msg_type)
+
+	@classmethod
+	def calc_penalty(cls, row):
+		def card_value(card):
+			if card == 55:
+				return 7
+			elif (card % 11) == 0:
+				return 5
+			elif (card % 10) == 0:
+				return 3
+			elif (card % 5) == 0:
+				return 2
+			return 1
+
+		return sum(map(card_value, row))

+ 64 - 0
bot_biggest_card.py

@@ -0,0 +1,64 @@
+import json
+import select
+import socket
+import sys
+import time
+from common_client import *
+PLAYER_NAME='BOT_BIGGEST'
+
+def listen(start=False):
+	global ssock
+	ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+	#ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+	ssock.connect((HOST, PORT))
+	print('connected:', ssock)
+	send_msg(ssock,set_name(PLAYER_NAME))
+	if start:
+		send_msg(ssock, start_game())
+	while True:
+		bmsg = ssock.recv(1024)
+		if len(bmsg) <= 0:
+			print('read 0; hangup')
+			exit(3)
+		msg = str(bmsg, 'utf-8').strip()
+		jmsg = json.loads(msg)
+		print(jmsg)
+		msg_type = parse_server_message(jmsg)
+		print('parsed msg_type:', msg_type)
+		if msg_type == 'query':
+			action = get_action(jmsg)
+			print('parsed action:', action)
+			if action == 'give_card':
+				bot_give_card(jmsg)
+			elif action == 'select_row':
+				bot_select_row(jmsg)
+			else:
+				print('unknown message action:', action)
+		else:
+			print('unknown message type:', msg_type)
+
+def bot_give_card(msg):
+	print('about to give card')
+	hand = msg.get('hand')
+	print(hand, '->', max(hand))
+	msg = give_card(max(hand))
+	send_msg(ssock,msg)
+	return max(hand)
+
+def bot_select_row(msg):
+	print('about to select row')
+	msg = choose_row(0)
+	send_msg(ssock,msg)
+	return 0
+
+
+
+
+if __name__ == '__main__':
+	try:
+		while True:
+			listen(len(sys.argv)>1)
+	except KeyboardInterrupt:
+		print('interrupt - exiting')
+		#TODO: close
+		exit(9)

+ 65 - 0
bot_first_card.py

@@ -0,0 +1,65 @@
+import json
+import select
+import socket
+import sys
+import time
+from common_client import *
+PLAYER_NAME='BOT_select_first'
+
+def listen(start=False):
+	global ssock
+	ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+	#ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+	ssock.connect((HOST, PORT))
+	print('connected:', ssock)
+	send_msg(ssock,set_name(PLAYER_NAME))
+	time.sleep(1)
+	if start:
+		send_msg(ssock, start_game())
+	while True:
+		bmsg = ssock.recv(1024)
+		if len(bmsg) <= 0:
+			print('read 0; hangup')
+			exit(3)
+		msg = str(bmsg, 'utf-8').strip()
+		jmsg = json.loads(msg)
+		print(jmsg)
+		msg_type = parse_server_message(jmsg)
+		print('parsed msg_type:', msg_type)
+		if msg_type == 'query':
+			action = get_action(jmsg)
+			print('parsed action:', action)
+			if action == 'give_card':
+				bot_give_card(jmsg)
+			elif action == 'select_row':
+				bot_select_row(jmsg)
+			else:
+				print('unknown message action:', action)
+		else:
+			print('unknown message type:', msg_type)
+
+def bot_give_card(msg):
+	print('about to give card')
+	hand = msg.get('hand')
+	print(hand, '->', hand[0])
+	msg = give_card(hand[0])
+	send_msg(ssock,msg)
+	return hand[0]
+
+def bot_select_row(msg):
+	print('about to select row')
+	msg = choose_row(0)
+	send_msg(ssock,msg)
+	return 0
+
+
+
+
+if __name__ == '__main__':
+	try:
+		while True:
+			listen(len(sys.argv)>1)
+	except KeyboardInterrupt:
+		print('interrupt - exiting')
+		#TODO: close
+		exit(9)

+ 64 - 0
bot_lowest_card.py

@@ -0,0 +1,64 @@
+import json
+import select
+import socket
+import sys
+import time
+from common_client import *
+PLAYER_NAME='BOT_lowest_first'
+
+def listen(start=False):
+	global ssock
+	ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+	#ssock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
+	ssock.connect((HOST, PORT))
+	print('connected:', ssock)
+	send_msg(ssock,set_name(PLAYER_NAME))
+	if start:
+		send_msg(ssock, start_game())
+	while True:
+		bmsg = ssock.recv(1024)
+		if len(bmsg) <= 0:
+			print('read 0; hangup')
+			exit(3)
+		msg = str(bmsg, 'utf-8').strip()
+		jmsg = json.loads(msg)
+		print(jmsg)
+		msg_type = parse_server_message(jmsg)
+		print('parsed msg_type:', msg_type)
+		if msg_type == 'query':
+			action = get_action(jmsg)
+			print('parsed action:', action)
+			if action == 'give_card':
+				bot_give_card(jmsg)
+			elif action == 'select_row':
+				bot_select_row(jmsg)
+			else:
+				print('unknown message action:', action)
+		else:
+			print('unknown message type:', msg_type)
+
+def bot_give_card(msg):
+	print('about to give card')
+	hand = msg.get('hand')
+	print(hand, '->', min(hand))
+	msg = give_card(min(hand))
+	send_msg(ssock,msg)
+	return min(hand)
+
+def bot_select_row(msg):
+	print('about to select row')
+	msg = choose_row(0)
+	send_msg(ssock,msg)
+	return 0
+
+
+
+
+if __name__ == '__main__':
+	try:
+		while True:
+			listen(len(sys.argv)>1)
+	except KeyboardInterrupt:
+		print('interrupt - exiting')
+		#TODO: close
+		exit(9)

+ 4 - 91
client.py

@@ -3,12 +3,10 @@ import select
 import socket
 import sys
 import time
+from common_client import *
 
-HOST = 'localhost'
-PORT = 6546
 
 ssock = None
-
 def listen():
 	global ssock
 	ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@@ -36,97 +34,13 @@ def listen():
 					print('read 0; hangup')
 					exit(3)
 				msg = str(bmsg, 'utf-8').strip()
-				parse_server_message(msg)
+				jmsg = json.loads(msg)
+				parse_server_message(jmsg)
 			else:
 				print('unknown socket:', s)
 				exit(2)
 
-def print_info_msg(msg):
-	spit_json(msg)
-
-def pretty_print_status(msg):
-	print('\nTURN:', msg.get('turn'), '\n')
-	for i, row in enumerate(msg.get('board')):
-		print(i + 1, ':', end='')
-		for card in row:
-			print(' %3d' % (card,), end='')
-		print('')
-	print('Your hand:', end='')
-	for card in msg.get('hand'):
-		print(' %3d' % (card,), end='')
-	print('')
-
-def print_query_msg(msg):
-	if msg.get('action') == 'give_card':
-		pretty_print_status(msg)
-	else:
-		spit_json(msg)
-
-def spit_json(msg):
-	for k, v in msg.items():
-		print(k, v)
-
-def parse_server_message(jmsg):
-	try:
-		msg = json.loads(jmsg)
-	except:
-		print('len:', len(jmsg), 'unable to parse:', jmsg,)
-		exit(8)
-	typ = msg.get('type')
-	if typ == 'info':
-		print_info_msg(msg)
-	elif typ == 'query':
-		print_query_msg(msg)
-	else:
-		print('unknown message type:', typ)
-
-def send_msg(msg):
-	jmsg = json.dumps(msg)
-	print('msg to send:', jmsg)
-	ssock.send(bytes(jmsg, 'utf-8'))
-
-def print_not_implemented():
-	print('not_implemented')
-
-def process_info(msg):
-	print_not_implemented()
 
-def process_query(msg):
-	print_not_implemented()
-
-def parse_msg(jmsg):
-	try:
-		msg = json.loads(jmsg)
-		typ = msg.get('type')
-		if typ == 'info':
-			process_info(msg)
-		elif typ == 'query':
-			process_query(msg)
-		else:
-			print('unknown type:', typ)
-
-		print('info:', msg)
-
-	except json.decoder.JSONDecodeError:
-		print('unparsable message:', jmsg)
-
-def build_action(action, rest = {}):
-	ret = { 'action': action}
-	ret = ret | rest
-
-	return ret
-
-def choose_row(row_index):
-	return build_action('select_row', { 'i': row_index })
-
-def set_name(name):
-	return build_action('set_name', { 'name': name })
-
-def start_game():
-	return build_action('start')
-
-def give_card(card):
-	return build_action('give_card', { 'card': card })
 
 def quit_game():
 	return build_action('quit')
@@ -155,8 +69,7 @@ def parse(line):
 	else:
 		print('unknown cmd:', cmd)
 		return
-
-	send_msg(msg)
+	send_msg(ssock,msg)
 
 if __name__ == '__main__':
 	try:

+ 86 - 0
common_client.py

@@ -0,0 +1,86 @@
+import json
+HOST = 'localhost'
+PORT = 6546
+
+def print_info_msg(msg):
+	spit_json(msg)
+
+def pretty_print_status(msg):
+	print('\nTURN:', msg.get('turn'), '\n')
+	for i, row in enumerate(msg.get('board')):
+		print(i + 1, ':', end='')
+		for card in row:
+			print(' %3d' % (card,), end='')
+		print('')
+	print('Your hand:', end='')
+	for card in msg.get('hand'):
+		print(' %3d' % (card,), end='')
+	print('')
+
+def print_query_msg(msg):
+	if msg.get('action') == 'give_card':
+		pretty_print_status(msg)
+	else:
+		spit_json(msg)
+
+def spit_json(msg):
+	for k, v in msg.items():
+		print(k, v)
+
+def parse_server_message2(jmsg):
+	try:
+		typ = jmsg.get('type')
+	except:
+		print('unable to get type')
+		exit(8)
+	return typ
+
+def parse_server_message(jmsg):
+	typ = jmsg.get('type')
+	if typ == 'info':
+		print_info_msg(jmsg)
+	elif typ == 'query':
+		print_query_msg(jmsg)
+	else:
+		print('unknown message type:', typ)
+	return typ
+
+def send_msg(ssock,msg):
+	jmsg = json.dumps(msg)
+	print('msg to send:', jmsg)
+	ssock.send(bytes(jmsg, 'utf-8'))
+
+def print_not_implemented():
+	print('not_implemented')
+
+def process_info(msg):
+	print_not_implemented()
+
+def process_query(msg):
+	print_not_implemented()
+
+def build_action(action, rest = {}):
+	ret = { 'action': action}
+	ret.update(rest)
+
+	return ret
+
+def start_game():
+	return build_action('start')
+
+def choose_row(row_index):
+	return build_action('select_row', { 'i': row_index })
+
+def set_name(name):
+	return build_action('set_name', { 'name': name })
+
+def give_card(card):
+	return build_action('give_card', { 'card': card })
+
+def get_action(jmsg):
+	try:
+		action = jmsg.get('action')
+	except:
+		print('unable to get action')
+		exit(8)
+	return action

+ 4 - 0
game.py

@@ -65,6 +65,10 @@ class Game:
 				self.lobby()
 			else:
 				self.game()
+				max_score=max([p.score for p in self.players.values()])
+				if max_score<self.score_limit:
+					self.game_state=GAME_STATE.PLAY
+
 
 	def process_lobby_message(self, client, msg):
 		action = self.get_controll_message_action(msg)

+ 1 - 1
main.py

@@ -9,7 +9,7 @@ PORT = 6546
 if __name__ == '__main__':
 	try:
 		serversock = Server_Socket(HOST, PORT)
-		game = game.Game(serversock, 66, 6)
+		game = game.Game(serversock, 10000, 10)
 		game.run()
 
 	except KeyboardInterrupt:

+ 24 - 0
minbot.py

@@ -0,0 +1,24 @@
+import bot
+
+class MinBot(bot.Bot):
+	def bot_give_card(self, msg):
+		hand = msg.get('hand')
+		card = min(hand)
+		msg = super().give_card(card)
+		super().send_msg(msg)
+
+	def bot_select_row(self, msg):
+		rows = msg.get('rows')
+		msg = self.choose_row(0)
+		super().send_msg(msg)
+
+if __name__ == '__main__':
+	mb = MinBot()
+	try:
+		mb.connect()
+		while True:
+			mb.play()
+	except KeyboardInterrupt:
+		print('interrupt - exiting')
+		#TODO: close
+		exit(9)