7 Коміти 883460ff4c ... 4d340dafa2

Автор SHA1 Опис Дата
  Bence DOMONKOS 4d340dafa2 nimmt: remove mostly debugging messages 1 рік тому
  GAcs a77e91f060 feat: running from command line 1 рік тому
  GAcs c6de9063f4 feat: running until 10000 score 9 clients 1 рік тому
  GAcs 0562dd9051 refact: eliminate enumeration and row_index 1 рік тому
  GAcs 1628384e89 pep: not None syntax num is not None 1 рік тому
  GAcs 5146dc3f6c feat: json decoder error handling 1 рік тому
  GAcs 8346f9c82e feat: bot refactor in bots folder 1 рік тому
10 змінених файлів з 128 додано та 220 видалено
  1. 2 2
      board.py
  2. 0 64
      bot_biggest_card.py
  3. 0 65
      bot_first_card.py
  4. 0 64
      bot_lowest_card.py
  5. 17 25
      bot.py
  6. 23 0
      bots/bot_biggest.py
  7. 40 0
      bots/bot_factory.py
  8. 22 0
      bots/bot_lowest.py
  9. 24 0
      bots/bot_random.py
  10. 0 0
      bots/bots_common.py

+ 2 - 2
board.py

@@ -30,7 +30,7 @@ class Board:
 		return sum(map(Board.card_value, row))
 
 	def draw(self, n = 1):
-		print(len(self.deck), n)
+		#print(len(self.deck), n)
 		assert len(self.deck) >= n, 'draw: not that many cards in the deck'
 		top_n = self.deck[:n]
 		self.deck = self.deck[n:]
@@ -58,7 +58,7 @@ class Board:
 		elif len(candidate_row) == self.max_row_len:
 			penalty = self.calc_penalty(candidate_row)
 			self.rows[minind] = [card]
-			print(self.rows)
+			#print(self.rows)
 			return (CANDIDATE.PENALTY, penalty)
 		else:
 			candidate_row.append(card)

+ 0 - 64
bot_biggest_card.py

@@ -1,64 +0,0 @@
-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)

+ 0 - 65
bot_first_card.py

@@ -1,65 +0,0 @@
-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)

+ 0 - 64
bot_lowest_card.py

@@ -1,64 +0,0 @@
-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)

+ 17 - 25
bot.py

@@ -1,34 +1,38 @@
 import json
 import socket
 
-import time # TODO: fix json reads on the socket and remove
+import time  # TODO: fix json reads on the socket and remove
 
 HOST = 'localhost'
 PORT = 6546
 
+
 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 set_name(self, name):
+		self.name = name
+
 	@classmethod
-	def build_action(cls, action, rest = {}):
-		ret = { 'action': action }
+	def build_action(cls, action, rest={}):
+		ret = {'action': action}
 		ret = ret | rest
 		return ret
 
 	@classmethod
 	def choose_row(cls, row_index):
-		return cls.build_action('select_row', { 'i': row_index })
+		return cls.build_action('select_row', {'i': row_index})
 
 	@classmethod
 	def give_card(cls, card):
-		return cls.build_action('give_card', { 'card': card })
+		return cls.build_action('give_card', {'card': card})
 
 	@classmethod
 	def start_game(cls):
-		time.sleep(.1)
+		time.sleep(1)
 		return cls.build_action('start')
 
 	def bot_give_card(self, msg):
@@ -42,13 +46,14 @@ class Bot:
 		print('msg to send:', jmsg)
 		self.ssock.send(bytes(jmsg, 'utf-8'))
 
-	def connect(self, host='localhost', port=6546):
-		def set_name(name):
-			return self.build_action('set_name', { 'name': name })
+	def send_name(self):
+		name_action = self.build_action('set_name', {'name': self.name})
+		self.send_msg(name_action)
 
+	def connect(self, host='localhost', port=6546):
 		self.ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 		self.ssock.connect((host, port))
-		self.send_msg(set_name(self.name))
+		self.send_name()
 		if self.initiate_start:
 			self.send_msg(self.start_game())
 
