五子棋棋盘等代码

package com.game.wuziqi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Arrays;

/**
 * 五子棋-棋盘类
 */
public class DrawPanel extends JPanel implements MouseListener{
    //棋子坐标
    private int x;
    private int y;
    //是否是黑子
    private boolean isBlack = true;
    //棋子数组
    private Chess[] chessList = new Chess[15*15];
    //棋子个数
    private int chessCount = 0;
    //游戏是否结束
    private boolean gameOver = false;


    public DrawPanel() {
        this.setBackground(Color.LIGHT_GRAY);
        this.addMouseListener(this);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        //画棋盘
        for (int i = 1; i <16; i++) {
            g.drawLine(35,35*i,525,35*i);
        }
        for (int j = 1; j <16; j++) {
            g.drawLine(35*j,35,35*j,525);
        }
        g.fillOval(35+35*7-5,35+35*7-5,10,10);
        g.fillOval(35+35*2-5,35+35*2-5,10,10);
        g.fillOval(35+35*12-5,35+35*2-5,10,10);
        g.fillOval(35+35*2-5,35+35*12-5,10,10);
        g.fillOval(35+35*12-5,35+35*12-5,10,10);

        //画棋子
        for (int i = 0; i < chessCount; i++) {
            int xPos = chessList[i].getX();
            int yPos = chessList[i].getY();
            g.setColor(chessList[i].getColor());
            g.fillOval(xPos-Chess.DIAMETER/2,yPos-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
            //最后落的一个棋子加上红色框
            if (i==chessCount-1){
                g.setColor(Color.red);
                g.drawRect(xPos-Chess.DIAMETER/2,yPos-Chess.DIAMETER/2,Chess.DIAMETER,Chess.DIAMETER);
            }
        }
    }

    /**
     * 自动调用
     * 设置当前组件的大小是最佳的大小
     * @return
     */
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(35*2+35*14,35*2+35*14);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        //将鼠标点击的坐标位置转换为网格索引
        x = (e.getX()-35+35/2)/35*35+35;
        y = (e.getY()-35+35/2)/35*35+35;

        //判断棋子是否可以下到棋盘上
        //游戏结束不能下棋子
        if (gameOver){
            return;
        }
        //棋盘外面不能下棋子
        if (x > 35+35*14 || y > 35+35*14){
            return;
        }
        //位置上已经有棋子不能下
        if (haveChess(x,y)){
            return;
        }

        //棋子对象
        Chess chess = new Chess(x,y,isBlack?Color.black:Color.white);
        chessList[chessCount++] = chess;
        this.repaint();

        //当某一方胜利时
        if (victory1()||victory2()||victory3()||victory4()){
            String msg = String.format("恭喜%s棋胜利!",isBlack?"黑":"白");
            JOptionPane.showMessageDialog(this,msg);
            gameOver = true;
        }

        //改变画笔颜色的判定值
        isBlack = !isBlack;
    }

    //位置上是否有棋子
    public boolean haveChess(int x,int y){
        for (Chess chess : chessList) {
            if (chess!=null&&chess.getX()==x&&chess.getY()==y){
                return true;
            }
        }
        return false;
    }

    //判断是否胜利
    /**
     * 得到棋盘上的棋子
     * @param x 本次将要落下的棋子的横坐标
     * @param y 本次将要落下的棋子的纵坐标
     * @param color 本次将要落下的棋子的颜色
     * @return  返回这个棋子
     */
    public Chess getChess(int x,int y,Color color){
        for (Chess chess : chessList) {
            if (chess!=null&&chess.getX()==x&&chess.getY()==y&&chess.getColor()==color){
                return chess;
            }
        }
        return null;
    }
    //上下
    public boolean victory1(){
        //连续棋子的个数
        int continueCount = 1;
        //向上寻找
        for (int xi=x,yi = y-35; yi>=35; yi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向下寻找
        for (int xi=x,yi = y+35; yi<=35+35*14; yi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    //左右
    public boolean victory2(){
        //连续棋子的个数
        int continueCount = 1;
        //向左寻找
        for (int xi=x-35,yi = y; xi>=35; xi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向右寻找
        for (int xi=x+35,yi = y; xi<=35+35*14; xi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    //左上-右下
    public boolean victory3(){
        //连续棋子的个数
        int continueCount = 1;
        //向左上寻找
        for (int xi=x-35,yi = y-35; xi>=35&&yi>=35; xi-=35,yi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向右下寻找
        for (int xi=x+35,yi = y+35; xi<=35+35*14&&yi<=35+35*14; xi+=35,yi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    //右上-左下
    public boolean victory4(){
        //连续棋子的个数
        int continueCount = 1;
        //向左下寻找
        for (int xi=x-35,yi = y+35; xi>=35&&yi<=35+35*14; xi-=35,yi+=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        //向右上寻找
        for (int xi=x+35,yi = y-35; xi<=35+35*14&&yi>=35; xi+=35,yi-=35){
            Color color = isBlack?Color.black:Color.white;
            if (getChess(xi,yi,color) !=null){
                continueCount++;
            }else {
                break;
            }
        }
        return continueCount >= 5;
    }

    /**
     * 重新开始游戏
     */
    public void restartGame(){
        //清除棋子
        Arrays.fill(chessList, null);
//        for (int i = 0; i < chessList.length; i++) {
//            chessList[i] = null;
//        }

        //恢复游戏相关的变量
        isBlack = true;
        gameOver = false;
        chessCount = 0;
        //重画棋盘
        this.repaint();
    }

    /**
     *
     */
    public void regretChess(){
        //没有棋子不能悔棋
        if (chessCount == 0){
        }
        chessList[chessCount-1]=null;
        chessCount--;
        if (chessCount > 0) {
            x=chessList[chessCount-1].getX();
            y=chessList[chessCount-1].getY();
        }

        isBlack=!isBlack;
        this.repaint();
    }
    @Override
    public void mouseReleased(MouseEvent e) {}
    @Override
    public void mouseEntered(MouseEvent e) {}
    @Override
    public void mouseExited(MouseEvent e) {}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值