May 25, 2009 ... Nonogram. Functional Programming Assignment. Joseph Cordina. May 6, 2009.
1 NonoGrams: The Game. NonoGrams1 is a game whereby ...
Nonogram Functional Programming Assignment Joseph Cordina May 6, 2009
1
NonoGrams: The Game
NonoGrams1 is a game whereby one tries to guess the location of filled squares in an initially empty grid. One can deduce the location of the filled squares by a series of numbers describing the sequence of consecutive filled squares in the columns and rows. The resultant filled boxes normally represent a particular image. You can find this game also under the name of Picross and you will find several examples of this game online. Start off by understanding how this game is played before proceeding reading this description. In your assignment, you will write a Haskell program that will generate, operate and solve Nonogram puzzles. I also have provided you with a visualizer for the board that you can use to view the NonoGram board.
2
Preliminaries
This assignment carries no mark for those taking CSA1016, but it is recommended that you try it and submit it by the stipulated deadline. This assignment carries 15% of the total mark for those taking CSA1024. Parts of this assignment may also be included in the exam. The deadline for the assignment is 25th May 2009. You may do this assignment in a group of maximum 2 people, yet you may do this assignment individually also. A single mark will be allocated to the whole group, so choose your partner carefully and make sure that the work gets evenly distributed. Under no circumstances should code be shared outside your group. You will submit your assignment making use of the ASS system2 . Those of you without login access, please contact me on joseph.cordina at um.edu.mt. Each of you might be assigned a ten minute slot in which you will be presenting the implementation of your assignment and during which your program will be executed. The students that will be called for this presentation will be allocated at random by the administration office. Please be reminded that you cannot copy and plagiarize to ease your way to a final submission. While you may discuss ideas with others, do not steal. You can find more information at http://www.cs.um.edu.mt/cs/links&resources/plagarism.html Read this information well since after wards no excuses will save you. Anyone resorting to such methods will be considered as trying to cheat and will even risk the removal from the degree program. You should submit one single .hs file containing your assignment. You do not need to include the NonoGram.hs file in your submission. There are some types and functions in your assignment that will be described in this document. You can find a file called 1 http://en.wikipedia.org/wiki/Nonogram 2 www.cs.um.edu.mt/˜jcord/ass
1
NonoGram.hs on my website3 that contains their definitions. Open NonoGram.hs to read its contents up to the point where it says “internal details”. To make use of this file, make sure you include the following line at the beginning of your Haskell code: import NonoGram
3
The Assignment
3.1
Question 1
Start of by defining an appropriate representation of your playing board i.e. the grid that makes up the NonoGram. Call this representation MyBoard.
3.2
Question 2
Using Question 1, define the following function: -- given a particular width for a board -- gives back a board full of empty squares emptyBoard :: Int -> MyBoard
3.3
Question 3
Using the definition from Question 1, define the following function: -- initBoard takes a width, -- a particular integer which represents a board configuration -- and gives back a populated board initBoard :: Int -> Int -> MyBoard The idea is that when calling initBoard with say board configuration 1, it will give a particular board setup, for the value 2 it will give a different setup and so on. Its like a game number. To help you out, I have provided the following function for you -- taking a particular value to be used as a random seed, -- it returns an infinite list of booleans generateSequence :: Int -> [Bool] Do not worry too much about how its implemented. Just remember that for the same integer input, it will always give the same random sequence. Yet this sequence is infinite !!
3.4
Question 4
Define the following function: -- given a board will give back the width of the board -- i.e. the number of squares on any side width :: MyBoard -> Int
3.5
Question 5
Define the following: -- will return true or false depending if -- position given on board is full isOn:: MyBoard -> (XPos, YPos) -> Bool 3 www.cs.um.edu.mt/˜jcord
2
3.6
Question 6
Make MyBoard an instance of the Eq a class.
3.7
Question 7
Define the following functions: -- will give back a list of lists -- i.e. the series of consecutive -- when looking at the rows rowDetails :: MyBoard -> [[Int]] -- will give back a list of lists -- i.e. the series of consecutive -- when looking at the cols colDetails :: MyBoard -> [[Int]]
3.8
of Ints describing rows squares found in the board
of Int describing cols squares found in the board
Question 8
Make MyBoard an instance of the class NonoGramBoard a whose definition you will find in the NonoGram.hs file. After you finish this question, you can call the following function -- given a board, and the size of the width in pixels (say 300) -- will display the board, -- press any key to close the window displayBoard ::(NonoGramBoard a) => a -> Int -> IO ()
3.9
Question 9
Make MyBoard an instance of the class Solveable a class. The solve function should be able to give you back a board populated with squares that satisfy the row descriptions (second argument) and the columns descriptions (third argument). Note that solve also takes a board as its first argument. You do not need to use the passed in board in your definition (just pass in any board of type MyBoard, even an empty one), yet it is necessary to avoid you some type checking headaches. Also note that I am not expecting a solution that implements any form of artificial intelligence. You can resort to using a simple expensive solution, as long as the solution is correct. Due to the complexity of the board, I recommend that you do not try to find solutions on boards larger than 5 by 5, since they will take quite a long while to solve.
3.10
Question 10
Write the following function: -- given a row description, a column description -- and the length in pixels -- displays the solution -- HINT: make use of the displayBoard again displaySolution ::[[Int]] -> [[Int]] -> Int -> IO() Note that this function returns IO(), which is a representation of anything that does an IO but then returns nothing to the caller (more details in more advanced courses on Haskell !!).
3
4
Conclusions
Good luck and if you need anything, contact me on joseph dot cordina at um.edu.mt. Even better, make use of the discussion group to discuss ideas and ask questions. You can find a link to the discussion group on my website. Other students might help you out in the solution. Just make sure you do not post any code or given concrete solutions. And do not leave the assignment to the last couple of days since you will definitely not make it in time!! Do it slowly and the ideas will mature.
4