告诉你什么是优雅的代码(9)----山寨版猜珍珠
程序员文章站
2022-05-17 20:12:35
...
国庆长假百无聊赖,于是玩玩3366的游戏。
玩到一款小游戏http://www.3366.com/game/28866.shtml,觉得不错,挺适合我们这种高IQ的人玩。
出于职业敏感,开始想象这个游戏是怎么实现的。
发现很简单,或许能写个出来也未可知。于是说干就干,1小时写好了内核。
本来想搞个swing版的出来,苦于没有小中华那些神奇刀法,所以还没有写。只搞了个控制台版的出来。
不过对于深谙MVC设计模式的我而言,良好的设计使得程序转移到任何的V上都是轻而易举的事情。
今天抽空重拾swing的剩余记忆,发现快遗忘殆尽,举步维艰,花了大半天时间,好不容易终于打造好了swing版。
期间一直无法显示图片浪费了我的大好光阴,头发掉了一大把,教训深刻,大家谨记不磨刀就不要砍柴。
闲话休提,先来上几张图:
你猜珍珠,我猜笑脸。
以下是UI部分代码,可运行文件提供下载,直接双击运行。
大家玩玩吧,看你能找回笑脸不。
玩到一款小游戏http://www.3366.com/game/28866.shtml,觉得不错,挺适合我们这种高IQ的人玩。
出于职业敏感,开始想象这个游戏是怎么实现的。
发现很简单,或许能写个出来也未可知。于是说干就干,1小时写好了内核。
本来想搞个swing版的出来,苦于没有小中华那些神奇刀法,所以还没有写。只搞了个控制台版的出来。
不过对于深谙MVC设计模式的我而言,良好的设计使得程序转移到任何的V上都是轻而易举的事情。
今天抽空重拾swing的剩余记忆,发现快遗忘殆尽,举步维艰,花了大半天时间,好不容易终于打造好了swing版。
期间一直无法显示图片浪费了我的大好光阴,头发掉了一大把,教训深刻,大家谨记不磨刀就不要砍柴。
闲话休提,先来上几张图:
你猜珍珠,我猜笑脸。
以下是UI部分代码,可运行文件提供下载,直接双击运行。
大家玩玩吧,看你能找回笑脸不。
package app.guesspos; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.border.BevelBorder; /** * 创建时间:Oct 9, 2010 9:06:19 AM * 项目名称:Test * @author D.Jun * @version 1.0 * @since JDK 1.6 * 文件名称:GameFrame.java * 类说明: */ //VS4E -- DO NOT REMOVE THIS LINE! public class GameFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; private JPanel jPanel0; private JPanel jPanel1; private JButton jButton0; private JButton jButton1; private Map model = new Map(); private JButton[][] btns; private int chance = 4; private int row; private int col; private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel"; public GameFrame() { initComponents(); } private void initComponents() { this.setTitle("Designed By YangGuo"); add(getJPanel0(), BorderLayout.NORTH); add(getJPanel1(), BorderLayout.CENTER); this.resetTitle(); setSize(560, 650); } private JButton getJButton1() { if (jButton1 == null) { jButton1 = new JButton(); jButton1.setBackground(new Color(255,0,0)); jButton1.setForeground(new Color(255,255,255)); } return jButton1; } private JButton getJButton0() { if (jButton0 == null) { jButton0 = new JButton(); jButton0.setBackground(new Color(255,0,0)); jButton0.setForeground(new Color(255,255,255)); } return jButton0; } private JPanel getJPanel1() { if (jPanel1 == null) { jPanel1 = new JPanel(); // jPanel1.setBackground(Color.blue); jPanel1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, null, null, null, null)); row = model.getHeight(); col = model.getWidth(); jPanel1.setLayout(new GridLayout(row, col,1,1)); btns = new JButton[row][col]; for (int i = 0; i < row; i++) { btns[i] = new JButton[col]; for (int j = 0; j < col; j++) { btns[i][j] = new JButton(); jPanel1.add(btns[i][j]); } } paintMap(); } return jPanel1; } private void paintMap() { ImageIcon sad = new ImageIcon(getClass().getResource("/app/guesspos/sad.jpg")); model.createMap(); for (int i = 0; i < row ; i++) { for (int j = 0; j < col; j++) { JButton obj = btns[i][j]; obj.setText(""); obj.removeActionListener(this); obj.setIcon(null); obj.setEnabled(false); if(!model.isEmpty(i, j)){ obj.setEnabled(true); obj.setIcon(sad); obj.addActionListener(this); obj.setActionCommand(i + "," + j); } } } } private JPanel getJPanel0() { if (jPanel0 == null) { jPanel0 = new JPanel(); jPanel0.setBackground(Color.yellow); jPanel0.setPreferredSize(new Dimension(100, 100)); jPanel0.setLayout(new FlowLayout()); jPanel0.add(getJButton0()); jPanel0.add(getJButton1()); } return jPanel0; } private static void installLnF() { try { String lnfClassname = PREFERRED_LOOK_AND_FEEL; if (lnfClassname == null) lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName(); UIManager.setLookAndFeel(lnfClassname); } catch (Exception e) { System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL + " on this platform:" + e.getMessage()); } } /** * Main entry of the class. * Note: This class is only created so that you can easily preview the result at runtime. * It is not expected to be managed by the designer. * You can modify it as you like. */ public static void main(String[] args) { installLnF(); System.out.println(System.getProperty("user.dir")); SwingUtilities.invokeLater(new Runnable() { public void run() { GameFrame frame = new GameFrame(); frame.setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE); frame.setTitle("GameFrame"); frame.getContentPane().setPreferredSize(frame.getSize()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public void actionPerformed(ActionEvent e) { if(e.getSource() instanceof JButton){ System.out.println(chance); chance--; JButton btn = (JButton)e.getSource(); btn.setIcon(null); String index = btn.getActionCommand(); int row = Integer.parseInt(index.split(",")[0]); int col = Integer.parseInt(index.split(",")[1]); // JOptionPane.showMessageDialog(this, index); int dis = model.farFrom(row, col); if(dis == 0){ btn.setIcon(new ImageIcon(this.getClass().getResource("/app/guesspos/smile.jpg"))); JOptionPane.showMessageDialog(this, "Congratulation! Go on!"); model.nextStep(); chance = 4; this.paintMap(); } else { btn.setEnabled(false); btn.setText("" + dis); if(chance == 0){ int play = JOptionPane.showConfirmDialog(this, "Game Over! Play again?"); if(play != 0){ this.dispose(); } else{ model.setStep(1); chance = 4; this.paintMap(); } } } this.resetTitle(); } } private void resetTitle() { jButton0.setText("step " + model.getStep() ); jButton1.setText("chance: " + chance); } }
上一篇: 还原javaeye的崇高文化
下一篇: Vite(打包)