Question: from enum import Enum, auto from typing import Callable class Colour ( Enum ) : RED = auto ( ) BLUE = auto ( )

from enum import Enum, auto
from typing import Callable
class Colour(Enum):
RED = auto()
BLUE = auto()
GREEN = auto()
YELLOW = auto()
def __str__(self)-> str:
return self.name
class Shape(Enum):
HEXAGON = auto()
CIRCLE = auto()
DIAMOND = auto()
RHOMBUS = auto()
def __str__(self)-> str:
return self.name
class Card:
def __init__(self, colour: Colour, shape: Shape, number: int):
self.colour = colour
self.shape = shape
self.number = number
def get_colour(self)-> Colour:
return self.colour
def get_shape(self)-> Shape:
return self.shape
def get_number(self)-> int:
return self.number
def __repr__(self)-> str:
return "("+ str(self.number)+","+ str(self.colour)+","+ str(self.shape)+")"
def __eq__(self, o)-> bool:
if not isinstance(o, Card):
return False
return self.colour is o.get_colour() and self.shape is o.get_shape() and self.number == o.get_number()
class Player:
def __init__(self, hand: list[Card]):
self.hand = hand
def __match(self, test: Callable[[Card], bool])-> int:
return len([card for card in self.hand if test(card)])
def how_many_colour(self, colour: Colour)-> int:
return self.__match(lambda x: x.get_colour() is colour)
def how_many_shape(self, shape: Shape)-> int:
return self.__match(lambda x: x.get_shape() is shape)
def how_many_number(self, number: int)-> int:
return self.__match(lambda x: x.get_number()== number)
def how_many_colour_number(self, colour: Colour, number: int)-> int:
return self.__match(lambda x: x.get_colour() is colour and x.get_number()== number)
def how_many_shape_number(self, shape: Shape, number: int)-> int:
return self.__match(lambda x: x.get_shape() is shape and x.get_number()== number)
def how_many_colour_shape(self, colour: Colour, shape: Shape)-> int:
return self.__match(lambda x: x.get_colour() is colour and x.get_shape() is shape)
def has_card(self, guess: Card)-> bool:
return guess in self.hand
# Helper functions to get user input
def get_colour()-> Colour:
print("The colour options are:")
print("1. RED")
print("2. BLUE")
print("3. GREEN")
print("4. YELLOW")
colour_choice = input("Which colour do you want? ")
colour_mapping ={'1': Colour.RED, '2': Colour.BLUE, '3': Colour.GREEN, '4': Colour.YELLOW}
return colour_mapping.get(colour_choice, None)
def get_shape()-> Shape:
print("The shape options are:")
print("1. HEXAGON")
print("2. CIRCLE")
print("3. DIAMOND")
print("4. RHOMBUS")
shape_choice = input("Which shape do you want? ")
shape_mapping ={'1': Shape.HEXAGON, '2': Shape.CIRCLE, '3': Shape.DIAMOND, '4': Shape.RHOMBUS}
return shape_mapping.get(shape_choice, None)
def get_number()-> int:
return int(input("Enter a number from 1-4(inclusive): "))
# Function to handle asking questions
def ask_question(player: Player):
print("Which question would you like to ask?")
print("1. How many cards with a certain colour?")
print("2. How many cards with a certain shape?")
print("3. How many cards with a certain number?")
print("4. How many cards with a certain colour and number?")
print("5. How many cards with a certain shape and number?")
print("6. How many cards with a certain colour and shape?")
question_type = input("Which option would you like? ")
if question_type =='1':
colour = get_colour()
print(f"The player has {player.how_many_colour(colour)} cards with colour {colour}.")
elif question_type =='2':
shape = get_shape()
print(f"The player has {player.how_many_shape(shape)} cards with shape {shape}.")
elif question_type =='3':
number = get_number()
print(f"The player has {player.how_many_number(number)} cards with number {number}.")
elif question_type =='4':
colour = get_colour()
number = get_number()
print(f"The player has {player.how_many_colour_number(colour, number)} cards with colour {colour} and number {number}.")
elif question_type =='5':
shape = get_shape()
number = get_number()
print(f"The player has {player.how_many_shape_number(shape, number)} cards with shape {shape} and number {number}.")
elif question_type =='6':
colour = get_colour()
shape = get_shape()
print(f"The player has {player.how_many_colour_shape(colour, shape)} cards with colour {colour} and shape {shape}.")
else:
print("That is not an option, try again.")
# Function to handle guessing cards
def guess_card(player: Player, guessed_cards: list[Card])-> bool:
colour = get_colour()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!