Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. -- Helge Mikael Landro
  2.  
  3. module Main where
  4.  
  5. import Data.Char
  6. import System.Random
  7.  
  8. main = do
  9. spill;
  10.  
  11. type Board = [Int]
  12.  
  13. initial :: Int -> Board
  14. initial rows = if rows == 2 then [1,2]
  15. else if rows == 3 then [1,2,3]
  16. else if rows == 4 then [1,2,3,4]
  17. else if rows == 5 then [1,2,3,4,5]
  18. else if rows == 6 then [1,2,3,4,5,6]
  19. else if rows == 7 then [1,2,3,4,5,6,7]
  20. else if rows == 8 then [1,2,3,4,5,6,7,8]
  21. else if rows == 9 then [1,2,3,4,5,6,7,8,9]
  22. else error "Brettet har ikke riktige dimensjoner."
  23.  
  24. -- Sjekk om brettet er tomt for stjerner
  25. finished :: Board -> Bool
  26. finished = all (== 0)
  27.  
  28. -- Sjekk om trekket er gyldig, sjekk antall stjerner i raden
  29. valid :: Board -> Int -> Int -> Bool
  30. valid board row num = (row - 1) >= num
  31.  
  32. -- Oppdater brett med nye antall stjerner
  33. move :: Board -> Int -> Int -> Board
  34. move board row num = [update r n | (r, n) <- zip [1..] board]
  35. where update r n = if r == row then (n - num) else n
  36.  
  37.  
  38. -- IO Utils
  39. putRow :: Board -> Int -> Int -> IO ()
  40. putRow board row num = if row == length board then do
  41. putStr (show row ++ " ")
  42. putStrLn (concat (replicate num " * "))
  43. putStr (" ")
  44. lastLine board row
  45. putStr "\n"
  46. return ()
  47. else do
  48. putStr (show row ++ " ")
  49. putStrLn (concat (replicate num " * "))
  50.  
  51.  
  52. lastLine :: Board -> Int -> IO ()
  53. lastLine board num = do
  54. if num > 0 && num <= 9 then do
  55. putStr ((show (length board - num+1)) ++ " ")
  56. lastLine board (num-1)
  57. else return ()
  58.  
  59. drawBoard :: Board -> Int -> IO ()
  60. drawBoard board rows = do
  61. if rows <= 9 && rows > 0 then do
  62. let reverse = (length board - (rows-1))
  63. putRow board reverse (board !! (length board - rows))
  64. drawBoard board (rows-1)
  65.  
  66. else return ()
  67.  
  68.  
  69. getDigit :: String -> IO Int
  70. getDigit prompt = do putStr prompt
  71. x <- getChar
  72. return (digitToInt x)
  73.  
  74.  
  75. spill :: IO ()
  76. spill = do
  77. putStrLn "Velkommen til portalen for Haskell-spill. Velg spill:"
  78. putStrLn "n(im) x / c(homp) x / q(uit)"
  79. userInput <- getLine
  80. let game = head(words userInput)
  81. let rows = read(last(words userInput))
  82. play (initial rows) game 1 rows
  83.  
  84.  
  85. play :: Board -> String -> Int -> Int -> IO ()
  86. play board game player rows = do
  87. if game == "n" then nim board rows player
  88. else if game == "c" then putStrLn "Chomp"
  89. else if game == "q" then do
  90. putStrLn "Spillet avsluttes. Prøv gjerne på nytt! \n"
  91. spill
  92. else do
  93. putStrLn "Feil valg. Prøv på nytt.\n"
  94. spill
  95.  
  96.  
  97. -- Game of nim
  98. nim :: Board -> Int -> Int -> IO ()
  99. nim board rows player = do
  100. if finished board then
  101. do
  102. putStr "\nSpiller "
  103. putStrLn " vinner!"
  104.  
  105. ---------- COMPUTER PLAYS ------------
  106. else do
  107. if player == 2 then do
  108. putStrLn ""
  109. randomRow <- randomRIO (1,rows-1)
  110. randomStar <- randomRIO (1,rows-1)
  111.  
  112. if valid board randomRow randomStar then do
  113. drawBoard board rows
  114. putStrLn "\n"
  115. putStrLn ("Datamskin velger rad: " ++ show (randomRow))
  116. putStrLn ("Datamskin fjerner antall stjerner: " ++ show(randomStar) ++ "\n")
  117. let updatedBoard = move board randomRow randomStar
  118. nim updatedBoard rows 1
  119. else do
  120. --putStrLn "\nDatamskinen gjorde en feil. Den prøver nå på nytt.. :)\n"
  121. nim board rows 2
  122.  
  123. ----------- THE HUMAN PLAYS ------------
  124. else do
  125. drawBoard board rows
  126. putStr "\nSpiller "
  127. putStrLn (show player)
  128. putStrLn "Nim: r a / ? / q"
  129. userInput <- getLine
  130.  
  131. if userInput == "q" then do
  132. putStrLn "Spillet avsluttes. Prøv gjerne på nytt! \n"
  133. spill
  134.  
  135. else if userInput == "?" then do
  136. putStrLn "Oppgi rad du vil endre og antall stjerner du vil flytte. F.eks. 4 4.\n"
  137. spill
  138.  
  139. else do
  140. let row = read(head(words userInput))
  141. let num = read(last(words userInput))
  142.  
  143. if valid board row num then do
  144. let updatedBoard = move board row num
  145. nim updatedBoard rows 2
  146. else do
  147. putStrLn "Ugyldig valg. Prøv på nytt.."
  148. play board "n" player rows
  149.  
  150. {--
  151.  
  152. play_Chomp :: Int -> Int -> IO ()
  153. play_Chomp player rows = do
  154. putStrLn "this is Chomp"
  155. putStrLn (show rows)
  156. let c_board = make_chomp_board rows
  157. --show_chomp_board c_board rows rows
  158. putStrLn (show c_board)
  159.  
  160. --play the game
  161. play_Chomp_loop c_board player rows
  162.  
  163.  
  164. play_Chomp_loop :: Chomp_board -> Int -> Int -> IO ()
  165. play_Chomp_loop cb p r = do
  166. if r < 0 then
  167. putStrLn "just stop..."
  168. else do
  169. if p == 0 then do
  170. let move = stupid_IA_do_stuff
  171. if move == (1,1) then
  172. putStrLn "Player wins"
  173. else
  174. putStrLn "stuff happened..."
  175. -- do move stuff
  176. -- change_board
  177.  
  178. else do
  179. putStrLn "X coordinate"
  180. user_X <- getLine
  181. putStrLn "Y coordinate"
  182. user_Y <- getLine
  183. if (user_X == 1) && (user_Y == 1) then
  184. putStrLn "Game over AI wins"
  185. else
  186. putStrLn "stuff happened..."
  187. -- do move stuff
  188. -- change_board
  189. let new_board = cb
  190. putStrLn "players plays"
  191. play_Chomp_loop cb (mod (p+1) 2) (r-1)
  192.  
  193.  
  194. --find the winner
  195.  
  196. stupid_IA_do_stuff :: Pos
  197. stupid_IA_do_stuff = (3,3)
  198.  
  199. --less_stupid_IA_do_stuff :: Chomp_board -> Pos
  200. --less_stupid_IA_do_stuff =
  201.  
  202.  
  203. change_board :: Chomp_board -> Pos -> Chomp_board
  204. change_board cb p = cb
  205. --magic stuff change the board
  206.  
  207.  
  208. make_chomp_board :: Int -> Chomp_board
  209. make_chomp_board r = [[ 1 | i <- [1..r]] | j <- [1..r]]
  210.  
  211. --}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement