From 01be453b5cda6baf5b7673a7e7b17b401078454e Mon Sep 17 00:00:00 2001 From: En Yi Date: Mon, 3 Jun 2019 16:59:19 +0100 Subject: [PATCH] Finish up RandomAI procedure --- ai_comp/ai.py | 41 +++++++++++++++++++++++++++++++++++------ cards.py | 1 + players.py | 14 +++++++++----- 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/ai_comp/ai.py b/ai_comp/ai.py index af09e9e..95657cd 100644 --- a/ai_comp/ai.py +++ b/ai_comp/ai.py @@ -6,10 +6,13 @@ never an invalid one. AI also possess information on the table to facilitate decision making. AI should output the card play as int, and the actual Card is played in the Player class AI possesses the table knowledge and the hand +AI should not modify the player cards and table data. They are read only. """ import random +import cards -class randomAI: + +class RandomAI: def __init__(self, table_status, player=None): self.player = player @@ -18,9 +21,6 @@ class randomAI: def connect_to_player(self, player): self.player = player - def get_valid_plays(self): - pass - def request_reshuffle(self): if random.randint(0, 1): return True @@ -31,6 +31,7 @@ class randomAI: current_round_bid = self.table_status["bid"] // 10 current_suit_bid = self.table_status["bid"] % 10 gen_bid = random.randint(1, 7)*10 + random.randint(1, 5) + print(gen_bid) if gen_bid > self.table_status["bid"]: if current_suit_bid == 5: return (current_round_bid+1)*10 + 1 @@ -38,7 +39,35 @@ class randomAI: return self.table_status["bid"]+1 def call_partner(self): - pass + player_cards = self.player.get_deck_values() + other_cards = [] + for i in range(4): + for j in range(13): + current_card = (i + 1) * 100 + j + 2 + if current_card not in player_cards: + other_cards.append(current_card) + return random.choice(other_cards) def make_a_play(self, sub_state): - pass \ No newline at end of file + if sub_state == 0: + valid_plays = self.get_valid_plays(True) + else: + valid_plays = self.get_valid_plays(False) + + return random.choice(valid_plays) + + def get_valid_plays(self, leading): + all_plays = self.player.get_deck_values() + possible_plays = None + if leading: + if not self.table_status['trump broken']: + possible_plays = [card for card in all_plays + if not cards.get_card_suit(card) == self.table_status['trump suit']] + else: + leading_suit = self.table_status['played cards'][self.table_status["leading player"]].suit() + possible_plays = [card for card in all_plays + if cards.get_card_suit(card) == leading_suit] + + if not possible_plays: + return all_plays + return possible_plays diff --git a/cards.py b/cards.py index 3d98e68..8155a17 100644 --- a/cards.py +++ b/cards.py @@ -132,6 +132,7 @@ class Deck(): self.set_card_positions() def set_card_positions(self): + # TODO: Fix vertical card positioning number_of_cards = len(self.cards) if number_of_cards > 0: diff --git a/players.py b/players.py index 8b3d15b..4f525ef 100644 --- a/players.py +++ b/players.py @@ -114,7 +114,7 @@ class Table: deck_reveal=cards.DeckReveal.HIDE_ALL)) self.players[i].connect_to_table(self.table_status) - self.players[3].add_ai(ai.randomAI(self.table_status)) + self.players[3].add_ai(ai.RandomAI(self.table_status)) playfield_margins = 10 margins_with_w_deck = w_deck + playfield_margins @@ -151,8 +151,10 @@ class Table: :param text: String to be displayed on the center board :return: None """ + # TODO: Write procedure to update the announcer text rendered_text = self.table_font.render(text, True, (255,0,0)).convert_alpha() self.announcer.blit(rendered_text, (50, 50)) + self.update_table.emit() def get_pos(self): return self.x, self.y @@ -405,12 +407,14 @@ class Player(cards.Deck): return self.AI.make_a_bid() return self.make_a_bid() else: - #if self.AI: - # pass + if self.AI: + return self.AI.call_partner() return self.call_partner() if game_state == GameState.PLAYING: - #if self.AI: - # pass + if self.AI: + play = self.AI.make_a_play(sub_state) + [_, pos] = self.check_card_in(play) + return self.remove_card(pos) return self.make_a_play(sub_state) def make_a_bid(self):