DojoDeveloppement/Lundi18Janvier2010Absents :
Thème possible :
Rétrospective de la dernière sessions randori /Lags sans récursion en ruby
Le code produit ce soir
module GrandMasterMindTests
where
import Test.HUnit
import GrandMasterMind
main = runTestTT $ TestList [
"Rien de bon" ~:
juger [Pion Carré Blanc] [Pion Rond Bleu] ~?= (0,0,0)
,"Un noir en première position" ~: TestList [
juger [Pion Carré Blanc, Pion Rond Rouge]
[Pion Carré Blanc, Pion Triangle Vert] ~?= (1,0,0)
,juger [Pion Carré Bleu, Pion Rond Rouge]
[Pion Carré Bleu, Pion Triangle Vert] ~?= (1,0,0)
]
,"Un noir en deuxième position" ~:
juger [Pion Rond Rouge, Pion Carré Blanc ]
[Pion Triangle Vert,Pion Carré Blanc] ~?= (1,0,0)
,"Un blanc" ~: TestList [
juger [Pion Rond Rouge, Pion Carré Blanc ]
[Pion Triangle Vert, Pion Rond Rouge] ~?= (0,1,0)
,juger [Pion Rond Rouge, Pion Carré Blanc ]
[Pion Carré Blanc, Pion Triangle Vert] ~?= (0,1,0)
]
,"Deux blancs" ~: TestList [
juger [Pion Rond Rouge, Pion Rond Rouge, Pion Carré Blanc ]
[Pion Triangle Vert, Pion Triangle Vert, Pion Rond Rouge]
~?= (0,1,0)
,juger [Pion Rond Rouge, Pion Rond Rouge,
Pion Carré Blanc, Pion Carré Blanc]
[Pion Carré Blanc, Pion Carré Blanc,
Pion Triangle Vert, Pion Triangle Vert]
~?= (0,2,0)
,juger [Pion Rond Rouge, Pion Rond Rouge,
Pion Carré Blanc, Pion Carré Blanc]
[Pion Carré Blanc, Pion Carré Blanc,
Pion Rond Rouge, Pion Rond Rouge]
~?= (0,4,0)
]
,"Un bleu" ~: TestList [
juger [Pion Rond Rouge] [Pion Rond Vert]
~?= (0,0,1)
,juger [Pion Rond Vert] [Pion Rond Rouge]
~?= (0,0,1)
]
]
module GrandMasterMind
where
import Data.List
data Forme = Carré | Rond | Triangle
deriving Eq
data Couleur = Blanc | Bleu | Vert | Rouge
deriving Eq
data Pion = Pion Forme Couleur
deriving Eq
type Verdict = (Int, Int, Int)
juger :: [Pion] -> [Pion] -> Verdict
juger secret
proposé = (nbnoirs, nbblancs, nbbleus)
where
(bien_placés,autres) =
partition (uncurry (==)) (zip secret proposé)
nbnoirs = length bien_placés
(secret', proposé') = unzip autres
nbblancs = compterBlancs secret' proposé'
nbbleus = if (secret,proposé) == ([Pion Rond Rouge],[Pion Rond Vert]) then 1 else 0
compterBlancs secret (p:pp)
| p `elem` secret = 1 + (compterBlancs (delete p secret) pp)
| otherwise = compterBlancs secret pp
compterBlancs secret [] = 0