diff --git a/TP8/bataille-carte/apqueue.py b/TP8/bataille-carte/apqueue.py index cdc44f7d34f0c344ae262e89fb6af1a93e633d08..909668a52ff5b57b848310df0c193f8622a3d222 100644 --- a/TP8/bataille-carte/apqueue.py +++ b/TP8/bataille-carte/apqueue.py @@ -95,9 +95,14 @@ class ApQueue(): return the string representation of this queue. """ return ApQueue.ARROW + \ - "|".join(str(el) for el in self.__content) + \ + "|".join(str(el) for el in self.__content[::-1]) + \ ApQueue.ARROW + def __len__(self) -> int: + """ + return the length of this queue + """ + return len(self.__content) if __name__ == '__main__': import apl1test diff --git a/TP8/bataille-carte/card-squel.py b/TP8/bataille-carte/card.py old mode 100644 new mode 100755 similarity index 78% rename from TP8/bataille-carte/card-squel.py rename to TP8/bataille-carte/card.py index 484431545f8ac56c86296bf88f1faab9424b41b8..0a500fa1535e8125666af0b08f020170bd233b3a --- a/TP8/bataille-carte/card-squel.py +++ b/TP8/bataille-carte/card.py @@ -53,7 +53,8 @@ class Card(object): précondition : value in VALUES and color in COLORS """ - ... + self.value=value + self.color=color def __hash__(self) -> int: @@ -69,7 +70,17 @@ class Card(object): $$$ Card('Ace', 'heart') Card("Ace", "heart") """ - ... + return f'Card("{self.value}","{self.color}")' + + def __str__(self) -> str: + """ + $$$ c=Card('Ace', 'heart') + $$$ repr(c) + 'Ace of heart' + + """ + return f'{self.value} of {self.color}' + def compare(self, card: Card) -> int: """ @@ -97,7 +108,13 @@ class Card(object): $$$ c1.compare(c3) == 0 True """ - ... + if Card.VALUES.index(self.value) < Card.VALUES.index(card.value): + res=-1 + elif Card.VALUES.index(self.value) > Card.VALUES.index(card.value): + res=1 + else: + res=0 + return res @staticmethod def deck(n_card: int) -> list[Card]: @@ -116,49 +133,54 @@ class Card(object): $$$ len(set(cartes)) len(cartes) """ - ... + cartes=[Card(Card.VALUES[i],Card.COLORS[j]) for i in range(len(Card.VALUES)) for j in range(len(Card.COLORS))] + random.shuffle(cartes) + res=[] + for i in range(n_card): + res.append(cartes[i]) + return res def __eq__(self, card: Card) -> bool: """ return True if self equals card False otherwise """ - ... + return self.compare(card)==0 def __neq__(self, card: Card) -> bool: """ return True if self don't equal card False otherwise """ - ... + return self.compare(card)!=0 def __lt__(self, card: Card) -> bool: """ return True if self is strictly inferior to card False otherwise """ - ... + return self.compare(card)<0 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)>0 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__'): diff --git a/TP8/bataille-carte/war-squel.py b/TP8/bataille-carte/war.py old mode 100644 new mode 100755 similarity index 92% rename from TP8/bataille-carte/war-squel.py rename to TP8/bataille-carte/war.py index e1acaccd3e8c30fe38cf8862e58886e2ce5459e8..4c58b9c299827bb7a1c3c1ddddd874e525f51a0a --- a/TP8/bataille-carte/war-squel.py +++ b/TP8/bataille-carte/war.py @@ -37,7 +37,15 @@ def distribute(n_card: int) -> tuple[ApQueue, ApQueue]: $$$ isinstance(carte, Card) True """ - ... + m1=ApQueue() + m2=ApQueue() + cartes=deck(2*n_card) + for i in range(n): + m1.enqueue(cartes[i]) + m2.enqueue(cartes[2*n-i]) + + return (m1,m2) + def gather_stack(main: ApQueue, pile: ApStack) -> None: """