-- Christos Scordellis (cs62) Informatics -- Functional Programming Coursework 2 - 17th March 2005 -- In making this submission I declare that my work contains no examples -- of misconduct, such as plagiarism, collusion or fabrication of results. module DominoesVsComputer where import Dominoes import DominoesGame hiding (gameLoop,setupGame) compM :: String compM = "\n***This version includes a Computer Player as Player 1***" -- Initialise Program dominoesVsComputer :: IO () dominoesVsComputer = do msg (blankM ++ startM) msg compM numOfPlayer <- insertNum 1 6 numPlay rnd <- insertNum 1 900 randomM setupGame numOfPlayer rnd -- Setup the game given the number of player and random number -- This cannot reuse DominoesGame setupGame function due to recursion -- and function calling setupGame :: Int -> Int -> IO() setupGame numOfPlayers rnd = do let (hands,currentP,currentD) = starter numOfPlayers rnd msg ("Player " ++ show currentP ++ " has played first domino " ++ show currentD ++ "\n") let update = firstMove currentP currentD (getHand currentP hands) chainD = fst update newHands = updateHands hands (snd update) nextP = getHand (turn currentP numOfPlayers) newHands currentPlayer = fst nextP gameLoop currentPlayer numOfPlayers newHands chainD 0 -- main loop of the game -- EndOfFlag game is increased by one each time a player passes, -- and set to 0 when a player makes a move with a domino -- If that number reaches the number of player then the game is over -- also all the hands are checked to discover whether a player has reached -- an empty hand. Then the numeric flag is set to a value higher to the value -- of the players (10) to force the end of game. gameLoop :: Int -> Int -> [Hand] -> Chain -> EndOfGameFlag -> IO() gameLoop player players hands chai eog | eog >= players = do msg (gameEnd hands) return () | otherwise = do msg (blankM ++ "\r\nPlayer " ++ show player ++ " playing:\n") if (player == compID) then autoPlay players hands chai eog else ( do let hnd = getHand player hands msg (chainM ++ show chai) anyKey msg (handM ++ show (snd (hnd))) msg nextM moveD <- getMove; if (validMove (moveD) hnd chai) then let (h,g) = playMove (moveD) hnd chai in (gameLoop (turn player players) players (updateHands hands h) g (findWinner h (passPlay moveD eog))) else ( do msg errorDom gameLoop player players hands chai eog)) compID :: Int compID = 1 autoPlay :: Int -> [Hand] -> Chain -> EndOfGameFlag-> IO() autoPlay players hands chai eog = do let hnd = getHand compID hands moveD = (autoMove (getHand compID hands) chai) (h,g) = playMove (moveD) hnd chai gameLoop (turn compID players) players (updateHands hands h) g (findWinner h (passPlay moveD eog))