Java简单实现的五子棋

来源:互联网 发布:twisted python 编辑:程序博客网 时间:2024/05/17 03:32
![开始界面](http://img.blog.csdn.net/20160705230450338)实现的基本思路1.绘制好基本的框架界面(棋盘,菜单等)2.能够在其棋盘上绘制棋子3.将棋子的位置规格化(就是摆放到网格的交叉点上,还要具有一定的偏差能力)4.判断游戏是否结束5.优化程序

//主窗体MainFrame

package com.FIveChess.View;import java.awt.BorderLayout;import javax.swing.JFrame;/*** * 主窗体 * @author XHH * */public class MainFrame extends JFrame{    public MainFrame() {        init();        setVisible(true);        addBoardPanel();    }    //添加棋盘    private void addBoardPanel() {        ChessBoard chessBoard=new ChessBoard(this);        this.add(chessBoard);    }    //设置窗体的基本框架    public void init(){        setTitle("简单五子棋");//窗体标题        setSize(800,800);//窗体大小        setLocationRelativeTo(null);//居中        setDefaultCloseOperation(3);    }    public static void main(String[] args) {        new MainFrame();    }}

//棋盘

package com.FIveChess.View;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import javax.swing.JOptionPane;import javax.swing.JPanel;public class ChessBoard extends JPanel{    MainFrame mainFrame;    //存放棋子状态的二维数组,0--无棋子    // 1--黑棋,2--白旗    int [][]chess=new int[18][18];    int x,y;    boolean chessFlag=true; //为true时黑 false--白    public ChessBoard(MainFrame f) {        this.mainFrame=f;        init();        addMouseListener(new MouseAdapter() {            @Override            public void mouseClicked(MouseEvent e) {                //获取当前点击点的坐标                x=e.getX()-40;                y=e.getY()-40;                int xIndex=x%40>20?x/40+1:x/40;                int yIndex=y%40>20?y/40+1:y/40;                //控制不能在已经有棋子的地方下子                if(chess[xIndex][yIndex]!=0)return;                if(chessFlag)                {                    chess[xIndex][yIndex]=1;                    chessFlag=false;                }                else                 {                    chess[xIndex][yIndex]=2;                    chessFlag=true;                }                //重绘,否则即使点击了也不会显示                mainFrame.repaint();                //判断游戏是否结束                if(checkGameIsOver(xIndex,yIndex)){                    JOptionPane.showMessageDialog(null, "游戏胜利");                    for(int i=0;i<chess.length;i++)                    {                        for(int j=0;j<chess[i].length;j++)                        {                            chess[i][j]=0;                        }                    }                       mainFrame.repaint();                }            }        });    }    private void init() {        setBackground(Color.gray);        //可以自己去设置图片显得更好看    }    //重写paint方法    @Override    public void paint(Graphics g) {        super.paint(g);        //画棋盘        for(int i=0;i<18;i++){            //竖线            g.drawLine(40+40*i, 40, 40+40*i, 720);            //横线            g.drawLine(40, 40+40*i, 720, 40+40*i);        }        //画棋子        for(int i=0;i<chess.length;i++)        {            for(int j=0;j<chess[i].length;j++)            {                if(chess[i][j]==1)//画黑棋子                {                    g.setColor(Color.BLACK);                    g.fillOval(40+i*40-18, 40+j*40-18, 36, 36);                }                if(chess[i][j]==2)//画白棋子                {                    g.setColor(Color.WHITE);                    g.fillOval(40+i*40-18, 40+j*40-18, 36, 36);                }            }        }    }    private boolean checkGameIsOver(int xIndex,int yIndex) {        //获取当前的棋子        int currentChess=chess[xIndex][yIndex];        int count=1; //棋子计数器        int tempXIndex = xIndex;        int tempYIndex = yIndex;        //不同的判断方向 上下、左右、左斜、右斜        int[][][] allChanges = {                 { { -1, -1 }, { 1, 1 } },                 { { 1, -1 }, { -1, 1 } },                 { { 0, -1 }, { 0, 1 } },                { { -1, 0 }, { 1, 0 } } };        for (int j = 0; j < allChanges.length; j++) {            int[][] changes = allChanges[j];            count = 1;            for (int[] change : changes) {                tempXIndex = xIndex;                tempYIndex = yIndex;                while (true) {                    tempXIndex = tempXIndex + change[0];                    tempYIndex = tempYIndex + change[1];                    if (tempXIndex < 0 || tempXIndex > 20 || tempYIndex < 0 || tempYIndex > 20) {                        break;                    }                    int i = chess[tempXIndex][tempYIndex];                    //当前走的方向有颜色相同的棋子就+1                    if (i == currentChess) {                        count++;                    } else {                        break;                    }                }            }            if (count >= 5) {                return true;            }        }        return false;    }}

效果图
美化界面的部分就不写了。。。

1 0