-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh10_1.java
More file actions
130 lines (109 loc) · 2.68 KB
/
Copy pathCh10_1.java
File metadata and controls
130 lines (109 loc) · 2.68 KB
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
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Ch10_1
{
/*游戏三种状态*/
public static enum Status
{
CONTINUE, WON, LOST
};
/*生成随机数对象*/
public static Random random = new Random();
/*游戏规则中的几个常量*/
private final static int SNAKE_EYES = 2;
private final static int TREY = 3;
private final static int SEVEN = 7;
private final static int YO_LEVEN = 11;
private final static int BOX_CARS = 12;
private List<String> results;//存放游戏的整个输赢状态
private int w;//赢
private int l;//输
public Ch10_1()
{
results = new ArrayList<String>();
w = 0;
l = 0;
}
public void play()
{
int myPoint = 0;
Status gameStatus = Status.CONTINUE;
int sumOfDice = rollDice(); //掷骰子
/*根据骰子的点数和修改游戏状态*/
switch (sumOfDice)
{
case SEVEN:
case YO_LEVEN:
gameStatus = Status.WON;
break;
case SNAKE_EYES:
case TREY:
case BOX_CARS:
gameStatus = Status.LOST;
break;
}
if(gameStatus == Status.CONTINUE)
{
myPoint = sumOfDice;
System.out.printf("点数是 %d\n", myPoint);
}
/*如果游戏状态为继续,继续掷骰子*/
while (gameStatus == Status.CONTINUE)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = Status.WON;
else if (sumOfDice == SEVEN)
gameStatus = Status.LOST;
}
/*输出本次游戏的结果,并记录到最终的游戏状态中*/
if (gameStatus == Status.WON)
{
System.out.println("玩家赢");
results.add("赢");
w++;
}
else
{
System.out.println("玩家输");
results.add("输");
l++;
}
}
/*掷骰子*/
public int rollDice()
{
/*随机生成点数*/
int die1 = 1 + random.nextInt(6);
int die2 = 1 + random.nextInt(6);
int sum = die1 + die2;
System.out.printf("玩家掷的点数 %d + %d = %d\n", die1, die2, sum);
return sum;
}
/*打印最终结果*/
public void print()
{
System.out.println("游戏结果统计:");
System.out.println("赢的次数: " + w);
System.out.println("输的次数: " + l);
int i = 0;
for(String rs: results)
{
System.out.println("第 "+(++i)+"次: " +rs);
}
}
public static void main(String[] args)
{
String answer;
Ch10_1 game=new Ch10_1();
do{
game.play();
System.out.println("继续游戏吗(y/n)?");
Scanner input = new Scanner(System.in);
answer = input.next();
}while("Y".equalsIgnoreCase(answer));
game.print();
}
}