Giter VIP home page Giter VIP logo

dice's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.