Java五子棋小游戏

  • Post author:
  • Post category:java


GUI图形页面

#前言

一次老师布置的作业,感觉挺好玩的,记录一下,写的不好,望大家勿喷。

##代码

package aaa;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


public class Gobang {
    private JFrame f=new JFrame("五子棋游戏");

    BufferedImage table;
    BufferedImage black;
    BufferedImage white;
    BufferedImage selected;


    //    声明图片宽和高
    final int TABLE_WIDTH=535;
    final int TABLE_HEIGHT=536;

    //    fi
    final int BOARD_SIZE=15;

    final int RATE =TABLE_WIDTH/BOARD_SIZE;

    //    记录棋子偏移量
    final int X_OFFSET=5;
    final int Y_OFFSET=6;

    //    声明一个二维数组,记录棋子,如果索引[i][j]处的值为 清除0, 黑棋,1    白棋,2
    int[][] board=new int[BOARD_SIZE][BOARD_SIZE];

    //    声明红色选择框的坐标
    int selectedX=-1;
    int selectedY=-1;




    //    自定义类继承Canvas
    private class ChessBoard extends JPanel{
        @Override
        public void paint(Graphics g){
//        绘图
//            绘制五子棋棋盘
            g.drawImage(table,0,0,null);

//            绘制选择框
            if (selectedX>0&&selectedY>0){
                g.drawImage(selected,selectedX*RATE+X_OFFSET,selectedY*RATE+Y_OFFSET,null);

            }

//            遍历数组, 绘制棋子
            for (int i = 0; i < BOARD_SIZE; i++) {
                for (int j = 0; j < BOARD_SIZE; j++) {
//                    绘制黑棋
                    if(board[i][j]==1){
                        g.drawImage(black,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
                    }
//                    绘制白棋
                    if(board[i][j]==2){
                        g.drawImage(white,i*RATE+X_OFFSET,j*RATE+Y_OFFSET,null);
                    }
                }

            }
        }
    }

    ChessBoard chessBoard=new ChessBoard();

    //    声明变量,记录下棋的颜色
    int chessCategory=1 ;

    //    声明底部需要用到的组件
    Panel p=new Panel();
    Button whiteBtn=new Button("白棋");
    Button blackBtn=new Button("黑棋");
    Button clrarBtn=new Button("删除");


//    ·······························································判断胜负方法
    public boolean isWon(int x, int y, int color){
        int count = 1;
        int posX = 0;
        int posY = 0;
            /*先判断水平方向上的胜负
            将水平方向以传入的点x上的y轴作为分割线分为两部分
            先向左遍历,判断到的相同的连续的点 count++ */
        for(posX = x-1; posX > 0;posX--){
            if (board[posX][y] == color) {
                count++;
                if (count >= 5)
                    return true;
            }else break;
        }
        for (posX = x +1; posX < BOARD_SIZE; posX++){
            if(board[posX][y] == color){
                count++;
                if(count>=5)
                    return true;
            }  else
                break;
        }
        count = 1;
        for(posY = y-1;posY>0;posY--){
            if(board[x][posY] == color){
                count++;
                if(count>=5)
                    return true;
            }  else
                break;
        }
        for(posY = y+1; posY<BOARD_SIZE; posY++){
            if(board[x][posY] == color){
                count++;
                if (count>=5)
                    return true;
            } else
                break;
        }

        count=1;
        for(posX = x-1, posY = y-1; posX>0&&posY>0; posX--,posY--){
            if (board[posX][posY] == color){
                count++;
                if(count>=5){
                    //count = 1;
                    return true;
                }
            } else
                break;
        }
        for (posX = x+1, posY = y+1;posX<BOARD_SIZE&&posY<BOARD_SIZE; posX++, posY++){
            if (board[posX][posY] == color){
                count++;
                if(count>=5){
                    //count = 1;
                    return true;
                }
            }else
                break;
        }
        count=1;
        for (posX = x+1, posY = y-1; posX<BOARD_SIZE&&posY>0; posX++,posY--){
            if (board[posX][posY] == color){
                count++;
                if(count>=5){
                    //count = 1;
                    return true;
                }
            }else
                break;
        }
        for (posX = x-1, posY = y+1; posX>0&&posY<BOARD_SIZE; posX--,posY++){
            if (board[posX][posY] == color){
                count++;
                if(count>=5){
                    //count = 1;
                    return true;
                }
            }else
                break;
        }
        return false;
    }


