斯坦福CS106A作业5

这次作业是设计yahtzee这款游戏。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* 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();

}