DonnumS
DonnumS

Reputation: 316

Does block indentation cause errors?

I'm working on a nim game in Haskell and run into a problem when I tried to implement a way to choose when the human player can make a move

The code works as it should, but only for human players. The problem is that when the computer ai is supposed to make a move. I therefore have to create an if to check who's turn it is.

I have not yet implemented the ai, but the goal is to get the ai to make a move when player == 2, or by using an else for the if player == 1 then

This is the code that runs my game:

-- To run the game
play :: Board -> Int -> IO ()
play board player =
   do newline
      putBoard board 1
      if finished board then
         do newline
            putStr "Player "
            putStr (show (next player))
            putStrLn " wins!"
      else
        if player == 1 then
            do newLine
               putStr "Player "
               putStrLn (show player)
               row <- getDigit "Enter a row number: "
               num <- getDigit "Stars to remove: "
               if valid board row num then
                 play (move board row num) (next player)
               else
                  do newline
                     putStrLn "That move is not valid, try again!"
                     play board player

nim :: Int -> IO ()
nim x = play (initial x) 1

The players are given an int at the start of the game (player). What I aim to achieve is that the function give the human player the move when this variable is equal to 1 (it changes every turn between 1 and 2). This is the error code I'm getting:

parse error (possibly incorrect indentation or mismatched brackets)
    |
124 | nim :: Int -> IO ()
    | ^

This error did not pop up before I added the if player == 1 then line.

Any help is greatly appreciated!

Upvotes: 0

Views: 96

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152707

Every if needs an else... including your if player == 1.

That said, I'd recommend two things:

  1. Use a custom type that has just the right values, instead of Int which has way too many values.
  2. Use case instead of if.

Like this:

data Player = Human | AI

play :: Board -> Player -> IO ()
play board player = do
    newline
    putBoard board player
    case (finished board, player) of
        (True, _) -> do
            newline
            putStrLn $ "Player " ++ show (next player) ++ " wins!"
        (_, Human) -> do
            newline
            row <- getDigit "Enter a row number: "
            {- ... etc. -}
        _ -> {- compute a move -}

Upvotes: 4

Related Questions