Jump to content

Help with Java Array

Beeeyeee

Hey! I'm writing a Yahtzee game for class. and we need to roll dice and compare the values in an array. 

I'm having trouble figuring out how to compare the array vlaues to eachother? I have no idea where to start. I wrote some 'if' statements if that'll help.  See the note I left just below the 'if' statements.
Thank you in advance. 

 

i've created a cheater mode that lets you enter the values instead of relying on rng to decide for you. this should help check the scores better. 


/***************
*Keyan North
*
*Yahtzee
*
*This program simulates 
the dice game Yahtzee
**************/
import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class Yahtzee
{
   public static void main (String[] args)
   {
      Scanner in = new Scanner (System.in);//import scanner 'in'
      Random r = new Random(); //import random number genterator
      
      int[] dice = new int[5];
     
      dice[0] = 0;
      dice[1] = 0;
      dice[2] = 0;
      dice[3] = 0;
      dice[4] = 0;
      
      String mode = ""; //game mode 
      String rk = ""; //reroll or keep
      int rng = 0; //random number
      int a = 0;
 

      //welcome
      System.out.println("Welcome to Yahtzee!");
      
      //cheater mode or normal mode?
      System.out.println("Do you want to play Cheater Mode or Normal Mode? C or N");
      mode = in.nextLine();
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      //roll
      if (mode.equalsIgnoreCase("n"))//normal mode
      {
         System.out.println("Normal Mode enabled");
         System.out.println("Rolling Dice...");
         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");

         for (int i = 0; i < dice.length; i++)
         {
            rng = r.nextInt(6)+1; //generate random number
            System.out.println("Dice #" + (i+1) + " rolled " + rng); //print each dice and what it rolled
            dice[i] = rng;
         }
         
         //reroll or keep?
         System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~");//add a space
         
         for (int i = 0; i < dice.length; i++)
         {
            System.out.println("Do you want to Keep or Reroll Dice #" + (i+1) + "? R or K");
            rk = in.nextLine();
            
            if (rk.equalsIgnoreCase("R"))//if reroll
            {
               dice[i] = rng = r.nextInt(6)+1; //generate random number
               System.out.println("Dice #" + (i+1) + " rerolled " + rng);//show what this dice rerolled
            }
            else if (rk.equalsIgnoreCase("K"))//if keep
            {
            }
            else
            {
               System.out.println("Error, try again. R or K?");
               rk = in.nextLine();
            }
         }//end rng roll      
      }//end normal mode    
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     
      //cheater mode
      else if (mode.equalsIgnoreCase("c"))//cheater mode
      {
         System.out.println("Cheater Mode enabled");
         System.out.println("Choose the values for each dice:");
         
         for (int i = 0; i < dice.length; i++)
         {
            System.out.println("What value should Dice #" + (i+1) + " be?");
            dice[i] = in.nextInt();//what variable do I use here to identify each dice in the array? I need to change each dice value indevidually in this loop.
         }
      }//end cheater mode
      else
      {
         System.out.println("Error. Try again. C or N?");
         mode = in.nextLine();
      }
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      //calculate score
      System.out.println(Arrays.toString(dice));
      
      if ()// = yahtzee, 5 of a kind
      {
         
      }
      else if ()// = large straight, 5 in a row
      {
      
      }
      else if () // = small straight, 4 in a row
      {
      
      }
      else if () //=four of a kind
      {
      
      }
      else if () //= full house, 2 of a kind and 3 of a kind
      {
      
      }
      else if () // 3 of a kind

      /*in the above '//calculate score' You dont need to actually calculate any point value. 
      what I need is to tell the highest score acheived. I've written them in order of importance:
      1. Yahtzee
      2. large straight
      3. small straight
      4. four of a kind
      5. full house
      6. three of a kind
      If a roll equals several or all of the above, show the highest score. 
      (ex. if I roll 3,3,3,3,3 then technically I have all the above, but it should just show Yahtzee.)
      
      I honestly have no idea how to set the dice[] array to compare values at all. Maybe I don't 
      even have to use 'if' statements! Please help me out many thanks!*/
      
      
      
      
      
      
      
        
   }//end main
}//end Yahtzee

Link to comment
Share on other sites

Link to post
Share on other sites

any errors in console? don't really have time to be pouring over every little bit of code - 3 warnings just came up, getting nervous about my own program

Link to comment
Share on other sites

Link to post
Share on other sites

The way I would do it is pre-sort the cards first, then look at each card and put them in "buckets" for the patterns you're trying to evaluate. Then after looking at each card, find out which pattern has a completed bucket and which one is the highest value.

 

Since the only patterns we're dealing with here are same value and straights, with the exception of the full house (which can be determined by looking at the same value patterns), it makes it easier to figure out which hand you have.

 

