import java.util.*;
public class Yahtzee{
public static boolean forWhile = true; // not needed out here. I'm to lazy to move it
public static boolean [] scoreOptions = new boolean[13]; // Keeps track of used scoring options
public static void main
(String [] args
){// Main Method Scanner input
= new Scanner
(System.
in); // scanner int [] player1 = new int [6];// player dice and score
System.
out.
println(" Welcome new player. \n You are about to play Robert's Yahtzee Program" + "\n In the event that you don't remember how to play yahtzee you can find the instructions here:"+
"\n http://w...content-available-to-author-only...o.com/common/instruct/Yahtzee.pdf"+
"\n The computer will roll the dice for you the first then you can either keep what you have "+
"\n or reroll and choose specific dice to keep. \n Alright! Now Let's Play");
while(running()){// start game loop
boolean [] keepDice = new boolean [5];// determines whether a particular die will be rerolled or not
for (int rollNum=0; rollNum<3; rollNum++){// cycle through three rolls
for (int roll= 0; roll < 5; roll++){ // cycle through each dice
if(!keepDice[roll]){// if dice gets rerolled
player1
[roll
] = ((int)(Math.
random()*100) %6
+ 1);//roll dice }
}// end dice rolling
//------------DISPLAYS CURRENT DICE AND SCORE-------------------------------------
System.
out.
print("Your Current dice are:\n"); for(int counter = 0; counter < 5; counter++){
System.
out.
print("Dice " + counter
+ " is " + player1
[counter
]+ " \n"); }
System.
out.
println("and your score is " +player1
[5]); //----------------------------------------------------------------------------------
if(rollNum
< 2)System.
out.
println("Would you like to roll again? (Y/N)"); if(rollNum < 2 && input.next().equalsIgnoreCase("n")){// breaks loop if you don't want to roll again
break;
}
//------------------CHOOSE WHICH DICE TO KEEP ON REROLL-------------------------------
else if(rollNum<2){
System.
out.
println("Please type in the dice numbers of the dice of you wish to save or type 7 to reroll all dice\n"+ " Example: if you wanted to keep dice 3 and 4 you would type: 34");
String response
= input.
next(); char [] keepers = response.toCharArray();
for(int counter = 0; counter<keepDice.length; counter++){
keepDice[counter] = false;
}
for(int counter = 0; counter <keepers.length; counter++){
if (keepers[counter]-48>= 0 && keepers[counter]-48 <= 4){
keepDice[keepers[counter]-48] = true;
}
}
}
//-- ----------------------------------------------------------------------------------------
}// end of dice rolls
//----------------SCORE OPTION MENU------------------------------------------------------------
while(forWhile){// loops until valid choice is selected
System.
out.
println("\nPlease pick your score option:\n 1) Ones 2) Twos 3) Threes \n 4) Fours 5) Fives 6) Sixs"+ "\n 7) 3 Of A Kind 8) 4 Of A Kind 9) Full House \n 10) Small Straight 11) Large Straight 12) Chance \n 13) YAHTZEE!");
int scoreChoice = 0;
do{
try{
scoreChoice = input.nextInt();// score option choice
}
catch(InputMismatchException e){
input.next();
System.
out.
println("Error: Invalid. Please type in an integer value!"); }
}while (scoreChoice == 0);
//-----------------------------------------------------------------------------------------------
//------------------Select choice, mark choice as used and run relevant method that returns an int value added to the score--------
if(scoreChoice>= 1 && scoreChoice <= 6){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1] = true;
player1[5] += diceNumber(player1, scoreChoice);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
if(scoreChoice == 7){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1]=true;
player1[5] += threeOfAKind(player1);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
if(scoreChoice == 8){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1]=true;
player1[5] += fourOfAKind(player1);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
if(scoreChoice == 9){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1]=true;
player1[5] += fullHouse(player1);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
if(scoreChoice == 10){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1]=true;
player1[5] += smallStraight(player1);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
if(scoreChoice == 11){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1]=true;
player1[5] += largeStraight(player1);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
if(scoreChoice == 12){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1]=true;
player1[5] += chance(player1);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
if(scoreChoice == 13){
if(!scoreOptions[scoreChoice-1]){
scoreOptions[scoreChoice-1]=true;
player1[5] += yahtzee(player1);
forWhile = false;
}
else{System.
out.
println("Error: Option already used. Please pick a new option!");} }
//----------------------------------------------------------------------------------------------------------------------
}
forWhile = true;
}// end of game loop
System.
out.
print("\nYour Final Score Is " + player1
[5]); }
// -----------------methods that check dice against rules for points--------------------------------------
public static int diceNumber(int [] dice, int x){
int sum = 0;
for(int counter = 0; counter < dice.length-1; counter ++){
if(dice[counter]== x){
sum+= x;
}
}
return sum;
}
public static int threeOfAKind(int [] dice){
int sum= 0, x=0,y=1;
for(int counter = 0; counter < dice.length-1; counter++){
y=1;
for(int counter2 =0; counter2 < dice.length-1; counter2++){
if(dice[counter]==dice[counter2]&& counter!=counter2){
x=dice[counter];
y++;
}
}
if(y>=3){
sum = x;
}
}
if(sum>0){
sum=0;
for(int counter=0; counter< dice.length-1; counter++){
sum+=dice[counter];
}
}
return sum;
}
public static int fourOfAKind(int [] dice){
int sum= 0, x=0,y=1;
for(int counter = 0; counter < dice.length-1; counter++){
y=1;
for(int counter2 =0; counter2 < dice.length-1; counter2++){
if(dice[counter]==dice[counter2]&& counter!=counter2){
x=dice[counter];
y++;
}
}
if(y>=4){
sum = x;
}
}
if(sum>0){
sum=0;
for(int counter=0; counter< dice.length-1; counter++){
sum+=dice[counter];
}
}
return sum;
}
public static int fullHouse(int [] dice){
int sum= 0,y=1,z=1;
for(int counter = 0; counter < dice.length-1; counter++){
y=1;
z=1;
for(int counter2 =0; counter2 < dice.length-1; counter2++){
if(dice[counter]==dice[counter2]&& counter!=counter2){
y++;
z++;
}
}
if((y==3)&&(sum==2 || sum==0)){
sum += y;
}
if((z==2)&&(sum==3 || sum==0)){
sum += z;
}
}
if(sum==5){
sum=25;}
return sum;
}
public static int chance(int [] dice){
int sum = 0;
for(int counter = 0; counter< dice.length-1; counter++){
sum+= dice[counter];
}
return sum;
}
public static int yahtzee(int [] dice){
int x=0;
for(int counter = 0; counter < dice.length-1; counter++){
if(dice[counter]==dice[counter+1])
x++;
}
if(x==4)
return 50;
return 0;
}
public static int smallStraight(int [] dice){
int []dice2 = {dice[0], dice[1], dice[2], dice[3], dice[4]};
dice2= Useful.sortIntArray(dice2);
int x = 0;
for(int counter = 0; counter < dice2.length-1; counter++){
if(dice2[counter]+1 == dice2[counter+1]){
x++;
}
}
if(x ==3){
return 30;
}
else{return 0;}
}
public static int largeStraight(int [] dice){
int []dice2 = {dice[0], dice[1], dice[2], dice[3], dice[4]};
dice2= Useful.sortIntArray(dice2);
int x = 0;
for(int counter = 0; counter < dice2.length-1; counter++){
if(dice2[counter]+1 == dice2[counter+1]){
x++;
}
}
if(x ==4){
return 40;
}
else{return 0;}
}
//----------------------------------------------------------------------------------------------
public static boolean running(){// game loop condition, is true while any score options aren't used
for( int counter = 0; counter < scoreOptions.length; counter++){
if(!scoreOptions[counter]){
return true;}
}
return false;
}
}