    public void updateBtnColor(Color whiteBtnColor,Color blackBtnColor,Color deleteBtnColor){
        whiteBtn.setBackground(whiteBtnColor);
        blackBtn.setBackground(blackBtnColor);
        clrarBtn.setBackground(deleteBtnColor);
    }


    public void init() throws Exception {
//        组装视图
        updateBtnColor(Color.LIGHT_GRAY,Color.GREEN,Color.LIGHT_GRAY);

        whiteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chessCategory = 2;
                updateBtnColor(Color.GREEN,Color.LIGHT_GRAY,Color.LIGHT_GRAY);
            }
        });
        blackBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chessCategory=1;
                updateBtnColor(Color.LIGHT_GRAY,Color.GREEN,Color.LIGHT_GRAY);
            }
        });
        clrarBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                chessCategory=0;
                updateBtnColor(Color.LIGHT_GRAY,Color.LIGHT_GRAY,Color.GREEN);
            }
        });



        p.add(whiteBtn);
        p.add(blackBtn);
        p.add(clrarBtn);

        f.add(p,BorderLayout.SOUTH);

//        组装棋盘

//        初始化图片
        try {
            table = ImageIO.read(new File("img\\board.gif"));
            white=ImageIO.read(new File("img\\white.gif"));
            black=ImageIO.read(new File("img\\black.gif"));
            selected=ImageIO.read(new File("img\\selected.gif"));
        }catch(IOException e){
            e.printStackTrace();
        }
        //初始化board数组,默认情况下,所有位置处都没有棋子
        for (int i = 0; i < BOARD_SIZE; i++) {
            for (int j = 0; j < BOARD_SIZE; j++) {
                board[i][j]=0;
            }
        }

        //设置chessBoard的最佳大小
        chessBoard.setPreferredSize(new Dimension(TABLE_WIDTH,TABLE_HEIGHT));

        //给chessBoard注册鼠标监听器
        chessBoard.addMouseListener(new MouseAdapter() {
            //鼠标单击会触发
            @Override
            public void mouseClicked(MouseEvent e) {
                //将用户鼠标的坐标,转换成棋子的坐标
                int xPos = (e.getX()-X_OFFSET)/RATE;
                int yPos = (e.getY()-Y_OFFSET)/RATE;
                if(board[xPos][yPos]==0 && chessCategory!=0)
                    board[xPos][yPos] = chessCategory;
                else if(chessCategory == 0)
                    board[xPos][yPos] = chessCategory;
                //重绘chessBoard
                chessBoard.repaint();
                if(chessCategory!=0){
                    if(isWon(xPos,yPos,chessCategory)) {
                        if (chessCategory == 1)
                            JOptionPane.showMessageDialog(null, "黑棋胜利", "结束",
                                    JOptionPane.WARNING_MESSAGE,new ImageIcon(""));
                        else
                            JOptionPane.showMessageDialog(null, "白棋胜利", "结束",
                                    JOptionPane.WARNING_MESSAGE, new ImageIcon(""));
                        for (int i = 0; i < BOARD_SIZE; i++)
                            for (int j = 0; j < BOARD_SIZE; j++)
                                board[i][j] = 0;
                        chessBoard.repaint();
                    }
                }
            }

            //当鼠标退出棋盘区域后,复位选中坐标,重绘chessBoard,要保证红色选中框显示正确
            @Override
            public void mouseExited(MouseEvent e) {
                selectedX=-1;
                selectedY=-1;
                chessBoard.repaint();
            }
        });

        //给chessBoard注册鼠标移动监听器
        chessBoard.addMouseMotionListener(new MouseMotionAdapter() {
            //当鼠标移动时,修正selectX和selectY,重绘chessBoard,要保证红色选中框显示正确
            @Override
            public void mouseMoved(MouseEvent e) {
                //将鼠标的坐标,转换成棋子的索引
                selectedX= (e.getX()-X_OFFSET)/RATE;
                selectedX= (e.getY()-Y_OFFSET)/RATE;
                chessBoard.repaint();
            }
        });

        //把chessBoard添加到Frame中
        f.add(chessBoard);

        f.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        //设置frame最佳大小并可见
        f.pack();
        f.setVisible(true);



//



    }
    public static void main(String[] args) throws Exception{
        new Gobang().init();
    }
}




版权声明:本文为m0_64208396原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。