So let's say we're dealing with ♥2, ♥3, ♥4, ♥5, ♣2

  • ♥2 goes into two pattern buckets (for straights and same value)
  • ♥3 goes into the straights pattern bucket with ♥2 and starts two more pattern buckets
  • ♥4 goes into the straights pattern bucket with ♥2/♥3 and the bucket with just the ♥3. It starts only a same value bucket because there's only two cards left and it's too small to start a straights pattern bucket.
  • ♥5 goes into the straights pattern bucket with ♥2/♥3/♥4 and the bucket with ♥3/♥4. It starts only a same value bucket in case there's a pair for a full house.
  • ♣2 goes into the same value bucket with ♥2, but starts no new buckets because this is the last card.

Now I look at the buckets we have:

  • A straight bucket with ♥2, ♥3, ♥,4, and ♥5
    • Run this through some evaluation function, have it spit out a value representing the "small straight" hand.
  • A same value bucket with ♥2 and ♣2
    • Run this through some evaluation function, have it spit out a value representing a pair.
  • A straight bucket with ♥3, ♥,4, and ♥5
    • Run this through some evaluation function, have it spit out nothing.
  • A same value with ♥3 (toss this one)
  • A same value with ♥4 (toss this one)
  • A same value with ♥5 (toss this one)

Then tally up what hands you have and use the one with the highest value. For a full house hand, if you catch a three-of-a-kind, see if you have a pair somewhere too. If you do, then you have a full-house and stop evaluating since you can't have any other pattern.

 

Well, that's the gist of it. I hope that made sense >_>

Link to comment
Share on other sites

Link to post
Share on other sites

The solution, as I see it, is to get an array of length 6, it will contain how many dice have rolled it's position.

Lets say you roll 2,3,3,3,5 then your array would be [0,1,3,0,1,0]

then you just have to define your conditions for each possible scoring condition, for example, 5 in any position of the array means Yahtzee

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, M.Yurizaki said:

Oh wait, this is dice. Sorry, somehow I read cards somewhere :D

 

Then ignore my post, but I'll keep it around for posterity.

actually is not that unrelated, you just don't check for the suit and just go up to 6 instead of 13

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

doesnt Yahtzee have two sets of dice and scores like poker? I would have a rules based decision tree

 

1. all same number -> check which player is higher

2. full house -> check which player is higher

             ☼

ψ ︿_____︿_ψ_   

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, gtx1060=value said:

any errors in console? don't really have time to be pouring over every little bit of code - 3 warnings just came up, getting nervous about my own program

Thats probably due to the 'if' statements at the bottom. I left them empty for this post

 

7 hours ago, M.Yurizaki said:

Oh wait, this is dice. Sorry, somehow I read cards somewhere :D

 

Then ignore my post, but I'll keep it around for posterity.

Haha no worries! still helpful.:)

 

7 hours ago, espurritado said:

The solution, as I see it, is to get an array of length 6, it will contain how many dice have rolled it's position.

Lets say you roll 2,3,3,3,5 then your array would be [0,1,3,0,1,0]

then you just have to define your conditions for each possible scoring condition, for example, 5 in any position of the array means Yahtzee

I have created a dice array with 6 elements. I don't see how "2,3,3,3,5" would show "[0,1,3,0,1,0]" at all.. what am I missing here? 
I've got it to work prettymuch perfectly, except I can't seem to find out how to ask the code 'Does dice[] contain four 1's? if not four 2's? if not four 3's? etc... I tried doing something like 'if (counts[] = 1,2,3,4,5 || counts[] = 2,3,4,5,6)' (for straights) but that doesn't work at all.. Check the code at the bottom of this reply.

 

6 hours ago, SCHISCHKA said:

doesnt Yahtzee have two sets of dice and scores like poker? I would have a rules based decision tree

 

1. all same number -> check which player is higher

2. full house -> check which player is higher

In reality, yes. but for this assignment its one set of dice, one score, one round. for the purpose of simplicity. 



