活跃农民
- 积分
- 928
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-2-28
- 最后登录
- 1970-1-1
|
Yahtzee.java- /*
- * 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 void run() {
- setupPlayers();
- initDisplay();
- playGame();
- }
-
- /**
- * Prompts the user for information about the number of players, then sets up the
- * players array and number of players.
- */
- private void setupPlayers() {
- nPlayers = chooseNumberOfPlayers();
-
- /* Set up the players array by reading names for each player. */
- playerNames = new String[nPlayers];
- for (int i = 0; i < nPlayers; i++) {
- /* IODialog is a class that allows us to prompt the user for information as a
- * series of dialog boxes. We will use this here to read player names.
- */
- IODialog dialog = getDialog();
- playerNames[i] = dialog.readLine("Enter name for player " + (i + 1));
- }
- }
-
- /**
- * Prompts the user for a number of players in this game, reprompting until the user
- * enters a valid number.
- *
- * @return The number of players in this game.
- */
- private int chooseNumberOfPlayers() {
- /* See setupPlayers() for more details on how IODialog works. */
- IODialog dialog = getDialog();
-
- while (true) {
- /* Prompt the user for a number of players. */
- int result = dialog.readInt("Enter number of players");
-
- /* If the result is valid, return it. */
- if (result > 0 && result <= MAX_PLAYERS)
- return result;
-
- dialog.println("Please enter a valid number of players.");
- }
- }
-
- /**
- * Sets up the YahtzeeDisplay associated with this game.
- */
- private void initDisplay() {
- display = new YahtzeeDisplay(getGCanvas(), playerNames);
- }
- /**
- * Actually plays a game of Yahtzee. This is where you should begin writing your
- * implementation.
- */
- private void playGame(){
- totalScore = new int[nPlayers];
- upperScore = new int[nPlayers];
- used_category = new boolean[nPlayers][17];
-
- for(int round = 1; round <= 14; round++){
- for(int i = 0; i< nPlayers; i++){
- /** First round */
- display.printMessage(playerNames[i] + "'s turn! Click \"Roll Dice\"button to roil the dice ");
- display.waitForPlayerToClickRoll(i);
- for(int dice_num = 0; dice_num < N_DICE; dice_num++){
- dice_ar[dice_num] = rgen.nextInt(1,6);
- }
- display.displayDice(dice_ar);
-
- /** Next two rounds */
- for(int ch_round=0; ch_round<2; ch_round++){
- display.printMessage("Select the dice you wish to re-roll and click \"Roll Again\".");
- display.waitForPlayerToSelectDice();
- for(int dice_chosed = 0; dice_chosed < N_DICE; dice_chosed++){
- if(display.isDieSelected(dice_chosed))
- dice_ar[dice_chosed] = rgen.nextInt(1,6);
- }
- display.displayDice(dice_ar);
- }
- choseCategory(i);
- }
- }
- judgeWinner();
- }
-
- /** Select category and update total scores */
- private void choseCategory(int i){
- int category;
- /** Check if category has been scored */
- display.printMessage("Select a category for this roll.");
- do{
- category = display.waitForPlayerToSelectCategory();
- }while(used_category[i][category]);
- used_category[i][category] = true;
-
- int score = computeScore(dice_ar,category);
- display.updateScorecard(category, i, score);
- totalScore[i] += score;
- display.updateScorecard(16, i, totalScore[i]);
- if( category < 6 ) upperScore[i]+= score;
- }
- /** Compute scores to update scorecard */
- private int computeScore(int ar_dice[],int category){
- int scores = 0;
- if(category < 6) category++; else category--; //adjust
- /* n's */
- if ( category >=1 && category<=6){
- for(int i=0; i<N_DICE; i++)
- if ( ar_dice[i] == category)
- scores += category;
- return scores;
- }else{
- int[] kind = new int[6];
- int dice_sum = 0, num_kind = 0;
- boolean flag = false;
-
- for(int i=0; i<N_DICE; i++){
- if(kind[ar_dice[i]-1] == 0) num_kind++;
- dice_sum += ar_dice[i];
- kind[ar_dice[i]-1]++;
- if((kind[ar_dice[i]-1]>=3 && category==7)||(kind[ar_dice[i]-1]==4 && category==8))
- flag = true;
- }
- /* Compute 7,8,9,12,13 */
- if ((category==7 || category==8 && flag) || category==13) return dice_sum;
- if (category == 12 && num_kind == 1) return 50; //Yahtzee
- if (category == 9 && num_kind == 2) return 25; //House
-
- /* Compute 10,11 */
- if( category == 10 || category == 11){
- int straight=0,st_temp= 0;
- for(int i = 0; i < 6; i++)
- if(kind[i] > 0) st_temp++;
- else if(st_temp > straight) straight = st_temp;
- if(st_temp > straight) straight = st_temp;
- if(category == 10 && straight >= 4) return 30;
- else if (straight == 5) return 40;
- }
- }
- return scores;
- }
-
- /** Judge who is the winner */
- private void judgeWinner(){
- int winner = 0;
-
- for(int i=0; i<nPlayers; i++){
- display.updateScorecard(6, i, upperScore[i]);
- display.updateScorecard(15, i, totalScore[i]-upperScore[i]);
- if(upperScore[i] > 63) {
- totalScore[i] += 35;
- display.updateScorecard(7, i, 35);
- display.updateScorecard(16, i, totalScore[i]);
- }else display.updateScorecard(7, i, 0);
- if(totalScore[i] > totalScore[winner]) winner = i;
- }
- display.printMessage("Congratulations,"+playerNames[winner]+",you're the winner " +
- "with a total score of "+totalScore[winner]+"!");
- }
-
- /* Private instance variables */
- private int nPlayers;
- private String[] playerNames;
- private YahtzeeDisplay display;
- private RandomGenerator rgen = RandomGenerator.getInstance();
- private int[] dice_ar = new int[N_DICE];
- private int[] totalScore;
- private int[] upperScore;
- private boolean[][] used_category;
- }
复制代码 YahtzeeConstants.java- /*
- * File: YahtzeeConstants.java
- * ---------------------------
- * This file declares several constants that are shared by the
- * different modules in the Yahtzee game.
- */
- public interface YahtzeeConstants {
- /** The width of the application window */
- public static final int APPLICATION_WIDTH = 600;
- /** The height of the application window */
- public static final int APPLICATION_HEIGHT = 350;
- /** The number of dice in the game */
- public static final int N_DICE = 5;
- /** The maximum number of players */
- public static final int MAX_PLAYERS = 4;
- /** The total number of categories */
- public static final int N_CATEGORIES = 17;
- /** The number of categories in which the player can score */
- public static final int N_SCORING_CATEGORIES = 13;
- /** The constants that specify categories on the scoresheet */
- public static final int ONES = 0;
- public static final int TWOS = 1;
- public static final int THREES = 2;
- public static final int FOURS = 3;
- public static final int FIVES = 4;
- public static final int SIXES = 5;
- public static final int UPPER_SCORE = 6;
- public static final int UPPER_BONUS = 7;
- public static final int THREE_OF_A_KIND = 8;
- public static final int FOUR_OF_A_KIND = 9;
- public static final int FULL_HOUSE = 10;
- public static final int SMALL_STRAIGHT = 11;
- public static final int LARGE_STRAIGHT = 12;
- public static final int YAHTZEE = 13;
- public static final int CHANCE = 14;
- public static final int LOWER_SCORE = 15;
- public static final int TOTAL = 16;
- }
复制代码 |
|