javafx 制作五子棋游戏——简单MVC框架
目录
- 游戏描述
- 上手须知
- 源码地址
- 项目结构
- 一、实体层
- 二、视图层
- 三、控制器
- 四、测试
- 效果图
游戏描述
一个五子棋游戏,能实现双方黑白对决,当一方获胜时给出提示信息,利用Javafx实现GUI界面实现
上手须知
jdk11以上由于官方已经讲javafx组件分离,需要自己下载相应的组件,需要点击configuration然后在vm options中添加下面参数,注意将你的javafx插件路径替换成你自己的(jdk1.8的盆友请忽略)
--module-path "E:\jdk11\javafx-sdk-11.0.2\lib" --add-modules=javafx.controls,javafx.fxml
源码地址
https://github.com/441712875al/five-chess
项目结构
一、实体层
FiveChess类
- 提供五子棋实体包含的所有信息
- 判断游戏是否结束
- play方法改变chess[][]棋盘中的数据
package entity;import enums.Side;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 Side currentSide=Side.BLACK;public char getFlag() {return flag;}private char flag=' ';private int[][] chess;public int[][] getChess() {return chess;}public void setChess(int[][] chess) {this.chess = chess;}public Side getCurrentSide() {return currentSide;}public void setCurrentSide(Side currentSide) {this.currentSide = currentSide;}public FiveChess(double width,double height,double cellLen){this.width=width;this.height=height;this.cellLen=cellLen;chess=new int[(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.getCode();changeSide();}}public void changeSide(){//更换下棋方setCurrentSide(currentSide==Side.BLACK?Side.WHITE:Side.BLACK);}public boolean judgeGame(int row, int col, Side chessColor){//判断游戏是否结束if(rowJudge(row,col,chessColor)&&colJudge(row,col,chessColor)&&mainDiagonalJudge(row,col,chessColor)&&DeputyDiagonalJudge(row,col,chessColor))return true;return false;}/*** 横向判断五子连线* @param row* @param col* @param chessColor* @return*/public boolean rowJudge(int row,int col,Side chessColor){int count = 0;for(int j = col;j<width;j++){if(chess[row][j]!=chessColor.getCode())break;count++;}for(int j=col-1;j>=0;j--){if(chess[row][j]!=chessColor.getCode())break;count++;}if(count>=5)return false;return true;}/*** 纵向判断* @param row* @param col* @param chessColor* @return*/public boolean colJudge(int row,int col,Side chessColor){int count=0;for(int i=row;i<height;i++){if(chess[i][col]!=chessColor.getCode())break;count++;}for(int i=row-1;i>=0;i--){if(chess[i][col]!=chessColor.getCode())break;count++;}if(count>=5)return false;return true;}/*** 主对角线方向* @param row* @param col* @param chessColor* @return*/public boolean mainDiagonalJudge(int row,int col,Side chessColor){int count=0;for(int i=row,j=col;i<height&&j<width;i++,j++){if(chess[i][j]!=chessColor.getCode())break;count++;}for(int i=row-1,j=col-1;i>=0&&j>=0;i--,j--){if(chess[i][j]!=chessColor.getCode())break;count++;}if(count>=5)return false;return true;}/*** 副对角线方向* @param row* @param col* @param chessColor* @return*/public boolean DeputyDiagonalJudge(int row,int col,Side chessColor){int count=0;for(int i=row,j=col;i>=0&&j<width;i--,j++){if(chess[i][j]!=chessColor.getCode())break;count++;}for(int i=row+1,j=col-1;i<height&&j>=0;i++,j--){if(chess[i][j]!=chessColor.getCode())break;count++;}if(count>=5)return false;return true;}
}
二、视图层
ChessPane
类继承Pane类实现棋盘和五子棋的绘制
package view;import entity.FiveChess;
import enums.Side;
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 canvas;public GraphicsContext gc;public FiveChess fiveChess;public Canvas getCanvas() {return canvas;}public GraphicsContext getGc() {return gc;}public FiveChess getFiveChess() {return fiveChess;}public void setFiveChess(FiveChess fiveChess) {this.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){/*获取棋子的位置*/int[][] chess=fiveChess.getChess();for(int i=0;i<fiveChess.getHeight();i++)for(int j=0;j<fiveChess.getWidth();j++){if(chess[i][j]== Side.BLACK.getCode()){gc.setFill(Color.BLACK);gc.fillOval(100+i*cell-cell/2,100+j*cell-cell/2,cell,cell);//画棋子}else if(chess[i][j]==Side.WHITE.getCode()){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
并传递的参数是鼠标事件,表示接受鼠标点击面板事件,该类可以使用lamda表达式替换。
package controller;import entity.FiveChess;
import enums.Side;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.input.MouseEvent;
import view.ChessPane;import java.io.*;/*** 时间处理类,可以使用lamda表达式替代*/
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;}@Overridepublic void handle(MouseEvent event){//获取棋盘的小方格的大小double cell = fiveChess.getCellLen();//获取鼠标点击坐标double x = event.getX();double y = event.getY();/*映射到数组中的坐标*/int i = (int)((x-100+cell/2)/cell);int j = (int)((y-100+cell/2)/cell);/*记录下落子位置*/try (FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.dir") + "/src/记录.txt"),true)) {out.write(new String(fiveChess.getCurrentSide().getDesc()+"("+j+","+i+")\n").getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}/*改变棋盘状态*/fiveChess.play(i,j);/*重新渲染棋子*/chessPane.drawChess(cell);/*判断是否结束*/if(!fiveChess.judgeGame(i,j,fiveChess.getCurrentSide()== Side.BLACK?Side.WHITE:Side.BLACK)){Alert alert = new Alert(Alert.AlertType.INFORMATION);alert.setTitle("五子棋游戏");alert.setHeaderText("提示信息");String msg = (fiveChess.getCurrentSide()==Side.BLACK?Side.WHITE:Side.BLACK).getDesc()+"取得胜利!";alert.setContentText(msg);try (FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.dir") + "/src/记录.txt"),true)) {out.write(msg.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}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;
import java.io.*;public class Test extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) {FiveChess fiveChess = new FiveChess(20,20,28.0);ChessPane chessPane=new ChessPane(fiveChess);/*记录下棋盘信息*/try(FileOutputStream out = new FileOutputStream(new File(System.getProperty("user.dir")+"/src/记录.txt"),true)){out.write(new String("棋局开始,黑子先行\n").getBytes());}catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}/*注册时间处理器*/chessPane.setOnMouseClicked(new PlayAction(fiveChess,chessPane));/*显示舞台*/Scene scene=new Scene(chessPane,800,700);primaryStage.setScene(scene);primaryStage.setTitle("五子棋游戏");primaryStage.show();}
}
效果图
查看src
目录下的记录.txt
文件保存的本次对局的记录
javafx 制作五子棋游戏——简单MVC框架相关推荐
- ajax注解解决中文乱码,基于注解的简单MVC框架的实现,以及jquery,prototype,ajax传输乱码问题的一点解决方法...
1:基于注解的简单MVC框架的实现 效果:1:用户只需要定义一些普通的java类来做为M层,也就是STRUTS的action类,该类里包含1到 N个控制方法,每个方法需要的form数据,由注解@Act ...
- 如何用python制作五子棋游戏_Python制作打地鼠小游戏
原文链接 Python制作小游戏(二十一)mp.weixin.qq.com 效果展示 打地鼠小游戏https://www.zhihu.com/video/1200492442610450432 简介 ...
- c++ 制作五子棋游戏
作者花了大量时间来制作这个五子棋,今天就给大家看一下本作者的成果,请大家点个赞吧!谢谢! #include <iostream> #include <windows.h> #i ...
- 小心踩雷!手把手教你制作扫雷游戏简单版本
扫雷游戏----C语言必写游戏之一 扫雷游戏 背景 起源与玩法 基本实现思路 涉及语言的主要内容 具体实现步骤 1.基本框架 2.菜单页面的实现 3.初始化棋盘 4.在屏幕中显示棋盘 5.布置好棋盘中 ...
- Servlet3.0实现的简单mvc框架
jar包准备:servlet-api-3.0.jar 项目结构如下: 注解Mapping ,用于映射url地址 /** 文件名:Action.java* 版权:Copyright 2007-2016 ...
- StarlingMVC:为Starling量身打造的MVC框架
详细了解StarlingMVC框架,请登录其官方站点: http://creativebottle.github.com/starlingMVC/ 以下中文翻译转自Starling中文站,仅供部分参考 ...
- python五子棋游戏15*15_python实现五子棋游戏(pygame版)
分享python 编写的五子棋游戏 分享高手给用python编写一个五子棋游戏,需要代码. .心里知道有这么回事,白天就很正常晚上就胡思乱想,事已至此小编们都该向前走一步了. 请用PYTHON编一个小 ...
- 源码分析系列 | 从零开始写MVC框架
1. 前言 2. 为什么要自己手写框架 3. 简单MVC框架设计思路 4. 课程目标 5. 编码实战 5.1 配置阶段 web.xml配置 config.properties 自定义注解 5.2 初始 ...
- 用c语言做一个五子棋程序,C语言制作简单五子棋游戏
原标题:C语言制作简单五子棋游戏 C语言制作简单的五子棋游戏 学习C语言的人很多,但是用C语言很少,而用来为自己所用,来做游戏的人就更少了,很多人都是跟着学校学习,学校讲到哪就坐到哪,但是以后却还是不 ...
最新文章
- Kaggle Tabular Playground Series - Jan 2022 的baseline和日期特征处理
- java IO(输入输出) 字节缓冲流
- windows 如何在Windows命令行下配置IP地址
- java打印等腰梯形
- 2017-2018-1 20155338 加分项目——PWD的实现
- 完全没法比!华为P40 Pro和iPhone 9宣传视频同曝光
- 牛客小白月赛8: I. 路灯孤影(区间DP)
- ASP.NET Core 中文文档 第四章 MVC(3.9)视图组件
- javaWeb过滤器——Filter
- sin的傅里叶变换公式_sin2t的傅里叶变换
- 通信总线模块:RS485、SP3232
- 算术关系和逻辑关系---皮尔斯逻辑之二
- 2021-2027全球与中国成像雷达市场现状及未来发展趋势
- 2020-07-16-----web前端开发中用到的PS基础操作(PS取色、PS测量、PS切片)
- 2019年北航、南大、东南、上科大及本校计算机系保研
- GOME-2 SIF 数据链接
- iOS 极光推送没有声音怎么办?
- #Tensorflow Process finished with exit code 3#
- 【java】把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。
- flash 批量编译发布fla 文件
热门文章
- TwinCAT3 ADS通讯笔记
- Python+Vue计算机毕业设计BeatHouse伴奏交易平台z19pu(源码+程序+LW+部署)
- 如何用人工的方式将Excel里的一堆数字变成一个数组
- (信息学奥赛一本通 1299)糖果#线性动态规划#
- 使用自定义的Layer和Cell实现手写汉字生成(Tensorflow2)
- Java C#分析WAV音频文件1Khz是否有声音
- matlab限幅滤波法,几种常用的滤波方法
- 知识付费APP的崛起
- 【花雕动手做】有趣好玩的音乐可视化系列项目(27)--磁搅LED水旋灯
- 我的电脑开机后桌面上没有图标