12
返回列表 发新帖
楼主: yiyiyaya
跳转到指定楼层
上一主题 下一主题
收起左侧

Stanford CS106A #5 Assignment5讨论帖

🔗
nprotect 2012-8-18 17:38:50 | 只看该作者
全局:
Yahtzee.java
  1. /*
  2. * File: Yahtzee.java
  3. * ------------------
  4. * This program will eventually play the Yahtzee game.
  5. */

  6. import acm.io.*;
  7. import acm.program.*;
  8. import acm.util.*;

  9. public class Yahtzee extends GraphicsProgram implements YahtzeeConstants {
  10.        
  11.         public static void main(String[] args) {
  12.                 new Yahtzee().start(args);
  13.         }
  14.        
  15.         public void run() {      
  16.                 IODialog dialog = getDialog();    // initialize the dialog
  17.                 nPlayers = dialog.readInt("Enter number of players");
  18.                 playerNames = new String[nPlayers]; // 1*n array (1-dimension array)
  19.                 for (int i = 1; i <= nPlayers; i++) {
  20.                         playerNames[i - 1] = dialog.readLine("Enter name for player " + i);  // read each name of the player
  21.                 }
  22.                 display = new YahtzeeDisplay(getGCanvas(), playerNames);
  23.                 playGame();
  24.         }
  25.        
  26.         private void playGame() {
  27.                 /** 13 times circle */
  28.                 for (int k=0; k<13; k++){
  29.                         while (playerNum <= nPlayers) {  // One round circle
  30.                                 display.printMessage("Its " + playerNames[playerNum-1]+ "'s turn now, roll the dices !");
  31.                                 diceArray = new int [N_DICE]; // create an array which have 5 slot to sore the dice number
  32.                                 for(int i=0; i<N_DICE; i++){
  33.                                         diceArray [i] = rgen.nextInt(1,6); // store N_DICE for random value 1~6
  34.                                 }
  35.                                 display.waitForPlayerToClickRoll(playerNum);
  36.                                 display.displayDice(diceArray);
  37.                                 /*1st time re-roll*/
  38.                                 display.printMessage("Hi "+playerNames[playerNum-1]+ ", select dices which you want to re-roll and roll the dices again !");
  39.                                 display.waitForPlayerToSelectDice();  
  40.                                 for (int i=0; i<5; i++){                    
  41.                                         if (display.isDieSelected(i)) diceArray[i] = rgen.nextInt(1,6);
  42.                                 }
  43.                                 display.displayDice(diceArray);
  44.                                 /*2nd time re-roll*/
  45.                                 display.printMessage("Hi "+playerNames[playerNum-1]+ ", select dices which you want to re-roll and roll the dices for your last chance !");
  46.                                 display.waitForPlayerToSelectDice();
  47.                                 for (int j=0; j<5; j++){                    
  48.                                         if (display.isDieSelected(j)) diceArray[j] = rgen.nextInt(1,6);
  49.                                 }
  50.                                 display.displayDice(diceArray); // roll end, at this time, diceArray holds the final stage of dices
  51.                                
  52.                                 display.printMessage(playerNames[playerNum-1]+ ", make your choice now !");
  53.                                 category = display.waitForPlayerToSelectCategory(); // return category number which selected by player
  54.                                 boolean p = MyYahtzeeMagic(diceArray, category); // temporary method
  55.                                 if (p == true){
  56.                                         switchCase();
  57.                                 }
  58.                                 else  display.updateScorecard(category, playerNum, 0);
  59.                                 playerNum++;
  60.                         }  // one round circle
  61.                         playerNum = 1; // reset playerNum =1
  62.                 }
  63.                 /** display upper score, lower score, total and if exist, store upper bonus */
  64.                 for (int r=1; r<=nPlayers; r++){
  65.                         display.updateScorecard(UPPER_SCORE, r, myArr[1][r]);
  66.                         display.updateScorecard(LOWER_SCORE, r, myArr[2][r]);
  67.                         if (myArr[1][r]>=63){
  68.                                 display.updateScorecard(UPPER_BONUS, r, 35);
  69.                                 myArr[0][r]=35;
  70.                         }
  71.                         else {
  72.                                 display.updateScorecard(UPPER_BONUS, r, 0);
  73.                                 myArr[0][r]=0;
  74.                         }
  75.                         display.updateScorecard(TOTAL, r, myArr[0][r]+myArr[1][r]+myArr[2][r]);
  76.                 }
  77.         }
  78.         /** whether p ==  true, put right value on the right position in screen*/
  79.         private void switchCase(){
  80.                 switch (category){
  81.                 case 1: int temp1 =0;
  82.                         for (int i=0; i<5; i++) {
  83.                                 if (diceArray[i]==1) temp1++;
  84.                         }
  85.                         display.updateScorecard(ONES, playerNum, temp1);  // display the result on the screen
  86.                         sumOfUpperCase(playerNum, temp1); // update information
  87.                         break;
  88.                 case 2: int temp2 =0;
  89.                         for (int i=0; i<5; i++) {
  90.                                 if (diceArray[i]==2) temp2=temp2+2;
  91.                         }
  92.                         display.updateScorecard(TWOS, playerNum, temp2);
  93.                         sumOfUpperCase(playerNum, temp2);
  94.                         break;
  95.                 case 3: int temp3 =0;
  96.                         for (int i=0; i<5; i++) {
  97.                                 if (diceArray[i]==3) temp3=temp3+3;
  98.                         }
  99.                         display.updateScorecard(THREES, playerNum, temp3);
  100.                         sumOfUpperCase(playerNum, temp3);
  101.                         break;
  102.                 case 4: int temp4 =0;
  103.                         for (int i =0; i<5; i++){
  104.                                 if (diceArray[i]==4) temp4=temp4+4;
  105.                         }
  106.                         display.updateScorecard(FOURS, playerNum, temp4);
  107.                         sumOfUpperCase(playerNum, temp4);
  108.                         break;
  109.                 case 5: int temp5 =0;
  110.                         for (int i =0; i<5; i++){
  111.                                 if (diceArray[i]==5) temp5=temp5+5;
  112.                         }
  113.                         display.updateScorecard(FIVES, playerNum, temp5);
  114.                         sumOfUpperCase(playerNum, temp5);
  115.                         break;
  116.                 case 6:        int temp6 =0;
  117.                         for (int i =0; i<5; i++){
  118.                                 if (diceArray[i]==6) temp6=temp6+6;
  119.                         }
  120.                         display.updateScorecard(SIXES, playerNum, temp6);
  121.                         sumOfUpperCase(playerNum, temp6);
  122.                         break;
  123.                        
  124.                 case 9: int temp9 =0;
  125.                         for (int i=0; i<5; i++){
  126.                                 temp9=temp9+diceArray[i];
  127.                         }
  128.                         display.updateScorecard(THREE_OF_A_KIND, playerNum, temp9);
  129.                         sumOfLowerCase(playerNum, temp9);
  130.                         break;
  131.                 case 10: int temp10 =0;
  132.                           for (int i=0; i<5; i++){
  133.                                  temp10=temp10+diceArray[i];
  134.                          }
  135.                          display.updateScorecard(FOUR_OF_A_KIND, playerNum, temp10);
  136.                          sumOfLowerCase(playerNum, temp10);
  137.                          break;
  138.                 case 11: display.updateScorecard(FULL_HOUSE, playerNum, 25);
  139.                          sumOfLowerCase(playerNum, 25);
  140.                          break;
  141.                 case 12: display.updateScorecard(SMALL_STRAIGHT, playerNum, 30);
  142.                          sumOfLowerCase(playerNum, 30);
  143.                          break;
  144.                 case 13: display.updateScorecard(LARGE_STRAIGHT, playerNum, 40);
  145.                          sumOfLowerCase(playerNum, 40);
  146.                          break;
  147.                 case 14: display.updateScorecard(YAHTZEE, playerNum, 50);
  148.                          sumOfLowerCase(playerNum, 50);
  149.                          break;
  150.                 case 15: int temp15 = 0;
  151.                          for (int i=0; i<5; i++){
  152.                                  temp15=temp15+diceArray[i];
  153.                          }
  154.                          display.updateScorecard(CHANCE, playerNum, temp15);
  155.                          sumOfLowerCase(playerNum, temp15);
  156.                          break;
  157.                 }
  158.         }
  159.         /** store players' upper score*/
  160.         private void sumOfUpperCase(int playerNum, int value){
  161.                 switch (playerNum){
  162.                 case 1: myArr [1][1] = myArr [1][1]+value; break;
  163.                 case 2: myArr [1][2] = myArr [1][2]+value; break;
  164.                 case 3: myArr [1][3] = myArr [1][3]+value; break;
  165.                 case 4: myArr [1][4] = myArr [1][4]+value; break;
  166.                 }
  167.         }
  168.         /** store players' lower score*/
  169.         private void sumOfLowerCase(int playerNum, int value){
  170.                 switch (playerNum){
  171.                 case 1: myArr [2][1] = myArr [2][1]+value; break;
  172.                 case 2: myArr [2][2] = myArr [2][2]+value; break;
  173.                 case 3: myArr [2][3] = myArr [2][3]+value; break;
  174.                 case 4: myArr [2][4] = myArr [2][4]+value; break;
  175.                 }
  176.         }
  177.         /** my method to determining whether a dice configuration meets the requirements*/
  178.         private boolean MyYahtzeeMagic (int[] dice, int category){
  179.                 int [] difference = new int[10];  // store 6 dices' 10 differences
  180.                 difference[0]= diceArray[0]-diceArray[1];
  181.                 difference[1]= diceArray[0]-diceArray[2];
  182.                 difference[2]= diceArray[0]-diceArray[3];
  183.                 difference[3]= diceArray[0]-diceArray[4];
  184.                 difference[4]= diceArray[1]-diceArray[2];
  185.                 difference[5]= diceArray[1]-diceArray[3];
  186.                 difference[6]= diceArray[1]-diceArray[4];
  187.                 difference[7]= diceArray[2]-diceArray[3];
  188.                 difference[8]= diceArray[2]-diceArray[4];
  189.                 difference[9]= diceArray[3]-diceArray[4];
  190.                 int counter =0;
  191.                 for (int i=0; i<10; i++){
  192.                         if (difference[i]==0) counter++;
  193.                 }
  194.         /** "three of a kind", "four of a kind", "full house", "yahtzee"*/
  195.                 if ((counter == 10)&&((category == YAHTZEE)||(category == THREE_OF_A_KIND)||(category == FOUR_OF_A_KIND))) return true;
  196.                 else if ((counter == 6)&&(category == THREE_OF_A_KIND)) return true;
  197.                 else if ((counter == 6)&&(category == FOUR_OF_A_KIND)) return true;
  198.                 else if ((counter == 4)&&(category == FULL_HOUSE)) return true;
  199.                 else if ((counter == 4)&&(category == THREE_OF_A_KIND)) return true;
  200.                 else if ((counter == 3)&&(category == THREE_OF_A_KIND)) return true;
  201.         /** "large straight", "small straight"*/
  202.                 else if (((diceHave(1)&&diceHave(2)&&diceHave(3)&&diceHave(4)&&diceHave(5))||(diceHave(2)&&diceHave(3)&&diceHave(4)&&diceHave(5)&&diceHave(6)))&&(category == LARGE_STRAIGHT)) return true;
  203.                 else if (((diceHave(1)&&diceHave(2)&&diceHave(3)&&diceHave(4))||(diceHave(2)&&diceHave(3)&&diceHave(4)&&diceHave(5))||(diceHave(3)&&diceHave(4)&&diceHave(5)&&diceHave(6)))&&(category == SMALL_STRAIGHT)) return true;
  204.                 /** "ones, twos, threes, fours, fives, sixes", "chance"*/
  205.                 else if (category == ONES) return true;
  206.                 else if (category == TWOS) return true;
  207.                 else if (category == THREES) return true;
  208.                 else if (category == FOURS) return true;
  209.                 else if (category == FIVES) return true;
  210.                 else if (category == SIXES) return true;
  211.                 else if (category == CHANCE) return true;
  212.                 return false;
  213.         }
  214.         /** testing whether diceArray have the particular dice value*/
  215.         private boolean diceHave(int compareValue){
  216.                 for (int i=0; i<5; i++){
  217.                         if (diceArray[i]== compareValue) return true;
  218.                 }
  219.                 return false;
  220.         }
  221.     /** Private instance variables */
  222.         private int nPlayers;      // variable "nPlayers" refer to number of players
  223.         private String[] playerNames;   // input players' name and store them in String
  224.         private YahtzeeDisplay display;  // YahtzeeDisplay class's object "display"
  225.         private RandomGenerator rgen = new RandomGenerator(); // ---
  226.         private int[] diceArray;  // diceArray store the dice's value
  227.         private int category;
  228.         private int playerNum =1;  // point to the first player
  229.         private int [][] myArr = new int [3][5]; // create an array to store the upper and lower score
  230. }
