[Haskell-beginners] nim programme

John Moore john.moore54 at gmail.com
Sun Oct 25 14:57:38 EDT 2009

Hi All,
        I'm attempting to write a program for the game nim.(The game of Nim
is played with two players and several piles of stones. On each move a
player removes as many stones as they would like but form only one pile. The
player who takes the last stone wins) It not as simple as I first thought.
Here is my basic starting points. Any comments would be greatly appreciated.
I not well versed in Haskell yet so simple(basic) Haskell rather than well
written haskell if you understand what I mean. Complicated monads are way
out of my league.

1) I first get the program to give me three random piles by doing
    nim = do
  x <- getStdRandom $ randomR (1,10)
  y <- getStdRandom $ randomR (1,10)
  z <- getStdRandom $ randomR (1,10)
  return [x,y,z]
 Cant get this to work!
2) Now I need to get the program to ask for a number and which pile to
remove the number  from. This is tricky I thought about asking to find the
elementAt :: [a] -> Int -> a
elementAt list i = list !! (i-1) put this in a variable
then asking the palyer how many to take away.
 and then subtracting the number from and then putting it back into the list
but this seem impossible.
Then the second player would do the same.
3) Finally we would end up with a case statement like
f x = in case of x
[0,0,1]-> You win
[0,1,0]-> You win
[0,0,1]-> You win
[_,_,_]-> keep playing.

Lets know what you think please, getting confused.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20091025/5a3da27e/attachment.html

More information about the Beginners mailing list