Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import System.Random
  2. import System.IO
  3. import System.Environment
  4. import System.IO.Error
  5. import Data.List
  6.  
  7. data Board = Board {size :: Int, cells :: [[Point]]}
  8.  
  9. data Point = Point {x :: Int, y :: Int, figure :: Figure}
  10.  
  11. data Figure = Circle | Cross | Empty deriving Eq
  12.  
  13. instance Show Board where
  14.     show (Board _ rows) = intercalate "\n" $ map show rows
  15.  
  16. instance Show Point where
  17.     show (Point x y figure) = show figure
  18.  
  19. instance Show Figure where
  20.     show Empty = " _ "
  21.     show Circle = " o "
  22.     show Cross = " x "
  23. -----------------------------------------------------------------------------------------
  24. makeColumns :: Int -> Int -> [Point]-> [Point]
  25. makeColumns x y list
  26.    |y == -1 = list
  27.    | otherwise = makeColumns x (y-1) (b:list)
  28.     where b = Point x y Empty
  29. -----------------------------------------------------------------------------------------
  30. makeBoard :: Int -> Int-> [[Point]] -> [[Point]]
  31. makeBoard x y list
  32.     | x == -1 = list
  33.     | otherwise = makeBoard (x-1) y (b:list)
  34.     where b = makeColumns x y []
  35. -----------------------------------------------------------------------------------------
  36. board :: Board
  37. board = Board 19 (makeBoard 18 18 [])
  38. -----------------------------------------------------------------------------------------
  39. insertFigure :: Board -> Int -> Int -> Figure -> Board
  40. insertFigure board x y thing
  41.     | x > 18  || y > 18 || x < 0 || y < 0 = board
  42.     | figure (cells board !! x !! y) == Empty = Board 19 (addFigure board x y thing)
  43.     | figure (cells board !! x !! y) == Cross || figure (cells board !! x !! y) == Circle = board
  44. -----------------------------------------------------------------------------------------
  45. addFigure :: Board -> Int -> Int -> Figure -> [[Point]]
  46. addFigure board x y figure =
  47.  let (a,b) = splitAt x (cells board)
  48.   in let c= [addPoint x y figure (head b)]
  49.    in a ++ (c) ++ (tail b)
  50. -----------------------------------------------------------------------------------------
  51. addPoint :: Int -> Int -> Figure -> [Point] -> [Point]
  52. addPoint x y figure points =
  53.  let(a,b) = splitAt y points
  54.   in a ++ [Point x y figure] ++ tail b
  55. -----------------------------------------------------------------------------------------
  56. findWin :: Board -> Figure -> Int -> Int -> Int -> Int
  57. findWin board thing x y i
  58.  | x==18 || y == 18 = i
  59.  | figure (cells board !! x !! y) == thing = checkUp board thing (x-1) y (i+1) 1
  60.  | otherwise = i
  61. -----------------------------------------------------------------------------------------
  62. checkUp :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
  63. checkUp board thing x y i l
  64.  | x<0  = checkDown board thing (x+l+1) y i
  65.  | figure (cells board !! x !! y) == thing = checkUp board thing (x-1) y (i+1) (l+1)
  66.  | otherwise && i==3 = i
  67.  | otherwise = checkDown board thing (x+l+1) y i
  68. -----------------------------------------------------------------------------------------
  69. checkDown :: Board -> Figure -> Int -> Int -> Int -> Int
  70. checkDown board thing x y i
  71.  | x> 18 = i
  72.  | figure ( cells board !! x !! y ) == thing = checkDown board thing (x+1) y (i+1)
  73.  | i==1 = checkLeft board thing (x-1) (y-1) i 1
  74.  | otherwise = i
  75. -----------------------------------------------------------------------------------------
  76. checkLeft :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
  77. checkLeft board thing x y i l
  78.  | y<0 = checkRight board thing x (y+l+1) i 1
  79.  | figure (cells board !! x !! y ) == thing = checkLeft board thing x (y-1) (i+1) (l+1)
  80.  | otherwise && i==3 = i
  81.  | otherwise = checkRight board thing x (y+l+1) i 1
  82. -----------------------------------------------------------------------------------------
  83. checkRight :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
  84. checkRight board thing x y i l
  85.  | y> 18 = i
  86.  | figure (cells board !! x !! y ) == thing = checkRight board thing x (y+1) (i+1) (l+1)
  87.  | otherwise && i==3 = i
  88.  | otherwise = checkUpLeft board thing (x-1) (y-l-1) 1 1
  89. -----------------------------------------------------------------------------------------
  90. checkUpLeft :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
  91. checkUpLeft board thing x y i l
  92.  | x<0 || y<0 = checkDownRight board thing (x+l+1) (y+l+1) i 1
  93.  | figure (cells board !! x !! y ) == thing = checkUpLeft board thing (x-1) (y-1) (i+1) (l+1)
  94.  | otherwise && i==3 = i
  95.  | otherwise = checkDownRight board thing (x+l+1) (y+l+1) 1 1
  96. -----------------------------------------------------------------------------------------
  97. checkDownRight :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
  98. checkDownRight board thing x y i l
  99.  | x> 18 || y> 18 = checkUpRight board thing (x-l-1) y 1 1
  100.  | figure (cells board !! x !! y ) == thing = checkDownRight board thing (x+1) (y+1) (i+1) (l+1)
  101.  | otherwise && i==3 = i
  102.  | otherwise = checkUpRight board thing (x-l-1) y 1 1
  103. -----------------------------------------------------------------------------------------
  104. checkUpRight :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
  105. checkUpRight board thing x y i l
  106.  | x<0 || y >18 = checkDownLeft board thing (x+l+1) (y-l-1) i 1
  107.  | figure (cells board !! x !! y ) == thing = checkUpRight board thing (x-1) (y+1) (i+1) (l+1)
  108.  | otherwise && i==3 = i
  109.  | otherwise = checkDownLeft board thing (x+l+1) (y-l-1) 1 1
  110. -----------------------------------------------------------------------------------------
  111. checkDownLeft :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
  112. checkDownLeft board thing x y i l
  113.  | y <0 || x> 18 = i
  114.  | figure (cells board !! x !! y) == thing = checkDownLeft board thing (x+1) (y-1) (i+1) (l+1)
  115.  | otherwise && i==3 = i
  116.  | otherwise = i
  117. -----------------------------------------------------------------------------------------
  118. main :: IO()
  119. main = do
  120.   loop board Circle
  121. -----------------------------------------------------------------------------------------
  122. ai :: IO()
  123. ai = do
  124.   aiLoop board Circle
  125. -----------------------------------------------------------------------------------------
  126. start :: IO()
  127. start = do
  128.  putStrLn "1. 1 vs 1"
  129.  putStrLn "2. 1 vs AI"
  130.  putStrLn "3. Exit"
  131.  putStrLn "Wciśnij 1,2 lub 3"
  132.  c <- getLine
  133.  case c of
  134.   "1" -> main
  135.   "2" -> ai
  136.   "3" -> return()
  137. -----------------------------------------------------------------------------------------
  138. isBusy :: Board -> Figure -> Int -> Int -> Bool
  139. isBusy board thing x y
  140.  | figure (cells board !! x !! y) == Circle || figure (cells board !! x !! y) == Cross = True
  141.  | otherwise = False
  142. -----------------------------------------------------------------------------------------
  143. aiLoop :: Board -> Figure -> IO()
  144. aiLoop helpBoard figure1=do
  145.  if(figure1==Circle) then do
  146.   putStrLn "Twoj ruch"
  147.   putStr "Podaj numer wiersza do wstawienia znaku (0-18) "
  148.   x <- getLine
  149.   putStr "Podaj numer kolumny do wstawienia znaku (0-18) "
  150.   y <- getLine
  151.   if(isBusy helpBoard figure1 (read x::Int) (read y::Int) == True) then do
  152.    putStrLn " Zajete"
  153.    aiLoop helpBoard figure1
  154.   else do
  155.    let helpBoard2 = insertFigure helpBoard (read x::Int) (read y::Int) figure1
  156.    putStrLn $ show helpBoard2
  157.    checkResultAI helpBoard2 figure1 (read x::Int) (read y::Int)
  158.  
  159.  else do
  160.   gen <-getStdGen
  161.   newStdGen
  162.   let (randNumber,_) = randomR (1,17) gen :: (Int, StdGen)
  163.   let (randNumber2,_) = randomR (1,17) gen :: (Int, StdGen)
  164.   if(isBusy helpBoard figure1 (randNumber) (randNumber2)) then do
  165.    aiLoop helpBoard figure1
  166.   else do
  167.    let helpBoard2 = insertFigure helpBoard (randNumber) (randNumber2) figure1
  168.    putStrLn $ show helpBoard2
  169.    checkResultAI helpBoard2 figure1 randNumber randNumber2
  170. -----------------------------------------------------------------------------------------
  171. checkResultAI :: Board -> Figure ->Int -> Int ->IO()
  172. checkResultAI board figure x y = do
  173.  if(findWin board figure x y 0 ==3) then do
  174.   gameover board figure
  175.  else do
  176.   if(figure==Circle) then do
  177.    let figure = Cross
  178.    aiLoop board figure
  179.   else do
  180.    let figure = Circle
  181.    aiLoop board figure
  182. -----------------------------------------------------------------------------------------
  183. loop :: Board -> Figure -> IO()
  184. loop helpBoard figure = do
  185.  putStr "Ruch gracza o znaku "
  186.  putStrLn $ show figure
  187.  putStr "Podaj numer wiersza do wstawienia znaku (0-18) "
  188.  x <- getLine
  189.  putStr "Podaj numer kolumny do wstawienia znaku (0-18) "
  190.  y <- getLine
  191.  if(isBusy helpBoard figure (read x::Int) (read y::Int) == True) then do
  192.   putStrLn " Zajete"
  193.   aiLoop helpBoard figure
  194.  else do
  195.   let helpBoard2 = insertFigure helpBoard (read x::Int) (read y::Int) figure
  196.   putStrLn $ show helpBoard2
  197.   checkResult helpBoard2 figure (read x::Int) (read y::Int)
  198. -----------------------------------------------------------------------------------------
  199. checkResult:: Board -> Figure->Int->Int->IO()
  200. checkResult board figure x y= do
  201.  if(findWin board figure x y 0 == 3) then do
  202.   gameover board figure
  203.  else do
  204.  if(figure==Circle) then do
  205.   let figure = Cross
  206.   loop board figure
  207.  else do
  208.   let figure = Circle
  209.   loop board figure
  210. -------------------------------------------------------------------------------------------
  211. gameover:: Board -> Figure -> IO()
  212. gameover board figure = do
  213. putStrLn $ show board
  214. putStr "Wygrał gracz ze znakiem "
  215. putStrLn $ show figure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement