Lab 3 Part 3: Putting it all together

 

Question 1

Now, you are finally ready to implement the full 1D version of "Lights On". Modify your code so that the game play is as it should be (i.e., when you toggle one light, the lights next to it also toggle). Remember, lights do not "wrap around". That is, the lights at the edge of the board have only one neighbor. The function runGenerations( L ) should then play the game from the initial starting list L.

Suggestions: Only setNewElement needs to change in order to implement this game. You will need to compare the value of i, which is checked for each light in the row, and x, which is the user's choice.

 

Starting from a random binary list

Question 2

Create a function named

randBL( N )

that takes in a nonnegative integer, N, and returns a list of length N in which each element is equally likely to be a 0 or a 1. Raw recursion is one way to handle this; list comprehensions are another.

This randBL function makes it easy to start a new, random game. Try running

runGenerations( randBL(9) )

to be sure it works.

 

Question 3: The final 2D version

The show command will display 2d grids of lights as well as the 1d rows that this lab has used thus far. For example, in your Python shell try

 

>>> show( [ [0,1,0], [1,1,0], [0,0,1] ] )

 

You will see a three-by-three grid of lights appear. Each element of the input list to show is, itself, a row of the resulting grid of squares. The rows are plotted from-low-to-high in the window.

For this question, implement the 2d game of lights out by writing a new function named runGenerations2d( L ). Clicking on a square should change its neighbors (if any) to the north, east, west, and south. You will need to rework some of the helper functions in order to implement run2dGenerations -- rather than write over or redefine your existing functions, create new names for them, For example, you may need

1. evolve2d

2. setNewElement2d

3. allOnes2d

4. randBL2d

You will also need the function sqinput2() which returns 2d data, as follows:

x, y = sqinput2()

Warnings:

Make sure you understand exactly how the displayed squares and their coordinates correspond to the positions in the list

If you copy and paste your code (recommended) be sure to change all the function calls within each function to their 2d counterpart.

Letting the computer play...

 

Question 4 (Optional for up to 5 extra credit)

Don't be selfish -- Write new versions of evolve and evolve2d so that the computer gets to play! That is, have computer choose (randomly) one of the possible indices and the game continues until it's solved. Warning: one-sixth of all binary lists are dead ends! That is, there is no combination of toggled lights that will end up at the all-ones list. So, letting the computer play for a long time on one of those dead-ends will result in Python running out of memory.

 

Submission of hw3pr1.py

That's it!

Before you do leave, be sure to submit your hw3pr1.py file under homework 3, problem #1 at Canvas .

Good luck with the other problems on this week's HW!

 

Next

hw3pr2

Lab 3

Homework 3