Java 小例子:编写石头剪刀布游戏

Code:
  1. import javax.swing.*;  
  2. import java.awt.BorderLayout;  
  3. import java.awt.GridLayout;  
  4. import java.awt.HeadlessException;  
  5. import java.awt.event.ActionEvent;  
  6. import java.awt.event.ActionListener;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9. import java.util.Random;  
  10.   
  11. /** 
  12.  * 石头剪刀布游戏,游戏介绍参见: 
  13.  * <p/> 
  14.  * http://en.wikipedia.org/wiki/Rock-paper-scissors 
  15.  * 
  16.  * @author yiding.he 
  17.  */  
  18. public class RockPaperScissors extends JFrame {  
  19.   
  20.     protected final JLabel messageLabel = new JLabel(INITIAL_MESSAGE);  
  21.   
  22.     private final GameProcessor gameProcessor = new GameProcessor();  
  23.   
  24.     private List<JButton> buttons = new ArrayList<JButton>();  
  25.   
  26.     protected JPanel buttonsPanel;  
  27.   
  28.     public static final String INITIAL_MESSAGE = "我准备好了,出招吧 XD";  
  29.   
  30.     public RockPaperScissors() throws HeadlessException {  
  31.         initFrame();  
  32.         initControls();  
  33.   
  34.         restartGame();  
  35.     }  
  36.   
  37.     /** 
  38.      * 设置窗体 
  39.      */  
  40.     private void initFrame() {  
  41.         this.setSize(300120);  
  42.         this.setLocation(300300);  
  43.         this.setTitle("野球拳");  
  44.         this.setDefaultCloseOperation(EXIT_ON_CLOSE);  
  45.     }  
  46.   
  47.     /** 
  48.      * 设置控件 
  49.      */  
  50.     private void initControls() {  
  51.         BorderLayout bl = new BorderLayout();  
  52.         bl.setVgap(5);  
  53.         getContentPane().setLayout(bl);  
  54.   
  55.         messageLabel.setBorder(BorderFactory.createEmptyBorder(10101010));  
  56.         messageLabel.setHorizontalAlignment(SwingConstants.CENTER);  
  57.         getContentPane().add(messageLabel, BorderLayout.NORTH);  
  58.   
  59.         buttonsPanel = new JPanel(new GridLayout(1, GameItem.values().length, 50));  
  60.         getContentPane().add(buttonsPanel);  
  61.   
  62.         createButtons();  
  63.     }  
  64.   
  65.     /** 
  66.      * 创建按钮 
  67.      */  
  68.     private void createButtons() {  
  69.         for (GameItem item : GameItem.values()) {  
  70.             ItemButton button = new ItemButton(item);  
  71.             buttons.add(button);  
  72.             buttonsPanel.add(button);  
  73.         }  
  74.     }  
  75.   
  76.     /** 
  77.      * 启用或禁用全部按钮 
  78.      * 
  79.      * @param enabled 启用或禁用 
  80.      */  
  81.     private void setButtonsEnabled(boolean enabled) {  
  82.         for (JButton button : buttons) {  
  83.             button.setEnabled(enabled);  
  84.         }  
  85.     }  
  86.   
  87.     /** 
  88.      * 处理并显示游戏结果 
  89.      * 
  90.      * @param gameItem 玩家选择 
  91.      */  
  92.     private void processAndShowGameResult(GameItem gameItem) {  
  93.         String result = gameProcessor.process(gameItem);  
  94.         messageLabel.setText(result);  
  95.     }  
  96.   
  97.     /** 
  98.      * 重置游戏界面 
  99.      */  
  100.     private void restartGame() {  
  101.         messageLabel.setText(INITIAL_MESSAGE);  
  102.         gameProcessor.reset();  
  103.     }  
  104.   
  105.     /  
  106.   
  107.     /** 
  108.      * 游戏逻辑处理 
  109.      */  
  110.     private static class GameProcessor {  
  111.   
  112.         private GameItem item;  
  113.   
  114.         private Random r = new Random();  
  115.   
  116.         /** 
  117.          * 重置游戏,机器生成自己的选项 
  118.          */  
  119.         public void reset() {  
  120.             int index = r.nextInt(GameItem.values().length);  
  121.             this.item = GameItem.values()[index];  
  122.         }  
  123.   
  124.         /** 
  125.          * 处理玩家输入的选项,得出本局结果 
  126.          * 
  127.          * @param gameItem 玩家选项 
  128.          * 
  129.          * @return 本局结果 
  130.          */  
  131.         public String process(GameItem gameItem) {  
  132.             int result = item.compareWith(gameItem);  
  133.             if (result > 0) {  
  134.                 return "哈哈,你输了,我出的是" + item.getLabel();  
  135.             } else if (result < 0) {  
  136.                 return "啊,我出了个" + item.getLabel() + ",不小心让你赢了一次……";  
  137.             } else {  
  138.                 return "嘿,我出的也是" + item.getLabel() + ",打个平手!";  
  139.             }  
  140.         }  
  141.     }  
  142.   
  143.     /  
  144.   
  145.     /** 
  146.      * 用户选择按钮,每个 ItemButton 对应一个 GameItem 
  147.      */  
  148.     private class ItemButton extends JButton {  
  149.   
  150.         private GameItem item;  
  151.   
  152.         public GameItem getGameItem() {  
  153.             return item;  
  154.         }  
  155.   
  156.         private ItemButton(String text) {  
  157.             super(text);  
  158.         }  
  159.   
  160.         private ItemButton(GameItem item) {  
  161.             this(item.getLabel());  
  162.             this.item = item;  
  163.   
  164.             this.addActionListener(new ActionListener() {  
  165.                 public void actionPerformed(final ActionEvent e) {  
  166.                     new Thread() {  
  167.   
  168.                         @Override  
  169.                         public void run() {  
  170.                             ItemButton button = (ItemButton) e.getSource();  
  171.   
  172.                             processAndShowGameResult(button.getGameItem());  
  173.                             sleepForAWhile();  
  174.                             restartGame();  
  175.                         }  
  176.                     }.start();  
  177.                 }  
  178.   
  179.                 private void sleepForAWhile() {  
  180.                     setButtonsEnabled(false);  
  181.                     try {  
  182.                         Thread.sleep(2000);  
  183.                     } catch (InterruptedException e1) {  
  184.                         e1.printStackTrace();  
  185.                     }  
  186.                     setButtonsEnabled(true);  
  187.                 }  
  188.             });  
  189.         }  
  190.     }  
  191.   
  192.     /  
  193.   
  194.     private static enum GameItem {  
  195.   
  196.         Rock("石头"), Scissor("剪刀"), Paper("布");  
  197.   
  198.         private String label;  
  199.   
  200.         public String getLabel() {  
  201.             return label;  
  202.         }  
  203.   
  204.         private GameItem(String label) {  
  205.             this.label = label;  
  206.         }  
  207.   
  208.         public int compareWith(GameItem item) {  
  209.             if (this == item) {  
  210.                 return 0;  
  211.             } else if ((this == Rock && item == Scissor) ||  
  212.                     (this == Scissor && item == Paper) ||  
  213.                     (this == Paper && item == Rock)) {  
  214.                 return 1;  
  215.             } else {  
  216.                 return -1;  
  217.             }  
  218.         }  
  219.     }  
  220.   
  221.     //  
  222.   
  223.     public static void main(String[] args) throws Exception {  
  224.         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  
  225.         new RockPaperScissors().setVisible(true);  
  226.     }  
  227. }  

谨作参考。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 55
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 55
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值