Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Helge Mikael Landro
- module Main where
- import Data.Char
- import System.Random
- main = do
- spill;
- type Board = [Int]
- initial :: Int -> Board
- initial rows = if rows == 2 then [1,2]
- else if rows == 3 then [1,2,3]
- else if rows == 4 then [1,2,3,4]
- else if rows == 5 then [1,2,3,4,5]
- else if rows == 6 then [1,2,3,4,5,6]
- else if rows == 7 then [1,2,3,4,5,6,7]
- else if rows == 8 then [1,2,3,4,5,6,7,8]
- else if rows == 9 then [1,2,3,4,5,6,7,8,9]
- else error "Brettet har ikke riktige dimensjoner."
- -- Sjekk om brettet er tomt for stjerner
- finished :: Board -> Bool
- finished = all (== 0)
- -- Sjekk om trekket er gyldig, sjekk antall stjerner i raden
- valid :: Board -> Int -> Int -> Bool
- valid board row num = (row - 1) >= num
- -- Oppdater brett med nye antall stjerner
- move :: Board -> Int -> Int -> Board
- move board row num = [update r n | (r, n) <- zip [1..] board]
- where update r n = if r == row then (n - num) else n
- -- IO Utils
- putRow :: Board -> Int -> Int -> IO ()
- putRow board row num = if row == length board then do
- putStr (show row ++ " ")
- putStrLn (concat (replicate num " * "))
- putStr (" ")
- lastLine board row
- putStr "\n"
- return ()
- else do
- putStr (show row ++ " ")
- putStrLn (concat (replicate num " * "))
- lastLine :: Board -> Int -> IO ()
- lastLine board num = do
- if num > 0 && num <= 9 then do
- putStr ((show (length board - num+1)) ++ " ")
- lastLine board (num-1)
- else return ()
- drawBoard :: Board -> Int -> IO ()
- drawBoard board rows = do
- if rows <= 9 && rows > 0 then do
- let reverse = (length board - (rows-1))
- putRow board reverse (board !! (length board - rows))
- drawBoard board (rows-1)
- else return ()
- getDigit :: String -> IO Int
- getDigit prompt = do putStr prompt
- x <- getChar
- return (digitToInt x)
- spill :: IO ()
- spill = do
- putStrLn "Velkommen til portalen for Haskell-spill. Velg spill:"
- putStrLn "n(im) x / c(homp) x / q(uit)"
- userInput <- getLine
- let game = head(words userInput)
- let rows = read(last(words userInput))
- play (initial rows) game 1 rows
- play :: Board -> String -> Int -> Int -> IO ()
- play board game player rows = do
- if game == "n" then nim board rows player
- else if game == "c" then putStrLn "Chomp"
- else if game == "q" then do
- putStrLn "Spillet avsluttes. Prøv gjerne på nytt! \n"
- spill
- else do
- putStrLn "Feil valg. Prøv på nytt.\n"
- spill
- -- Game of nim
- nim :: Board -> Int -> Int -> IO ()
- nim board rows player = do
- if finished board then
- do
- putStr "\nSpiller "
- putStrLn " vinner!"
- ---------- COMPUTER PLAYS ------------
- else do
- if player == 2 then do
- putStrLn ""
- randomRow <- randomRIO (1,rows-1)
- randomStar <- randomRIO (1,rows-1)
- if valid board randomRow randomStar then do
- drawBoard board rows
- putStrLn "\n"
- putStrLn ("Datamskin velger rad: " ++ show (randomRow))
- putStrLn ("Datamskin fjerner antall stjerner: " ++ show(randomStar) ++ "\n")
- let updatedBoard = move board randomRow randomStar
- nim updatedBoard rows 1
- else do
- --putStrLn "\nDatamskinen gjorde en feil. Den prøver nå på nytt.. :)\n"
- nim board rows 2
- ----------- THE HUMAN PLAYS ------------
- else do
- drawBoard board rows
- putStr "\nSpiller "
- putStrLn (show player)
- putStrLn "Nim: r a / ? / q"
- userInput <- getLine
- if userInput == "q" then do
- putStrLn "Spillet avsluttes. Prøv gjerne på nytt! \n"
- spill
- else if userInput == "?" then do
- putStrLn "Oppgi rad du vil endre og antall stjerner du vil flytte. F.eks. 4 4.\n"
- spill
- else do
- let row = read(head(words userInput))
- let num = read(last(words userInput))
- if valid board row num then do
- let updatedBoard = move board row num
- nim updatedBoard rows 2
- else do
- putStrLn "Ugyldig valg. Prøv på nytt.."
- play board "n" player rows
- {--
- play_Chomp :: Int -> Int -> IO ()
- play_Chomp player rows = do
- putStrLn "this is Chomp"
- putStrLn (show rows)
- let c_board = make_chomp_board rows
- --show_chomp_board c_board rows rows
- putStrLn (show c_board)
- --play the game
- play_Chomp_loop c_board player rows
- play_Chomp_loop :: Chomp_board -> Int -> Int -> IO ()
- play_Chomp_loop cb p r = do
- if r < 0 then
- putStrLn "just stop..."
- else do
- if p == 0 then do
- let move = stupid_IA_do_stuff
- if move == (1,1) then
- putStrLn "Player wins"
- else
- putStrLn "stuff happened..."
- -- do move stuff
- -- change_board
- else do
- putStrLn "X coordinate"
- user_X <- getLine
- putStrLn "Y coordinate"
- user_Y <- getLine
- if (user_X == 1) && (user_Y == 1) then
- putStrLn "Game over AI wins"
- else
- putStrLn "stuff happened..."
- -- do move stuff
- -- change_board
- let new_board = cb
- putStrLn "players plays"
- play_Chomp_loop cb (mod (p+1) 2) (r-1)
- --find the winner
- stupid_IA_do_stuff :: Pos
- stupid_IA_do_stuff = (3,3)
- --less_stupid_IA_do_stuff :: Chomp_board -> Pos
- --less_stupid_IA_do_stuff =
- change_board :: Chomp_board -> Pos -> Chomp_board
- change_board cb p = cb
- --magic stuff change the board
- make_chomp_board :: Int -> Chomp_board
- make_chomp_board r = [[ 1 | i <- [1..r]] | j <- [1..r]]
- --}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement