Skip to content

Jackkamb/Dice

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 

Repository files navigation

Dice

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.

  1. First put the basics in the Die class. 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)
  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's draw method is empty right now, so nothing will be drawn. Add code to the draw method of the Die class, 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.
  2. Once you like the shape of your die, add one dot to it. Right now your die always has a value of 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 the Die.
  3. Adapt the constructor so it randomly chooses a value (1-6) every time a new Die is created. Add some ifs to the draw method to draw the correct number of dots on the face of the Die. If you are clever, you can combine some of the ifs and avoid duplicate code.
  4. Now in the drawing section of the DiceRoller class, use nested loops to create and draw at least nine instances of the Die class. 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.
  5. Finally, add code to the drawing section of DiceRoller so that your program displays the total for the roll to the screen. (How will the DiceRoller class know what the value of each Die is?)

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.

Optional Extras

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.

Samples of Student Work

Dean
Sharon
Ben
Gina
Eric

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%