Computer Science 252
Problem Solving with Java

Fall 2013, The College of Saint Rose

Lab 5: Boxball
Due: 11:59 PM, Thursday, October 24, 2013

Your task for this lab is to implement a game called Boxball, where players attempt to drop a ball into a box from above a minimum height line. You may work alone or with a partner on this lab.

When the game begins, the playing area is displayed along with three "buttons" that allow the player to select a level of difficulty. The player drops a ball by pressing the mouse in the region of the playing area above the minimum height line. If the ball falls completely within the box, the box is moved to a new random location. Otherwise, it remains where it is. In either case, the player may go again, dropping another ball.

A working solution for this program will appear below. Click inside the applet to interact with it.



To help you get started, we have provided a starter BlueJ project with the class headers and some additional information. The starter contains three classes. Boxball.java is the WindowController extension named that will control the play of the game. Ball.java is the ActiveObject extension that will represent a ball falling toward the box. Box.java is a class that draws and controls the target box.

General Design

The Boxball class is responsible for drawing the playing area and handling mouse events. If the player presses on an easy, medium, or hard button, the starting line should move to the appropriate height and the box should be told to resize. If the player presses within the playing area above the starting line, a new ball should be created with its center at the press point and dropped from that height.

The Box class will allow you to create and manipulate the box at the bottom of the playing area. A box is responsible for knowing how to move to a new random location when the ball lands in the box. It also is responsible for changing size if the player changes the difficulty level.

The Ball class represents a ball that falls down the screen, checking if it lands in the box when it reaches the bottom of the playing area. The player should then be told whether the ball landed in the box. The ball should then disappear. If the ball lands in the box, the box should move to a new location. As part of the advanced functionality, your ball, which you should first implement to fall at a constant rate, should be subject to some acceleration due to gravity and to some minor wind, to make the game a little more interesting.

Detailed Problem Description

This section provides a detailed description of the program requirements and outlines an approach to tackling the problem.

Basic Setup

Lay out your playing area and difficulty buttons. The layout should adjust based on the starting canvas dimensions. Add the capability to have the level change when the buttons are pressed (at the start, this is just the height of the starting line). This should all happen in Boxball.java.

Adding the Box

Next, implement the Box class that controls the box object at the bottom of the playing area. The Box is seen as a FilledRect, but you will need to be careful to pass the needed information to its constructor. Its horizontal position will change over time, but its vertical position will always be the same. (Think: what are the extreme left and right values for its horizontal position?)

The width of the box changes depending on the difficulty level. Its initial width should be set in the constructor and should correspond to the "easy" level.

When your Box constructor is finished, update your Boxball class to construct it as part of its setup.

The default difficulty is "Easy". If the player presses on the "Medium" or "Hard" button, the box should get smaller (or much smaller). The Box needs a method, setSize, to allow its size to change. Write this method and modify your Boxball class to call it at appropriate times with the appropriate parameters so the width of the box changes when the level changes.

Dropping a Ball

We create a Ball, which is an ActiveObject, when the player presses in the playing area above the starting line. The constructor for the Ball class should draw a ball at the appropriate location and start it moving.

You need to think carefully about what information a ball needs to know to construct itself properly. Recall that Boxball knows how big the ball should be (so that the ball and the box can be sized appropriately). Boxball also knows where the mouse was pressed. This should be the starting location for the ball. You need to pass this information to the Ball constructor so that it can draw itself and fall. The code to perform the falling is similar to many of our class examples.

Once you have written the Ball constructor and its run method, modify your Boxball class to construct a ball when the player presses above the starting line in the playing area. Do not worry yet about whether the ball falls in the box. Just check that it is drawn at the right starting location and that it makes its way to the bottom of the playing area.

Checking the Box

When the ball reaches the bottom of the playing area, it needs to compare its location to the box's location. To accomplish this, the Box class needs to provide methods getLeft and getRight that give the positions of the edges of the box. To call those methods, the Ball class must know about the box. Modify your Ball constructor to take the Box as an additional parameter.

If the ball lands in the box, you should display a message like "You got it in!". If not, display something like "Try again!". Since the Boxball controller is responsible for the layout of the game, it should construct a Text object that displays a greeting message. The Text object should be passed to the Ball constructor as a parameter so that the ball can change the message appropriately when it hits or misses the box.

Finally, pick a new random location for the box when the player gets the ball in the box. The box should be responsible for picking the new location and moving itself, but it is the ball that knows when it lands in the box. To do this, add a method to the Box class called moveBox that is called by the ball when it lands in the box.

Submitting Your Work

Before 11:59 PM, Thursday, October 24, 2013, submit your Java program for grading. There are three things you need to do to complete the submission: (i) upload a copy of your Java program (the .java files only; submit each file separately) using Submission Box under assignment "Boxball", (ii) print a copy of your program and hand it to your instructor, and (iii) demonstrate the execution of your program for your instructor (2-day grace period for demos).

Don't forget to check your programs for compliance with the Style Guide for CSC 252 Programs

Grading

This assignment is worth 50 points, which are distributed as follows:

> FeatureValueScore
Style, Design, and Efficiency
Appropriate comments 4
Good variable names and declaration types 4
Good use of constants 4
Appropriate formatting 2
Does not generate new objects unnecessarily 2
Good overall design 4
Correctness
Initial playing area drawn 4
Starting line moves according to level 2
Box size changes according to level 2
Ball created only on presss in playing area above starting line 4
Ball animation 4
Correct detection of ball in box (or not) 6
Box moves to random location when ball lands in 4
Message updated correctly (remaining centered) 2
Box may be in any valid location, never in a bad location 2
Extra Credit (up to 4 total)
Total 50