斯坦福CS106A作业5
这次作业是设计yahtzee这款游戏。
/*
* 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");
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() {
int[] upper=new int[nPlayers];
int[] lower=new int[nPlayers];
//记录哪些访问过
int[][] pick=new int[nPlayers][N_CATEGORIES];
for(int l=0;l<N_SCORING_CATEGORIES;l++) {
for(int k=1;k<=nPlayers;k++) {
//display.printMessage展示信息
display.printMessage(k+"'s turn! , Click the \"Roll Dice\" button to roll the dice");
//选手1
display.waitForPlayerToClickRoll(k);
//展示骰子
int[] a=new int[5];
for(int i=0;i<5;i++) {
a[i]=rgen.nextInt(1, 6);
}
display.displayDice(a);
for(int j=0;j<2;j++) {
//display.printMessage展示信息
display.printMessage("Select the dice you wish to re-roll and click \"Roll Again\"");
//修改骰子
display.waitForPlayerToSelectDice();
for (int i=0;i<5;i++) {
if(display.isDieSelected(i)) {
a[i]=rgen.nextInt(1, 6);
}
}
display.displayDice(a);
}
display.printMessage("Select a category for this roll");
//获得类别
int category;
//判断是否选取过,如果选取过一直做循环
do{
category = display.waitForPlayerToSelectCategory();
display.printMessage("You already picked that category.Please choose another category");
}while(pick[k-1][category-1]!=0);
pick[k-1][category-1]=1;
//计算分数
int score=getScore(a,category);
//更新lower,upper
if (category<=SIXES) {
upper[k-1]+=score;
}else {
lower[k-1]+=score;
}
//展示分数
display.updateScorecard(category, k, score);
}
}
for(int k=1;k<=nPlayers;k++) {
//计算bonus
int bonus=0;
if(upper[k-1]>=63) {
bonus=35;
}
int total=upper[k-1]+bonus+lower[k-1];
display.updateScorecard(UPPER_SCORE, k, upper[k-1]);
display.updateScorecard(UPPER_BONUS, k, bonus);
display.updateScorecard(LOWER_SCORE, k, lower[k-1]);
display.updateScorecard(TOTAL, k, total);
}
}
//这里构造一个数组,计算13项的分别得分,然后和category匹配,注意这里的category范围
private int getScore(int[] dice,int category) {
//记录每一项对应的得分
int[] score=new int[N_SCORING_CATEGORIES];
//记录次数
int[] cnt=new int[6];
//记录总分
int chance=0;
//遍历之后前6已经计算完成
for (int i=0;i<N_DICE;i++) {
int num=dice[i];
score[num-1]+=num;
cnt[num-1]+=1;
chance+=num;
}
//更新7,8,12
//记录2,3的次数
int three=0;
int two=0;
for (int i=0;i<6;i++) {
if(cnt[i]>=3) {
score[6]=chance;
}
if(cnt[i]>=4) {
score[7]=chance;
}
if(cnt[i]==5) {
score[11]=50;
}
//记录2,3的次数
if(cnt[i]==3) {
three=1;
}
if(cnt[i]==2) {
two=1;
}
}
//更新9
if(three==1 && two==1) {
score[8]=25;
}
//更新10,11
if((cnt[0]>0&&cnt[1]>0&&cnt[2]>0&&cnt[3]>0)|
(cnt[4]>0&&cnt[1]>0&&cnt[2]>0&&cnt[3]>0)|
(cnt[4]>0&&cnt[5]>0&&cnt[2]>0&&cnt[3]>0)) {
score[9]=30;
}
if((cnt[0]>0&&cnt[1]>0&&cnt[2]>0&&cnt[3]>0&&cnt[4]>0)|
(cnt[1]>0&&cnt[2]>0&&cnt[3]>0&&cnt[4]>0&&cnt[5]>0)) {
score[10]=40;
}
//更新13
score[12]=chance;
for(int i=0;i<N_SCORING_CATEGORIES;i++) {
println(score[i]);
}
int index;
if(category<=SIXES) {
index=category;
}else {
index=category-2;
}
return score[index-1];
}
/* Private instance variables */
private int nPlayers;
private String[] playerNames;
private YahtzeeDisplay display;
private RandomGenerator rgen = new RandomGenerator();
}
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Doraemonzzz!
评论
ValineLivere