GithubHelp home page GithubHelp logo

haskell-nim's Introduction

Programming in Haskell - Graham Hutton - Ch10 - Nim

TABLE OF CONTENTS

Nim game:

A variant of game of name, played on a board comprising five numbered rows of stars initially set up as follows:

  • 1:✦✦✦✦✦
  • 2:✦✦✦✦
  • 3:✦✦✦
  • 4:✦✦
  • 4:✦

Two player in bottom-up manner, two players taking turns to remove one or more stars form the end of single row. The winner is who removes the final star.

Imports

import System.IO
import Data.List
import Data.Char

Game utilities:

next function:

For simplicity players will shown as 1 2 integers using the following function

next :: Int -> Int
next 1 = 2
next 2 = 1

Board

Show each row as a list.

type Board = [Int]

initial :: Board
initial = [5, 4, 3, 2, 1]

finished :: Board -> Bool
finished = all (== 0)

valid

A move in the game is specified by a row number and the number of stars to be removed, and is valid if the row contains at this many stars.

valid :: Board -> Int -> Int -> Bool
valid board row num = board !! (row-1) >=num

move

If valid then do the move.

move :: Board -> Int -> Int -> Board
move board row num = [update r n | (r,n) <- zip [1..] board]
                     where update r n = if r == row then n-num else n
                           -- update is a small function that takes two args "r" and "n"
                           -- it looks for the row number within the board
                           -- then subtract the number of stars given by the player
                           -- otherwise it will return the row value as it is "n".

IO utilities

putRow function

We start with a function that displays a row of the board on the screen, given the row number and the number of stars remaining.

putRow :: Int -> Int -> IO ()
putRow row num = do putStr (show row)
                    putStr ": "
                    putStrLn (concat (replicate num ""))

putBoard function

We can use `putRow` to show the board.

putBoard :: Board -> IO ()
putBoard [a,b,c,d,e] = do putRow 1 a
                          putRow 2 b
                          putRow 3 c
                          putRow 4 d
                          putRow 5 e

getDigit function

A function that displays a prompt and reads single character from the keyboard, if the character is digit, the corresponding integer is returned as the result value, otherwise an error message is displayed and the user prompted to enter a digit again.

getDigit :: String -> IO Int
getDigit prompt = do putStr prompt
                     x <- getChar
                     newline
                     if isDigit x then
                        return (digitToInt x)
                     else
                        do putStrLn "ERROR: Invalid digit"
                           getDigit prompt

newLine function

Move onto new line.

newline :: IO ()
newline = putChar '\n'

Game of Nim:

Using above utilities, we can implement the main game loop.

play :: Board -> Int -> IO ()
play board player =
  do newline
     putBoard board
     if finished board then
        do newline
           putStr "Player "
           putStr (show (next player))
           putStrLn " wins!!"
     else
        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 "ERROR: Invalid move"
                 play board player

Invoking game loop

nim :: IO ()
nim = play initial 1

haskell-nim's People

Contributors

firwanaa avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.