复制代码
YahtzeeConstants.java
  1. /*
  2. * File: YahtzeeConstants.java
  3. * ---------------------------
  4. * This file declares several constants that are shared by the
  5. * different modules in the Yahtzee game.
  6. */

  7. public interface YahtzeeConstants {

  8. /** The width of the application window */
  9.         public static final int APPLICATION_WIDTH = 600;

  10. /** The height of the application window */
  11.         public static final int APPLICATION_HEIGHT = 350;

  12. /** The number of dice in the game */
  13.         public static final int N_DICE = 5;

  14. /** The maximum number of players */
  15.         public static final int MAX_PLAYERS = 4;

  16. /** The total number of categories */
  17.         public static final int N_CATEGORIES = 17;

  18. /** The number of categories in which the player can score */
  19.         public static final int N_SCORING_CATEGORIES = 13;

  20. /** The constants that specify categories on the scoresheet */
  21.         public static final int ONES = 1;
  22.         public static final int TWOS = 2;
  23.         public static final int THREES = 3;
  24.         public static final int FOURS = 4;
  25.         public static final int FIVES = 5;
  26.         public static final int SIXES = 6;
  27.         public static final int UPPER_SCORE = 7;
  28.         public static final int UPPER_BONUS = 8;
  29.         public static final int THREE_OF_A_KIND = 9;
  30.         public static final int FOUR_OF_A_KIND = 10;
  31.         public static final int FULL_HOUSE = 11;
  32.         public static final int SMALL_STRAIGHT = 12;
  33.         public static final int LARGE_STRAIGHT = 13;
  34.         public static final int YAHTZEE = 14;
  35.         public static final int CHANCE = 15;
  36.         public static final int LOWER_SCORE = 16;
  37.         public static final int TOTAL = 17;
  38.   
  39. }
