javafx 五子棋 两小时实现最low-五指棋
用javaFx 做界面,
直接上代码
package com.clyz.canvas.net;import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.input.MouseButton;
import javafx.scene.paint.Color;
import javafx.scene.shape.ArcType;
import javafx.scene.text.Font;
import javafx.stage.Stage;import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;/*** 网 图*/
public class NetMain extends Application {public static void main(String[] args) {launch(args);}@Overridepublic void start(Stage primaryStage) throws Exception {primaryStage.setTitle("网");Group root = new Group();Canvas canvas = new Canvas(800, 800);GraphicsContext gc = canvas.getGraphicsContext2D();drawShapes(gc);final Boolean[] who = {true};Pot pot = new Pot();canvas.setOnMouseClicked(e -> {// 鼠标左键单击if (e.getButton() == MouseButton.PRIMARY) {if (who[0]) {gc.setFill(Color.RED);} else {gc.setFill(Color.BLACK);}who[0] = !who[0];double sceneX = e.getSceneX();double sceneY = e.getSceneY();if (sceneX < 40 || sceneX > 800 || sceneY < 40 || sceneY > 800) {return;}double x = getLocation(sceneX);double y = getLocation(sceneY);boolean b = pot.set(!who[0], x, y);if (!b) {return;}gc.fillArc(x - 15, y - 15, 30, 30, 0, 360, ArcType.ROUND);//绘制完整的原型boolean valid = pot.valid(!who[0], x, y);if (valid) {gc.setFont(new Font(28));gc.strokeText("OVER", 300, 300);}} else if (e.getButton() == MouseButton.SECONDARY) {gc.clearRect(0, 0, 800, 800);pot.pots.clear();drawShapes(gc);}//刷新,否则不能接收到键盘事件了canvas.requestFocus();});root.getChildren().add(canvas);primaryStage.setScene(new Scene(root));primaryStage.show();}private double getLocation(double l) {double v = l % 40;double v1 = Math.floor(l / 40);return v > 20.0 ? v1 * 40 + 40 : v1 * 40;}private void drawShapes(GraphicsContext gc) {gc.setStroke(Color.GREY);gc.setLineWidth(1);for (int i = 40; i < 800; i = i + 40) {gc.strokeLine(i, 40, i, 760);}for (int i = 40; i < 800; i = i + 40) {gc.strokeLine(40, i, 760, i);}}class Pot {/*** 记录 棋子点位 0/1_x_y* 0/1 表示 黑/白* x x 坐标 小标 实际 位置 为 x*40 从1开始*/private Set<String> pots = new ConcurrentSkipListSet<>();public String getKey(Boolean bool, double x, double y) {int i = bool ? 1 : 0;double indexX = Math.floor(x / 40);double indexY = Math.floor(y / 40);return i + "_" + indexX + "_" + indexY;}public boolean set(Boolean bool, double x, double y) {String key = getKey(bool, x, y);String key1 = getKey(!bool, x, y);if (pots.contains(key) || pots.contains(key1)) {return false;} else {pots.add(key);return true;}}public boolean valid(Boolean bool, double x, double y) {int r = bool ? 1 : 0;double indexX = Math.floor(x / 40);double indexY = Math.floor(y / 40);boolean heng = heng(r, indexX, indexY);if (heng) return true;boolean su = su(r, indexX, indexY);if (su) return true;boolean xieZheng = xieZheng(r, indexX, indexY);if (xieZheng) return true;return xieSu(r, indexX, indexY);}private boolean xieZheng(int r, double x, double y) {StringBuilder buffer = new StringBuilder();for (int i = (int) (x > 4 ? x - 4 : 1); i <= (int) (x > 15 ? 19 : x + 4); i++) {buffer.append(have(r, i, x + y - i));}return buffer.toString().contains("11111");}private boolean xieSu(int r, double x, double y) {StringBuilder buffer = new StringBuilder();for (int i = (int) (y > 4 ? y - 4 : 1); i <= (int) (y > 15 ? 19 : y + 4); i++) {buffer.append(have(r, x - (y - i), i));}return buffer.toString().contains("11111");}private boolean su(int r, double x, double y) {// 横 x 不变StringBuilder buffer = new StringBuilder();for (int i = (int) (y > 4 ? y - 4 : 1); i <= (int) (y > 15 ? 19 : y + 4); i++) {buffer.append(have(r, x, i));}return buffer.toString().contains("11111");}private boolean heng(int r, double x, double y) {// 横 y 不变StringBuilder buffer = new StringBuilder();for (int i = (int) (x > 4 ? x - 4 : 1); i <= (int) (x > 15 ? 19 : x + 4); i++) {buffer.append(have(r, i, y));}return buffer.toString().contains("11111");}private String have(int r, double x, double y) {return pots.contains(r + "_" + x + "_" + y) ? "1" : "0";}}
}
代码完毕,点击运行
javafx 五子棋 两小时实现最low-五指棋相关推荐
- 两小时搞定负载均衡51CTO技术沙龙现场视频2
田逸田老师幽默风趣的案例讲解 7月5日下午,51CTO技术社区主办的"两小时搞定负载均衡 全真实战案例解析"活动圆满结束.参加沙龙的100多位用户和嘉宾相聚近三个小时,生动的课程. ...
- 排队两小时买墨茉、虎头局,但别指望年轻人“味蕾忠诚”
(图片来源于网络,侵删) 文 | 易不二 来源 | 螳螂财经 虽然"面包总会有的",但没准儿你爱吃的那家,一不留神就偷偷关门了. 前段时间,在上海开了22年的宜芝多全部关门了.宜芝 ...
- vba htm转成图片_提取1000页PPT的图片,同事花了两小时,我只花了两分钟
临下班前老板又双叒搞临时任务,丢来几份几百页的PPT,让我们把里面的图片都提取出来,同事一边哀嚎一边埋头苦干,疯狂地另存为,我瞥了一眼, 目测她要两小时才能搞定. 其实不用那么麻烦,提取图片这种小事情 ...
- 两表格合并_六个高效办公Excel小技巧,让你提前下班两小时!
每天下午一点,PPT技能进步一点 大家中午好呀! 大家好这是我们的抖音号 @PPT模板 抖音搜索"PPT1080" 就可以啦更多福利等你来拿! 小编 发现抖音里无奇不有 有关于Ex ...
- 入职必备两小时入门SVN(二)
入职必备两小时入门SVN(二) 1.SVN使用 1.创建版本库 打开SVN服务端(开始菜单) 右击Repository.点击Create Repository,出现下面画面,选择默认就好 2.导入项目 ...
- 入职必备两小时入门SVN(一)
入职必备两小时入门SVN(一) 1.简介 SVN的全称是Subversion,即版本控制系统.它是最流行的一个开放源代码的版本控制系统.作为一个开源的版本控制系统,Subversion管理着随时间改变 ...
- mpvue两小时,产出一个《点钞辅助工具》小程序
代码地址如下: http://www.demodashi.com/demo/13564.html CoffeeScript,Pug,Sass使用 以下内容门槛较高,如看不懂或觉得需要继续了解,请前往官 ...
- 海龟绘图两小时上手C语言 - 5 彩色螺旋线
海龟绘图两小时上手C语言 - 5 彩色螺旋线 整个教程的目录: 童晶:海龟绘图两小时上手C语言 - 0 引言 童晶:海龟绘图两小时上手C语言 - 1 绘制直线 童晶:海龟绘图两小时上手C语言 - 2 ...
- 海龟绘图两小时上手C语言 - 4 任意螺旋线
海龟绘图两小时上手C语言 - 4 任意螺旋线 整个教程的介绍和目录在这里: 童晶:海龟绘图两小时上手C语言 - 0 引言 4.1 计算正多边形的角度 数值之间也可以进行加.减.乘.除.取余运算,在代码 ...
最新文章
- 小功能 - 收藏集 - 掘金
- 基于开源、混合云 惠普Helion开年3款新品发布
- 为什么在旺遍天下生成代码我在线却是离线状态 旺旺在线代码生成
- SpringMVC中重定向
- 5、ResultSet结果集
- 在国企当程序员是什么体验?
- usbserials
- 一次性上传多个文件到服务器端(一)
- bimmercode刷隐藏教程_PS教程:快速提取人物像素,制作人物海报主体,简单易学...
- java面向对象基础代码_JAVA基础知识点之Java面向对象
- ABAP中的F4帮助怎么用
- 《图解机器学习-杉山将著》读书笔记---CH5
- 制度汇编格式怎么生成目录_规章制度编写规范
- 语音专题第四讲,语音识别之解码器技术简介|大牛讲堂
- react-native 错误捕捉与处理
- 计算机在档案部门应用范围,计算机在档案管理中的相关运用
- UE4 四叉树 QuadTree
- 月销10w支!不用洗牙,牙齿也能白到发光!买它,这支黑科技美白电动牙刷
- 用例文档应该包括哪些内容
- CVPR ECCV ICCV论文汇总