javafx实现五子棋游戏
程序员文章站
2023-11-27 08:11:10
需求描述
一个五子棋游戏,能实现双方黑白对决,当一方获胜时给出提示信息,利用gui界面实现
项目结构如下图
一、实体
fivechess类
提供五子棋...
需求描述
一个五子棋游戏,能实现双方黑白对决,当一方获胜时给出提示信息,利用gui界面实现
项目结构如下图
一、实体
fivechess类
- 提供五子棋实体包含的所有信息
- 判断游戏是否结束
- play方法改变chess[][]棋盘中的数据
package entity; import javafx.scene.control.alert; public class fivechess{ public double getwidth() { return width; } public void setwidth(double width) { this.width = width; } public double getheight() { return height; } public void setheight(double height) { this.height = height; } public double getcelllen() { return celllen; } public void setcelllen(double celllen) { this.celllen = celllen; } /** * 维度 */ private int n; private double width; private double height; private double celllen; private char currentside='b'; public char getflag() { return flag; } private char flag=' '; private char[][] chess; public char[][] getchess() { return chess; } public void setchess(char[][] chess) { this.chess = chess; } public char getcurrentside() { return currentside; } public void setcurrentside(char currentside) { this.currentside = currentside; } //其他请补充 public fivechess(double width,double height,double celllen){ this.width=width; this.height=height; this.celllen=celllen; chess=new char[(int)height][(int)width]; for(int i=0;i<height;i++) for(int j=0;j<width;j++) chess[i][j]=' '; } public void play(int x,int y){ //将当前的棋子放置到(x,y) if(chess[x][y]==' '){ chess[x][y]=currentside; if(!judgegame(x,y,currentside)){ //游戏结束弹出信息框 alert alert = new alert(alert.alerttype.information); alert.settitle("五子棋游戏"); alert.setheadertext("提示信息"); alert.setcontenttext(currentside+"方取得胜利!"); alert.showandwait(); } changeside(); } } public void changeside(){ //更换下棋方 setcurrentside(currentside=='b'?'w':'b'); } public boolean judgegame(int row, int col, char chesscolor){ //判断游戏是否结束 if(rowjudge(row,col,chesscolor)&&coljudge(row,col,chesscolor)&&maindiagonaljudge(row,col,chesscolor)&&deputydiagonaljudge(row,col,chesscolor)) return true; return false; } public boolean rowjudge(int row,int col,char chesscolor){ //判断一行是否五子连线 int count=0; for(int j=col;j<width;j++){ if(chess[row][j]!=chesscolor) break; count++; } for(int j=col-1;j>=0;j--){ if(chess[row][j]!=chesscolor) break; count++; } if(count>=5) return false; return true; } public boolean coljudge(int row,int col,char chesscolor){ //判断一列是否五子连线 int count=0; for(int i=row;i<height;i++){ if(chess[i][col]!=chesscolor) break; count++; } for(int i=row-1;i>=0;i--){ if(chess[i][col]!=chesscolor) break; count++; } if(count>=5) return false; return true; } public boolean maindiagonaljudge(int row,int col,char chesscolor){ //判断主对角线是否五子连线 int count=0; for(int i=row,j=col;i<height&&j<width;i++,j++){ if(chess[i][j]!=chesscolor) break; count++; } for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--){ if(chess[i][j]!=chesscolor) break; count++; } if(count>=5) return false; return true; } public boolean deputydiagonaljudge(int row,int col,char chesscolor){ //判断副对角线是否五子连线 int count=0; for(int i=row,j=col;i>=0&&j<width;i--,j++){ if(chess[i][j]!=chesscolor) break; count++; } for(int i=row+1,j=col-1;i<height&&j>=0;i++,j--){ if(chess[i][j]!=chesscolor) break; count++; } if(count>=5) return false; return true; } }
二、视图
chesspane类继承pane类实现棋盘和五子棋的绘制
package view; import entity.fivechess; import javafx.scene.canvas.canvas; import javafx.scene.canvas.graphicscontext; import javafx.scene.layout.pane; import javafx.scene.paint.color; public class chesspane extends pane { public canvas getcanvas() { return canvas; } public canvas canvas; public graphicscontext getgc() { return gc; } public graphicscontext gc; public fivechess getfivechess() { return fivechess; } public void setfivechess(fivechess fivechess) { this.fivechess = fivechess; } public fivechess fivechess; public chesspane(fivechess fivechess){ this.fivechess=fivechess; double cell=fivechess.getcelllen(); drawpane(cell); drawchess(cell); getchildren().add(canvas); } public void drawpane(double cell){ canvas = new canvas(800,700); gc = canvas.getgraphicscontext2d(); gc.clearrect(0,0,canvas.getwidth(),canvas.getheight()); //绘制棋盘 gc.setstroke(color.black); for(int i=0;i<fivechess.getwidth();i++) for(int j=0;j<fivechess.getheight();j++){ gc.strokerect(100+i*cell,100+cell*j,cell,cell);//清理一个矩形取区域的内容 } } public void drawchess(double cell){ char[][] chess=fivechess.getchess(); for(int i=0;i<fivechess.getheight();i++) for(int j=0;j<fivechess.getwidth();j++){ if(chess[i][j]=='b'){ gc.setfill(color.black);//设置填充色 gc.filloval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell); } else if(chess[i][j]=='w'){ gc.setfill(color.white); gc.filloval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);//填充椭圆 gc.strokeoval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);//绘制轮廓 } } } }
三、控制器
playaction类继承自事件处理器eventhandler并传递的参数是鼠标事件,表示接受鼠标点击面板事件
package controller; import entity.fivechess; import javafx.event.eventhandler; import javafx.scene.control.alert; import javafx.scene.input.mouseevent; import view.chesspane; public class playaction implements eventhandler<mouseevent> { /**fivechess表示五子棋游戏模型*/ private fivechess fivechess; /**chesspane表示五子棋显示面板*/ private chesspane chesspane; public playaction(fivechess fivechess,chesspane chesspane){ this.chesspane=chesspane; this.fivechess = fivechess; } @override public void handle(mouseevent event) { //处理鼠标点击事件 double cell=fivechess.getcelllen(); //event.getx()获取鼠标点击x坐标,返回double类型 double x=event.getx(); double y=event.gety(); int i=(int)((x-100+cell/2)/cell); int j=(int)((y-100+cell/2)/cell); system.out.println(i+" "+j); fivechess.play(i,j); chesspane.drawchess(cell); if(!fivechess.judgegame(i,j,fivechess.getcurrentside()=='b'?'w':'b')){ alert alert = new alert(alert.alerttype.information); alert.settitle("五子棋游戏"); alert.setheadertext("提示信息"); alert.setcontenttext((fivechess.getcurrentside()=='b'?"白":"黑")+"方取得胜利!"); alert.showandwait(); } } }
四、测试
import controller.playaction; import entity.fivechess; import javafx.application.application; import javafx.scene.scene; import javafx.scene.control.alert; import javafx.stage.stage; import view.chesspane; import javax.print.attribute.standard.fidelity; public class test extends application { public static void main(string[] args) { launch(args); } @override public void start(stage primarystage) { fivechess fivechess = new fivechess(20,20,28.0); chesspane chesspane=new chesspane(fivechess); chesspane.setonmouseclicked(new playaction(fivechess,chesspane));//事件源绑定处理器 scene scene=new scene(chesspane,800,700); primarystage.setscene(scene); primarystage.settitle("五子棋游戏"); primarystage.show(); } }
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: C#连接MySQL的两个简单代码示例