复制代码


回复

使用道具 举报

无效楼层,该帖已经被删除
全局:
又是先开始想着往深了写,到最后细节懒得写了…………实在懒得改中间的message了,就这样吧…………
验证的方式都十分弱智,完全不能generalize的类型
  1. /*
  2. * File: Yahtzee.java
  3. * ------------------
  4. * This program will eventually play the Yahtzee game.
  5. */

  6. import acm.io.*;
  7. import acm.program.*;
  8. import acm.util.*;

  9. public class Yahtzee extends GraphicsProgram implements YahtzeeConstants {
  10.        
  11.         public static void main(String[] args) {
  12.                 new Yahtzee().start(args);
  13.         }
  14.        
  15.         public void run() {
  16.                 IODialog dialog = getDialog();
  17.                 nPlayers = dialog.readInt("Enter number of players");
  18.                 playerNames = new String[nPlayers];
  19.                 for (int i = 1; i <= nPlayers; i++) {
  20.                         playerNames[i - 1] = dialog.readLine("Enter name for player " + i);
  21.                 }
  22.                 display = new YahtzeeDisplay(getGCanvas(), playerNames);
  23.                 playGame();
  24.         }

  25.         private void playGame() {
  26.                 /* You fill this in */
  27.                 for(int x=0;x<MAX_PLAYERS;x++){
  28.                         for(int y=0;y<17;y++){
  29.                                 use[x][y]=-1;
  30.                         }
  31.                 }
  32.                        
  33.                 for(int h=1;h<=N_SCORING_CATEGORIES;h++){
  34.                         for(int i=1;i<=nPlayers;i++){
  35.                                 display.waitForPlayerToClickRoll(i);
  36.                                 rolldice();
  37.                                
  38.                                 display.displayDice(dice);
  39.                                 for(int j=0;j<2;j++){
  40.                                                                        
  41.                                         display.waitForPlayerToSelectDice();
  42.                                         for(int k=0;k<N_DICE;k++){
  43.                                                 if(display.isDieSelected(k)){
  44.                                                         dice[k]=rgen.nextInt(1,6);
  45.                                                        
  46.                                                 }
  47.                                                
  48.                                         }
  49.                                         display.displayDice(dice);
  50.                                 }
  51.                                 category=display.waitForPlayerToSelectCategory();
  52.                                 while(use[i][category]!=-1){
  53.                                         display.printMessage("category used, try again.");
  54.                                         category=display.waitForPlayerToSelectCategory();
  55.                                 }
  56.                                                                
  57.                                 display.updateScorecard(category,i,score(i));
  58.                                 use[i][category]=score(i);
  59.                                 display.printMessage(message());
  60.                                

  61.                                 }
  62.                 }
  63.                         int highscore=0;
  64.                         int[] player=new int[MAX_PLAYERS+1];
  65.                         for(int i=0;i<=MAX_PLAYERS;i++){
  66.                                 player[i]=0;
  67.                         }
  68.                        
  69.                         for(int i=1;i<=nPlayers;i++){
  70.                                 int temp=0;
  71.                                 int sumofUpper=use[i][1]+use[i][2]+use[i][3]+use[i][4]+use[i][5]+use[i][6];
  72.                                 int sumofLower=use[i][9]+use[i][10]+use[i][11]+use[i][12]+use[i][5]+use[i][13]+use[i][14]+use[i][15];
  73.                                 display.updateScorecard(7,i,sumofUpper);
  74.                                 display.updateScorecard(16,i,sumofLower);
  75.                                 if(sumofUpper>=63){
  76.                                        
  77.                                         display.updateScorecard(8,i,35);
  78.                                         temp=35;
  79.                                 } else {
  80.                                         display.updateScorecard(8,i,0);
  81.                                         temp=0;
  82.                                 }
  83.                                 int totalscore=sumofUpper+sumofLower+temp;
  84.                                 display.updateScorecard(17,i,totalscore);
  85.                                
  86.                                 totalscore=player[i];
  87.                                
  88.                                 if(totalscore>=highscore){
  89.                                         highscore=totalscore;
  90.                                 }
  91.                         }
  92.                         int i=0;
  93.                         if(player[1]>player[2]&&player[1]>player[3]&&player[1]>player[4]){
  94.                                 i=1;
  95.                         } else if(player[2]>player[1]&&player[2]>player[3]&&player[2]>player[4]){
  96.                                 i=2;
  97.                         } else if(player[3]>player[2]&&player[3]>player[1]&&player[3]>player[4]){
  98.                                 i=3;
  99.                         } else if(player[4]>player[2]&&player[4]>player[3]&&player[4]>player[1]){
  100.                                 i=4;
  101.                         }
  102.                         display.printMessage("Congratulations, "+playerNames[i]+", you are the winner with a total score of "+player[i]);
  103.                        
  104.                        
  105.                        
  106.                
  107.                        
  108.         }
  109.        
  110.        
  111.        
  112.        
  113.         private int score(int i){
  114.                
  115.                 if(category==1){
  116.                        
  117.                         return(firstSix(1));
  118.                
  119.                
  120.                 }else if(category==2){
  121.                         return(firstSix(2));
  122.                        
  123.                
  124.                 }else if(category==3){
  125.                         return(firstSix(3));
  126.                
  127.                 }
  128.                 else if(category==4){
  129.                         return(firstSix(4));
  130.                        
  131.                 }
  132.                 else if (category==5){
  133.                         return(firstSix(5));
  134.                 }
  135.                 else if (category==6){
  136.                         return(firstSix(6));
  137.                 }
  138.                 else if (category==9){
  139.                         if(nine()){
  140.                                 return(dice[0]+dice[1]+dice[2]+dice[3]+dice[4]);
  141.                         } else return(0);
  142.                                        
  143.                 }
  144.                 else if (category==10){
  145.                         if(ten()){
  146.                                 return(dice[0]+dice[1]+dice[2]+dice[3]+dice[4]);
  147.                         } else return(0);
  148.                 }
  149.                 else if (category==11){
  150.                         int a=dice[0];

  151.                         if(a==dice[1]){
  152.                                 if(a==dice[2]){
  153.                                         if(dice[3]==dice[4] && dice[3]!=a)return (25);
  154.                                         else return (0);
  155.                                 }else if(dice[2]!=a&&dice[3]==a){
  156.                                         if(dice[2]==dice[4])return (25);
  157.                                         else return (0);
  158.                                 }else if(dice[2]!=a&&dice[2]==dice[3]&&dice[3]==dice[4]){return (25);
  159.                                 }else return (0);
  160.                         }else if(a!=dice[1]&&a==dice[2]){
  161.                                 if(dice[3]==a){
  162.                                         if(dice[1]==dice[4])return (25);
  163.                                         else return (0);
  164.                                 }else if(dice[3]!=a&&dice[4]==a){
  165.                                         if(dice[1]==dice[3])return (25);
  166.                                         else return (0);
  167.                                 }else if(dice[1]==dice[3]&& dice[3]==dice[4]) {return (25);
  168.                                 }else return (0);
  169.                         }else if(a!=dice[1]&&a!=dice[2]&&a==dice[3]){
  170.                                 if(a==dice[4]){
  171.                                         if(dice[1]==dice[2])return (25);
  172.                                         else return (0);
  173.                                 }else if(dice[1]==dice[2]&& dice[2]==dice[4]){return (25);
  174.                         }else return(0);
  175.                         }else if(a!=dice[1]&&a!=dice[2]&&a!=dice[3]&&a==dice[4]){
  176.                                 if(dice[1]==dice[2]&&dice[2]==dice[3])return (25);
  177.                                 else return (0);
  178.                         }
  179.                                 else return (0);
  180.                                
  181.                 }
  182.                 else if (category==12){
  183.                         if(twelve()){
  184.                                 return(30);
  185.                         } else return(0);
  186.                 }
  187.                 else if (category==13){
  188.                         if(thirteen()){
  189.                                 return(40);
  190.                         }else return(0);
  191.                 }
  192.                 else if (category==14){
  193.                         if(dice[0]==dice[1]&&dice[1]==dice[2]&&dice[2]==dice[3]&&dice[3]==dice[4]&&dice[4]==dice[5]){
  194.                                 return(50);
  195.                         } else return(0);
  196.                 }
  197.                 else if (category==15){
  198.                         return(dice[0]+dice[1]+dice[2]+dice[3]+dice[4]);
  199.                 }
  200.                 else return (0);
  201.                 }
  202.        
  203.         private boolean thirteen(){
  204.                 int a=0;
  205.                 int b=0;
  206.                 int c=0;
  207.                 int d=0;
  208.                 for(int i=0;i<5;i++){
  209.                         if(dice[i]==2){
  210.                                 a++;
  211.                         }
  212.                         if(dice[i]==3){
  213.                                 b++;
  214.                         }
  215.                         if(dice[i]==4){
  216.                                 c++;
  217.                         }
  218.                         if(dice[i]==5){
  219.                                 d++;
  220.                         }
  221.                        
  222.                 }
  223.                 if(a>0&&b>0&&c>0&&d>0){
  224.                         if(dice[0]==1||dice[1]==1||dice[2]==1||dice[3]==1||dice[4]==1||
  225.                                         dice[0]==6||dice[1]==6||dice[2]==6||dice[3]==6||dice[4]==6){
  226.                                 return true;
  227.                         }else return false;
  228.                 }else return false;
  229.         }
  230.        
  231.         private boolean twelve(){
  232.                 int a=0;
  233.                 int b=0;
  234.                 for(int i=0;i<5;i++){
  235.                         if(dice[i]==3){
  236.                                 a++;
  237.                         }
  238.                         if(dice[i]==4){
  239.                                 b++;
  240.                         }
  241.                 }
  242.                 if (a>0 && b>0){
  243.                         if(dice[0]==2||dice[1]==2||dice[2]==2||dice[3]==2||dice[4]==2){
  244.                                 if(dice[0]==1||dice[1]==1||dice[2]==1||dice[3]==1||dice[4]==1||
  245.                                                 dice[0]==5||dice[1]==5||dice[2]==5||dice[3]==5||dice[4]==5){
  246.                                         return true;
  247.                                 }else return false;
  248.                         }else if(dice[0]==6||dice[1]==6||dice[2]==6||dice[3]==6||dice[4]==6){
  249.                                 if(dice[0]==5||dice[1]==5||dice[2]==5||dice[3]==5||dice[4]==5){
  250.                                         return true;
  251.                                 }else return false;
  252.                         }else return false;
  253.                 }else return false;
  254.         }
  255.        
  256.         private boolean nine(){
  257.                
  258.                         int a=7;
  259.                         int b=8;
  260.                         int c=9;
  261.                         for(int i=0;i<3;i++){
  262.                                 for(int j=i+1;j<4;j++){
  263.                                         for(int k=j+1;k<5;k++){
  264.                                                 a=dice[i];
  265.                                                 b=dice[j];
  266.                                                 c=dice[k];
  267.                                                 if(a==b&&b==c) return true;
  268.                                                 }
  269.                                         }
  270.                                 }
  271.                         return false;
  272.         }
  273.        
  274.         private boolean ten(){
  275.                
  276.                 int a=7;
  277.                 int b=8;
  278.                 int c=9;
  279.                 int d=10;
  280.                 for(int i=0;i<2;i++){
  281.                         for(int j=i+1;j<3;j++){
  282.                                 for(int k=j+1;k<4;k++){
  283.                                         for(int l=k+1;l<5;l++){
  284.                                         a=dice[i];
  285.                                         b=dice[j];
  286.                                         c=dice[k];
  287.                                         d=dice[l];
  288.                                         if(a==b&&b==c&&c==d) return true;
  289.                                         }
  290.                                 }
  291.                                 }
  292.                         }
  293.                 return false;
  294. }
  295.        
  296.         private int firstSix(int x){
  297.                 int a=0;
  298.                 for(int i=0;i<N_DICE;i++){
  299.                         if(dice[i]==x){
  300.                                 a++;
  301.                                 }
  302.                 }
  303.                         return(a*x);
  304.         }
  305.                

  306.         private String message(){
  307.                 return("a");
  308.         }
  309.        
  310.        
  311.         private void rolldice(){
  312.                 for (int i=0;i<N_DICE;i++){
  313.                        
  314.                         dice[i]=rgen.nextInt(1,6);
  315.                
  316.                 }
  317.         }
  318.        
  319.                        
  320.         private int[][] use=new int[MAX_PLAYERS+1][17];
  321.         private int category;
  322.         private int[] dice=new int[N_DICE];
  323. /* Private instance variables */
  324.         private int nPlayers;
  325.         private String[] playerNames;
  326.         private YahtzeeDisplay display;
  327.         private RandomGenerator rgen = new RandomGenerator();

  328. }


  329. //things left: 9-11, finish the game and add up scores, label