I've fixed up and made my code a bit more effecient. and for the purpose of simplicity I left out the rng part. So that way you can manually enter each dice for debugging and ease of use without 20 gallons of noisy code. Here's what i've got: 

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class Yahtzee
{
   public static void main (String[] args)
   {
      Scanner in = new Scanner (System.in);//import scanner 'in'
      Random r = new Random(); //import random number genterator
      
      int[] dice = new int[5];//create dice array
     
      dice[0] = 0;
      dice[1] = 0;
      dice[2] = 0;
      dice[3] = 0;
      dice[4] = 0;
      
      String mode = ""; //game mode 
      String rk = ""; //reroll or keep
      boolean win = false; //if win
 

     
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     
      //cheater mode
         System.out.println("Choose the values for each dice:");
         
         for (int i = 0; i < dice.length; i++)
         {
            System.out.println("What value should Dice #" + (i+1) + " be?");
            dice[i] = in.nextInt();//what variable do I use here to identify each dice in the array? I need to change each dice value indevidually in this loop.
         }
      }//end cheater mode
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      
      //calculate score
      System.out.println(Arrays.toString(dice));
  
      int[] counts = new int[6]; //new count array
      
      for (int i = 0; i < counts.length; i++)//set all counts value to zero
      {
         counts[i] = 0;
      }
      
      for(int i = 0; i < dice.length; i++)//count up how many numbers were rolled
      {
         int diceIndex = dice[i] - 1;
         counts[diceIndex]++;
      }
      
      System.out.println(); //add blank space
      

 

 

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

HERES THE PART I NEED HELP WITH

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      do //I need some sort of overarching loop for these 'if' statements.. is do/while the right one?
      {
         for (int i = 0; i < 6; i++) //display number of each number rolled
         {
            System.out.println("The number of " + (i+1) + "'s is: " + counts[i]);
         }
         
         //find Win Condition
         if (counts[i] = 5)// = yahtzee, 5 of a kind
         {
            System.out.println("You got a Yahtzee!");
            win = true;
         }

//if large straight, 5 in a row
         else if (counts[] = 1,2,3,4,5 || counts[] = 2,3,4,5,6)
         {
            if (!win = true)//check if higher condition was met
            {
               System.out.println("You got a Large Straight!");
               win = true;
            }
         }

//if small straight, 4 in a row
         else if (counts[] = 1,2,3,4 || counts[] = 2,3,4,5 || counts[] = 3,4,5,6) // = small straight, 4 in a row
         {
            if (!win = true)//check if higher condition was met
            {
               System.out.println("You got a Small Straight!");
               win = true;
            }
         }

//if four of a kind
         else if (counts[i] = 4) 
         {
            if (!win = true)//check if higher condition was met
            {
               System.out.println("You got Four of a Kind!");
               win = true;
            }
         }

//if full house (2 of a kind and 3 of a kind)
         else if (counts[] = 2 && counts[] = 3) 
         {
            if (!win = true)//check if higher condition was met
            {
               System.out.println("You got a Full House!");
               win = true;
            }
         }

//if 3 of a kind
         else if ()
         {
            if (!win = true)//check if higher condition was met
            {
               System.out.println("You got Three of a Kind!");
               win = true;
            }
         }

//if none apply
         else
         {
            System.out.println("Sorry! You didn't get anything! Try again!");
         }
      }while (win = false)   
     

 

 


      /*in the above '//calculate score' You dont need to actually calculate any point value. 
      what I need is to tell the highest score acheived. I've written them in order of importance:
      1. Yahtzee
      2. large straight
      3. small straight
      4. four of a kind
      5. full house
      6. three of a kind
      If a roll equals several or all of the above, show the highest score. 
      (ex. if I roll 3,3,3,3,3 then technically I have all the above, but it should just show Yahtzee.)
      
      I honestly have no idea how to set the dice[] array to compare values at all. Maybe I don't 
      even have to use 'if' statements! Please help me out many thanks!*/
      
      
      
      
      
      
      
        
   }//end main
}//end Yahtzee

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Beeeyeee said:

I have created a dice array with 6 elements. I don't see how "2,3,3,3,5" would show "[0,1,3,0,1,0]" at all.. what am I missing here? 
I've got it to work prettymuch perfectly, except I can't seem to find out how to ask the code 'Does dice[] contain four 1's? if not four 2's? if not four 3's? etc... I tried doing something like 'if (counts[] = 1,2,3,4,5 || counts[] = 2,3,4,5,6)' (for straights) but that doesn't work at all.. Check the code at the bottom of this reply.

lets take it step by step:

  • you roll 2,3,3,3,5; your counts list is [0,0,0,0,0,0]
  • you start reading your rolls
    • read the 2; you increase the second element in your array by one; your counts list is [0,1,0,0,0,0]
    • read the 3; you increase the third element in your array by one; your counts list is [0,1,1,0,0,0]
    • read the 3; you increase the third element in your array by one; your counts list is [0,1,2,0,0,0]
    • read the 3; you increase the third element in your array by one; your counts list is [0,1,3,0,0,0]
    • read the 5; you increase the fifth element in your array by one; your counts list is [0,1,3,0,1,0]

therefor [0,1,3,0,1,0].

then, you just have to ask: is the counts list [1,1,1,1,1,0] or [0,1,1,1,1,1]? if it is, you've got a straight

for four of a kind is even simpler, you just ask: is there a four in counts?

 

also

 

= means assignment

== means comparison

 

you've got every single condition wrong

 

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×