Assignment 5: Games, Equality for Unions
Goals: Design a game, explore the complexity of defining equality of two objects in a union.
5.1 Instructions
the names of classes,
the names and types of the fields within classes,
the names, types and order of the arguments to the constructor,
the names, types and order of arguments to methods, or
filenames,
Make sure you follow the style guidelines that we enforce. For now the most important ones are: using spaces instead of tabs, indenting by 2 characters, following the naming conventions (data type names start with a capital letter, names of fields and methods start with a lower case letter), and having spaces before curly braces.
You will submit this assignment by the deadline using the online submission system. You may submit as many times as you wish. Be aware of the fact that close to the deadline the system may slow down to handle many submissions - so try to finish early.
There will be a separate submission for each problem - it makes it easier to grade each problem, and to provide you with feedback for each problem you work on.
The submissions will be organized as follows:
Homework 5 Problem 1: All the files needed for your game, in one .zip file
Thursday, February 23rd, 9:00pm
Practice Problems
Work out these problems on your own. Save them in an electronic portfolio, so you can show them to your instructor, review them before the exam, use them as a reference when working on the homework assignments.
Problem 20.2 on page 296
Problem 20.6 on page 305
Problem 20.6 on page 305
Problem 20.8 on page 306
Problem 20.9 on page 306
Problem 21.1 on page 312
Problem 21.3 on page 312
Problem 21.4 on page 315
Problem 21.5 on page 320
Problem 21.6 on page 320
Problem 21.7 on page 320
Simon Says
Implement the game Simon Says. Simon Says is a memory game with four colors, where the game generates a random sequence of the colors and the player needs to replay those colors in order. On each successful playback, the game generates another color and lengthens the sequence. Some design suggestions:
Your game consists of two phases: (1) generating a sequence of lights and displaying it to the player, and (2) accepting user clicks and checking whether their clicks so far match the intended sequence. You can choose whether to implement these as two distinct subclasses of World, or as a single subclass of World that has a bunch of extra fields. Both designs have advantages and disadvantages, so either might be acceptable. Please be sure to explain in a comment in your code why you chose the one you did.
You may need a method that checks whether this list of buttons begins with the given list of buttons. Implement any methods that check for sameness of union data using double dispatch.
The Color class has methods .brighter() and .darker() that give you lighter or dimmer versions of an existing color, which may be of use in drawing active and inactive buttons. You may choose any four colors, as long as they are easily distinguishable.
We recommend starting your game via yourWorld.bigBang(SOME_WINDOW_WIDTH, SOME_WINDOW_HEIGHT, 0.5), so that your game ticks twice per second. You can speed that up if you want the game to be more difficult. You cannot change the tick rate while your program is running, though; the javalib library doesn’t support that ability.
Look at the documentation for the onMouseClicked, onMousePressed and onMouseReleased events – you will want to use one or more of these methods in your game.
Your game should end if the player does not replay the current sequence of colors. There should be a final scene indicating that the player has lost the game.
You may use the starter code provided in Simon_Starter.java if you wish.
A note about randomness
There are two ways to generate random numbers in Java. The easiest is to use Math.random(), which generates a double between 0 (inclusive) and 1 (exclusive). You can multiply this number by some integer to make it bigger, then coerce to an int to produce a random integer in the range you wish. However, this is not easily testable: you’ll get different random values every time.
The better way to generate random numbers is: First, import java.util.Random at the top of your file. Next, create a new Random() object, and use its nextInt(int maxVal) method, which will give you a random integer between zero (inclusive) and maxVal (exclusive).
This is known as a "pseudorandom number generator", since the numbers aren’t really random if they can be reliably repeated...
One of them, to be used for testing, should take in a Random object whose seed value you specify. This way your game will be utterly predictable every single time you test it.
The second constructor should not take in a Random object, but should call the other constructor, and pass along a really random object:
import java.util.Random; import javalib.funworld.*; class YourWorld extends World { Random rand // The constructor for use in "real" games YourWorld() { this(new Random()); } // The constructor for use in testing, with a specified Random object YourWorld(Random rand) { this.rand = rand; ... } } Now, your tests can be predictable while your game can still be random, and the rest of your code doesn’t need to change at all.
Submission notes
UserGuide.txt should be a short file describing how to play the game.
Design.txt should describe each of the classes in your design, and what they all do. If you think it’s helpful, include an ASCII-art class diagram to show the relationships between the classes.