Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import System.Random
- import System.IO
- import System.Environment
- import System.IO.Error
- import Data.List
- data Board = Board {size :: Int, cells :: [[Point]]}
- data Point = Point {x :: Int, y :: Int, figure :: Figure}
- data Figure = Circle | Cross | Empty deriving Eq
- instance Show Board where
- show (Board _ rows) = intercalate "\n" $ map show rows
- instance Show Point where
- show (Point x y figure) = show figure
- instance Show Figure where
- show Empty = " _ "
- show Circle = " o "
- show Cross = " x "
- -----------------------------------------------------------------------------------------
- makeColumns :: Int -> Int -> [Point]-> [Point]
- makeColumns x y list
- |y == -1 = list
- | otherwise = makeColumns x (y-1) (b:list)
- where b = Point x y Empty
- -----------------------------------------------------------------------------------------
- makeBoard :: Int -> Int-> [[Point]] -> [[Point]]
- makeBoard x y list
- | x == -1 = list
- | otherwise = makeBoard (x-1) y (b:list)
- where b = makeColumns x y []
- -----------------------------------------------------------------------------------------
- board :: Board
- board = Board 19 (makeBoard 18 18 [])
- -----------------------------------------------------------------------------------------
- insertFigure :: Board -> Int -> Int -> Figure -> Board
- insertFigure board x y thing
- | x > 18 || y > 18 || x < 0 || y < 0 = board
- | figure (cells board !! x !! y) == Empty = Board 19 (addFigure board x y thing)
- | figure (cells board !! x !! y) == Cross || figure (cells board !! x !! y) == Circle = board
- -----------------------------------------------------------------------------------------
- addFigure :: Board -> Int -> Int -> Figure -> [[Point]]
- addFigure board x y figure =
- let (a,b) = splitAt x (cells board)
- in let c= [addPoint x y figure (head b)]
- in a ++ (c) ++ (tail b)
- -----------------------------------------------------------------------------------------
- addPoint :: Int -> Int -> Figure -> [Point] -> [Point]
- addPoint x y figure points =
- let(a,b) = splitAt y points
- in a ++ [Point x y figure] ++ tail b
- -----------------------------------------------------------------------------------------
- findWin :: Board -> Figure -> Int -> Int -> Int -> Int
- findWin board thing x y i
- | x==18 || y == 18 = i
- | figure (cells board !! x !! y) == thing = checkUp board thing (x-1) y (i+1) 1
- | otherwise = i
- -----------------------------------------------------------------------------------------
- checkUp :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
- checkUp board thing x y i l
- | x<0 = checkDown board thing (x+l+1) y i
- | figure (cells board !! x !! y) == thing = checkUp board thing (x-1) y (i+1) (l+1)
- | otherwise && i==3 = i
- | otherwise = checkDown board thing (x+l+1) y i
- -----------------------------------------------------------------------------------------
- checkDown :: Board -> Figure -> Int -> Int -> Int -> Int
- checkDown board thing x y i
- | x> 18 = i
- | figure ( cells board !! x !! y ) == thing = checkDown board thing (x+1) y (i+1)
- | i==1 = checkLeft board thing (x-1) (y-1) i 1
- | otherwise = i
- -----------------------------------------------------------------------------------------
- checkLeft :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
- checkLeft board thing x y i l
- | y<0 = checkRight board thing x (y+l+1) i 1
- | figure (cells board !! x !! y ) == thing = checkLeft board thing x (y-1) (i+1) (l+1)
- | otherwise && i==3 = i
- | otherwise = checkRight board thing x (y+l+1) i 1
- -----------------------------------------------------------------------------------------
- checkRight :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
- checkRight board thing x y i l
- | y> 18 = i
- | figure (cells board !! x !! y ) == thing = checkRight board thing x (y+1) (i+1) (l+1)
- | otherwise && i==3 = i
- | otherwise = checkUpLeft board thing (x-1) (y-l-1) 1 1
- -----------------------------------------------------------------------------------------
- checkUpLeft :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
- checkUpLeft board thing x y i l
- | x<0 || y<0 = checkDownRight board thing (x+l+1) (y+l+1) i 1
- | figure (cells board !! x !! y ) == thing = checkUpLeft board thing (x-1) (y-1) (i+1) (l+1)
- | otherwise && i==3 = i
- | otherwise = checkDownRight board thing (x+l+1) (y+l+1) 1 1
- -----------------------------------------------------------------------------------------
- checkDownRight :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
- checkDownRight board thing x y i l
- | x> 18 || y> 18 = checkUpRight board thing (x-l-1) y 1 1
- | figure (cells board !! x !! y ) == thing = checkDownRight board thing (x+1) (y+1) (i+1) (l+1)
- | otherwise && i==3 = i
- | otherwise = checkUpRight board thing (x-l-1) y 1 1
- -----------------------------------------------------------------------------------------
- checkUpRight :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
- checkUpRight board thing x y i l
- | x<0 || y >18 = checkDownLeft board thing (x+l+1) (y-l-1) i 1
- | figure (cells board !! x !! y ) == thing = checkUpRight board thing (x-1) (y+1) (i+1) (l+1)
- | otherwise && i==3 = i
- | otherwise = checkDownLeft board thing (x+l+1) (y-l-1) 1 1
- -----------------------------------------------------------------------------------------
- checkDownLeft :: Board -> Figure -> Int -> Int -> Int -> Int -> Int
- checkDownLeft board thing x y i l
- | y <0 || x> 18 = i
- | figure (cells board !! x !! y) == thing = checkDownLeft board thing (x+1) (y-1) (i+1) (l+1)
- | otherwise && i==3 = i
- | otherwise = i
- -----------------------------------------------------------------------------------------
- main :: IO()
- main = do
- loop board Circle
- -----------------------------------------------------------------------------------------
- ai :: IO()
- ai = do
- aiLoop board Circle
- -----------------------------------------------------------------------------------------
- start :: IO()
- start = do
- putStrLn "1. 1 vs 1"
- putStrLn "2. 1 vs AI"
- putStrLn "3. Exit"
- putStrLn "Wciśnij 1,2 lub 3"
- c <- getLine
- case c of
- "1" -> main
- "2" -> ai
- "3" -> return()
- -----------------------------------------------------------------------------------------
- isBusy :: Board -> Figure -> Int -> Int -> Bool
- isBusy board thing x y
- | figure (cells board !! x !! y) == Circle || figure (cells board !! x !! y) == Cross = True
- | otherwise = False
- -----------------------------------------------------------------------------------------
- aiLoop :: Board -> Figure -> IO()
- aiLoop helpBoard figure1=do
- if(figure1==Circle) then do
- putStrLn "Twoj ruch"
- putStr "Podaj numer wiersza do wstawienia znaku (0-18) "
- x <- getLine
- putStr "Podaj numer kolumny do wstawienia znaku (0-18) "
- y <- getLine
- if(isBusy helpBoard figure1 (read x::Int) (read y::Int) == True) then do
- putStrLn " Zajete"
- aiLoop helpBoard figure1
- else do
- let helpBoard2 = insertFigure helpBoard (read x::Int) (read y::Int) figure1
- putStrLn $ show helpBoard2
- checkResultAI helpBoard2 figure1 (read x::Int) (read y::Int)
- else do
- gen <-getStdGen
- newStdGen
- let (randNumber,_) = randomR (1,17) gen :: (Int, StdGen)
- let (randNumber2,_) = randomR (1,17) gen :: (Int, StdGen)
- if(isBusy helpBoard figure1 (randNumber) (randNumber2)) then do
- aiLoop helpBoard figure1
- else do
- let helpBoard2 = insertFigure helpBoard (randNumber) (randNumber2) figure1
- putStrLn $ show helpBoard2
- checkResultAI helpBoard2 figure1 randNumber randNumber2
- -----------------------------------------------------------------------------------------
- checkResultAI :: Board -> Figure ->Int -> Int ->IO()
- checkResultAI board figure x y = do
- if(findWin board figure x y 0 ==3) then do
- gameover board figure
- else do
- if(figure==Circle) then do
- let figure = Cross
- aiLoop board figure
- else do
- let figure = Circle
- aiLoop board figure
- -----------------------------------------------------------------------------------------
- loop :: Board -> Figure -> IO()
- loop helpBoard figure = do
- putStr "Ruch gracza o znaku "
- putStrLn $ show figure
- putStr "Podaj numer wiersza do wstawienia znaku (0-18) "
- x <- getLine
- putStr "Podaj numer kolumny do wstawienia znaku (0-18) "
- y <- getLine
- if(isBusy helpBoard figure (read x::Int) (read y::Int) == True) then do
- putStrLn " Zajete"
- aiLoop helpBoard figure
- else do
- let helpBoard2 = insertFigure helpBoard (read x::Int) (read y::Int) figure
- putStrLn $ show helpBoard2
- checkResult helpBoard2 figure (read x::Int) (read y::Int)
- -----------------------------------------------------------------------------------------
- checkResult:: Board -> Figure->Int->Int->IO()
- checkResult board figure x y= do
- if(findWin board figure x y 0 == 3) then do
- gameover board figure
- else do
- if(figure==Circle) then do
- let figure = Cross
- loop board figure
- else do
- let figure = Circle
- loop board figure
- -------------------------------------------------------------------------------------------
- gameover:: Board -> Figure -> IO()
- gameover board figure = do
- putStrLn $ show board
- putStr "Wygrał gracz ze znakiem "
- putStrLn $ show figure
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement