public class Yahtzee extends GraphicsProgram implements YahtzeeConstants {
public static void main(String[] args) {
new Yahtzee().start(args);
}
public void run() {
IODialog dialog = getDialog();
nPlayers = dialog.readInt("Enter number of players");
playerTotal=nPlayers;
playerNames = new String[nPlayers];
for (int i = 1; i <= nPlayers; i++) {
playerNames[i - 1] = dialog.readLine("Enter name for player " + i);
}
display = new YahtzeeDisplay(getGCanvas(), playerNames);
playGame();
}
private void playGame() {
for (int i=0;i<playerTotal;i++){
for (int j=0;j<17;j++){
checkWhetherCategory[i][j]=1;
}
checkWhetherCategory[i][UPPER_SCORE-1]=0;
checkWhetherCategory[i][UPPER_BONUS-1]=0;
checkWhetherCategory[i][LOWER_SCORE-1]=0;
checkWhetherCategory[i][TOTAL-1]=0;
}
for (int i=0;i<13;i++){
for (int j=0;j<playerTotal;j++){
oneround(j+1);
}
}
for (int i=0;i<playerTotal;i++){
sum[i]=totalscore(i);
}
int winnernumber=0;
for (int i=0;i<playerTotal-1;i++){
if (sum[i+1]>sum[winnernumber]) winnernumber=i+1;
}
String winnername=playerNames[winnernumber];
display.printMessage("The winnder is: "+winnername);
}
private int totalscore(int no){
int temp=0;
for (int i=0;i<6;i++){
temp=temp+scores[no][i];
}
scores[no][6]=temp;
display.updateScorecard(UPPER_SCORE,no+1, scores[no][6]);
if (scores[no][6]>63){
scores[no][7]=35;
} else {
scores[no][7]=0;
}
display.updateScorecard(UPPER_BONUS,no+1, scores[no][7]);
temp=0;
for (int i=8;i<15;i++){
temp=temp+scores[no][i];
}
scores[no][15]=temp;
display.updateScorecard(LOWER_SCORE,no+1, scores[no][15]);
scores[no][16]=scores[no][6]+scores[no][7]+scores[no][15];
display.updateScorecard(TOTAL, no+1, scores[no][16]);
return scores[no][16];
}
private void oneround(int whichPlayer){
display.waitForPlayerToClickRoll(whichPlayer);
for (int i=0;i<N_DICE;i++){
dice[i]=rgen.nextInt(1,6);
}
display.displayDice(dice);
for (int i=0;i<2;i++){
display.waitForPlayerToSelectDice();
for (int j=0;j<N_DICE;j++){
if (display.isDieSelected(j)) dice[j]=rgen.nextInt(1,6);
}
display.displayDice(dice);
}
while(true){
boolean indicator=false;
category=display.waitForPlayerToSelectCategory();
if (checkWhetherCategory[whichPlayer-1][category-1]==1){
checkWhetherCategory[whichPlayer-1][category-1]=0;
indicator=true;
}
if (indicator) {
break;
} else{
display.printMessage("Please select again.");
}
}
oneroundscore(category,whichPlayer-1);
}
private boolean p(int[] result,int value){
for (int i=0;i<4;i++){
int temp=0;
for (int j=0;j<4-i;j++){
temp=0;
if (dice[j]>dice[j+1]){
temp=dice[j];
dice[j]=dice[j+1];
dice[j+1]=temp;
}
}
}
boolean indicator =false;
switch(value){
case ONES:
indicator=true;
break;
case TWOS:
indicator=true;
break;
case THREES:
indicator=true;
break;
case FOURS:
indicator=true;
break;
case FIVES:
indicator=true;
break;
case SIXES:
indicator=true;
break;
case THREE_OF_A_KIND:
if ((result[0]==result[1])&(result[1]==result[2])) indicator=true;
if ((result[1]==result[2])&(result[2]==result[3])) indicator=true;
if ((result[2]==result[3])&(result[3]==result[4])) indicator=true;
break;
case FOUR_OF_A_KIND:
if (result[0]==result[1]&result[1]==result[2]&result[2]==result[3]) indicator=true;
if (result[1]==result[2]&result[2]==result[3]&result[3]==result[4]) indicator=true;
break;
case FULL_HOUSE:
if (result[0]==result[1]&result[1]==result[2]&!(result[2]==result[3])&result[3]==result[4]) indicator=true;
if (result[2]==result[3]&result[3]==result[4]&!(result[2]==result[1])&result[1]==result[0]) indicator=true;
break;
case SMALL_STRAIGHT:
int index=0;
for (int i=0;i<4;i++){
if (result[i]==result[i+1]) {
result[i+1]=10;
index++;
}
}
if (index>1) {
indicator=false;
break;
}
if (index==1){
for (int i=0;i<4;i++){
int temp=0;
for (int j=0;j<4-i;j++){
temp=0;
if (dice[j]>dice[j+1]){
temp=dice[j];
dice[j]=dice[j+1];
dice[j+1]=temp;
}
}
}
if (result[0]==(result[1]-1)&result[1]==(result[2]-1)&result[2]==(result[3]-1)) indicator=true;
} else if (index==0){
if (result[0]==(result[1]-1)&result[1]==(result[2]-1)&result[2]==(result[3]-1)) indicator=true;
if (result[1]==(result[2]-1)&result[2]==(result[3]-1)&result[3]==(result[4]-1)) indicator=true;
}
break;
case LARGE_STRAIGHT:
if (result[0]==1&result[1]==2&result[2]==3&result[3]==4&result[4]==5) indicator=true;
if (result[0]==2&result[1]==3&result[2]==4&result[3]==5&result[4]==6) indicator=true;
break;
case YAHTZEE:
if (result[0]==result[1]&result[1]==result[2]&result[2]==result[3]&result[3]==result[4]) indicator=true;
break;
case CHANCE:
indicator=true;
break;
}
return indicator;
}
private void oneroundscore(int showcategory,int no){
switch (showcategory){
case ONES:
if (p(dice, ONES)){
for (int i=0;i<5;i++){
if (dice[i]==1) scores[no][ONES-1]++;
}
} else{
scores[no][ONES-1]=0;
}
display.updateScorecard(ONES, no+1, scores[no][ONES-1]);
break;
case TWOS:
if (p(dice, TWOS)){
for (int i=0;i<5;i++){
if (dice[i]==2) scores[no][TWOS-1]=scores[no][TWOS-1]+2;
}
} else{
scores[no][TWOS-1]=0;
}
display.updateScorecard(TWOS, no+1, scores[no][TWOS-1]);
break;
case THREES:
if (p(dice, THREES)){
for (int i=0;i<5;i++){
if (dice[i]==3) scores[no][THREES-1]=scores[no][THREES-1]+3;
}
} else{
scores[no][THREES-1]=0;
}
display.updateScorecard(THREES, no+1, scores[no][THREES-1]);
break;
case FOURS:
if (p(dice, FOURS)){
for (int i=0;i<5;i++){
if (dice[i]==4) scores[no][FOURS-1]=scores[no][FOURS-1]+4;
}
} else{
scores[no][FOURS-1]=0;
}
display.updateScorecard(FOURS, no+1, scores[no][FOURS-1]);
break;
case FIVES:
if (p(dice, FIVES)){
for (int i=0;i<5;i++){
if (dice[i]==5) scores[no][FIVES-1]=scores[no][FIVES-1]+5;
}
} else{
scores[no][FIVES-1]=0;
}
display.updateScorecard(FIVES,no+1, scores[no][FIVES-1]);
break;
case SIXES:
if (p(dice, SIXES)){
for (int i=0;i<5;i++){
if (dice[i]==6) scores[no][SIXES-1]=scores[no][SIXES-1]+6;
}
} else{
scores[no][SIXES-1]=0;
}
display.updateScorecard(SIXES, no+1, scores[no][SIXES-1]);
break;
case 7:
case 8:
case THREE_OF_A_KIND:
if (p(dice, THREE_OF_A_KIND)){
for (int i=0;i<5;i++){
scores[no][THREE_OF_A_KIND-1]=scores[no][THREE_OF_A_KIND-1]+dice[i];
}
} else{
scores[no][THREE_OF_A_KIND-1]=0;
}
display.updateScorecard(THREE_OF_A_KIND, no+1, scores[no][THREE_OF_A_KIND-1]);
break;
case FOUR_OF_A_KIND:
if (p(dice, FOUR_OF_A_KIND)){
for (int i=0;i<5;i++){
scores[no][FOUR_OF_A_KIND-1]=scores[no][FOUR_OF_A_KIND-1]+dice[i];
}
} else{
scores[no][FOUR_OF_A_KIND-1]=0;
}
display.updateScorecard(FOUR_OF_A_KIND, no+1, scores[no][FOUR_OF_A_KIND-1]);
break;
case FULL_HOUSE:
if (p(dice, FULL_HOUSE)){
scores[no][FULL_HOUSE-1]=25;
} else{
scores[no][FULL_HOUSE-1]=0;
}
display.updateScorecard(FULL_HOUSE, no+1, scores[no][FULL_HOUSE-1]);
break;
case SMALL_STRAIGHT:
if (p(dice, SMALL_STRAIGHT)){
scores[no][SMALL_STRAIGHT-1]=30;
} else{
scores[no][SMALL_STRAIGHT-1]=0;
}
display.updateScorecard(SMALL_STRAIGHT, no+1, scores[no][SMALL_STRAIGHT-1]);
break;
case LARGE_STRAIGHT:
if (p(dice, LARGE_STRAIGHT)){
scores[no][LARGE_STRAIGHT-1]=40;
} else{
scores[no][LARGE_STRAIGHT-1]=0;
}
display.updateScorecard(LARGE_STRAIGHT, no+1, scores[no][LARGE_STRAIGHT-1]);
break;
case YAHTZEE:
if (p(dice, YAHTZEE)){
scores[no][YAHTZEE-1]=50;
} else{
scores[no][YAHTZEE-1]=0;
}
display.updateScorecard(YAHTZEE, no+1, scores[no][YAHTZEE-1]);
break;
case CHANCE:
if (p(dice, CHANCE)){
for (int i=0;i<5;i++){
scores[no][CHANCE-1]=scores[no][CHANCE-1]+dice[i];
}
} else{
scores[no][CHANCE-1]=0;
}
display.updateScorecard(CHANCE, no+1, scores[no][CHANCE-1]);
break;
case 16: