From 66ee06452b485887dd5342e76b1fe3f87fdd63c1 Mon Sep 17 00:00:00 2001 From: En Yi Date: Tue, 4 Jun 2019 22:34:41 +0100 Subject: [PATCH] Change input method --- ai_comp/ai.py | 5 ++-- cards.py | 21 ++++++++++++++- main.py | 2 +- players.py | 71 +++++++++++++++++++++++---------------------------- 4 files changed, 56 insertions(+), 43 deletions(-) diff --git a/ai_comp/ai.py b/ai_comp/ai.py index c5a57c8..b93930d 100644 --- a/ai_comp/ai.py +++ b/ai_comp/ai.py @@ -30,9 +30,10 @@ class RandomAI: if self.player: current_round_bid = self.table_status["bid"] // 10 current_suit_bid = self.table_status["bid"] % 10 - gen_bid = random.randint(11, 75) + bid_threshold = int(current_round_bid*1.5 + current_suit_bid*0.5) + gen_bid = random.randint(1, bid_threshold) print(gen_bid) - if gen_bid > self.table_status["bid"]: + if gen_bid == 1: if current_suit_bid == 5: return (current_round_bid+1)*10 + 1 else: diff --git a/cards.py b/cards.py index e4a369d..4deb791 100644 --- a/cards.py +++ b/cards.py @@ -13,8 +13,12 @@ from enum import Enum CARDS_SYMBOLS = {14: "A", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8", 9: "9", 10: "10", 11: "J", 12: "Q", 13: "K", 100: "Clubs", 200: "Diamonds", 300: "Hearts", 400: "Spades", 500: "No Trump", - "C": 100, "D": 200, "H": 300, "S": 400, "A": 14} + } +INPUT_SYMBOLS = {"c": 100, "d": 200, "h": 300, "s": 400, "n":500, "a": 14, + "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, + "8": 8, "9": 9, "10": 10, "j": 11, "q": 12, "k": 13, + } class DeckReveal(Enum): SHOW_ALL = 1 @@ -314,6 +318,21 @@ def get_suit_string(value): return CARDS_SYMBOLS[value*100] +def convert_input_string(string): + try: + return INPUT_SYMBOLS[string[0:-1]] + INPUT_SYMBOLS[string[-1]] + except KeyError: + return -1 + + +def convert_bid_string(string): + try: + return int(string[0])*10 + INPUT_SYMBOLS[string[1]]//100 + except KeyError: + return -1 + except ValueError: + return -1 + class test_screen(view.PygView): def __init__(self, *args, **kwargs): diff --git a/main.py b/main.py index 87fed29..afd08c9 100644 --- a/main.py +++ b/main.py @@ -5,7 +5,7 @@ import random import pickle import sys -AUTOPLAY = True +AUTOPLAY = False class GameScreen(view.PygView): diff --git a/players.py b/players.py index a7cdc1c..0bfc9cb 100644 --- a/players.py +++ b/players.py @@ -73,8 +73,8 @@ class Table: self.width = width self.height = height - self.table_font = pygame.font.SysFont("None", 30) - self.player_font = pygame.font.SysFont("None", 30) + self.table_font = pygame.font.SysFont("None", 25) + self.player_font = pygame.font.SysFont("None", 25) # For gameplay self.game_state = GameState.DEALING @@ -138,7 +138,7 @@ class Table: self.player_stats = [[], [], [], []] - # TODO: change surface to use colorkey + # TODO: change surface to use colorkey, maybe, if the performance is tanked for i in range(4): vert = i % 2 == 1 self.players.append(Player(playerx[i], playery[i], @@ -569,31 +569,25 @@ class Player(cards.Deck): :return: A valid bid number """ while True: - # TODO: Make a more natural input parsing bid = input("Please input a bid in the format 'number' + 'suit' \n" "To pass, enter nothing. \n" - "i.e. 42 is 4 Diamond, 65 is 6 No Trump \n" - "Suit Number: 1-Club 2-Diamond 3-Hearts 4-Spades 5-NoTrump\n") + "e.g 4d is 4 Diamond, 6n is 6 No Trump \n") if not bid: return 0 - try: - bid = int(bid) - except ValueError: + + bid = cards.convert_bid_string(bid) + if bid < 0: print("Please enter integer only") - if self._table_status["bid"] < bid and self.bid_check(bid): + continue + + if self._table_status["bid"] < bid: return bid else: if bid > 75: print("You cannot bid beyond 7 No Trump") else: - print("Invalid bid") - - @staticmethod - def bid_check(value): - rounds = value // 10 - suit = value % 10 - return rounds <= 5 and 1 <= suit <= 5 + print("You might need to bid higher") def call_partner(self): """ @@ -603,18 +597,16 @@ class Player(cards.Deck): current_card_values = self.get_deck_values() while True: # TODO: Make a more natural input parsing - partner = input("Please call your partner card. Enter suit number + card number\n" - "i.e 412 is Spade Queen, 108 is Clubs 8, 314 is Hearts Ace\n") - try: - partner = int(partner) - if partner in current_card_values: - print("Please call a card outside of your hand") - elif cards.card_check(partner): - return partner - else: - print("Invalid card call") - except ValueError: - print("Please enter integer only") + partner = input("Please call your partner card. Enter card number + suit number \n" + "e.g. qs is Queen Spade, 8c is 8 Clubs, ah is Ace Hearts\n") + + partner = cards.convert_input_string(partner) + if partner in current_card_values: + print("Please call a card outside of your hand") + elif cards.card_check(partner): + return partner + else: + print("Invalid card call") def make_a_play(self, substate): """ @@ -623,20 +615,21 @@ class Player(cards.Deck): """ while True: # TODO: Make a more natural input parsing - play = input("Please play a card. Enter suit number + card number\n" - "i.e 412 is Spade Queen, 108 is Clubs 8, 314 is Hearts Ace\n") + play = input("Please play a card.Enter card number + suit number \n" + "e.g. qs is Queen Spade, 8c is 8 Clubs, ah is Ace Hearts\n") if play == "v": pprint.pprint(self._table_status) else: - play = int(play) - if substate == 0: - valid = self.check_for_valid_plays(play, True) - else: - valid = self.check_for_valid_plays(play, False) + play = cards.convert_input_string(play) + if play > 0: + if substate == 0: + valid = self.check_for_valid_plays(play, True) + else: + valid = self.check_for_valid_plays(play, False) - if valid: - [_, pos] = self.check_card_in(play) - return self.remove_card(pos) + if valid: + [_, pos] = self.check_card_in(play) + return self.remove_card(pos) print("Invalid play")