Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Untitled

By: a guest on Jan 21st, 2012  |  syntax: Haskell  |  size: 7.52 KB  |  hits: 68  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. -- *************** Comments *****************
  2.  
  3. --Add game ending stuff = code done needs figuring out
  4. --Create AI = very simple atm, would like best choice
  5. --Fix annoying thing = tracked down ish, needs running through paper
  6. --Check re console stuff
  7.  
  8. -- **************** Declaring stuff ******************
  9.  
  10. data Cell = B | W | X | O
  11.         deriving (Read, Show, Eq)
  12.  
  13. type Board = [[Cell]]
  14.  
  15. myBoard :: Board
  16.  
  17. myBoard = [
  18.           [O,O,O,O,O,O,O,O,O,O],
  19.           [O,X,X,X,X,X,X,X,X,O],
  20.           [O,X,X,X,X,X,X,X,X,O],
  21.           [O,X,X,X,X,X,X,X,X,O],
  22.           [O,X,X,X,B,W,X,X,X,O],
  23.           [O,X,X,X,W,B,X,X,X,O],
  24.           [O,X,X,X,X,X,X,X,X,O],
  25.           [O,X,X,X,X,X,X,X,X,O],
  26.           [O,X,X,X,X,X,X,X,X,O],
  27.           [O,O,O,O,O,O,O,O,O,O]
  28.           ]
  29.  
  30. -- ***************** Basic Functions ********************
  31.  
  32. row y board = board !! (y)
  33.  
  34. get x y board = (row y board) !! (x)
  35.  
  36. getNextCoOrd c1 c2 = c2 + (c2 - c1)
  37.  
  38. getNextXY x1 y1 x2 y2 = [getNextCoOrd x1 x2] ++ [getNextCoOrd y1 y2]
  39.  
  40. getOppon player =
  41.         if player == B
  42.                 then W
  43.                 else B
  44.  
  45. -- **************** Board Output ******************
  46.  
  47. showBoard board = do
  48.         putStrLn ("    1 2 3 4 5 6 7 8")
  49.         putStrLn ("    _ _ _ _ _ _ _ _")
  50.         putStrLn ("1  |" ++ returnRow 1 1 board ++ "|")
  51.         putStrLn ("2  |" ++ returnRow 1 2 board ++ "|")
  52.         putStrLn ("3  |" ++ returnRow 1 3 board ++ "|")
  53.         putStrLn ("4  |" ++ returnRow 1 4 board ++ "|")
  54.         putStrLn ("5  |" ++ returnRow 1 5 board ++ "|")
  55.         putStrLn ("6  |" ++ returnRow 1 6 board ++ "|")
  56.         putStrLn ("7  |" ++ returnRow 1 7 board ++ "|")
  57.         putStrLn ("8  |" ++ returnRow 1 8 board ++ "|")
  58.  
  59. returnRow x y board =
  60.         if x /= 8
  61.                 then (getBlank x y board) ++ "|" ++ returnRow (x+1) y board
  62.                 else (getBlank x y board)
  63.  
  64. getBlank x y board =
  65.         if (get x y board) == X
  66.                 then "_"
  67.                 else (show (get x y board))
  68.  
  69. -- ***************** Get Scores *************
  70.  
  71. showStatus board = do
  72.         putStrLn ("Board:")
  73.         putStrLn ("")
  74.         showBoard board
  75.         putStrLn ("Black score: " ++ show (getBoardScore B 0 1 1 board))
  76.         putStrLn ("White score: " ++ show (getBoardScore W 0 1 1 board))
  77.         putStrLn ("")
  78.  
  79. getRowScore player score x y board =
  80.         if x /= 9
  81.                 then if (get x y board) == player
  82.                         then getRowScore player (score+1) (x+1) y board
  83.                         else getRowScore player score (x+1) y board
  84.                 else score
  85.  
  86. getBoardScore player score x y board =
  87.         if y /= 9
  88.                 then (getBoardScore player score x (y+1) board) + (getRowScore player score x y board)
  89.                 else getRowScore player score x y board
  90.  
  91. -- *************** Game Input/AI Logic ********************
  92.  
  93. main = do
  94.         putStrLn ("Welcome to Reversi!")
  95.         putStrLn ("")
  96.         humanCompTurn myBoard
  97.  
  98. humanHumanTurn player board = do
  99.         showStatus board
  100.         putStrLn ("Player " ++ show (player) ++ " turn:")
  101.         putStrLn ("Please enter x co-ordinate")
  102.         xc <- getLine
  103.         putStrLn ("Please enter y co-ordinate")
  104.         yc <- getLine
  105.         humanHumanTurn (getOppon player) (makeMove (read xc) (read yc) player board)
  106.  
  107. humanCompTurn i board = do
  108.         showStatus board
  109.         if i /= 30
  110.                 then    putStrLn ("Player turn:")
  111.                         putStrLn ("Please enter x co-ordinate")
  112.                         xc <- getLine
  113.                         putStrLn ("Please enter y co-ordinate")
  114.                         yc <- getLine
  115.                         computerTurn i (makeMove (read xc) (read yc) B board)
  116.                 else    putStrLn ("Game Over!")
  117.                         if (getBoardScore B 0 1 1 board) > (getBoardScore W 0 1 1 board)
  118.                                 then putStrLn ("Black wins!")
  119.                                 else if (getBoardScore W 0 1 1 board) > (getBoardScore B 0 1 1 board)
  120.                                         then putStrLn ("White wins!")
  121.                                         else putStrLn ("It's a draw!")
  122.  
  123. computerTurn i board = do
  124.         showStatus board
  125.         putStrLn ("Computer turn...")
  126.         humanCompTurn (i+1) (makeMove (head (head (checkGoodCellsBoard 1 1 board))) ((head (checkGoodCellsBoard 1 1 board)) !! 1) W board)
  127.        
  128. checkGoodCellsRow x y board =
  129.         if x /= 9
  130.                 then if (checkPosition x y B W board)
  131.                         then [[x,y]] ++ checkGoodCellsRow (x+1) y board
  132.                         else checkGoodCellsRow (x+1) y board
  133.                 else []
  134.  
  135. checkGoodCellsBoard x y board =
  136.         if y /= 8
  137.                 then (checkGoodCellsRow x y board) ++ (checkGoodCellsBoard x (y+1) board)
  138.                 else checkGoodCellsRow x y board
  139.  
  140. -- ***************** Checking if cell is valid *******************
  141.  
  142. checkSuccessMove x1 y1 x2 y2 oppon player board =
  143.         if (get x1 y1 board) == oppon
  144.                 then if (get x2 y2 board) == player
  145.                         then True
  146.                         else checkSuccessMove x2 y2 (getNextCoOrd x1 x2) (getNextCoOrd y1 y2) oppon player board
  147.                 else False
  148.  
  149. checkDirection x y xD yD oppon player board =
  150.         if checkSuccessMove xD yD (getNextCoOrd x xD) (getNextCoOrd y yD) oppon player board == True
  151.                 then True
  152.                 else False
  153.  
  154. checkNW x y oppon player board =
  155.         if (checkDirection x y (x-1) (y-1) oppon player board) == True
  156.                 then True
  157.                 else False
  158.  
  159. checkN x y oppon player board =
  160.         if (checkDirection x y x (y-1) oppon player board) == True
  161.                 then True
  162.                 else False
  163.  
  164. checkNE x y oppon player board =
  165.         if (checkDirection x y (x+1) (y-1) oppon player board) == True
  166.                 then True
  167.                 else False
  168.  
  169. checkW x y oppon player board =
  170.         if (checkDirection x y (x-1) y oppon player board) == True
  171.                 then True
  172.                 else False
  173.  
  174. checkE x y oppon player board =
  175.         if (checkDirection x y (x+1) y oppon player board) == True
  176.                 then True
  177.                 else False
  178.  
  179. checkSW x y oppon player board =
  180.         if (checkDirection x y (x-1) (y+1) oppon player board) == True
  181.                 then True
  182.                 else False
  183.  
  184. checkS x y oppon player board =
  185.         if (checkDirection x y x (y+1) oppon player board) == True
  186.                 then True
  187.                 else False
  188.  
  189. checkSE x y oppon player board =
  190.         if (checkDirection x y (x+1) (y+1) oppon player board) == True
  191.                 then True
  192.                 else False
  193.  
  194. checkPosition x y oppon player board =
  195.         if (get x y board) /= B
  196.                 then if (get x y board) /= W
  197.                         then if (checkNW x y oppon player board) == True
  198.                                 then True
  199.                                 else if (checkN x y oppon player board) == True
  200.                                         then True
  201.                                         else if (checkNE x y oppon player board) == True
  202.                                                 then True
  203.                                                 else if (checkW x y oppon player board) == True
  204.                                                         then True
  205.                                                         else if (checkE x y oppon player board) == True
  206.                                                                 then True
  207.                                                                 else if (checkSW x y oppon player board) == True
  208.                                                                         then True
  209.                                                                         else if (checkS x y oppon player board) == True
  210.                                                                                 then True
  211.                                                                                 else if (checkSE x y oppon player board) == True
  212.                                                                                         then True
  213.                                                                                         else False
  214.                 else False
  215.                         else False
  216.  
  217. -- ************ Making a move and changing cells ****************
  218.  
  219. changeRow x y v board = take (x) (row y board) ++ [v] ++ drop (x+1) (row y board)
  220.  
  221. changeCell x y v board = take (y) board ++ [(changeRow x y v board)] ++ drop (y+1) board
  222.  
  223. -- Fail is below ______________________________________________________________________
  224.  
  225. changeCellRec x1 y1 x2 y2 xD1 yD1 xD2 yD2 player board =
  226.         if checkDirection xD1 yD1 xD2 yD2 (getOppon player) player board
  227.                 then if (get x2 y2 board) /= player
  228.                         then take (y1) (changeCellRec x2 y2 (getNextCoOrd x1 x2) (getNextCoOrd y1 y2) xD1 yD1 xD2 yD2 player board) ++ [changeRow x1 y1 player board] ++ drop (y1+1) (changeCellRec x2 y2 (getNextCoOrd x1 x2) (getNextCoOrd y1 y2) xD1 yD1 xD2 yD2 player board)
  229.                         else changeCell x1 y1 player board
  230.                 else board
  231.  
  232. -- Fail is above """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  233.  
  234. makeMove x y player board =
  235.         if checkPosition x y (getOppon player) player board == True
  236.                 then changeCellRec x y (x-1) (y-1) x y (x-1) (y-1) player (changeCellRec x y x (y-1) x y x (y-1) player (changeCellRec x y (x+1) (y-1) x y (x+1) (y-1) player (changeCellRec x y (x-1) y x y (x-1) y player (changeCellRec x y (x+1) y x y (x+1) y player (changeCellRec x y (x-1) (y+1) x y (x-1) (y+1) player (changeCellRec x y x (y+1) x y x (y+1) player (changeCellRec x y (x+1) (y+1) x y (x+1) (y+1) player board)))))))
  237.                 else board