Skip to content

Instantly share code, notes, and snippets.

@nna774
Created August 15, 2012 15:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nna774/3361116 to your computer and use it in GitHub Desktop.
Save nna774/3361116 to your computer and use it in GitHub Desktop.
オセロ
-- オセロ
--import Data.Functor
import Maybe
import Control.Monad
data Color = Black | White deriving (Eq, Show, Read)
type Cell = Maybe Color
type Board = [[ Cell ]]
newBoard :: Board
newBoard =
  [ [ Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing],
  [ Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing],
  [ Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing],
  [ Nothing,Nothing,Nothing,Just White,Just Black,Nothing,Nothing,Nothing],
  [ Nothing,Nothing,Nothing,Just Black,Just White,Nothing,Nothing,Nothing],
  [ Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing],
  [ Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing],
  [ Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing] ]
countStone :: Color -> Board -> Int
countStone color board  = length $ filter (==Just color) $ concat board
type Point = ( Int , Int ) -- ( row , column ) each value is in [1,8]
type Hand = ( Color , Point )
getCell :: Board -> Point -> Cell
getCell board ( row , column ) = board !! (row-1) !! (column-1)
putStone :: Board -> Hand -> Board
putStone board hand = undefined
findNext :: Board -> Color -> [ Point ]
findNext board color = map (\(_,p) -> p ) $ filter (isPossibleHand board) [ (color,(r,c)) | r<-[1..8],c<-[1..8] ]
nextPossibleBoard :: Board -> Color -> [ Board ]
nextPossibleBoard board color = undefined
isPossibleHand :: Board -> Hand -> Bool
isPossibleHand board (color,(row,column)) =
if (row < 1) || (row > 8) || (column < 1) || (column > 8)
then False
else
if getCell board (row,column) /= Nothing
then False
else
if isAloneC board (color,(row,column))
then False
else True -- undefined
{-
isAlone :: Board -> Point -> Bool
isAlone board (row,column) = Nothing == msum list
where
list = map (getCell board) list_i
list_i =
case (row,column) of
(1,1) -> [(1,2),(2,2),(2,1)]
(8,1) -> [(8,2),(7,2),(7,1)]
(1,8) -> [(1,7),(2,8),(2,7)]
(8,8) -> [(8,7),(7,8),(7,7)]
(1,_) -> [ (row,column-1),(row,column+1),(row+1,column-1),(row+1,column),(row+1,column+1) ]
(8,_) -> [ (row,column-1),(row,column+1),(row-1,column-1),(row-1,column),(row-1,column+1) ]
(_,1) -> [ (row-1,column),(row+1,column),(row-1,column+1),(row,column+1),(row+1,column+1) ]
(_,8) -> [ (row-1,column),(row+1,column),(row-1,column-1),(row,column-1),(row+1,column-1) ]
(_,_) -> [ (row-1,column-1),(row,column-1),(row+1,column-1),(row-1,column),(row+1,column),(row-1,column+1),(row,column+1),(row+1,column+1) ]
-}
isAloneC :: Board -> Hand -> Bool
isAloneC board (color,(row,column)) = not $ elem (Just $ theOtherColor color) list
where
list = map (getCell board) list_i
list_i =
case (row,column) of
(1,1) -> [(1,2),(2,2),(2,1)]
(8,1) -> [(8,2),(7,2),(7,1)]
(1,8) -> [(1,7),(2,8),(2,7)]
(8,8) -> [(8,7),(7,8),(7,7)]
(1,_) -> [ (row,column-1),(row,column+1),(row+1,column-1),(row+1,column),(row+1,column+1) ]
(8,_) -> [ (row,column-1),(row,column+1),(row-1,column-1),(row-1,column),(row-1,column+1) ]
(_,1) -> [ (row-1,column),(row+1,column),(row-1,column+1),(row,column+1),(row+1,column+1) ]
(_,8) -> [ (row-1,column),(row+1,column),(row-1,column-1),(row,column-1),(row+1,column-1) ]
(_,_) -> [ (row-1,column-1),(row,column-1),(row+1,column-1),(row-1,column),(row+1,column),(row-1,column+1),(row,column+1),(row+1,column+1) ]
theOtherColor :: Color -> Color 
theOtherColor Black = White
theOtherColor White = Black
main = print $ findNext newBoard $ theOtherColor White
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment