Dice Java Game Download

0 views
Skip to first unread message

Dibe Naro

unread,
May 9, 2024, 10:21:33 PM5/9/24
to leyredfsimpdi

I'm making a dice rolling game! 2 dice will be rolled and 2 random numbers between 1-6 will be generated. The sum will be taken from the 2 numbers and used to decide what is next. If user's sum is 2,3,12 then they lose. If the sum is 7,11 then they win. If sum is 4,5,6,8,9,10 then the program automatically rolls the dice again until the user wins or loses. Also, after every sum displayed, underneath, display the amount of games they have won/lost. Here is my code so far:

dice java game download


DOWNLOAD ››› https://t.co/wYbR32sFRy



I'm making a java program where a random dice face is selected and displayed. Ultimately, I want to show five dice side by side in the console. I have made some strings that are ascii representations of the 6 dice faces.

Here's what I came up with. There's a Die class that lets you create an Iterator through the different lines, and then we get a list of these Iterator's and print out the first line for each of the dice, then the second line, etc.

The last part reads "Create a test main method for the Dice class that creates a dice and rolls it many times. Can you keep track of how many times a number comes up? Describe how or implement it in the program."

You will also need to know how to define a class of your own, called Dice with an instance variable numberShowing and instance methods roll and main. You can research these things by Googling something like "java introduction tutorial create class" or similar for each of these.

You will need to create an object of the java.util.Random class. To do this you will need to import this class. Then you need to create using its constructor, and call an appropriate method - be sure to check the API document to understand how the method works.

This is the standard entry point for running a class as a program. You should be able to find an example of this easily by googling any getting started or introduction to java tutorial. (Sorry, I ran out of time).

Working on learning java from an intermediate python background atm! This isn't anything special but I'm proud I have one "completed" thing so far. I say completed loosely as I plan to extend this program as I learn more and become more comfortable with the language. Very fun so far! It's nice having a new challenge :>

Greg Brannon wrote:So you have an array that can hold 6 values, 1 - 6, and a random number generator that generates random values 1 - 6. With two dice, the array will have to be scaled to hold the values 2 - 12 (11 total), and your random number generator will have to be modified to generate values from 2 - 12. Modifying the code you've already written should be relatively easy.

I know what a Dice is and I know people that can use the dice to roll it, but calling them "DiceRoller" would be strange and I cannot imagine what a DiceRoller would be otherwise, so I would suggest renaming the class in a way that isn't that much concerned about the behaviour of the class.

You've hardcoded the dices and this is bad for you and the user of your class. If I were to use your class, I would wonder, why there isn't a, for example, thousand dice. It doesn't differ from the others based on the logic. It would be just another number, but you hardcoded the different variants into the enum and into the methods names. Additionally, you have code duplication with that practice. Your d4 method, for example, is nearly the same as the d6 method. You should make it more general.

Exercise 5.1:In all versions of the PairOfDice class in Section 2,the instance variables die1 and die2 are declared to bepublic. They really should be private, so that they are protectedfrom being changed from outside the class. Write another version of the PairOfDiceclass in which the instance variables die1 and die2 are private.Your class will need methods that can be used to find out the values of die1and die2. (The idea is to protect their values from being changed from outsidethe class, but still to allow the values to be read.) Include other improvementsin the class, if you can think of any. Test your class with a short program thatcounts how many times a pair of dice is rolled, before the total of the two diceis equal to two.

After a PairOfDice object has just been created, the number on each die is definitely between 1 and 6, just like the number on a real die. Can we be sure that this will always be true? Not if the instancevariables die1 and die2 are public, sincethey can be changed from outside the class. There is nothing to stopsomeone from changing them to 42 and -17 or anything else. It's not good enough tosay that you're not supposed to do that with dice. I want an absoluteguarantee that my dice objects can only have the values that real dicecould have. By making die1 and die2 private,I can have that guarantee, because the code that I write in thePairOfDice class is the only code that will ever affectthe values of the variables.

So, we will make die1 and die2 private, and addinstance methods getDie1() and getDie2() to returnthe values of die1 and die2. As for otherimprovements, I can forsee that people who use my class will oftenbe interested in the total on the dice, so they will tend to saythings like "dice.getDie1() + dice.getDie2()"a lot. If this is going to be done over and over, why not providea method in the class to do it? So, I will also add a methodgetTotal() that returns the total value showing on the two dice. The complete, modified PairOfDice class is shownbelow.

(Here is another improvement that I thought about. We could modifythe roll() method so that in addition to rolling the dice, itwould also return an int value giving the total on the dice.For example, this would allow us to replace "dice.roll();val = dice.getTotal()" with "val = dice.roll()".Since it's legal to call a function with a subroutine call statement,we could still say "dice.roll()" if we just wantto roll the dice without recording the total immediately. However,I decided that it was a little clearer to leave this feature out.)

The main program is easy, especially since we've done thesame problem before without using objects (in Exercise 3.1).Note how the pair of dice object is used. To test whether ornot the total on the dice is 2, I use the test "while (dice.getTotal() != 2)".To show the numbers on the two dice, I use System.out.println("The dice come up " + dice.getDie1() + " and " + dice.getDie2());Remember that a function call such as dice.getDie1() represents a value,so it can be used anyplace where a literal number or variable could be used.There is no requirement that you assign the value returned by the function to a variable.You can use it directly.

The basic turn structure is score, roll, blend or research, then activate any other abilities. You score if you have either the Featured Blend or Rainbow Blend coaster in front of you. marking it on your score sheet. The value of each changes depending on the number of players. Next, you roll 5 dice and may use any abilities you have earned to manipulate them. Then you choose to either research using up to 5 dice of one bean color (value) or use the dice to create a blend.

When you blend, you can create the less valuable Rainbow blend from any set of 5 different beans. It always replaces the current Rainbow blend. You can instead try to form the more valuable Featured Blend. A featured blend is a set of at least 2 beans of the same bean color. The more you have, the better the blend, and ties are broken by bean color. If you have a better blend than the current Featured Blend, you take the Featured Blend coaster and place your dice on it. When you successfully create a Featured or Rainbow Blend, you earn 1 point. However, you can only have one at a time.

Hello Everybody. This program needs to use an array to count the amount of times a certain roll appears on the dice, after the user enters how many dice and rolls they would like to use. I was able to figure out the code to print out the values of each roll, however I'm not sure what I would have to do to use an array as a counter. Any help would be appreciated.

In this example, we import the java.util.Random class and create a new instance of Random called rand. We then use the nextInt() method to generate a random integer. The System.out.println(number); line prints this random number to the console.

The Random class in Java is part of the java.util package. It allows us to generate random numbers, which can be very useful in a variety of scenarios like testing, gaming, or in any situation where you need to simulate a random outcome.

In this code snippet, we first import the java.util.Random class. Then we create a new instance of Random called rand. The nextInt() method is then used to generate a random integer, which is stored in the number variable. Finally, we print the random number to the console using System.out.println(number);.

08ab062aa8
Reply all
Reply to author
Forward
0 new messages