五子棋棋盘等代码

编程入门 行业动态 更新时间:2024-10-16 02:22:48

五子棋<a href=https://www.elefans.com/category/jswz/34/1769098.html style=棋盘等代码"/>

五子棋棋盘等代码

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);}@Overridepublic 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*/@Overridepublic Dimension getPreferredSize() {return new Dimension(35*2+35*14,35*2+35*14);}@Overridepublic void mouseClicked(MouseEvent e) {}@Overridepublic 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();}@Overridepublic void mouseReleased(MouseEvent e) {}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}
}

更多推荐

五子棋棋盘等代码

本文发布于:2023-07-01 00:53:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/968462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:棋盘   五子   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!