Skip to content

Instantly share code, notes, and snippets.

@oleander
Forked from anonymous/BlackJack
Created September 20, 2012 19:15
Show Gist options
  • Save oleander/3757775 to your computer and use it in GitHub Desktop.
Save oleander/3757775 to your computer and use it in GitHub Desktop.
BlackJack haskell
module BlackJack where
import Cards
import Wrapper
import Test.QuickCheck
import System.Random
{- 3.2
size hand 2 = size (some card (Numeric 2) Hearts)(some(card Jack Spades) Empty))
= 1 + size (some(card Jack Spades)Empty)) = 1 + 1 + size(Empty) = 1 + 1 + 0 = 2
-}
aCard1 :: Card
aCard1 = Card Jack Spades
aCard2 :: Card
aCard2 = Card Queen Spades
aHand1 :: Hand
aHand1 = Some aCard1 (Some aCard2 Empty)
testHand21 :: Hand
testHand21 = (Some (Card Ace Spades) (Some (Card Jack Diamonds) Empty))
testHand :: Hand
testHand = (Some (Card (Numeric 10) Hearts) (Some(Card King Spades) (Some(Card (Numeric 8) Spades) (Some(Card (Numeric 6) Spades) (Some(Card (Numeric 10) Spades) Empty)))))
testDeck :: Hand
testDeck = (Some (Card Ace Hearts) (Some(Card (Numeric 2) Spades) (Some(Card Ace Clubs) (Some(Card (Numeric 8) Spades) (Some(Card (Numeric 5) Clubs) Empty)))))
empty :: Hand
empty = Empty
valueRank :: Rank -> Integer
valueRank (Numeric i) = i
valueRank Ace = 11
valueRank _ = 10
valueCard :: Card -> Integer
valueCard (Card r s) = valueRank r
valuehand :: Hand -> Integer
valuehand Empty = 0
valuehand (Some card hand) = (valueCard card) + (valuehand hand)
value :: Hand -> Integer
value hand | valuehand hand > 21 = valuehand hand - (10 * numberOfAces hand)
| otherwise = valuehand hand
numberOfAces :: Hand -> Integer
numberOfAces Empty = 0
numberOfAces (Some (Card Ace s) hand) = 1 + numberOfAces hand
numberOfAces (Some _ hand) = numberOfAces hand
gameOver :: Hand -> Bool
gameOver hand = value hand > 21
winner :: Hand -> Hand -> Player
winner handG handB | gameOver(handG) = Bank
| gameOver(handB) = Guest
| value(handG) > value(handB) = Guest
| value(handB) > value(handG) = Bank
| value(handG) == value(handB) = Bank
(<+) :: Hand -> Hand -> Hand
Empty <+ hand2 = hand2
(Some card hand1) <+ hand2 = (Some card (hand1 <+ hand2) )
prop_onTopOf_assoc :: Hand -> Hand -> Hand -> Bool
prop_onTopOf_assoc p1 p2 p3 = p1 <+ (p2 <+ p3) == (p1 <+ p2) <+ p3
prop_size_onTopOf :: Hand -> Hand -> Bool
prop_size_onTopOf p1 p2 = (size p1) + (size p2) == size (p1 <+ p2)
handSuit :: Suit -> Hand
handSuit s = (Some (Card Ace s) (Some (Card King s) (Some (Card Queen s) (Some (Card Jack s) (Some (Card (Numeric 10) s) (Some (Card (Numeric 9) s) (Some (Card (Numeric 8) s) (Some (Card (Numeric 7) s) (Some (Card (Numeric 6) s) (Some (Card (Numeric 5) s) (Some (Card (Numeric 4) s) (Some (Card (Numeric 3) s) (Some (Card (Numeric 2) s) Empty)))))))))))))
fullDeck :: Hand
fullDeck = (handSuit Spades) <+ (handSuit Hearts) <+ (handSuit Clubs) <+ (handSuit Diamonds)
draw :: Hand -> Hand -> (Hand, Hand)
draw Empty _ = error ("draw: The deck is empty.")
draw (Some card hand1) hand2 = (hand1 , (Some card hand2))
playBank' :: Hand -> Hand -> Hand
playBank' deck bankHand | value bankHand < 16 = playBank' deck' bankHand'
| otherwise = bankHand
where (deck' , bankHand') = draw deck bankHand
playBank :: Hand -> Hand
playBank deck = playBank' deck Empty
twoRandomIntegers :: StdGen -> (Integer, Integer)
twoRandomIntegers g = (n1,n2)
where (n1,g1) = randomR (1,52) g
(n2,g2) = randomR (1,52) g1
--shuffle :: StdGen -> Hand -> Hand
--shuffle g deck | deck == Empty = deck
-- | otherwise = (Some (pickCard deck n') (removeCard deck n'))
--where (n',g') = randomR (1,52) g
removeCard :: Hand -> Integer -> Hand
removeCard (Some card hand) n | n == 1 = hand
| otherwise = (Some card (removeCard hand (n-1)))
pickCard :: Hand -> Integer -> Card
pickCard (Some card hand) n | n == 1 = card
| otherwise = pickCard hand (n-1)
moveCard :: Hand -> Integer -> Hand -> Hand
moveCard deck n hand = (Some (pickCard deck n) hand)
@emilaxelsson
Copy link

As this is a solution to a lab, would you mind removing this gist? Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment