CPSC 124 Introduction to Programming Spring 2008

Programming Assignment 3: Snake Pit Game - Part II

Due date: Thursday, May 8th at 5pm

Introduction

The programming projects are an opportunity for you to work on a larger program, where you create more of it on your own from scratch. To begin, copy the files from /classes/s08/cs124/projects/project3 to your home directory. This directory should contain five files: SnakePit.java, Snake.class (my working Snake class file), SnakeCell.java, Apples.java, and GameBoard.java. SnakePit.java and SnakeCell.java are simple classes, which are already completed. You completed Snake.java in project 2, and for this project, I've given you my Snake.class file. In this project you will complete Apples.java and GameBoard.java, which already contain some skeleton code.

There is a directory within the project3 directory called working. As in project 2, this contains a working program that you can run. This will allow you to see how the program should work. Your program should eventually work like this one.

For an overview of the game and a description of the structure of the program see the writeup for project 2. Below are some details that are specific to this assignment.

The interface for all of the classes is available here.

Apples Class (must implement for project 3)

The Apples class models the apples on the board in the same way that the Snake class models the snake on the board. It provides methods for determining what positions contain apples and for eating apples. The variables and methods of the Apple class are discussed below. Note: you may find that you need to add other variables and methods in addition to the ones described below.

GameBoard Class (must implement for project 3)

The GameBoard class handles the program graphics as well as the game engine (the logic for playing the game). To display the graphics, it extends JPanel and overrides the paintComponent() method to draw the board onto the panel. It creates a window (JFrame object) and adds itself (the 'this' object) as the center panel. It also listens for three types of events: button clicks (for starting the game), key pushes (for moving the snake), and timers (for periodically moving the snake in the current direction). The variables and methods of the GameBoard class are discussed below. Note: you may find that you need to add other variables and methods in addition to the ones described below.

Testing

Use my .class files to run the program and see how it's supposed to work. To run the program, cd into the directory working/ and then use "java SnakePit". Your program should eventually work like mine.

Incremental Programming

When you do project 3, you should write the code it incrementally. Start by writing the Apples class. When you finish the Apples class you should test it out before moving on to the GameBoard class. You can copy the file GameBoard.class from the working/ directory to your project 3 directory and then run the program to see if it works correctly. It should run like the program in the working/ directory.

The GameBoard class is a large enough class that you should also break it up into steps. You should start by writing the constructor. First, get the window to display with all of the appropriate components such as panels, buttons, text fields, etc. (your window should look exactly like the program in the working/ directory). Note: you will not see the board at this point since paintComponent() isn't implemented. Then implement initBoard() and paintComponent(), and get the board initially drawn correctly whenever initBoard() is called (i.e., draw yellow walls as well as black, red, blue blocks). Finally, implement the other methods to get the full program working.

Extra Credit

There are lot of opportunities to earn extra credit on this programming assignment. Here is a list of some. This list is by no means exhaustive. If you decide to try for extra credit you should make two versions of SnakePit. The base version that meets the requirements above and the extra credit version. Submit these in two separate directories within your project3 handin folder.

Collaboration and All That

Remember the collaboration policy for individual programming assignments: This assignment is to be completed by you. You are not allowed to talk to other students about any aspect of this assignment, including general plans or specific debugging problems. You may ask a tutor for debugging help only and must indicate in writing who helped you and explain how they helped. You may talk to me as much as you want about any aspect of the assignment - general strategy, programming details, debugging help, etc.

Documentation

Your programs should be heavily commented. At the start of the Apples class and GameBoard class should be a multiline comment describing the class and indicating the author. At the start of all methods should be a description of what the method does, what it's inputs are (i.e., parameters), and what it returns. You should also comment all complicated statements within the methods. You should also properly indent all of your code and use indicative variable names.

In addition, you should write up how your program works in a file called SnakePit.txt. This should be at least a half of page describing in detail how your program works. You can focus your write-up on the Apples and GameBoard classes. Your write-up should also describe how any help you received from tutors, teaching assistants, or the instructor.

Handin

Verify that your project3 folder contains all the files you modified, then copy your entire folder project3 to the handin directory ~mcorliss/handin/cs124/username (where username is replaced with your username).