In this assignment we'll practice using OOP (Object Oriented Programming) techniques. We'll create a class to model what a single die has and does, and then we will create at least 9 instances of that class arranged in a grid shape.
Your program must also display the total of all the dice and draw the dice with dots or similar marks.
You may find the slides of APCS-04-LowellDice.pptx presentation helpful, as well as the nested-loops and the math-dot-random worksheets.
Start by forking this repository. You will see two files: Die.java and DiceRoller.java.
- First put the basics in the
Dieclass. For now you should:
- create instance variables for the die's x and y coordinates, the size (pixel length of one side of the square), the color, and the value
- add code to the constructor which sets the x and y coordinates as provided in the arguments, sets the size and color to values of your choosing, and sets the value to 1 (yes, for now it's a cheat die and will always roll 1)
- Now write two lines in the drawing section of the DiceRoller class. The first line will create one new Die and the second line will draw it. The
Die'sdrawmethod is empty right now, so nothing will be drawn. Add code to thedrawmethod of theDieclass, to simply draw the squace face of the die. Don't worry about the dots for now, just get the shape of the die on the screen. - Once you like the shape of your die, add one dot to it. Right now your die always has a
valueof 1, so check to see that you can get that one dot where it is supposed to be, by adding some small amount to the x and y coordinates of theDie. - Adapt the constructor so it randomly chooses a value (1-6) every time a new Die is created. Add some
ifs to thedrawmethod to draw the correct number of dots on the face of the Die. If you are clever, you can combine some of theifs and avoid duplicate code. - Now in the drawing section of the
DiceRollerclass, use nested loops to create and draw at least nine instances of theDieclass. This is the power of OOP. It's not that much more work to make 1000 dice as it is to make one. Make sure the dots are on the dice. - Finally, add code to the drawing section of
DiceRollerso that your program displays the total for the roll to the screen. (How will the DiceRoller class know what the value of eachDieis?)
Notice the DiceRoller class includes mouse and key listeners to redraw when you click or press space.
Have fun and be creative. Your dice program doesn't have to look or work like any other.
Submit your assignment by copying the text of your two .java files into your forked repo, and creating a pull request.
For a challenge, you might see how many legible dice you can fit in a 500x500 window. You can also keep track of all the rolls in each redraw and display an average roll, or maybe a graph that shows how often each of the numbers has come up. This is useful in some dice games like Settlers of Catan. Check the links below for examples of other students work.