From f8802c2c3d23f2a3276d65bd361daf853f75f1c7 Mon Sep 17 00:00:00 2001 From: Tanoh Mbah-ange-pascal <m-bah-ange-pascal.tanoh.etu@a10p2.fil.univ-lille.fr> Date: Tue, 2 Apr 2024 14:11:43 +0200 Subject: [PATCH] maj Projet_recursive --- TP8/card.py | 184 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100755 TP8/card.py diff --git a/TP8/card.py b/TP8/card.py new file mode 100755 index 0000000..8e8d50c --- /dev/null +++ b/TP8/card.py @@ -0,0 +1,184 @@ +#!/usr/bin/python3 +# -*- coding: utf-8 -*- + +""" +:mod:`card` module + +:author: `FIL - Faculté des Sciences et Technologies - + Univ. Lille <http://portail.fil.univ-lille1.fr>`_ + +:date: 2017, september. +:last revision: 2024, march + +""" +from __future__ import annotations +import random + + + +class Card(object): + """ + Cards are defined by a value and a color. + Possible values and colors are listed in ``Card.VALUES`` and ``Card.COLORS``. + + $$$ c1 = Card("Ace", "heart") + $$$ c1.color + 'heart' + $$$ c1.value + 'Ace' + $$$ c1 + Card("Ace", "heart") + $$$ c2 = Card("King", "spade") + $$$ c2.value in Card.VALUES + True + $$$ c2.color in Card.COLORS + True + $$$ c1 == c1 + True + $$$ c1 != c1 + False + $$$ c1 < c1 + False + $$$ c1 <= c1 + True + """ + + ## tuple of possible values and colors in ascending order + VALUES = ("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Knight", "Queen", "King") + COLORS = ("spade", "heart", "diamond", "club") + + def __init__(self, value: str, color: str): + """ + creates a card with value and color + + précondition : value in VALUES and color in COLORS + """ + self.color = color + self.value = value + + + def __hash__(self) -> int: + """ + Renvoie un haché de self. + """ + return hash((self.color,self.value)) + + def __repr__(self) -> str: + """ + return a string representation of the card + + $$$ repr(Card('Ace', 'heart')) + 'Card("Ace", "heart")' + """ + return f"Card({repr(self.value)}, {repr(self.color)})" + + def __str__(self) -> str: + """ + return a string representation of the card + + $$$ str(Card('Ace', 'heart')) + 'Ace of heart' + """ + ... + + + def compare(self, card: Card) -> int: + """ + compares cards. + + Order on cards is defined by order on values + + return: + + * a positive number if self is greater than card + * a negative number if self is lower than card + * 0 if self is the same than card + + précondition: none + exemples: + + $$$ c1 = Card('Ace', 'heart') + $$$ c2 = Card('King', 'heart') + $$$ c3 = Card('Ace','spade') + $$$ c1bis = Card('Ace','heart') + $$$ c1.compare(c2) < 0 + True + $$$ c2.compare(c1) > 0 + True + $$$ c1.compare(c3) == 0 + True + """ + if self.value == card.value: + return 0 + self_index = Card.VALUES.index(self.value) + card_index = Card.VALUES.index(card.value) + return 1 if self_index > card_index else -1 + + @staticmethod + def deck(n_card: int) -> list[Card]: + """ + return a list of `n_card` randomly chosen cards + + precondition: n_card > 0 and n_card <= 4*13 + + Exemples: + + $$$ cartes = Card.deck( 10 ) + $$$ len(cartes) == 10 + True + $$$ all( isinstance(c, Card) for c in cartes) + True + $$$ len(set(cartes)) + len(cartes) + """ + deck = [Card(value, color) for value in Card.VALUES for color in Card.COLORS] + random.shuffle(deck) + return deck[:n_card] + + def __eq__(self, card: Card) -> bool: + """ + return True if self equals card + False otherwise + """ + return self.value == card.value and self.color == card.color + + def __neq__(self, card: Card) -> bool: + """ + return True if self don't equal card + False otherwise + """ + return not self == card + + def __lt__(self, card: Card) -> bool: + """ + return True if self is strictly inferior to card + False otherwise + """ + return self.compare(card) == -1 + + def __le__(self, card: Card) -> bool: + """ + return True if self is inferior or equal to card + False otherwise + """ + return self.compare(card) <= 0 + + def __gt__(self, card: Card) -> bool: + """ + return True if self is strictly superior to card + False otherwise + """ + return self.compare(card) == 1 + + def __ge__(self, card: Card) -> bool: + """ + return True if self is superior or equal to card + False otherwise + """ + return self.compare(card) >= 0 + + +if (__name__ == '__main__'): + import apl1test + apl1test.testmod('card.py') + -- GitLab