Homework 9

Due Tuesday 6-April, at 10pm


For this hw, you may submit up to 999 times, but only your last submission counts.

While you may submit to Autolab as often as you like for this assignment, there is no autograded portion, so you will be responsible for testing your code and making sure it meets the problem requirements. As stated in the style guide, you do not have to write test cases for interactive, random, graphics, data initialization or event functions. Instead, you should test by visually inspecting your code’s behavior as you complete steps of each problem, where reasonably possible. This will make debugging your code much easier.


  1. Sudoku Animation [50 pts] [manually graded]
    Write an animation that displays an interactive game allowing the user to play Sudoku. You can (and should) make use of the Sudoku related code you wrote for previous homeworks, especially the SudokuGame class and drawSudokuBoard.

    For this animation, we will not require you to follow a specific appearance or a specific approach. You may build your Sudoku game any way you like, as long as it meets the following requirements:

    1. You must have a function called starterBoard() which returns the starter board for the game (defined as a 2D list of integers). You should then call starterBoard() in the appStarted function to set up the initial board. You can then test the game by returning different boards in starterBoard. In fact, this is part of how we will test your code!

    2. The game must start by displaying the full N2xN2 grid (in the format of a standard Sudoku board) and filling in the numbers already included in the starter board. Note that this doesn't need to be a 9x9 board- a 16x16 board should work too! (Note that it is OK for the game to break down if the starter board is too big to fit on the screen, like a 100x100 board. However, 9x9 boards and a 16x16 boards should not be too big for the game).

    3. At all times, a single cell on the board is highlighted (using either a different color or different outline than the rest of the cells). The player can change the highlighted cell by clicking on a new cell with the mouse, or by moving from the current cell with the up, down, left, and right arrows. Note that the game must support wraparound: typing up from row 0 leads to row N2-1, typing left from column 0 leads to column N2-1, typing down from row N2-1 leads to row 0, and typing right from column N2-1 leads to column 0. If the user clicks outside of the board, the highlighted cell should not change.

    4. To make a move, the player can press a single digit key to insert a number into an empty square. The move is only allowed if it will still result in a valid board. (Luckily, your code for SudokuGame already handles this.) The player can also clear the number from the highlighted square by entering a 0 or hitting backspace.

    5. The player can press 'u' to undo the last move. They can press 'r' to redo the last move that was undone.

    6. Initial numbers (squares that were filled in before game play began) should be a different color than numbers added by a player. In addition, the player cannot modify initial numbers.

    7. If, after a move, the player has properly filled in the entire board and won the game, a message should be displayed congratulating the player. After this, all further key presses and mouse presses should be ignored.

    Note: There are many tiny details left unanswered here (how large should the board be? what colors should I use? Exactly what should the board look like? should the blocks be outlined? what font for the numbers? etc, etc, etc). You have to decide them for yourself. Do not ask on piazza, do not ask at OH. Just decide. Keep it simple- we are not looking for anything amazing here, just a simple playable game that follows the rules above. Have fun!

    Addendum 1: If you want to test whether you can win the game properly, but don't want to play lots of Sudoku, consider testing with an almost-complete board such as this one:

    [
      [ 1, 2, 3, 4, 5, 6, 7, 8, 9],
      [ 5, 0, 8, 1, 3, 9, 6, 2, 4],
      [ 4, 9, 6, 8, 7, 2, 1, 5, 3],
      [ 9, 5, 2, 3, 8, 1, 4, 6, 7],
      [ 6, 4, 1, 2, 9, 7, 8, 3, 5],
      [ 3, 8, 7, 5, 6, 4, 0, 9, 1],
      [ 7, 1, 9, 6, 2, 3, 5, 4, 8],
      [ 8, 6, 4, 9, 1, 5, 3, 7, 2],
      [ 2, 3, 5, 7, 4, 8, 9, 1, 6]
    ]
    

    Addendum 2: You may solve this problem any way you wish. That said, here are some hints/suggestions for one approach to solving the problem. Use this or not as you wish. Good luck!
    Note: These videos do not demonstrate mouse functionality or undo/redo, but they're still a pretty good guide.

    1. Read the problem
    2. Display the board
    3. Improve the board display
    4. Highlight a cell
    5. Show initial numbers
    6. Handle moves
    7. Win the game

  2. Tetris [50 pts] [manually graded]
    Write Tetris according to the design given in this step-by-step tutorial. You may not use a different design, even if you think there's a better way to do it (there probably is, but you still have to do it this way). This may seem limiting, but sometimes you have to write code according to a specific algorithm, rather than writing code to solve a specific problem.

    To get full credit, you'll need to complete the basic implementation according to the design spec.