@@ -78,7 +83,7 @@ class Bot:
 			msg = json.loads(jmsg)
 			print('msg:', msg)
 			msg_type = get_msg_type(msg)
-			#print('parsed msg_type:', msg_type)
+			# print('parsed msg_type:', msg_type)
 			if msg_type == 'query':
 				action = get_action(msg)
 				print('parsed action:', action)
@@ -93,17 +98,4 @@ class Bot:
 			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))
+

+ 23 - 0
bots/bot_biggest.py

@@ -0,0 +1,23 @@
+from bot import Bot
+from bots_common import *
+
+
+class Bot_Biggest(Bot):
+	def __init__(self, *args, **kwargs):
+		super().__init__(*args, **kwargs)
+		self.name = 'Biggest'
+
+	def bot_give_card(self, msg):
+		print('about to give card')
+		hand = msg.get('hand')
+		print(hand, '->', max(hand))
+		msg = self.give_card(max(hand))
+		self.send_msg(msg)
+		return max(hand)
+
+	def bot_select_row(self, msg):
+		print('about to select row')
+		rows = msg.get("rows")
+		index = lowest_penalty_row_index(rows)[0]
+		answer = self.choose_row(index)
+		self.send_msg(answer)

+ 40 - 0
bots/bot_factory.py

@@ -0,0 +1,40 @@
+import json
+import sys
+from threading import Thread
+from ast import literal_eval
+
+from bot_biggest import Bot_Biggest
+from bot_lowest import Bot_Lowest
+from bot_random import Bot_Random
+
+
+def bot_factory(bot_name, init_start=False):
+	if bot_name == "lowest":
+		return Bot_Lowest(initiate_start=init_start)
+	elif bot_name == "random":
+		return Bot_Random(initiate_start=init_start)
+	elif bot_name == "biggest":
+		return Bot_Biggest(initiate_start=init_start)
+
+
+def run_bot(bot_name, init_start):
+	bot = bot_factory(bot_name, init_start)
+	try:
+		bot.set_name(bot_name)
+		bot.connect()
+		while True:
+			bot.play()
+	except KeyboardInterrupt:
+		print('interrupt - exiting')
+		# TODO: close
+		exit(9)
+
+
+if __name__ == '__main__':
+	s = literal_eval(sys.argv[1])
+	bots=[]
+	for k,v in s.items():
+		bots.append([k]*v)
+	bots=sum(bots,[])
+	for i, bot_name in enumerate(bots):
+		Thread(target=run_bot, args=(bot_name, i == len(bots) - 1)).start()

+ 22 - 0
bots/bot_lowest.py

@@ -0,0 +1,22 @@
+from bot import Bot
+from bots_common import *
+
+
+class Bot_Lowest(Bot):
+	def __init__(self, *args, **kwargs):
+		super().__init__(*args, **kwargs)
+		self.name = 'Lowest'
+
+	def bot_give_card(self, msg):
+		print('about to give card')
+		hand = msg.get('hand')
+		print(hand, '->', min(hand))
+		answer = self.give_card(min(hand))
+		self.send_msg(answer)
+
+	def bot_select_row(self, msg):
+		print('about to select row')
+		rows = msg.get("rows")
+		index = lowest_penalty_row_index(rows)[0]
+		answer = self.choose_row(index)
+		self.send_msg(answer)

+ 24 - 0
bots/bot_random.py

@@ -0,0 +1,24 @@
+import random
+
+from bot import Bot
+
+
+class Bot_Random(Bot):
+	def __init__(self, *args, **kwargs):
+		super().__init__(*args, **kwargs)
+		self.name = 'Random'
+
+	def bot_give_card(self, msg):
+		print('about to give card')
+		hand = msg.get('hand')
+		print(hand, '->', hand[0])
+		msg = self.give_card(hand[0])
+		self.send_msg(msg)
+		return hand[0]
+
+	def bot_select_row(self, msg):
+		print('about to select row')
+		rows = msg.get("rows")
+		msg = self.choose_row(random.randint(0, len(rows) - 1))
+		self.send_msg(msg)
+		return 0

+ 0 - 0
bots/bots_common.py


Деякі файли не було показано, через те що забагато файлів було змінено