I'm making a game for an extra assignment for my college and I have to create a dice game known as "balut" I'm having some issues with assigning values to an array, and having the dice values stored within this array.
I'm in week 9 of 11 of my course we've covered arrays and methods however this is a new concept for me. The goal is as follows:
Balut = all five dice have the same number.
Straight = a total of 15 Or 20.
Sixes = 1 or more sixes.
Fives = 1 or more fives.
Fours = 1 or more fours.
10 rounds.
Total scoring of categories.
total of scores.
If no category is met "none" is printed.
I've put at least 14 hours into this and it was intended to be a 6 to 8 hour program and I still am struggling, questioning if I have the intelligence for the course and am hoping someone here can explain what I'm doing wrong or even what I should be studying.
I've tried creating a single array and assigning all dice values to this array, I run into the problem of when it comes to comparing the values I don't know how to do dice 1 == dice 2 == dice 3 etc.
I've then attempted to make 5 arrays 1 for each dice and use the compare array method which again I can only seem to get it to compare 2 arrays or variables I can't get it to compare all 5 like I'm attempting.
public static void main(String[] args) {
int[] Dicearraytotal1 = new int[10];
int[] Dicearraytotal2 = new int[10];
int[] Dicearraytotal3 = new int[10];
int[] Dicearraytotal4 = new int[10];
int[] Dicearraytotal5 = new int[10];
for (int i = 0; i < Dicearraytotal1.length; i++) {
Integer dice1 = (int) (Math.random() * 6 + 1);
Integer dice1val = dice1;
Integer dice2 = (int) (Math.random() * 6 + 1);
Integer dice2val = dice2;
Integer dice3 = (int) (Math.random() * 6 + 1);
Integer dice3val = dice3;
Integer dice4 = (int) (Math.random() * 6 + 1);
Integer dice4val = dice4;
Integer dice5 = (int) (Math.random() * 6 + 1);
Integer dice5val = dice5;
Dicearraytotal1[i] = (dice1val);
Dicearraytotal2[i] = (dice2val);
Dicearraytotal3[i] = (dice3val);
Dicearraytotal4[i] = (dice4val);
Dicearraytotal5[i] = (dice5val);
Integer total = (dice1val+dice2val+dice3val+dice4val+dice5val);
System.out.println("Total Of Numbers Generated1: " + Arrays.toString(Dicearraytotal1));
System.out.println("Total Of Numbers Generated2: " + Arrays.toString(Dicearraytotal2));
System.out.println("Total Of Numbers Generated3: " + Arrays.toString(Dicearraytotal3));
System.out.println("Total Of Numbers Generated4: " + Arrays.toString(Dicearraytotal4));
System.out.println("Total Of Numbers Generated5: " + Arrays.toString(Dicearraytotal5));
System.out.println("Total: " + total);
You are missing an end bracket to stop the for loop, so I assume you were attempting to print the arrays each iteration of the loop. I cleaned up your code a lot and you should take note on some of the changes in order to organize your code better which will make it easier to understand.
public static void main(String[] args) {
int[] diceArrayTotal1 = new int[10];
int[] diceArrayTotal2 = new int[10];
int[] diceArrayTotal3 = new int[10];
int[] diceArrayTotal4 = new int[10];
int[] diceArrayTotal5 = new int[10];
int[] totals = new int[10];
for (int i = 0; i < diceArrayTotal1.length; i++) {
diceArrayTotal1[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal2[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal3[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal4[i] = (int) (Math.random() * 6 + 1);
diceArrayTotal5[i] = (int) (Math.random() * 6 + 1);
totals[i] = (diceArrayTotal1[i] + diceArrayTotal2[i] + diceArrayTotal3[i] + diceArrayTotal4[i] + diceArrayTotal5[i]);
}
System.out.println("Total Of Numbers Generated1: " + Arrays.toString(diceArrayTotal1));
System.out.println("Total Of Numbers Generated2: " + Arrays.toString(diceArrayTotal2));
System.out.println("Total Of Numbers Generated3: " + Arrays.toString(diceArrayTotal3));
System.out.println("Total Of Numbers Generated4: " + Arrays.toString(diceArrayTotal4));
System.out.println("Total Of Numbers Generated5: " + Arrays.toString(diceArrayTotal5));
System.out.println("Totals: " + Arrays.toString(totals));
}
Additionally I added a totals array that keeps the total for each index instead of printing it every loop like you were doing. You did not add your compare code, so I cannot assist you with that. Let me know if you need any clarification on any changes. I ran this code and it successfully generated the arrays you need and the totals.
public static void main(String[] args) {
int[] results = new int[6]; // This array will hold the number of time each dice was rolled, so for example results[0] is how many 1 s you have results[5] is how many 6 you have
Random random = new Random();
for (int i = 0; i < 5; i++) { // roll the dice 5 times
results[random.nextInt(6)]++; //increase the counter of the appropriate value
}
boolean balut = false;
for (int i = 0; i < 6; i++) {
System.out.println("Number of " + (i+1) + ": " + results[i]);
if (results[i] == 5) {
balut = true;
}
}
if (balut) {
System.out.println("Balut!");
}
}
Here I implemented only the check for the Balut, but the main point is how I am couting the dices results. Hope it helps.
Related
Basically, two dice are rolled randomly a bunch of times and then the two dice values are added together (You roll a 6 and a 3, your total is 9).
The frequency of how many times the total is rolled is stored in totalTally[].
So say you roll 10 times, 3 of those times you roll a total of 9, totalTally[9] = 3.
Here's my code:
import java.util.Random;
public class dice
{
public static void main(String a[])
{
System.out.println("Creating arrays to store information. This might take a while.");
int[][] tally = new int[6][6]; //Creates an array called tally to keep track of how many times each number was rolled. [Dice1][Dice2]
int[] totalTally = new int[12]; //Creates an array called totalTally to keep track of frequency of totals rolled.
int[][] roll = new int[36000000][2]; //Creates an array to store dice roll info.
System.out.println("Rolling two 6-sided dice " + roll.length + " times...");
Random r = new Random(); //Creates a new random number generator
for (int i = 0; i < roll.length; i++) //For loop that rolls the dice to fill the length of the array.
{
roll[i][0] = (r.nextInt(6) + 1); //Assigns random number between 1 and 6 to the array for dice 1 result.
roll[i][1] = (r.nextInt(6) + 1); //Assigns random number between 1 and 6 to the array for dice 2 result.
tally[roll[i][0]-1][roll[i][1]-1]++; //Increments by 1 the respective result in the tally array.
totalTally[roll[i][0] + roll[i][1]-2]++; //Increments by 1 the respective result in the totalTally array.
}
System.out.println("All done. Results are below.");
//Following lines print first table header
System.out.println("\n ROLL SUMMARY:");
System.out.println("Dice 1 + Dice 2 Frequency");
System.out.println("---------------------------------");
for (int i = 1; i <= totalTally.length; i++)//for loop goes through totalTally values
{
System.out.println(" " + i + " " + totalTally[i-1]);
}
//Following lines print second table header
System.out.println("\n DETAILED VIEW:");
System.out.println("Dice 1 Dice 2 Total Frequency");
System.out.println("---------------------------------------------");
for (int j = 1; j <= 6; j++) //For loop goes through dice 1 values
{
for (int k = 1; k <= 6; k++) // Nested for loop goes through dice 2 values
{
System.out.println(j + " " + k + " " + (j+k) + " " + tally[j-1][k-1]); //Prints line for table with dice values
}
}
}
}
and here is the output I am getting for the first table with that code:
Dice 1 + Dice 2 Frequency
1 998639
2 1997209
3 2998118
4 4000336
5 4999210
6 6001277
7 5001144
8 4000794
9 3002596
10 2001501
11 999176
12 0
Here's my issue: It's not possible to roll a 1 if you are rolling two dice.
And it IS possible to roll a 12.
So all my values need to be shifted.
What am I doing wrong?
totalTally[roll[i][0] + roll[i][1]-2]++
Why you subtract 2 ?
you should subtract 1 instead
totalTally[roll[i][0] + roll[i][1]-1]++
This is how you "tally" a single die.
Random r = new Random();
int[] die1 = new int[5]; //Creates an array to tally die 1
for (int roll = 0; roll < totalRolls; roll++) {
die1[r.nextInt(6)] += 1; // increment the index of the random roll
}
Do that for each die.
Your total tally would look like this to count individual rolls
for (int side = 1; side <= 6; side++) {
System.out.printf("%d\t%d\n", side, die1[side-1] + die2[side-1]);
}
If you want to tally the total of the rolls, sure, start printing at 2, not 1
System.out.println("Dice 1 + Dice 2 Frequency");
System.out.println("---------------------------------");
/// See here
for (int i = 1; i <= totalTally.length; i++)//for loop goes through totalTally values
{
System.out.println(" " + i + " " + totalTally[i-1]);
}
I need to print the circumference with Math.random() * Math.Pi; but i'm doing something wrong or missing something. Each random generated number equals the radius of the circle. My idea was to calculate Pi in the getRandomNumberInRange method but when I do, I get error:
Bad operand for type double
import java.util.Random;
import java.util.Scanner;
final static double PI = 3.141592564;
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
//ask the player to enter a number less than or equal to 18 and higher to 9.
System.out.println(" Please enter a number less than or equal to 18 and above 9: ");
int random = sc.nextInt ();
//send error message if bad input
if (random < 9 || random > 18) {
System.out.println(" Error. Unauthorized entry . You need to enter a number less than or equal to 18 and above 9 ");
} else
//If the answer is yes , generate nine different random numbers from 0.
for (int i = 0; i < 9; i++) {
double surface = PI * (random * 2);
System.out.println(getRandomNumberInRange(9, 18) + " : " + " The circumference is : " + surface );
}}
The method called:
private static int getRandomNumberInRange(int min, int max) {
Random r = new Random();
return r.nextInt((max - min) + 1) + min;
}
You call getRandomNumberInRange() in the for loop, but don't assign it to anything, or use it.
This is probably closer to what you want:
for (int i = 0; i < 9; i++) {
int r2 = getRandomNumberInRange(9, 18);
double surface = PI * (r2 * 2);
System.out.println(r2 + " : " + " The circumference is : " + surface);
}
I'm trying to create a java program for my class that throws 3 dice. The player rolls the dice and earns a profit in dollars based on the three values obtained. In particular, the profit is equal to the number of even values multiplied by the sum of those values, plus the number of odd values multiplied by the sum of those values.
For example, if the player rolls 4, 5, and 2 we calculate:
2 x 6 (two even values summing to 6)
And
1 x 5 (one odd value summing to 5).
So the profit is 12 + 5 = $17.
Here is what I have so far -
import java.util.Scanner;
public class Maiorca_Hw2
{
public static void main (String [] args)
{
Scanner keyboard = new Scanner (System.in);
System.out.println("Enter Values of Dice 1");
d1 = keyboard.nextDouble();
System.out.println("Enter Values of Dice 2");
d2 = keyboard.nextDouble();
System.out.println("Enter Values of Dice 3");
d3 = keyboard.nextDouble();
double numbers = 1,2,3,4,5,6;
double profit, dice, d1, d2, d3, oddsum, evensum;
if (d1 == 1 || d1 == 3 || d1 == 5)
oddsum =
I personally think that the program should be done in a different way and you should also use clases.
I hope this little code helps you!
First of all we must create a class called Dice, where we will set the behaviour of a dice and the interactions allowed.
You say that the player rolls, so there must be a random event, I think.
//CODE:
import java.util.Random;
public class Dice {
private final int SIDES;
//Constructor for a dice
public Dice(int sides){
//Establishes the number of sides of the dice we're using.
SIDES = sides;
}
//Methods:
public int roll(){
Random rnd = new Random();
return rnd.nextInt(SIDES) + 1;
}
}
After that, in the main class, we put it to the test.
The why of everything is in the comments
//CODE:
public class MainClass {
public static void main(String[] args) {
int moneyEarned = 0;
//We generate 3 dices with 6 sides each.
Dice d1 = new Dice(6);
Dice d2 = new Dice(6);
Dice d3 = new Dice(6);
// Then we start the simulation:
int res1, res2, res3;
res1 = d1.roll();
res2 = d2.roll();
res3 = d3.roll();
//Show results:
System.out.println("Roll results: dice 1 = " + res1 +
"; dice 2 = " + res2 + "; dice 3 = " + res3);
//We obtain the money the player must earn:
//An even number modulo 2 can only be either 1 or 0 so the same number + 1 will
//switch from 0 to 1 and the opposite thing.
//With this number we can choose which numbers we want to sum:
int oddNumbersPoints = ((res1 * (res1%2))+(res2 * (res2 % 2))+( res3 * (res3 % 2)));
int evenNumbersPoints = ((res1 * ((res1 + 1) %2)) + (res2 * ((res2+1) % 2))+( res3 * ((res3+1) % 2)));
moneyEarned = (evenNumbersPoints * 2) + oddNumbersPoints;
//Finally display the final result:
System.out.println("Even Numbers Points: " + evenNumbersPoints);
System.out.println("Odd Numbers Points: " + oddNumbersPoints);
System.out.println("Congratulations, you just earned: " + moneyEarned + "$");
}
}
/////
Finally you can change the main class to change the number of dices, the scores, so on...
(Add '}' and the main class head)
What you can do declaring primitive types, in this case double, is:
double profit = 0, dice = 2, d2 = 3;
To declare an array of doubles:
double[] numbers = {1,2,3,4,5,6};
I am writing a method that prints guesses = [random number here from 1-50 inclusive] multiple times on a new line until the value is larger than 48. Once it is larger than 48 I try to print the number of guesses it took (No scanner used though, the 'guesses' are the number produced by the Math.random).
Here is example output:
guess = 43
guess = 47
guess = 45
guess = 27
guess = 49
total guesses = 5
and this is my output:
guess = 44
guess = 47
guess = 45
guess = 27
total guesses = 4
The reason I am getting almost the same random numbers is because it's in Practice-It.
Here is my code:
public static void makeGuesses(){
int totalGuesses = 0;
double randomNumber = (Math.random() * 50 + 1);
while(randomNumber < 48){
System.out.print("guess = ");
System.out.println(randomNumber + 1);
randomNumber = (int)(Math.random() * 51);
totalGuesses++;
}
System.out.print("total guesses = " + totalGuesses);
}
Currently, I am not getting the last line of required output. What do I need to make the condition on my while loop?
You're on the right track in that you need to cast to int, but you need to cast to int when you generate the number.
Furthermore, if you use your current implementation, you are generating numbers from 0-50, inclusive. You want to generate from 1-50, inclusive. You can fix this by multiplying by 50, then simply adding 1.
int randomNumber = (int) (Math.random() * 50 + 1);
Your first random isn't an int because you forgot the cast
double randomNumber = (int) (Math.random() * 51);
I suggest you make the type an int as well
int randomNumber = (int) (Math.random() * 51);
The problem is that the condition on your while checks a number you have not yet printed. If you use a debugger and step through the code, you will notice this.
int totalGuesses = 0;
int randomNumber;
while (true) {
randomNumber = 1 + (int)Math.floor(Math.random() * 50);
totalGuesses += 1;
System.out.print("guess = ");
System.out.println(randomNumber);
if (randomNumber > 48) break;
}
System.out.print("total guesses = " + totalGuesses);
Sorry I can't format code well on my phone.
When you need to generate random numbers again and again, you should use java.util.Random for this.
public static void makeGuesses() {
int totalGuesses = 0;
Random rdm = new Random();
int randomNumber;
do {
randomNumber = rdm.nextInt(50) + 1;
System.out.println("guess = " + randomNumber);
totalGuesses++;
} while (randomNumber < 48);
System.out.println("total guesses = " + totalGuesses);
}
}
Output :
guess = 15
guess = 29
guess = 26
guess = 14
guess = 3
guess = 1
guess = 49
total guesses = 7
okay so I am attempting to create a dice game where args[0] is the amount of times the game is played. the game.... two dice are rolled and if the sum does not equal 7 their value is added to a sum. If the sum equals 7 the game is over. I want to keep track of the largest sum out of all the games and the smallest which should be zero always because when the sum equals 7 it sets the sum to 0.
Here is my code. I don't think what it is printing is what I am going for...help?Also how do i auto format in eclipse?
public class diceGame {
public static void main(String[] args) {
int dice1;
int dice2;
int count=0;
int theSum=0;
int lowest=500;
int finalSum=0;
int diceSum=0;
while (count !=Integer.parseInt(args[0])){
count=count+1;
theSum=0;
while(diceSum!=7){
diceSum=0;
dice1=1 + (int)(Math.random() * ((6 - 1) + 1));
dice2=1 + (int )(Math.random() * ((6 - 1) + 1));
diceSum=dice1+dice2;
if (diceSum !=7){
theSum=theSum+diceSum;
if (theSum>finalSum){
finalSum=theSum;
}
if (theSum<lowest){
lowest=theSum;
}
}
}
}
System.out.println("After "+args[0]+" simulations: ");
System.out.println("Biggest sum: "+finalSum);
System.out.println("Smallest sum: "+lowest);
}
}
I fixed it
public class diceGame {
public static void main(String[] args) {
int dice1;
int dice2;
int count = 0;
int theSum = 0;
int lowest = Integer.MAX_VALUE;
int finalSum = 0;
int diceSum;
int totalSum=0;
while (count < Integer.parseInt(args[0])) {
count = count + 1;
diceSum=0;
theSum=0;
while (diceSum!=7) {
diceSum = 0;
dice1 = 1 + (int) ((Math.random() * (6 - 1)) + 1);
dice2 = 1 + (int) ((Math.random() * (6 - 1)) + 1);
diceSum = dice1 + dice2;
if (diceSum != 7) {
theSum = theSum + diceSum;
}
//System.out.println("the sum is "+theSum);
}
if (theSum > finalSum) {
finalSum = theSum;
}
if (theSum < lowest) {
lowest = theSum;
}
totalSum=totalSum+theSum;
}
double average=(double)totalSum/(Double.parseDouble(args[0]));
System.out.println("After " + args[0] + " simulations: ");
System.out.println("Biggest sum: " + finalSum);
System.out.println("Smallest sum: " + lowest);
System.out.println("The average is: "+average);
}
}
It is because the lowest value of 2 dice when added is 2, not 0. And if you roll a 7 on the first roll, then you will not update your biggest and lowest. You need to move those checks outside the loop.
while(diceSum!=7){
diceSum=0;
dice1=1 + (int)(Math.random() * ((6 - 1) + 1));
dice2=1 + (int )(Math.random() * ((6 - 1) + 1));
diceSum=dice1+dice2;
if (diceSum !=7) {
theSum=theSum+diceSum;
}
}
if (theSum>finalSum){
finalSum=theSum;
}
if (theSum<lowest){
lowest=theSum;
}
If diceSum is seven, your check for min/max is not executed because you put it into braces of if(diceSum!=7). So if diceSum is seven, the mininum is not updated.
Also the lowest sum is not always 0. For example:
First diceroll:
Dice1: Value 5
Dice2: Value 3
Which makes a diceSum=8.
So it's not equal 7, so theSum becomes 0+8=8
Because 8>0 (theSum>finalSum) finalSum gets updated: finalSum=8;
Because 8<500 (theSum<lowest) lowest gets updated: lowest=8;
Next dice roll
Dice1: Value 4
Dice2: Value 3
diceSum=7
Even if you corret your braces like
while (count !=Integer.parseInt(args[0])){
count=count+1;
theSum=0;
while(diceSum!=7){
diceSum=0;
//Corrected Random Brace (see comment below your question)
dice1=1 + (int)((Math.random() * (6 - 1)) + 1);
dice2=1 + (int)((Math.random() * (6 - 1)) + 1);
diceSum=dice1+dice2;
if (diceSum !=7){
theSum=theSum+diceSum;
} // CHANGED BRACE POSITION HERE
if (theSum>finalSum){
finalSum=theSum;
}
if (theSum<lowest){
lowest=theSum;
}
}
}
lowest will still be set to 8. Not to zero. The lowest will only be zero, if a game round exist where the dice sum is 7 in the FIRST dice roll.