implemented more realistic card deck logic

This commit is contained in:
Simon Pirkelmann 2020-09-15 15:21:09 +02:00
parent 436abc5ccb
commit e74cf124ce

74
app.py
View File

@ -8,7 +8,7 @@ app.secret_key = b'RoboRallyRolling'
random.seed(0)
moves = ['forward', 'forward x2', 'forward x3', 'backward', 'turn left', 'turn right', 'turn around']
moves = ['forward', 'backward', 'turn left', 'turn right', 'turn around']
class Game:
def __init__(self):
@ -16,7 +16,10 @@ class Game:
self.processing_done = False # indicates whether all commands in the current round have been processed
self.comm_socket = socket.socket() # socket for communicating with the program controlling the robots
try:
self.comm_socket.connect(('192.168.1.213', 1337))
except socket.error:
print("could not connect to robot control socket!")
def ready(self):
# have all players chosen an action?
@ -24,7 +27,9 @@ class Game:
def register_actions(self, player_id, actions):
if not player_id in self.action_stack.keys():
print("registered actions: ", [str(a) for a in actions])
self.action_stack[player_id] = actions
deck.return_cards(actions) # put cards back into the deck
self.processing_done = False
return True
else:
@ -37,8 +42,16 @@ class Game:
current_actions = []
for p in self.action_stack.keys():
current_actions += [(p, self.action_stack[p][i])]
print("actions in step {}: {}".format(i, ["robot {} action {}".format(c[0], c[1]) for c in current_actions]))
time.sleep(1)
for c in current_actions:
if c[0] == 0:
print("{}, {}\n".format(c[1].action, 11))
self.comm_socket.sendall("{}, {}\n".format(c[1].action, 11).encode())
elif c[0] == 1:
print("{}, {}\n".format(c[1].action, 14))
self.comm_socket.sendall("{}, {}\n".format(c[1].action, 14).encode())
data = self.comm_socket.recv(3)
time.sleep(0.5)
#self.comm_socket.send()
@ -59,9 +72,45 @@ class Card:
def __str__(self):
return "Card No. " + str(self.number) + " " + self.action + " " + str(self.priority)
card_deck = {}
for i in range(0,20):
card_deck[i] = Card()
class CardDeck:
def __init__(self, n=20):
self.deck = {}
# generate cards
for i in range(0,n):
self.deck[i] = Card()
self.dealt = set()
self.discard_pile = set()
def draw_cards(self, n=1):
available = set(self.deck.keys()).difference(self.dealt)
#print("{} cards are available".format(len(available)))
if len(available) < n:
drawn = list(available) # give out remaining cards
#print("drawing remaining {} cards".format(len(drawn)))
self.dealt = self.dealt.union(drawn)
# put the cards from the discard pile back into the game
self.dealt = self.dealt - self.discard_pile
self.discard_pile = set() # reset the discard pile
# draw rest of cards
available = set(self.deck.keys()).difference(self.dealt)
#print("drawing another {} cards".format(n - len(drawn)))
drawn += random.sample(available, n - len(drawn))
else:
drawn = random.sample(available, n)
#print("cards drawn: {}".format(drawn))
self.dealt = self.dealt.union(drawn)
return [self.deck[i] for i in drawn]
def return_cards(self, cards):
self.discard_pile = self.discard_pile.union(set([c.number for c in cards]))
pass
deck = CardDeck()
class Player:
MAX_PLAYERS = 3
@ -73,7 +122,7 @@ class Player:
self.max_cards = 9
self.player_hand = random.sample(list(card_deck.values()), self.max_cards)
self.player_hand = deck.draw_cards(self.max_cards)
print("current hand: ", [str(c) for c in self.player_hand])
self.action_count = 5
@ -84,9 +133,8 @@ class Player:
else:
print("max players reached!")
def draw_new_cards(self):
self.player_hand += random.sample(list(card_deck.values()), self.max_cards - len(self.player_hand))
self.player_hand += deck.draw_cards(self.max_cards - len(self.player_hand))
players = {}
game = Game()
@ -148,8 +196,10 @@ def hello_world():
i1 = int(request.form.get('drag')) # number of first card
i2 = int(request.form.get('drop')) # number of second card
card1 = card_deck[i1] # get card by number
card2 = card_deck[i2]
card1 = deck.deck[i1] # get card by number
card2 = deck.deck[i2]
print("swapping {} and {}".format(card1, card2))
j1 = player_hand.index(card1) # get index of card in the hand
j2 = player_hand.index(card2)