/Dice

An AP Computer Science A programming assignment for Mr. Simon's APCSA course at Lowell High School, San Francisco, USA

Primary LanguageJavaScript

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. Note that for full credit your program must display the total of all the dice and draw the dice with dots or similar marks. Also, you must customize the title, header and footer of index.html. You may find the slides of the OOP--Classes slide presentation helpful, as well as the Nested Loops, and the Math.random worksheets.

Suggested steps to start the assignment

  1. Start by forking this repository. Open the pde file and you should see the following code:
        void setup()
	{
	    noLoop();
	}
	void draw()
	{
	    //your code here
	}
	void mousePressed()
	{
	    redraw();
	}
	class Die //models one single dice cube
	{
	    //member variable declarations here
	    
	    Die(int x, int y) //constructor
	    {
	        //variable initializations here
	    }
	    void roll()
	    {
	        //your code here
	    }
	    void show()
	    {
	        //your code here
	    }
	}
  1. Write the member variable declarations for the Die class
  2. Then, write code in the constructor to initialize the member variables
  3. Next, complete the draw() function. For now it should:
    • make a background() on the screen
    • declare and initialize one instance of the Die class
    • Call the show() function for that Die (even though we won't see anything yet)
  4. Now lets work on the show() function.
    • Notice that the constructor takes arguments. We'll use those arguments to position the individual die cubes. Don't worry about the dots at first, just get the shape of the dice on the screen for now.
    • Once you like the shape of your die, go back to show() and add some if statements to check how many dots you need to put on the die. Start by "forcing" the die to always roll a one. Check to see that you can get one dot where it is suppose to be, and move on to two, and so on. If you are clever, you can combine some of the ifs and avoid duplicate code.
  5. Now, use nested loops to display 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. Your show() function will need to position the dots by adding some small amount to the x and y coordinates of the rect() of the Die
  6. Finally, add code to the draw() function so that your program displays the total for the roll to the screen. You may find the Coin and Token programs on the slide presentation helpful.

Program requirements

The steps above are only a suggestion. Your program needs to:

  1. Use a class to model a single Die cube
  2. The Die constructor needs to use the two arguments to position the x and y coordinates of the Die cube
  3. Create at least 9 instances of the Die class
  4. Use Math.random() for all random numbers in the assignment
  5. Display the total of all the dice and draw the dice with dots or similar marks
  6. Personalize the title, header and footer of index.html

Other than that, your dice program doesn't have to work or look like any other. Have fun and be creative!

Optional Extras

For a challenge, you might see how many legible dice you can fit on the screen. You can also keep track of the rolls. You could display the average roll, or maybe a graph that shows how often each of the numbers from 2 to 12 have 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

Keneth
Chris
Michelle
Kaitlin
Tiffany
Tyler
Gui
Andrea
Devin
Elsa
Aiden
Damian
Cassidy
Justin
Elton
Marjkelly
Luna
Teodora
Kazu
Vassili
Kang
Jason
Melanie
Rafael
Kang
Ian
Allister
Claire
Kai
Shannon
Jasmine
Tomas
Matt
Marlon
Sam
Xinjian
Yanzhu
Jasper
Lucas
Maanav
Andrew
Tommy
Marvin
Dylan
Phuoc
Claire
PJ
Anna
Kimia
Robert
Fiona
Joshua
Fred
Richard
Thomas
Ethan
Jalen
Akira
Celina
Emma
David
Esther
Sean
Caitlin
Brendan
Mia
Andy
Alexis
Jason
Calvin
Mampu
Lolita
Gee
Cappillen
Brendan
Michael
Alvin
Dylan
David
Michael
Racine
William
Andrew
Mariko
Amy
Andy
Ethan
Jessie
Peter
David
Daniel
Vicky
Brandon
Lu-Aung
Wesley
Carmen
Marisa
Willis
Marvin
Zachary
Katie
Wilson
Josephine
Kris
Jessie
Domonic
Lena
Emi
Lancy
Mandy
Brian
Kelvin
Jing
Emil
Kelly
Chanel
Emma
Jeffrey Emily
Albert
Connie
Andrew
Cynthia
James
Linus
Lily
Alexander
Oona
Alexander
Tristan
Victor
Karlee
Emily
Clarisse
Chase
Kevin
Maryann
Jeffrey
Michelle
Justin
Ben
Jeffrey
Vincent
Jonathan
Ryan
Rolland
Bryce
Aiden
Akemi
Alvin
Andrew
Austin
Breanna
Bruno
Christina
Christopher
Damian
Edward
Emily
Eric
Haden
Irisa
Ivana
Jason
Jeffrey
Joanne
Joshua
Karina
Matthew
Van
William
Wing
Yejin
Andrew
Brian
Caden
Caleb
Daniel
Darren
David
Eric
Hayden
Jacob
Joseph
Joshua
Joyce
Justin
Justin
Lexian
Liam
Luke
Nathaniel
Nicolas
Noella
Rafael
Stella
Stephen
Tommy
Vivian
Wendy
Aaron
Andy
Annie
Audrey
Brennan
Christina
Deion
Emely
Gabriel
Emily
Ivan
Jackie
Jason
Joselino
Koen
Kyle
Mika
Nate
Nathan
Nathan
Noelle
Pyry
Ryan
Tobias
William
Willa
James
Kathleen
Juan
Toby
Ryan
Evangeline
Miriam
Ethan
Loren
Sally
Tennyson
Allynaraizel
Alvin
Sonia
AndreiRock
Maxwell
Humphrey
Hannah
Piero
Fiona
Isaac
Karla
Saw
Aiden
Tara
Maya
Dylan
Nicholas
Samson
Diego
Luca
Anna
Laura
Noel
Ben
Samantha
Charlotte
Dylan
Joanna
Victor
Ivan
Kaijun
Michelle
Daniel
Jerry
Rock
Gage
Angela
Diego
Ivona
Sandy
Jonathan
Robin
Kayla
Justin
Leanna
Philix
Audrey
Emily
Haoheng
Gabriel
Austin
Tania
Evan
Xiaoyi
Marie-Fleur
Richard
Supath
Amy
Lisa
Jeffrey
Garvin
Ethan
Ekaterina
Cole
Alice
Raymond
Jocelyn
Davyn
Francisco
Lucas
Jenny
Justin
Jason
Amiya
Ngoc
Yuki
Niko
Naomi
Caspar
Charlene
Stanley
Ella
Noah
Yasmine
Ben
Wingyan
Joseph
Alejandro
Angela
Kelvin
Lily
Aaron
Terrance
Jessica
Grace
Matthew
Leanna
Andre
Felicia
Andrew
Calvin
Aaron
Justin
Kyla
Calvin
Alessandra
Yuxi(1)
Yuxi(2)
Aaron
Hao
Katie
Benjamin
Matthew
Kaitlyn
Wanyan