励志
众里寻他千百度,蓦然回首,那人却在灯火阑珊处
一、石头剪刀布游戏
1.1【问题描述】
用 JAVA 语言设计如下界面的石头剪刀布游戏。 游戏的一方点击界面上石头、剪刀、布按钮,游戏的另一方计算机随机选择, 然后给出游戏结果。
1.2【基本要求】
按照下面给出的步骤设计石头剪刀布游戏, 并完成以下工作:
- 在“//” 后给出必要的注释;
- 为程序添加菜单; 至少 2 个菜单: 结果显示方式和关于游戏; 结果显示方式菜单有 3 个菜单项: 文本显示、 图形显示、 结束游戏; 实现菜单中的功能, 自行搜索能体现石头、剪刀、布的图形或图片, 用这些图形或图片显示游戏结果。
1.3【模块划分】
public class Two{
public static void main(String[] args) {
new Work();
}
static class Work extends JFrame implements ActionListener{}
}
static class Work extends JFrame implements ActionListener{
JButton
JPanel
JLabel
JMenuItem
private static final String BASE_PATH
int theme
int score
public void initMenu(){}
public void initFrame(){
setTitle("石头剪刀布游戏");
setLocation(250,250);
setSize(350,350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setAlwaysOnTop(true);
}
public Work() {
initFrame();
initMenu();
paintViewTop();
paintViewBot();
setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e){}
}
public void initMenu(){
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenu1 = new JMenu("显示方式");
JMenu jMenu2 = new JMenu("关于我们");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenu1.add(item1);
jMenu1.add(item2);
jMenu1.add(item3);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
setJMenuBar(jMenuBar);
}
public void paintViewTop(){
panel1.setBackground(Color.yellow);
panel1.add(jiandao);
panel1.add(shitou);
panel1.add(bu);
panel1.add(clear);
shitou.addActionListener(this);
bu.addActionListener(this);
jiandao.addActionListener(this);
clear.addActionListener(this);
add(panel1,BorderLayout.NORTH);
}
public void paintViewBot(){
pane12.setLayout(null);
pane12.setBackground(Color.green);
scoreLabel = new JLabel("得分: " + score);
scoreLabel.setBounds(120, 20, 100, 20);
pane12.add(scoreLabel);
pane12.add(user);
user.setBounds(10,50,100,100);
pane12.add(com);
com.setBounds(230,50,100,100);
pane12.add(label);
label.setBounds(120,50,100,100);
pane12.add(userLabel);
userLabel.setBounds(10, 20, 100, 20);
pane12.add(comLabel);
comLabel.setBounds(230, 20, 100, 20);
add(pane12,BorderLayout.CENTER);
}
1.4【算法思想】
巧用加法实现判断猜拳结果
public void judgeGame(int user){
Random num = new Random();
int i = 4 + (3 *((int)(num.nextInt() * 10)) % 3);
int res = i + user;
if(res == 6|| res == 7 || res == 11){
showView(theme,2,user,(user + 2)%3);
}else if(res == 5 || res == 9 || res == 10){
showView(theme,0,user,(user + 1)%3);
}else if(res == 4 || res == 8 || res == 12){
showView(theme,1,user,user);
}
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()== jiandao){
userOpt = 0;
}
else if(e.getSource()== shitou){
userOpt = 1;
}
else if(e.getSource()== bu){
userOpt = 2;
}
else if(e.getSource()==clear){
clearAll();
userOpt = -1;
}
if(userOpt != -1){
judgeGame(userOpt);
}
}
1.5【题解】
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
public class Two{
public static void main(String[] args) {
new Work();
}
static class Work extends JFrame implements ActionListener{
JButton jiandao = new JButton("剪刀"),
shitou = new JButton("石头"),
bu = new JButton("布"),
clear = new JButton("清除");
JPanel pane12 = new JPanel(), panel1 = new JPanel();
JLabel label = new JLabel("选择剪刀、石头、布开始游戏"), scoreLabel,
userLabel = new JLabel("用户"),comLabel = new JLabel("电脑"),
user = new JLabel(),com = new JLabel();
JMenuItem item1 = new JMenuItem("文本显示"),
item2 = new JMenuItem("图形显示"),
item3 = new JMenuItem("结束游戏");
private static final String BASE_PATH = "C:\\Users\\Administrator\\Desktop\\homework\\Two\\新建文件夹\\";
String[] transOne = {"剪刀","石头","布"};
String[] transTwo = {"失败","平手","胜利"};
int theme = 0;
int score = 0;
int userOpt = -1;
public void initMenu(){
JMenuBar jMenuBar = new JMenuBar();
JMenu jMenu1 = new JMenu("显示方式");
JMenu jMenu2 = new JMenu("关于我们");
jMenuBar.add(jMenu1);
jMenuBar.add(jMenu2);
jMenu1.add(item1);
jMenu1.add(item2);
jMenu1.add(item3);
item1.addActionListener(this);
item2.addActionListener(this);
item3.addActionListener(this);
setJMenuBar(jMenuBar);
}
public void initFrame(){
setTitle("石头剪刀布游戏");
setLocation(250,250);
setSize(350,350);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setAlwaysOnTop(true);
}
public void paintViewTop(){
panel1.setBackground(Color.yellow);
panel1.add(jiandao);
panel1.add(shitou);
panel1.add(bu);
panel1.add(clear);
shitou.addActionListener(this);
bu.addActionListener(this);
jiandao.addActionListener(this);
clear.addActionListener(this);
add(panel1,BorderLayout.NORTH);
}
public void paintViewBot(){
pane12.setLayout(null);
pane12.setBackground(Color.green);
scoreLabel = new JLabel("得分: " + score);
scoreLabel.setBounds(120, 20, 100, 20);
pane12.add(scoreLabel);
pane12.add(user);
user.setBounds(10,50,100,100);
pane12.add(com);
com.setBounds(230,50,100,100);
pane12.add(label);
label.setBounds(120,50,100,100);
pane12.add(userLabel);
userLabel.setBounds(10, 20, 100, 20);
pane12.add(comLabel);
comLabel.setBounds(230, 20, 100, 20);
add(pane12,BorderLayout.CENTER);
}
public void judgeGame(int user){
Random num = new Random();
int i = 4 + (3 *((int)(num.nextInt() * 10)) % 3);
int res = i + user;
if(res == 6|| res == 7 || res == 11){
showView(theme,2,user,(user + 2)%3);
}else if(res == 5 || res == 9 || res == 10){
showView(theme,0,user,(user + 1)%3);
}else if(res == 4 || res == 8 || res == 12){
showView(theme,1,user,user);
}
}
public void showView(int theme, int res, int i, int j){
if(theme == 0){
clearIcon();
user.setText("用户: " + transOne[i]);
com.setText("电脑:" + transOne[j]);
label.setText("本局结果: " + transTwo[res] + "!");
}else if(theme == 1){
clearText();
user.setIcon(new ImageIcon(BASE_PATH + theme + i + ".png"));
com.setIcon(new ImageIcon(BASE_PATH + theme + j + ".png"));
label.setIcon(new ImageIcon(BASE_PATH + theme + "_" + res + ".png"));
}
if(res == 2){
score += 10;
}
scoreLabel.setText("得分: " + score);
}
public Work() {
initFrame();
initMenu();
paintViewTop();
paintViewBot();
setVisible(true);
}
public void clearAll(){
clearText();
clearIcon();
label.setText("选择剪刀、石头、布开始游戏");
score = 0;
scoreLabel.setText("得分: " + score);
}
public void clearIcon(){
user.setIcon(null);
com.setIcon(null);
label.setIcon(null);
}
public void clearText(){
user.setText(null);
com.setText(null);
}
@Override
public void actionPerformed(ActionEvent e){
if(e.getSource()== jiandao){
userOpt = 0;
}
else if(e.getSource()== shitou){
userOpt = 1;
}
else if(e.getSource()== bu){
userOpt = 2;
}
else if(e.getSource()==clear){
clearAll();
userOpt = -1;
}
if (e.getSource() == item1) {
theme = 0;
} else if (e.getSource() == item2) {
theme = 1;
} else if (e.getSource() == item3) {
theme = 2;
dispose();
}
if(userOpt != -1){
judgeGame(userOpt);
}
}
}
}
用到的图片素材:
文件命名格式
运行界面:
关于中文乱码、界面白屏,请打开IDAE设置 输入-Dfile.encoding=gbk
总结
|