Java 小例子:编写石头剪刀布游戏
2010-08-30 17:56
381 查看
Code:
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 石头剪刀布游戏,游戏介绍参见:
* <p/>
* http://en.wikipedia.org/wiki/Rock-paper-scissors
*
* @author yiding.he
*/
public class RockPaperScissors extends JFrame {
protected final JLabel messageLabel = new JLabel(INITIAL_MESSAGE);
private final GameProcessor gameProcessor = new GameProcessor();
private List<JButton> buttons = new ArrayList<JButton>();
protected JPanel buttonsPanel;
public static final String INITIAL_MESSAGE = "我准备好了,出招吧 XD";
public RockPaperScissors() throws HeadlessException {
initFrame();
initControls();
restartGame();
}
/**
* 设置窗体
*/
private void initFrame() {
this.setSize(300, 120);
this.setLocation(300, 300);
this.setTitle("野球拳");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* 设置控件
*/
private void initControls() {
BorderLayout bl = new BorderLayout();
bl.setVgap(5);
getContentPane().setLayout(bl);
messageLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(messageLabel, BorderLayout.NORTH);
buttonsPanel = new JPanel(new GridLayout(1, GameItem.values().length, 5, 0));
getContentPane().add(buttonsPanel);
createButtons();
}
/**
* 创建按钮
*/
private void createButtons() {
for (GameItem item : GameItem.values()) {
ItemButton button = new ItemButton(item);
buttons.add(button);
buttonsPanel.add(button);
}
}
/**
* 启用或禁用全部按钮
*
* @param enabled 启用或禁用
*/
private void setButtonsEnabled(boolean enabled) {
for (JButton button : buttons) {
button.setEnabled(enabled);
}
}
/**
* 处理并显示游戏结果
*
* @param gameItem 玩家选择
*/
private void processAndShowGameResult(GameItem gameItem) {
String result = gameProcessor.process(gameItem);
messageLabel.setText(result);
}
/**
* 重置游戏界面
*/
private void restartGame() {
messageLabel.setText(INITIAL_MESSAGE);
gameProcessor.reset();
}
/////////////////////////////////////////////////////////
/**
* 游戏逻辑处理
*/
private static class GameProcessor {
private GameItem item;
private Random r = new Random();
/**
* 重置游戏,机器生成自己的选项
*/
public void reset() {
int index = r.nextInt(GameItem.values().length);
this.item = GameItem.values()[index];
}
/**
* 处理玩家输入的选项,得出本局结果
*
* @param gameItem 玩家选项
*
* @return 本局结果
*/
public String process(GameItem gameItem) {
int result = item.compareWith(gameItem);
if (result > 0) {
return "哈哈,你输了,我出的是" + item.getLabel();
} else if (result < 0) {
return "啊,我出了个" + item.getLabel() + ",不小心让你赢了一次……";
} else {
return "嘿,我出的也是" + item.getLabel() + ",打个平手!";
}
}
}
/////////////////////////////////////////////////////////
/**
* 用户选择按钮,每个 ItemButton 对应一个 GameItem
*/
private class ItemButton extends JButton {
private GameItem item;
public GameItem getGameItem() {
return item;
}
private ItemButton(String text) {
super(text);
}
private ItemButton(GameItem item) {
this(item.getLabel());
this.item = item;
this.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
new Thread() {
@Override
public void run() {
ItemButton button = (ItemButton) e.getSource();
processAndShowGameResult(button.getGameItem());
sleepForAWhile();
restartGame();
}
}.start();
}
private void sleepForAWhile() {
setButtonsEnabled(false);
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
setButtonsEnabled(true);
}
});
}
}
/////////////////////////////////////////////////////////
private static enum GameItem {
Rock("石头"), Scissor("剪刀"), Paper("布");
private String label;
public String getLabel() {
return label;
}
private GameItem(String label) {
this.label = label;
}
public int compareWith(GameItem item) {
if (this == item) {
return 0;
} else if ((this == Rock && item == Scissor) ||
(this == Scissor && item == Paper) ||
(this == Paper && item == Rock)) {
return 1;
} else {
return -1;
}
}
}
//////////////////////////////////////////////////////////
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new RockPaperScissors().setVisible(true);
}
}
谨作参考。
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* 石头剪刀布游戏,游戏介绍参见:
* <p/>
* http://en.wikipedia.org/wiki/Rock-paper-scissors
*
* @author yiding.he
*/
public class RockPaperScissors extends JFrame {
protected final JLabel messageLabel = new JLabel(INITIAL_MESSAGE);
private final GameProcessor gameProcessor = new GameProcessor();
private List<JButton> buttons = new ArrayList<JButton>();
protected JPanel buttonsPanel;
public static final String INITIAL_MESSAGE = "我准备好了,出招吧 XD";
public RockPaperScissors() throws HeadlessException {
initFrame();
initControls();
restartGame();
}
/**
* 设置窗体
*/
private void initFrame() {
this.setSize(300, 120);
this.setLocation(300, 300);
this.setTitle("野球拳");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
/**
* 设置控件
*/
private void initControls() {
BorderLayout bl = new BorderLayout();
bl.setVgap(5);
getContentPane().setLayout(bl);
messageLabel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
messageLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(messageLabel, BorderLayout.NORTH);
buttonsPanel = new JPanel(new GridLayout(1, GameItem.values().length, 5, 0));
getContentPane().add(buttonsPanel);
createButtons();
}
/**
* 创建按钮
*/
private void createButtons() {
for (GameItem item : GameItem.values()) {
ItemButton button = new ItemButton(item);
buttons.add(button);
buttonsPanel.add(button);
}
}
/**
* 启用或禁用全部按钮
*
* @param enabled 启用或禁用
*/
private void setButtonsEnabled(boolean enabled) {
for (JButton button : buttons) {
button.setEnabled(enabled);
}
}
/**
* 处理并显示游戏结果
*
* @param gameItem 玩家选择
*/
private void processAndShowGameResult(GameItem gameItem) {
String result = gameProcessor.process(gameItem);
messageLabel.setText(result);
}
/**
* 重置游戏界面
*/
private void restartGame() {
messageLabel.setText(INITIAL_MESSAGE);
gameProcessor.reset();
}
/////////////////////////////////////////////////////////
/**
* 游戏逻辑处理
*/
private static class GameProcessor {
private GameItem item;
private Random r = new Random();
/**
* 重置游戏,机器生成自己的选项
*/
public void reset() {
int index = r.nextInt(GameItem.values().length);
this.item = GameItem.values()[index];
}
/**
* 处理玩家输入的选项,得出本局结果
*
* @param gameItem 玩家选项
*
* @return 本局结果
*/
public String process(GameItem gameItem) {
int result = item.compareWith(gameItem);
if (result > 0) {
return "哈哈,你输了,我出的是" + item.getLabel();
} else if (result < 0) {
return "啊,我出了个" + item.getLabel() + ",不小心让你赢了一次……";
} else {
return "嘿,我出的也是" + item.getLabel() + ",打个平手!";
}
}
}
/////////////////////////////////////////////////////////
/**
* 用户选择按钮,每个 ItemButton 对应一个 GameItem
*/
private class ItemButton extends JButton {
private GameItem item;
public GameItem getGameItem() {
return item;
}
private ItemButton(String text) {
super(text);
}
private ItemButton(GameItem item) {
this(item.getLabel());
this.item = item;
this.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
new Thread() {
@Override
public void run() {
ItemButton button = (ItemButton) e.getSource();
processAndShowGameResult(button.getGameItem());
sleepForAWhile();
restartGame();
}
}.start();
}
private void sleepForAWhile() {
setButtonsEnabled(false);
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
setButtonsEnabled(true);
}
});
}
}
/////////////////////////////////////////////////////////
private static enum GameItem {
Rock("石头"), Scissor("剪刀"), Paper("布");
private String label;
public String getLabel() {
return label;
}
private GameItem(String label) {
this.label = label;
}
public int compareWith(GameItem item) {
if (this == item) {
return 0;
} else if ((this == Rock && item == Scissor) ||
(this == Scissor && item == Paper) ||
(this == Paper && item == Rock)) {
return 1;
} else {
return -1;
}
}
}
//////////////////////////////////////////////////////////
public static void main(String[] args) throws Exception {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new RockPaperScissors().setVisible(true);
}
}
谨作参考。
相关文章推荐
- (一)NetBeans IDE上的Java手机游戏例子
- (一)NetBeans IDE上的Java手机游戏例子
- Java编写大嘴怪游戏--原创
- (一)NetBeans IDE上的Java手机游戏例子
- 用java编写的一个小财富游戏
- Java Random经典例子【猜拳游戏】
- [真格邦]JAVA实战之人与计算机对战-石头剪刀布游戏讲解、源代码
- (一)NetBeans IDE上的Java手机游戏例子
- 【转载】Java数据接口编写简单例子,Java Json解析,服务端接口输出Json数据,客户端通过HTTP获取接口Json数据,作者:Jaiky_杰哥
- JAVA 编写托盘例子
- (一)NetBeans IDE上的Java手机游戏例子
- (一)NetBeans IDE上的Java手机游戏例子
- 猜数字游戏。编写一个Java应用程序,实现功能:
- (一)NetBeans IDE上的Java手机游戏例子
- Java编写的24点纸牌游戏
- java编写游戏的几本参考书
- (一)NetBeans IDE上的Java手机游戏例子
- Java 小例子:老手如何编写计算器
- java吃豆豆游戏-1控制按钮编写
- Java编写的五子棋游戏