Craps游戏规则:
玩家掷两个骰子,每个骰子6个面,点数分别为1到6。骰子停下来后,计算上面两个骰子的点数之和,若第一次投掷的点数之和为7或11,则玩家赢,如果点数之和为2、3和12(称为Craps),则玩家输,庄家赢,如果点数之和为4,5,6,8,9或10,则这个和就成为玩家的“点数”,继续掷骰子。如投掷的点数之和等于点数,则玩家赢;否则,再掷骰子直至分出输赢。
练习知识点:容器及容器的布局管理器;事件监听器的使用;label、TextField、Button等控件的使用;事件驱动思想;控制结构使用;方法的定义及使用等。
程序代码如下:
1
/**
2
* @(#)CrapsGame.java
3
*
4
* CrapsGame application
5
*
6
* @author
7
* @version 1.00 2008/9/19
8
*/
9
10
import java.awt.*;
11
import java.awt.event.*;
12
13
import javax.swing.*;
14
15
16
public class CrapsGame extends JApplet implements ActionListener{
17
18
final int WON=0,LOSE=1,CONTINUE=2;
19
boolean FirstRoll=true;
20
int SumOfDice=0;
21
int MyPoint=0;
22
int GameStatus=CONTINUE;
23
24
JLabel Dice1L,Dice2L,SumL,PointL;
25
JTextField Dice1T,Dice2T,SumT,PointT;
26
JButton RollButton;
27
28
public void init(){
29
Container cont=getContentPane();
30
cont.setLayout(new FlowLayout());
31
32
Dice1L=new JLabel("Dice1:");
33
cont.add(Dice1L);
34
Dice1T=new JTextField(10);
35
Dice1T.setEnabled(false);
36
cont.add(Dice1T);
37
38
Dice2L=new JLabel("Dice2:");
39
cont.add(Dice2L);
40
Dice2T=new JTextField(10);
41
Dice2T.setEnabled(false);
42
cont.add(Dice2T);
43
44
SumL=new JLabel("Sum :");
45
cont.add(SumL);
46
SumT=new JTextField(10);
47
SumT.setEnabled(false);
48
cont.add(SumT);
49
50
PointL=new JLabel("Point:");
51
cont.add(PointL);
52
PointT=new JTextField(10);
53
PointT.setEnabled(false);
54
cont.add(PointT);
55
56
RollButton=new JButton("Roll Dice");
57
RollButton.addActionListener(this);
58
cont.add(RollButton);
59
}
60
61
public void actionPerformed(ActionEvent actionEvent){
62
if(FirstRoll){
63
SumOfDice=rollDice();
64
switch(SumOfDice){
65
case 7:case 11:
66
GameStatus=WON;
67
PointT.setText("");
68
break;
69
case 2:case 3:case 12:
70
GameStatus=LOSE;
71
PointT.setText("");
72
break;
73
default:
74
GameStatus=CONTINUE;
75
FirstRoll=false;
76
MyPoint=SumOfDice;
77
PointT.setText(Integer.toString(MyPoint));
78
break;
79
}
80
}else{
81
SumOfDice=rollDice();
82
if(SumOfDice==MyPoint)
83
GameStatus=WON;
84
else if(SumOfDice==7)
85
GameStatus=LOSE;
86
}
87
DisplayMessage();
88
}
89
90
public int rollDice(){
91
int dice1,dice2,sum;
92
dice1=(int)(Math.random()*6+1);
93
dice2=(int)(Math.random()*6+1);
94
sum=dice1+dice2;
95
96
Dice1T.setText(Integer.toString(dice1));
97
Dice2T.setText(Integer.toString(dice2));
98
SumT.setText(Integer.toString(sum));
99
100
return sum;
101
}
102
103
public void DisplayMessage(){
104
if(GameStatus==CONTINUE)
105
showStatus("Roll Again!");
106
else{
107
if(GameStatus==WON)
108
showStatus("You are the WINNER!"+" Click Roll Dice to Play Again!");
109
else
110
showStatus("You are the Loser!"+" Click Roll Dice to Play Again!");
111
FirstRoll=true;
112
113
}
114
}
115
}
116
/**2
* @(#)CrapsGame.java3
*4
* CrapsGame application5
*6
* @author 7
* @version 1.00 2008/9/198
*/9

10
import java.awt.*;11
import java.awt.event.*;12

13
import javax.swing.*;14

15
16
public class CrapsGame extends JApplet implements ActionListener{17
18
final int WON=0,LOSE=1,CONTINUE=2;19
boolean FirstRoll=true;20
int SumOfDice=0;21
int MyPoint=0;22
int GameStatus=CONTINUE;23
24
JLabel Dice1L,Dice2L,SumL,PointL;25
JTextField Dice1T,Dice2T,SumT,PointT;26
JButton RollButton;27
28
public void init(){29
Container cont=getContentPane();30
cont.setLayout(new FlowLayout());31
32
Dice1L=new JLabel("Dice1:");33
cont.add(Dice1L);34
Dice1T=new JTextField(10);35
Dice1T.setEnabled(false);36
cont.add(Dice1T);37
38
Dice2L=new JLabel("Dice2:");39
cont.add(Dice2L);40
Dice2T=new JTextField(10);41
Dice2T.setEnabled(false);42
cont.add(Dice2T);43
44
SumL=new JLabel("Sum :");45
cont.add(SumL);46
SumT=new JTextField(10);47
SumT.setEnabled(false);48
cont.add(SumT); 49
50
PointL=new JLabel("Point:");51
cont.add(PointL);52
PointT=new JTextField(10);53
PointT.setEnabled(false);54
cont.add(PointT);55
56
RollButton=new JButton("Roll Dice");57
RollButton.addActionListener(this);58
cont.add(RollButton); 59
}60
61
public void actionPerformed(ActionEvent actionEvent){62
if(FirstRoll){63
SumOfDice=rollDice();64
switch(SumOfDice){65
case 7:case 11:66
GameStatus=WON;67
PointT.setText("");68
break;69
case 2:case 3:case 12:70
GameStatus=LOSE;71
PointT.setText("");72
break;73
default:74
GameStatus=CONTINUE;75
FirstRoll=false;76
MyPoint=SumOfDice;77
PointT.setText(Integer.toString(MyPoint));78
break;79
}80
}else{81
SumOfDice=rollDice();82
if(SumOfDice==MyPoint)83
GameStatus=WON;84
else if(SumOfDice==7)85
GameStatus=LOSE;86
}87
DisplayMessage();88
}89
90
public int rollDice(){91
int dice1,dice2,sum;92
dice1=(int)(Math.random()*6+1);93
dice2=(int)(Math.random()*6+1);94
sum=dice1+dice2;95
96
Dice1T.setText(Integer.toString(dice1));97
Dice2T.setText(Integer.toString(dice2));98
SumT.setText(Integer.toString(sum));99
100
return sum;101
}102
103
public void DisplayMessage(){104
if(GameStatus==CONTINUE)105
showStatus("Roll Again!");106
else{107
if(GameStatus==WON)108
showStatus("You are the WINNER!"+" Click Roll Dice to Play Again!");109
else110
showStatus("You are the Loser!"+" Click Roll Dice to Play Again!");111
FirstRoll=true;112
113
}114
}115
}116

运行结果:
MS不是很难么......![]()


浙公网安备 33010602011771号