Assignment 9: Connections
Goals: Design the Connections game using mutable worlds, ArrayLists and loops
9.1 Instructions
This is a partner assignment.
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 in two parts by the deadlines 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 make sure to submit at least a few minutes before the deadline.
As always, be sure to follow the Design Recipe and avoid accessing fields of fields, getters, type-checking and casting. Dynamic dispatch is your friend!
You will submit this project twice. For Part 1, you must have completed the tasks of defining your world class with any auxilary classes needed and rendering the game.
For Part 2, in addition to rendering the game, you must complete the rest of the gameplay, plus being able to reset the game.
Part 1 Due: Thursday March 27th at 9:00pm
Part 2 Due: Monday March 31st at 9:00pm
The javalib library
The javalib library provides the support for the design of interactive games and creating images composed by combining geometric shapes as well as image files. See The Image Library for more information.
To use the library, download the javalib file above and add it to your project the same way you have added the tester library.
At the top of the .java file where the library is used, add the following import statements:
import tester.*; // The tester library import javalib.worldimages.*; // images, like RectangleImage or OverlayImages import javalib.impworld.*; // the abstract World class and the big-bang library import java.awt.Color; // general colors (as triples of red,green,blue values) // and predefined colors (Red, Green, Yellow, Blue, Black, White)
Note that you should use impworld which uses mutation in all of the event handlers except for makeScene.
Connections
Implement the game Connections. Connections is a word game where the player must group 16 words into 4 groups that share a common theme. Each group is assigned a color: yellow, green, blue or purple. The yellow category is considered straightforward and the categories increase in difficulty where purple is considered to be the most tricky. Each group has a short label describing what the words had in common.
For instance, a game from last year presented the following words:
The heart with the number 4 indicates that there are 4 tries left. You should also include some indication of the number of tries left, although you are not required to use a heart image.
After an unsuccessful try, the number of tries should decrease by 1 (provided there is more than 1 try left):
If there are no more tries left, the game should end. The player should be able to reset the game by pressing the ’r’ key.
If the player has made a successful guess, the grouped words should be shown with the label and color for the group.
To win this game, the words needed to be grouped in the following 4 groups within 4 tries:
This website holds an archive of past Connections games: https://connectionsplus.io/
For this assignment, you should have at least 5 sets of 16 words to play with. You can take these sets from the archive or you can make up your own. When the game starts, it chooses one of the sets randomly.
Here are some tasks to help you get started:
Part 1:
Review the The Image Library documentation to understand how impworld works.
Decide if you need to define any constants for the game. These are things that do not change throughout the life of the program, such as height and width of the game’s background. Review Lecture 10: Customizing constructors for correctness and convenience about defining constants.
Design the ConnectionsWorld class. This should extend the World class from javalib. What fields should be included in this class? The field(s) of this class will represent what can change in the world program. For this assignment, you should use ArrayLists and loops.
Design and implement the makeScene method which is required in the ConnectionsWorld class. This will draw the current state of the world. Note that the makeScene method is the only one event handler in impworld that is not a void method, however placeImageXY for WorldScene is void.
Part 2:
- Decide which events your ConnectionsWorld class should handle.
Does the game need to respond to the passage of time? Then you need to override onTick in the ConnectionsWorld class.
Does the game need to respond to key presses? Then you will need to override one of the key handler methods.
Does the game need to respond to mouse clicks Then you will need to override one of the mouse handler methods.
Review the The Image Library to see the documentation for the event handler methods that are available.
Your game should end if all words have been grouped correctly or the player has run out of tries. There should be a final scene indicating whether the player has won or lost the game. Study The Image Library documentation for information about ending world programs.
9.2 Extra credit
If you want to earn extra credit on this assignment, you can complete any number of enhancements.
Enhancements will only count towards extra credit if they are convincingly and thoroughly
tested. Moreover, the rest of the game
must be at least as well tested —
Here are some examples:
Enhance the graphics.
Display a message if the player correctly guessed 3 out of 4 of the words in a group.
Add a button to allow the player to shuffle the words to display them in a different order.
Add a button to allow the player to deselect all selected words.
If the player wins, tailor the win message based on how many tries the player used.
You are welcome to think of other enhancements to the game. Have fun!
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. If you did any extra credit, add descriptions of the features you added to make it easier to grade.