Skip to content
Browse files

Created Makefile and added .gitignore.

  • Loading branch information...
1 parent 9a05521 commit b8f9acbe592afd5289bac589b5a8e197f04d4838 @3noch committed with Elliot Cameron Jan 31, 2012
Showing with 80 additions and 58 deletions.
  1. +3 −0 .gitignore
  2. +10 −0 Makefile
  3. +56 −57 Nim.hs
  4. +11 −1 README.md
View
3 .gitignore
@@ -0,0 +1,3 @@
+*.hi
+*.o
+nim
View
10 Makefile
@@ -0,0 +1,10 @@
+MAIN = Nim.hs
+EXE = nim
+
+all: $(EXE)
+
+clean:
+ rm -f *.o *.hi $(EXE)
+
+$(EXE): $(wildcard *.hs)
+ ghc -O2 -o $(EXE) --make $(MAIN)
View
113 Nim.hs
@@ -1,57 +1,56 @@
-module Nim where
- import System.IO
-
- main = play [1,2,3,4,5] ["Player 1","Player 2"]
-
- play :: [Int] -> [String] -> IO ()
- play board players = do
- (newBoard,winner) <- takeTurns board players
- if null winner
- then play newBoard players
- else do putStrLn (winner ++ " won!")
- return ()
-
- takeTurns :: [Int] -> [String] -> IO ([Int],String)
- takeTurns board [] = return (board,[])
- takeTurns board (player:players) = do
- newBoard <- takeTurn board player
- if sum newBoard == 0
- then return (newBoard,player)
- else takeTurns newBoard players
-
- takeTurn :: [Int] -> String -> IO [Int]
- takeTurn board player = do
- putStrLn ("\n" ++ player ++ "'s Turn")
- displayBoard board
- row <- getRow board
- count <- getInt "How many stars? " 1 (board!!row)
- return $ take row board ++ [board!!row - count] ++ drop (row+1) board
-
- displayBoard :: [Int] -> IO ()
- displayBoard [] = return ()
- displayBoard board = do
- putStrLn $ show (length board) ++ " : " ++ take (last board) (repeat '*')
- displayBoard (init board)
-
- getRow :: [Int] -> IO Int
- getRow board = do
- row <- getInt "Which row? " 1 (length board)
- if board!!(row-1) == 0
- then do putStrLn "That row is empty!"
- getRow board
- else return (row-1)
-
- getInt msg min max = do
- putStr msg
- input <- getLine
- let parsed = reads input :: [(Int,String)]
- if null parsed
- then badNumber "That's not a number!"
- else testNumber (fst (head parsed))
- where
- badNumber error = do putStrLn error
- getInt msg min max
- testNumber number
- | number < min = badNumber "That number is too small."
- | number > max = badNumber "That number is too big."
- | otherwise = do return number
+import System.IO
+
+main = play [1,2,3,4,5] ["Player 1","Player 2"]
+
+play :: [Int] -> [String] -> IO ()
+play board players = do
+ (newBoard,winner) <- takeTurns board players
+ if null winner
+ then play newBoard players
+ else do putStrLn (winner ++ " won!")
+ return ()
+
+takeTurns :: [Int] -> [String] -> IO ([Int],String)
+takeTurns board [] = return (board,[])
+takeTurns board (player:players) = do
+ newBoard <- takeTurn board player
+ if sum newBoard == 0
+ then return (newBoard,player)
+ else takeTurns newBoard players
+
+takeTurn :: [Int] -> String -> IO [Int]
+takeTurn board player = do
+ putStrLn ("\n" ++ player ++ "'s Turn")
+ displayBoard board
+ row <- getRow board
+ count <- getInt "How many stars? " 1 (board!!row)
+ return $ take row board ++ [board!!row - count] ++ drop (row+1) board
+
+displayBoard :: [Int] -> IO ()
+displayBoard [] = return ()
+displayBoard board = do
+ putStrLn $ show (length board) ++ " : " ++ replicate (last board) '*'
+ displayBoard (init board)
+
+getRow :: [Int] -> IO Int
+getRow board = do
+ row <- getInt "Which row? " 1 (length board)
+ if board!!(row-1) == 0
+ then do putStrLn "That row is empty!"
+ getRow board
+ else return (row-1)
+
+getInt msg min max = do
+ putStr msg
+ input <- getLine
+ let parsed = reads input :: [(Int,String)]
+ if null parsed
+ then badNumber "That's not a number!"
+ else testNumber (fst (head parsed))
+ where
+ badNumber error = do putStrLn error
+ getInt msg min max
+ testNumber number
+ | number < min = badNumber "That number is too small."
+ | number > max = badNumber "That number is too big."
+ | otherwise = do return number
View
12 README.md
@@ -8,5 +8,15 @@ Written in 2010 by Elliot Cameron
Public Domain.
+ [Nim]: http://en.wikipedia.org/wiki/Nim
-[Nim]: http://en.wikipedia.org/wiki/Nim
+Building
+--------
+
+To build, run
+
+ make
+
+Then run the game like this
+
+ ./nim

0 comments on commit b8f9acb

Please sign in to comment.
Something went wrong with that request. Please try again.