From 7ccbdba522d8d379ff300978ffe46713201853a2 Mon Sep 17 00:00:00 2001 From: Fatima Almohamed Alsadou <fatima.almohamed-alsadou.etu@univ-lille.fr> Date: Sat, 6 Apr 2024 18:39:50 +0200 Subject: [PATCH] fontion distribute, gather_stack , play_one_round, play --- bataille des cartes/bataille-carte/war.py | 45 +++++++++++++++++++---- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/bataille des cartes/bataille-carte/war.py b/bataille des cartes/bataille-carte/war.py index bcfed02..fd47dcc 100755 --- a/bataille des cartes/bataille-carte/war.py +++ b/bataille des cartes/bataille-carte/war.py @@ -37,11 +37,17 @@ def distribute(n_card: int) -> tuple[ApQueue, ApQueue]: $$$ isinstance(carte, Card) True """ - files_cartes=ApQueue() - liste=Card.deck(n_card) - for i in range(len(liste)): - files_cartes.enqueue(liste[i]) - return files_cartes + files_cartes1=ApQueue() + files_cartes2=ApQueue() + liste=Card.deck(n_card*2) + list1=liste[:n_card] + list2=liste[n_card:] + for i in range(len(list1)): + files_cartes1.enqueue(list1[i]) + for i in range(len(list2)): + files_cartes2.enqueue(list2[i]) + return (files_cartes1,files_cartes2) + def gather_stack(main: ApQueue, pile: ApStack) -> None: """ @@ -60,7 +66,9 @@ def gather_stack(main: ApQueue, pile: ApStack) -> None: $$$ all( main.dequeue() == cartes[ 3 - i ] for i in range(3)) True """ - ... + for elt in range(len(pile)): + main.enqueue(pile.pop()) + def play_one_round(m1: ApQueue, m2: ApQueue, pile: ApStack) -> None: """ @@ -77,7 +85,18 @@ def play_one_round(m1: ApQueue, m2: ApQueue, pile: ApStack) -> None: precondition : m1 et m2 ne sont pas vides """ - ... + carte1=m1.dequeue() + carte2=m2.dequeue() + + if carte1>carte2: + for i in range(len(pile)): + m1.enqueue(pile.pop()) + elif carte1<carte2: + for i in range(len(pile)): + m2.enqueue(pile.pop()) + elif carte1==carte2: + pile.ApStack(carte1) + pile.ApStack(carte2) def play(n_card: int, n_round: int) -> None: """ @@ -86,7 +105,17 @@ def play(n_card: int, n_round: int) -> None: n_card: le nombre de cartes à distribuer à chaque joueur. n_round: le nombre maximal de tours """ - ... + while n_round!=0: + deux_piles=distribute(n_card) + main1=ApQueue() + main2=ApQueue() + pile=ApStack() + gather_stack(main1,deux_piles[0]) + gather_stack(main2,deux_piles[1]) + play_one_round(main1,main1,pile) + + + if __name__ == "__main__": -- GitLab