复制代码
回复

使用道具 举报

🔗
sewen67 2013-8-3 01:00:36 | 只看该作者
全局:
/*
* File: Yahtzee.java
* ------------------
* This program will eventually play the Yahtzee game.
*/

import acm.io.*;
import acm.program.*;
import acm.util.*;

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:
                       
                case 17:
                }
        }
               
/* Private instance variables */
        private int playerTotal;
        private int[] sum=new int[4];       
        private int[][] scores=new int[4][17];
        private int category;
        private int[][] checkWhetherCategory=new int[4][17];
        private int nPlayers;
        private String[] playerNames;
        private YahtzeeDisplay display;
        private RandomGenerator rgen = new RandomGenerator();
        private int[] dice=new int[5];
}
回复

使用道具 举报

🔗
jby1797 2014-1-14 17:03:43 | 只看该作者
全局:
感觉大段代码最大的坏处就是看着眼睛好痛啊
有时间一定把上面的代码都看一遍。

我觉得这个作业最难的应该就是在lower score的validity了
个人是先把所有dices 从小到大排序,然后再用for循环来挑是否有连续三个一样,如果有,进入到下一个method,判断三个一样的后一个是否与这三个一样。
用一个instance variable来存int,3就是三个一样,4就是四个一样。
然后再判断是否五个一样。
这样就避免了点three of a kind 但是有四个一样,这个bug

straight,我是用一个ArrayList, 用contains命令判断五个里面是否包含1,2,3,4(,5);或者2,3,4,5(,6);或者3,4,5,6.(用for循环)

  • 下面上传我和yanyan游戏的截屏






评分

参与人数 1学分 +1 收起 理由
sanguine + 1 好赞阿!!!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表