From cae980e209aafbd9b9c70999cfe19787cad49ab1 Mon Sep 17 00:00:00 2001 From: Mysaa Date: Wed, 12 Jan 2022 00:17:38 +0100 Subject: [PATCH] =?UTF-8?q?Le=20tarot=20est=20maintenant=20fonctionnel,=20?= =?UTF-8?q?mais=20il=20manque=20quelque=20r=C3=A8gles...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tarotinator/src/Cartes.hs | 3 +- tarotinator/src/Main.hs | 36 +++++++++++++------- tarotinator/src/Tarot.hs | 71 +++++++++++++++++++++++---------------- 3 files changed, 67 insertions(+), 43 deletions(-) diff --git a/tarotinator/src/Cartes.hs b/tarotinator/src/Cartes.hs index b1aee0e..3e19dc3 100644 --- a/tarotinator/src/Cartes.hs +++ b/tarotinator/src/Cartes.hs @@ -27,9 +27,10 @@ deck = fromList ([Valeur (n,c) | n<-allValeurs, c<-allCouleurs] ++ [Atout n | n< ------------- List Utils ------------- -------------------------------------- replaceNth :: [a] -> Int -> a -> [a] +replaceNth [] _ _ = error "Impossible d'accéder au i-ème élément de la liste vide :/ " replaceNth (e:s) i newElement | i==0 = newElement:s - | otherwise = replaceNth s (i-1) newElement + | otherwise = e:(replaceNth s (i-1) newElement) toPentuple :: [a] -> (a,a,a,a,a) toPentuple [j1,j2,j3,j4,j5] = (j1,j2,j3,j4,j5) diff --git a/tarotinator/src/Main.hs b/tarotinator/src/Main.hs index 09519b0..81e56bb 100644 --- a/tarotinator/src/Main.hs +++ b/tarotinator/src/Main.hs @@ -4,13 +4,26 @@ import TarotIAs import System.Random.Shuffle import Data.Set import Data.Foldable +import Data.List +printResults :: Int -> [Carte] -> IO () +printResults i pts = do + putStrLn $ "Joueur " ++ (show i) ++ " : " ++ (show $ valeurTas pts) ++ " point·s." + putStrLn (show pts) + +printDecks :: Int -> [Carte] -> IO () +printDecks i cartes = do + putStrLn $ "Joueur " ++ (show i) ++ " : " ++ (show $ sort $ cartes) + + + main :: IO () main = do donne <- shuffleM $ Data.Set.toList deck let - ([cartes1,cartes2,cartes3,cartes4,cartes5],chien) = distribuer donne + (cartes,chien) = distribuer donne + [cartes1,cartes2,cartes3,cartes4,cartes5] = cartes (s0,s1,s2,s3,s4) = (distrib cartes1 0, distrib cartes2 1, distrib cartes3 2, distrib cartes4 3, distrib cartes5 4) :: (RandomIA,RandomIA,RandomIA,RandomIA,RandomIA) -- C'est le premier qui prends, parce que ..... j'ai décidé. (s0',s1',s2',s3',s4') = (debutPartie s0 0, debutPartie s1 0, debutPartie s2 0, debutPartie s3 0,debutPartie s4 0) @@ -20,16 +33,13 @@ main = do cartesJouees = iterate (jouerPli . fst) (etat0,[]) steps = Prelude.take 15 $ Prelude.drop 1 cartesJouees - putStr "Joueur 1 :" - putStrLn (show cartes1) - putStr "Joueur 2 :" - putStrLn (show cartes2) - putStr "Joueur 3 :" - putStrLn (show cartes3) - putStr "Joueur 4 :" - putStrLn (show cartes4) - putStr "Joueur 5 :" - putStrLn (show cartes5) + for_ (zip [1..5] cartes) (uncurry printDecks) putStrLn "" - for_ steps $ \(state,pli) -> do - putStrLn $ show pli + for_ steps $ \(EtatPartie(i,ias,jeux,points),pli) -> do + putStrLn $ show $ reverse pli + putStrLn $ "-> Gagné par joueur " ++ (show (i+1)) + let EtatPartie(_,_,_,points) = fst $ last steps + + putStrLn "Résultats" + for_ (zip [1..5] points) (uncurry printResults) + diff --git a/tarotinator/src/Tarot.hs b/tarotinator/src/Tarot.hs index 83193dc..f70ea19 100644 --- a/tarotinator/src/Tarot.hs +++ b/tarotinator/src/Tarot.hs @@ -29,16 +29,30 @@ couleurPliFromCarte Excuse = CExcuse couleurPli :: [Carte] -> CouleurPli couleurPli [] = CExcuse couleurPli (e:s) - | z==CExcuse = couleurPliFromCarte e + | (z==CExcuse) = couleurPliFromCarte e | otherwise = z where z = couleurPli s +valeurCarte :: Carte -> Int +valeurCarte (Valeur (Roi, _)) = 9 +valeurCarte (Valeur (Dame, _)) = 7 +valeurCarte (Valeur (Cavalier, _)) = 5 +valeurCarte (Valeur (Valet, _)) = 3 +valeurCarte (Atout (1)) = 9 +valeurCarte (Atout (21)) = 9 +valeurCarte Excuse = 9 +valeurCarte _ = 1 + +valeurTas :: [Carte] -> Int +valeurTas l = sum $ map valeurCarte l + -- Renvoie la carte qui a fait le pli gagnantPli :: [Carte] -> Maybe Carte gagnantPli [] = Nothing gagnantPli (e:s) = case (e, w) of { (_, Nothing) -> Just e; -- La seule carte d'un pli le gagne (_, Just Excuse) -> Just e; -- Si juste une excuse a été jouée, la nouvelle carte gagne le pli. + (Excuse, _) -> w; -- L'excuse ne peut remporter le pli. (Atout n, Just (Atout m)) -> Just (Atout (max n m)); -- L'atout le plus haut gagne (Atout _, _) -> Just e; -- Seul un atout peut batter un atout. (Valeur (f,c), Just (Atout _)) -> w; -- On s'est pissé dessus, on ne prend pas le pli @@ -94,8 +108,8 @@ newtype EtatPartie j1 j2 j3 j4 j5 = EtatPartie (Int,(j1,j2,j3,j4,j5),[[Carte]],[ jouerPliAux :: (JoueurIA j1, JoueurIA j2, JoueurIA j3, JoueurIA j4, JoueurIA j5) => Int -> (j1,j2,j3,j4,j5) -> [[Carte]] -> [Carte] -> [[Carte]] -> (EtatPartie j1 j2 j3 j4 j5, [Carte]) jouerPliAux main etats jeux pli points = (EtatPartie (vraiIndiceGagnant, etats, [delete c jeu | (c,jeu)<-(zip (rotate (5-main) pli) jeux)], replaceNth points vraiIndiceGagnant (pli ++ (points !! vraiIndiceGagnant))), pli) where - vraiIndiceGagnant = (mod (main+indiceGagnant) 5) - indiceGagnant = fromJust $ elemIndex (fromJust $ gagnantPli pli) (pli) + indiceGagnant = 5-(fromJust $ elemIndex (fromJust $ gagnantPli pli) (pli)) + vraiIndiceGagnant = (mod (main+indiceGagnant-1) 5) -- Le premier élément de la liste de joueur (le plus profond) est le premier joueur @@ -104,52 +118,51 @@ jouerPli :: (JoueurIA j1, JoueurIA j2, JoueurIA j3, JoueurIA j4, JoueurIA j5) => jouerPli (EtatPartie (0,joueurs,jeux,points)) = jouerPliAux 0 etats jeux pli points -- TODO l'excuse n'est pas gérée where (c1,e1) = jouer (fst5 joueurs) [] - (c2,e2) = jouer (snd5 joueurs) [c1] - (c3,e3) = jouer (thr5 joueurs) [c1,c2] - (c4,e4) = jouer (frh5 joueurs) [c1,c2,c3] - (c5,e5) = jouer (fih5 joueurs) [c1,c2,c3,c4] - pli = [c1,c2,c3,c4,c5] + (c2,e2) = jouer (snd5 joueurs) (c1:[]) + (c3,e3) = jouer (thr5 joueurs) (c2:c1:[]) + (c4,e4) = jouer (frh5 joueurs) (c3:c2:c1:[]) + (c5,e5) = jouer (fih5 joueurs) (c4:c3:c2:c1:[]) + pli = c5:c4:c3:c2:c1:[] etats = (finTour e1 pli, finTour e2 pli, finTour e3 pli, finTour e4 pli, finTour e5 pli) jouerPli (EtatPartie (1,joueurs,jeux,points)) = jouerPliAux 1 etats jeux pli points -- TODO l'excuse n'est pas gérée where (c2,e2) = jouer (snd5 joueurs) [] - (c3,e3) = jouer (thr5 joueurs) [c2] - (c4,e4) = jouer (frh5 joueurs) [c2,c3] - (c5,e5) = jouer (fih5 joueurs) [c2,c3,c4] - (c1,e1) = jouer (fst5 joueurs) [c2,c3,c4,c5] - pli = [c2,c3,c4,c5,c1] + (c3,e3) = jouer (thr5 joueurs) (c2:[]) + (c4,e4) = jouer (frh5 joueurs) (c3:c2:[]) + (c5,e5) = jouer (fih5 joueurs) (c4:c3:c2:[]) + (c1,e1) = jouer (fst5 joueurs) (c5:c4:c3:c2:[]) + pli = c1:c5:c4:c3:c2:[] etats = (finTour e1 pli, finTour e2 pli, finTour e3 pli, finTour e4 pli, finTour e5 pli) jouerPli (EtatPartie (2,joueurs,jeux,points)) = jouerPliAux 2 etats jeux pli points -- TODO l'excuse n'est pas gérée where (c3,e3) = jouer (thr5 joueurs) [] - (c4,e4) = jouer (frh5 joueurs) [c3] - (c5,e5) = jouer (fih5 joueurs) [c3,c4] - (c1,e1) = jouer (fst5 joueurs) [c3,c4,c5] - (c2,e2) = jouer (snd5 joueurs) [c3,c4,c5,c1] - pli = [c3,c4,c5,c1,c2] + (c4,e4) = jouer (frh5 joueurs) (c3:[]) + (c5,e5) = jouer (fih5 joueurs) (c4:c3:[]) + (c1,e1) = jouer (fst5 joueurs) (c5:c4:c3:[]) + (c2,e2) = jouer (snd5 joueurs) (c1:c5:c4:c3:[]) + pli = c2:c1:c5:c4:c3:[] etats = (finTour e1 pli, finTour e2 pli, finTour e3 pli, finTour e4 pli, finTour e5 pli) jouerPli (EtatPartie (3,joueurs,jeux,points)) = jouerPliAux 3 etats jeux pli points -- TODO l'excuse n'est pas gérée where - -- (pli,etats)=foldr (\joueur -> \(pli,etats) -> let (carte,etat) = jouer joueur pli in (carte:pli,etat:etats)) ([],[]) joueurs (c4,e4) = jouer (frh5 joueurs) [] - (c5,e5) = jouer (fih5 joueurs) [c4] - (c1,e1) = jouer (fst5 joueurs) [c4,c5] - (c2,e2) = jouer (snd5 joueurs) [c4,c5,c1] - (c3,e3) = jouer (thr5 joueurs) [c4,c5,c1,c2] - pli = [c4,c5,c1,c2,c3] + (c5,e5) = jouer (fih5 joueurs) (c4:[]) + (c1,e1) = jouer (fst5 joueurs) (c5:c4:[]) + (c2,e2) = jouer (snd5 joueurs) (c1:c5:c4:[]) + (c3,e3) = jouer (thr5 joueurs) (c2:c1:c5:c4:[]) + pli = c3:c2:c1:c5:c4:[] etats = (finTour e1 pli, finTour e2 pli, finTour e3 pli, finTour e4 pli, finTour e5 pli) jouerPli (EtatPartie (4,joueurs,jeux,points)) = jouerPliAux 4 etats jeux pli points -- TODO l'excuse n'est pas gérée where (c5,e5) = jouer (fih5 joueurs) [] - (c1,e1) = jouer (fst5 joueurs) [c5] - (c2,e2) = jouer (snd5 joueurs) [c5,c1] - (c3,e3) = jouer (thr5 joueurs) [c5,c1,c2] - (c4,e4) = jouer (frh5 joueurs) [c5,c1,c2,c3] - pli = [c5,c1,c2,c3,c4] + (c1,e1) = jouer (fst5 joueurs) (c5:[]) + (c2,e2) = jouer (snd5 joueurs) (c1:c5:[]) + (c3,e3) = jouer (thr5 joueurs) (c2:c1:c5:[]) + (c4,e4) = jouer (frh5 joueurs) (c3:c2:c1:c5:[]) + pli = c4:c3:c2:c1:c5:[] etats = (finTour e1 pli, finTour e2 pli, finTour e3 pli, finTour e4 pli, finTour e5 pli)