Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete using the Haskell programming language John Conway's Game of Life is a well known game with a very simple set of rules that can

Complete using the Haskell programming language

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

John Conway's Game of Life is a well known "game" with a very simple set of rules that can simulate incredibly sophisticated behavior (as sophisticated, in fact, as any computer program). Its setting -- which we'll refer to as the game's "world" -- is a two-dimensional grid of cells, each one either living or dead, and considered to be in contact with its eight neighbors (i.e., orthogonally or diagonally adjacent cells). Given the state of each cell in the world at time tn we can compute the state of each cell at time tn+1 (i.e., one generation later) using the following rules: - Birth rule: a dead cell with exactly three living neighbors comes alive. - Survival rule: a living cell with two to three neighbors remains alive. - Death rule: a living cell with zero or one neighbors dies in isolation; a living cell with four or more neighbors dies of overcrowding The image below depicts a world before and after the rules are applied to a group of cells. Many living cell patterns have been discovered that result in stableon-changing shapes and repeatedly oscillating shapes, among other types (see the example patterns in the Game of Life Wikipedia article for some). An interesting fact about the Game of Life is that there is no algorithm which can reliably predict whether or not a given pattern will appear in the world given some starting state (this is related to the halting problem, covered in CS 330). Since each world state (after the initial state) is a pure function of the preceding one, and the game world can be easily modeled using lists, implementing the Game of Life makes for another fun list processing exercise! Implementation in Haskell We will represent the game world using the type: ((Int, Int), [[Bool ] ]). I.e., as a tuple where the first element is a tuple of two integers (w,h) representing the width and height of the world, and the second element is a list of lists of Booleans representing the 2D grid, where each sublist of length w represents the cells in a row of the world, with h sublists total. A barren (i.e., containing only dead cells) world can be created with the following function, which takes as input the width and height: makeWorld : : (Int, Int) -> ((Int, Int), [[Bool]]) makeWorld dims@ (w,h)=( dims, replicate h (replicate w False)) In "src/MP2b.hs" you will find declarations for functions you must complete to implement the Game of Life. The first two functions compute the number of live neighbors of a cell and the next world state: To animate the game, you will need to write a function that returns a picture for a given world state: The picture of the world should be scaled to fit the fixed size 500500 output window, with cells depicted as squares in a 2D grid, shaded black/white (or whatever color scheme you choose, provided it has reasonable contrast) to indicate their status. Here's a screenshot of a running game: To do your drawing, you will likely need to use the following functions from the Gloss library: color, rectanglesolid, scale, translate, pictures. Consult the Gloss documentation for details. The window coordinate system has (0,0) centered in the window, and you will need to map your cells onto that coordinate system. To keep things consistent, we ask that you map cells as shown below (the blue lines depict the window coordinate system, while the red lines delineate a 55 cell grid -- your implementation should work for grids of arbitrary size up to 5050) The easiest way to draw your world is to draw unit (1x1) squares for each cell starting at (0,0), then translate and scale the entire resulting picture to fit inside the window. The following code, for instance, produces the picture below it: Finally, you will implement a function that processes events in the window affecting the world: We provide a starter definition for the handleEvents function for you, shown here: handleEvents :: Event (( Int, Int), [[ Bool ]])( Int, Int), [[ Bool ]]) handleEvents (EventKey (MouseButton LeftButton) Up (mx,my)) world = undefined handleEvents _ world = world The only event we're interested in is the left mouse button (or trackpad) click. When the button is released, this function will be called with the event for which we include a pattern match. The window coordinates where the click occurred (mx,my) are found in the event, which you must map onto your cell grid. Clicking on a cell forces it to be alive in the resulting world (the rest of the world is unchanged). When you're ready to test your animation and/or event handling code, run after a successful build. If you wish, you may tweak the starting world state specified in "app/Main.hs" (it defaults to starting with a barren landscape of 2525 cells)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Prepare an ID card of the continent Antarctica?

Answered: 1 week ago

Question

What do you understand by Mendeleev's periodic table

Answered: 1 week ago