首页 > 其他 > 详细

结对作业:五子棋

时间:2015-05-17 20:11:40      阅读:179      评论:0      收藏:0      [点我收藏+]
package com.temptation.gobang;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class FiveChessGame {
    /**
     * 鎺т欢灞炴??
     */
    private JFrame frame; 
    private GameMap map; 
    private JMenuBar bar; 
    private JMenu game; 
    private JMenuItem[] items; 

    /**
     * 闈欐?佹暟鎹睘鎬?
     */
    private static final int BOARD_SIZE = 15; 
    private static final int ROW_WIDTH = 36; 
    private static final int SPACE = ROW_WIDTH / 2; 

    /**
     * 鏁版嵁灞炴??
     */
    private boolean player; 
    private char[][] board; 

    public FiveChessGame() {
        player = true; 
        board = new char[BOARD_SIZE][BOARD_SIZE]; 
        clearMap();
        createGUI();
    }


    private void createGUI() {
        frame = new JFrame("五子棋"); 
        map = new GameMap();
        bar = new JMenuBar();
        game = new JMenu("开始");
        items = new JMenuItem[2];
        game.add(items[0] = new JMenuItem("重新开始"));
        game.add(items[1] = new JMenuItem("关闭")); 
    }


    protected void clearMap() {
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j] = +;
            }
        }
    }

    
    public void start() {
        map.setPreferredSize(new Dimension(ROW_WIDTH * (BOARD_SIZE - 1) + SPACE
                * 2, ROW_WIDTH * (BOARD_SIZE - 1) + SPACE * 2));
        map.addMouseListener(new MouseAdapter() {
            
            public void mouseClicked(MouseEvent e) {
                
                int xPos = (int) (e.getX() / ROW_WIDTH);
                int yPos = (int) (e.getY() / ROW_WIDTH);
                if (board[xPos][yPos] == +) {// 
                    board[xPos][yPos] = player ? b : w; // 
                    map.repaint(); 
                    if (isWin(xPos, yPos)) {
                        map.repaint(); 
                        String str = player ? "黑棋获胜" : "白棋获胜";
                        JOptionPane.showMessageDialog(null, str, "游戏结束",
                                JOptionPane.PLAIN_MESSAGE); 
                        clearMap(); 
                        map.repaint();
                        player = true; 
                    } else {
                        player = player ? false : true; 
                    }
                }
            }
        });
        // 
        items[0].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = "是否重新开始";
                // 
                if (JOptionPane.showConfirmDialog(null, str, "重新开始",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    clearMap(); // 
                    map.repaint(); // 
                    player = true; // 
                }
            }
        });
    
        items[1].addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String str = "是否关闭游戏";
                
                if (JOptionPane.showConfirmDialog(null, str, "关闭游戏",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    System.exit(0); 
                }
            }
        });
        
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                String str = "是否关闭游戏";
                
                if (JOptionPane.showConfirmDialog(null, str, "关闭游戏",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    System.exit(0); 
                }
            }
        });
        bar.add(game);
        frame.setJMenuBar(bar);
        frame.add(map);
        frame.pack();
        frame.setLocation(250, 50);
        frame.setResizable(false);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    }

    /**
     * 绾靛悜鎵?
     */
    protected boolean find(int x, int y) {
        int i, num = 1;
        char temp = player ? b : w;
        for (i = x + 1; i < 15; i++) {
            if (board[i][y] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = x - 1; i >= 0; i--) {
            if (board[i][y] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }

    /**
     * 妯悜鎵?
     */
    protected boolean find2(int x, int y) {
        int i, num = 1;
        char temp = player ? b : w;
        for (i = y + 1; i < 15; i++) { 
            if (board[x][i] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = y - 1; i >= 0; i--) {
            if (board[x][i] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }

    /**
     * \鏂瑰悜
     */
    protected boolean find3(int x, int y) {
        int i, j, num = 1;
        char temp = player ? b : w;
        for (i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) {
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) {
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }

    /**
     * /鏂瑰悜
     */
    protected boolean find4(int x, int y) {
        int i, j, num = 1;
        char temp = player ? b : w;
        for (i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) {// 鍚戜笅
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        for (i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) { // 鍚戜笂
            if (board[i][j] == temp) {
                num++;
                if (num == 5) {
                    return true;
                }
            } else {
                break;
            }
        }
        return false;
    }

    /**
     *鍒ゆ柇鑳滆礋
     */
    protected boolean isWin(int x, int y) {
        return (find(x, y) || find2(x, y) || find3(x, y) || find4(x, y));
    }

    /**
     * 鍐呴儴瀹瑰櫒绫伙紝鐢ㄤ簬瀹炵幇鍥惧儚澶勭悊
     */
    private class GameMap extends JPanel {
        private static final long serialVersionUID = 16578987565248L;

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            g.setColor(new Color(200, 100, 50));
            g.fillRect(0, 0, ROW_WIDTH * (BOARD_SIZE + 1), ROW_WIDTH
                    * (BOARD_SIZE + 1));
            g.setColor(Color.black);
            for (int i = 0; i < BOARD_SIZE; i++) {
                g.drawLine(SPACE + ROW_WIDTH * i, SPACE, SPACE + ROW_WIDTH * i,
                        SPACE + ROW_WIDTH * (BOARD_SIZE - 1));
            }
            for (int i = 0; i < BOARD_SIZE; i++) {
                g.drawLine(SPACE, SPACE + ROW_WIDTH * i, SPACE + ROW_WIDTH
                        * (BOARD_SIZE - 1), SPACE + ROW_WIDTH * i);
            }
            // 鐢荤偣
            g.fillOval(SPACE + 3 * ROW_WIDTH - 3, SPACE + 3 * ROW_WIDTH - 3, 7,
                    7);
            g.fillOval(SPACE + 11 * ROW_WIDTH - 3, SPACE + 3 * ROW_WIDTH - 3,
                    7, 7);
            g.fillOval(SPACE + 3 * ROW_WIDTH - 3, SPACE + 11 * ROW_WIDTH - 3,
                    7, 7);
            g.fillOval(SPACE + 7 * ROW_WIDTH - 3, SPACE + 7 * ROW_WIDTH - 3, 7,
                    7);
            g.fillOval(SPACE + 11 * ROW_WIDTH - 3, SPACE + 11 * ROW_WIDTH - 3,
                    7, 7);

            
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
                    if (board[i][j] == b) {
                        g.setColor(Color.black);
                        g.fillOval(SPACE + i * ROW_WIDTH - ROW_WIDTH / 2, SPACE
                                + j * ROW_WIDTH - ROW_WIDTH / 2, ROW_WIDTH,
                                ROW_WIDTH);
                    }
                    if (board[i][j] == w) {
                        g.setColor(Color.white);
                        g.fillOval(SPACE + i * ROW_WIDTH - ROW_WIDTH / 2, SPACE
                                + j * ROW_WIDTH - ROW_WIDTH / 2, ROW_WIDTH,
                                ROW_WIDTH);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        FiveChessGame game = new FiveChessGame();
        game.start();
    }
}

运行截图如下

 

 

技术分享

 

 

技术分享

 

 

 

技术分享

 

 

技术分享

 

 

技术分享

 

 

 

程序能够良好运行

 

通过本次结对编程,我了解到了合作对于编程的重要性,能让我在日后工作中,与他人积极合作完成任务。

 

结对作业:五子棋

原文:http://www.cnblogs.com/zjt1023/p/4510191.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!