📄 gomoku.java
字号:
package net.sourceforge.gomoku;/** Gomoku.java* Copyright 2004 Douglas Ryan Richardson** This program is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/import java.awt.BorderLayout;import java.awt.Button;import java.awt.Component;import java.awt.Container;import java.awt.FlowLayout;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import javax.swing.ButtonGroup;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JRadioButton;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.SwingUtilities;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;public final class Gomoku implements GomokuConstants, MoveListener { private final JFrame frame; private GomokuButton[] buttons; private JButton connectionButton; private byte networkConnectionType = -1;// private String connStatus; private JTextArea connectionStatusText; private Game game; private HumanPlayer human; private ComputerPlayer compPlayer; private byte rows = Board.ROWS; private byte cols = Board.COLS; private final String VERSION = "1.0"; private Gomoku() { this.human = new HumanPlayer(USER_PIECE); this.compPlayer = new ComputerPlayer(COMPUTER_PIECE); JFrame.setDefaultLookAndFeelDecorated(true); frame = new JFrame("双机互连五子棋"); frame.setIconImage(new ImageIcon(getClass().getResource("images/Gomoku.png")).getImage()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(createBoard(), BorderLayout.CENTER); frame.setJMenuBar(createMenuBar()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); newSingleGame(); } void newSingleGame() { if (game != null) { game.cancel(); } human.startNewGame(); compPlayer.startNewGame(); game = new Game(human, compPlayer, rows, cols); game.addMoveListener(this); for (int i = 0; i < buttons.length; ++i) { buttons[i].setPiece(NO_PIECE); } new Thread(new Runnable() { public void run() { final byte test = game.play(); SwingUtilities.invokeLater(new Runnable() { public void run() { switch (test) { case COMPUTER_WIN: JOptionPane.showMessageDialog(frame, "I win, silly human!"); break; case USER_WIN: JOptionPane.showMessageDialog(frame, "Arg! I am vanquished."); break; case DRAW: JOptionPane.showMessageDialog(frame, "A draw? How unsatisfying."); break; default: break; } } }); } }).start(); } void initNewGamePanel(byte step, final JDialog dialog, final Container container){ if(step == selectedGameType){ //显示的内容 final JPanel gameTypeRadioButtonsPanel = new JPanel(new GridLayout(2,1)); ButtonGroup gameTypeGroup = new ButtonGroup(); final JRadioButton isNetWorkGame = new JRadioButton("网络连机游戏", true); final JRadioButton isSingleGame = new JRadioButton("单机游戏", false); gameTypeGroup.add(isNetWorkGame); gameTypeGroup.add(isSingleGame); gameTypeRadioButtonsPanel.add(isNetWorkGame); gameTypeRadioButtonsPanel.add(isSingleGame); // 按钮面板(选择游戏类型) final JPanel buttonPanel = new JPanel(); Button okButton = new Button("确定"); okButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub try{ final byte gameType; if(isNetWorkGame.isSelected()) gameType = NETWORK_GAME; else gameType = SINGLE_GAME; newGame(gameType); } catch(Exception e1){ e1.printStackTrace(); } } private void newGame(byte gameType) throws Exception{ if(gameType == NETWORK_GAME) initNewGamePanel(selectedNetworkSide, dialog, container); else if(gameType == SINGLE_GAME){ newSingleGame(); dialog.dispose(); } else throw new Exception("在Gomoku中点击确定按钮时没有找到指定的游戏模式"); } }); Button cancelButton = new Button("取消"); cancelButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dialog.dispose(); } }); buttonPanel.add(okButton); buttonPanel.add(cancelButton); container.add("South", buttonPanel); container.add("Center",gameTypeRadioButtonsPanel); dialog.pack(); dialog.setLocationRelativeTo(frame); dialog.setVisible(true); } else if(step == selectedNetworkSide){ container.removeAll(); GridBagConstraints gbc = new GridBagConstraints(); // 选择服务器端或客户端 JPanel networkConfigPanel = new JPanel(new GridBagLayout()); ButtonGroup networkConfigGroup = new ButtonGroup(); JRadioButton serverSide = new JRadioButton("服务器端", true); serverSide.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) { // TODO Auto-generated method stub try{ JRadioButton serverRadioButton = (JRadioButton)e.getSource(); if(connectionButton == null) throw new Exception("connectionButton为空"); if(serverRadioButton.isSelected()){ networkConnectionType = serverConnection; connectionButton.setText("开始服务器端监听"); } } catch(Exception e1){ e1.printStackTrace(); } } }); JRadioButton clientSide = new JRadioButton("客户端", false); clientSide.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) { // TODO Auto-generated method stub try{ JRadioButton clientRadioButton = (JRadioButton)e.getSource(); if(connectionButton == null) throw new Exception("connectionButton为空"); if(clientRadioButton.isSelected()){ networkConnectionType = clientConnection; connectionButton.setText("开始客户端连接"); } } catch(Exception e1){ e1.printStackTrace(); } } }); networkConfigGroup.add(serverSide); networkConfigGroup.add(clientSide); // 状态报告 connectionStatusText = new JTextArea(10, 5); connectionStatusText.setEditable(false); //按钮面板(网络游戏) JPanel networkConfigButtonsPanel = new JPanel(); JButton startGameButton = new JButton("开始游戏"); startGameButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }); startGameButton.setEnabled(false); connectionButton = new JButton("开始服务器端监听"); connectionButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(networkConnectionType == -1 || networkConnectionType == serverConnection){ // 开始服务器端监听 connectionStatusText.setText("服务器端开始监听,请稍候...\n"); } else if(networkConnectionType == clientConnection){ // 开始客户端连接 connectionStatusText.setText("客户端开始连接,请稍候...\n"); } } }); JButton cancelButton = new JButton("取消"); cancelButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub dialog.dispose(); } }); /*将组件添加到容器中*/ gbc.weightx = 100; gbc.fill = GridBagConstraints.BOTH; add(networkConfigPanel, serverSide, gbc, 0, 0, 1, 1); add(networkConfigPanel, clientSide, gbc, 0, 1, 1, 1); gbc.weighty = 100; add(networkConfigPanel, new JScrollPane(connectionStatusText), gbc, 0, 2, 1, 1); networkConfigButtonsPanel.add(startGameButton); networkConfigButtonsPanel.add(connectionButton); networkConfigButtonsPanel.add(cancelButton); container.add("South", networkConfigButtonsPanel); container.add("Center", networkConfigPanel); dialog.validate(); dialog.pack(); dialog.setLocationRelativeTo(frame); } } private void add(Container container, Component c, GridBagConstraints gbc, int x, int y, int w, int h){ gbc.gridx = x; gbc.gridy = y; gbc.gridwidth = w; gbc.gridheight = h; container.add(c, gbc); } public JTextArea getConnectionStatusText(){ return this.connectionStatusText; } JMenuBar createMenuBar() { final JMenuBar menuBar = new JMenuBar(); JMenu menu; JMenuItem menuItem; // 游戏菜单 menu = new JMenu("游戏"); menu.setMnemonic(KeyEvent.VK_G); menuBar.add(menu); // 新游戏 menuItem = new JMenuItem("新游戏"); menuItem.setMnemonic(KeyEvent.VK_N); menu.add(menuItem); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { final JDialog d = new JDialog(frame, "新游戏", true); final Container container = d.getContentPane(); initNewGamePanel(selectedGameType, d, container); } }); // 搜索深度 menuItem = new JMenuItem("搜索深度..."); menuItem.setMnemonic(KeyEvent.VK_S); menu.add(menuItem); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { if (compPlayer instanceof ComputerPlayer) { final String[] vals = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10"}; final String sd = (String) JOptionPane.showInputDialog(frame, "选择一个搜索深度.", "搜索深度", JOptionPane.PLAIN_MESSAGE, null, vals, Integer.toString(((ComputerPlayer) compPlayer).getSearchDepth())); if (sd != null) { ((ComputerPlayer) compPlayer).setSearchDepth(Integer.parseInt(sd)); } } } }); menu.addSeparator(); // 退出 menuItem = new JMenuItem("退出"); menuItem.setMnemonic(KeyEvent.VK_E); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { System.exit(0); } }); menu.add(menuItem); // 帮助菜单 menu = new JMenu("帮助"); menu.setMnemonic(KeyEvent.VK_H); menuBar.add(menu); menuItem = new JMenuItem("关于..."); menuItem.setMnemonic(KeyEvent.VK_A); menu.add(menuItem); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { final JDialog d = new JDialog(frame, "关于 双机对战五子棋", true); final Container p = d.getContentPane(); JPanel panel = new JPanel(new FlowLayout()); p.setLayout(new GridLayout(2, 1)); panel.add(new JLabel(new ImageIcon("Gomoku.png"))); panel.add(new JLabel("<html>" + "<p>双机对战五子棋 " + VERSION + "<p>Copyright (C) 2005-2006 高嵩" + "<p>首钢工学院" + "</html>")); p.add(panel); panel = new JPanel(new FlowLayout()); final JButton ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { d.setVisible(false); } }); panel.add(ok); p.add(panel); d.pack(); d.setLocationRelativeTo(frame); d.setVisible(true); } }); return menuBar; } Component createBoard() { final JPanel pane = new JPanel(new GridLayout(rows, cols, 0, 0)); buttons = new GomokuButton[rows * cols]; for (int i = 0; i < rows; i++) { for (int k = 0; k < cols; k++) { int pos = Board.toPosition(i, k); buttons[pos] = new GomokuButton(SquareFactory.create(i, k)); buttons[pos].addActionListener(this.human); pane.add(buttons[pos]); } } return pane; } public void moveMade(final Square move, byte piece) { buttons[Board.toPosition(move.getRow(), move.getCol())] .setPiece(piece); } public static void main(final String[] args) { new Gomoku(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -