[Pocketgames-devel] SF.net SVN: pocketgames:[230] games
Status: Beta
Brought to you by:
idominguez
|
From: <ido...@us...> - 2009-01-12 03:46:15
|
Revision: 230
http://pocketgames.svn.sourceforge.net/pocketgames/?rev=230&view=rev
Author: idominguez
Date: 2009-01-12 03:45:27 +0000 (Mon, 12 Jan 2009)
Log Message:
-----------
Game written in haskell. Uses gtk2hs, buttons use 0 and 1 to represent the lights when off and on respectively
Added Paths:
-----------
games/lightsoff/
games/lightsoff/Game.hs
games/lightsoff/Main.hs
games/lightsoff/Makefile
Added: games/lightsoff/Game.hs
===================================================================
--- games/lightsoff/Game.hs (rev 0)
+++ games/lightsoff/Game.hs 2009-01-12 03:45:27 UTC (rev 230)
@@ -0,0 +1,35 @@
+module Game (
+ -- Exported stuff comes here
+ Game
+ , exampleGame1
+ , toogle
+ , isGameFinished
+ ) where
+
+type Game = [Bool]
+
+isGameFinished :: Game -> Bool
+isGameFinished = all (== False)
+
+exampleGame1 :: Game
+exampleGame1 = [ False, True, False, False, False
+ , False, False, False, False, False
+ , False, True, False, False, False
+ , False, False, False, False, False
+ , False, False, False, False, False]
+
+toogle :: Game -> Int -> Int -> Game
+toogle g i j = foldl toogleSimple g ls
+ where ls = getAllToogled i j
+
+getAllToogled :: Int -> Int -> [(Int,Int)]
+getAllToogled i j = filter (\(x,y) -> x > -1 && x < 5 && y > -1 && y < 5)
+ [(i,j), ((i-1),j), ((i+1), j), (i,(j-1)), (i, (j+1))]
+
+
+-- FIXME: generalise for any board size
+toogleSimple :: Game -> (Int,Int) -> Game
+toogleSimple [] (i,j) = error ("Coodinates out of bounds: " ++ (show (i,j)))
+toogleSimple (x:xs) (0, 0) = (not x:xs)
+toogleSimple (x:xs) (n, 0) = (x:toogleSimple xs ((n-1), 4))
+toogleSimple (x:xs) (n, m) = (x:toogleSimple xs (n, (m-1)))
Added: games/lightsoff/Main.hs
===================================================================
--- games/lightsoff/Main.hs (rev 0)
+++ games/lightsoff/Main.hs 2009-01-12 03:45:27 UTC (rev 230)
@@ -0,0 +1,87 @@
+import Graphics.UI.Gtk
+import Graphics.UI.Gtk.ModelView.TreeView as TV
+import Graphics.UI.Gtk.ModelView as MV
+import System.IO
+import Data.IORef
+import Game
+
+main :: IO ()
+main = do
+ startGUI
+
+startGUI :: IO()
+startGUI = do
+ initGUI
+ window <- windowNew
+ windowSetDefaultSize window 400 600 -- Default window size, fits in FR
+
+ buttonAdd <- buttonNew
+
+ table <- tableNew 5 5 True
+ containerAdd window table
+
+ let lista = [1..25] in do
+ buttons' <- mapM (\_ -> buttonNewWithLabel "0") lista
+
+ -- Create the state that will be passed to all event handling functions
+ state <- newIORef (Environment exampleGame1 buttons')
+
+ -- Set all buttons to the appropriate appearance
+ representGame exampleGame1 buttons'
+
+ -- Set event handlers
+ setActions buttons' state
+ attachToTable table buttons'
+
+ onDestroy window mainQuit
+ widgetShowAll window
+ mainGUI
+
+-- State that will be used for all elements in the program. It's not
+-- just the view, it's also the internal datatypes (ie. tasks.)
+data Environment = Environment Game [Button]
+
+action :: IORef Environment -> Int -> Int -> IO()
+action state i j = do
+ env@(Environment game buttons) <- readIORef state
+ let g' = toogle game i j in do
+ if (isGameFinished g')
+ then putStrLn "Game finished"
+ else return()
+ representGame g' buttons
+ writeIORef state (Environment g' buttons)
+
+representGame :: [Bool] -> [Button] -> IO()
+representGame (True:xs) (b:bs) = do
+ set b [ buttonLabel := "1" ]
+ representGame xs bs
+representGame (False:xs) (b:bs) = do
+ set b [ buttonLabel := "0" ]
+ representGame xs bs
+representGame [] _ = return ()
+
+setActions :: [Button] -> IORef Environment -> IO()
+setActions bs state = setActions' bs state 0 0
+
+setActions' :: [Button] -> IORef Environment
+ -> Int -> Int -> IO()
+setActions' (b:[]) state 4 4 = do onClicked b (action state 4 4)
+ return ()
+setActions' (b:bs) state n 4 = do onClicked b (action state n 4)
+ setActions' bs state (n+1) 0
+ return ()
+setActions' (b:bs) state n m = do onClicked b (action state n m)
+ setActions' bs state n (m+1)
+ return ()
+
+attachToTable :: Table -> [Button] -> IO()
+attachToTable table bs = attachToTable' table bs 0 0
+
+attachToTable' table (b:[]) 4 4 = tableAttachDefaults table b 4 5 4 5
+attachToTable' table (b:bs) n 4 = do
+ tableAttachDefaults table b 4 5 n (n+1)
+ attachToTable' table bs (n+1) 0
+attachToTable' table (b:bs) n m = do
+ tableAttachDefaults table b m (m+1) n (n+1)
+ attachToTable' table bs n (m+1)
+
Added: games/lightsoff/Makefile
===================================================================
--- games/lightsoff/Makefile (rev 0)
+++ games/lightsoff/Makefile 2009-01-12 03:45:27 UTC (rev 230)
@@ -0,0 +1,5 @@
+all:
+ ghc --make Main.hs -o main
+
+clean:
+ rm -f *.hi *.o main
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|