做这个项目,本身目的仅仅是想应用学过的知识做个小项目,想知道它们在实际开发中应该如何应用,顺便帮我对几个月来的学习的知识更深入的了解。等我学完了数据库,再做个更大的项目,应该不成问题。所以,这篇文章适合学完Java基础的人学习,这也是我对自己Java学习第一阶段做的总结。
这是关于棋子文件上传代码
saveButton.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { //创建保存框对象 FileChooser fileChooser=new FileChooser(); if(!isWin) return; //展示Window window = ; File file=fileChooser.showSaveDialog(stage); BufferedWriter bw=null; if (file!=null){ try { bw=new BufferedWriter(new FileWriter(file) ); //一次写一个字符串 for (int i=0;i 复盘代码
replay.setOnAction(new EventHandler单机模式完整代码() { @Override public void handle(ActionEvent event) { //首先应该在点击复盘的时候清空所有棋子 //怎么删除呢,怎么添加怎么删除,pane.getChildren()是一个单列集合 pane.getChildren().removeIf(new Predicate package com.zht.ui.single; import com.zht.domain.Chess; import javafx.application.Application; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.alert; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import javafx.scene.layout.Background; import javafx.scene.layout.BackgroundFill; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import javafx.stage.FileChooser; import javafx.stage.Stage; import java.io.*; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Iterator; import java.util.Scanner; import java.util.Timer; import java.util.TimerTask; import java.util.function.Predicate; public class SingleUI extends Application { private boolean isBLACK=true; private Chess[] chesses=new Chess[100];//装棋子的容器 private int count=0;//棋盘上妻子个数 private int iswincount=1; private boolean isWin=false; private Stage stage=null; private Pane pane=null; Scanner scanner=null; public static void main(String[] args) { launch(args); } public void start (Stage stage) { this.stage=stage; //获取画板 this.pane=getPane(); pane.setBackground(new Background(new BackgroundFill(Color.BISQUE,null,null))); //给画板对象,绑定鼠标点击事件,就可以执行某些动作 moveInChess(pane); //创建场景对象,把画板对象放进去 Scene scene=new Scene(pane,850,860); stage.setScene(scene); stage.show(); } private void moveInChess(Pane pane) { pane.setOnMouseClicked(new EventHandler单机版实体类(){ //当鼠标点击画板,就会执行该方法 @Override public void handle(MouseEvent event) { //注意这里 if (isWin){ return; } //获取鼠标点击位置x和y的坐标 double x=event.getX(); double y=event.getY(); if(!(x>=50&&x<=750&&y>=50&&y<=750)){ return; } else{ int x1=(int)((x+25)/50)*50; int y1=(int)((y+25)/50)*50; //判断x和y上是否有棋子,使棋子无法同时在一点 if(isHas(x1,y1)){ System.out.println("该位置有棋子"); return; } Circle circle=null; Chess chess=null; if(isBLACK){ circle=new Circle(x1,y1,15, Color.BLACK); isBLACK=false; chess=new Chess(x1,y1,Color.BLACK); } else { circle=new Circle(x1,y1,15, Color.WHITE); isBLACK=true; chess=new Chess(x1,y1,Color.WHITE); } pane.getChildren().add(circle); //向容器里放一个棋子对象 chesses[count]=chess; count++; if(isWin(chess)){ //System.out.println("胜利了"); //弹窗 alert alert=new alert(alert.alertType.INFORMATION); alert.setContentText("五子棋大师!!!!!!"); alert.showAndWait(); isWin=true; } } }}); } //当落完子后,连续超过5个同颜色棋子即为胜利 private boolean isWin(Chess chess){ int x=chess.getX(); int y=chess.getY(); //向右 for (int i=x+50;i<=x+200&&i<750;i+=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(i,y); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } //向左 for (int i=x-50;i>=x-200&&i>=0;i-=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(i,y); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } if(iswincount>=5){ iswincount=1; return true; } iswincount=1; //判断垂直方向 for (int i=y+50;i<=y+200&&i<800;i+=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(x,i); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } for (int i=y-50;i>=y-200&&i>=0;i-=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(x,i); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } if(iswincount>=5){ iswincount=1; return true; } iswincount=1; //判断右斜 for (int i=x+50, j=y+50;i<=x+200&&i<750&&j<=y+200&&j<750;i+=50,j+=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(i,j); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } for (int i=y-50,j=x-50;i>=y-200&&i>=0&&j>=x-200&&j>=0;i-=50,j-=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(j,i); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } if(iswincount>=5){ iswincount=1; return true; } iswincount=1; //判断左斜 for (int i=x+50, j=y-50;i<=x+200&&i<750&&j>=y-200&&j>=0;i+=50,j-=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(i,j); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } for (int i=x-50, j=y+50;i>=x-200&&i>=0&&j<=y+200&&j<=750;i-=50,j+=50){ //判断这个位置,有没有棋子,颜色是什么 Chess _chess=getChess(i,j); if(_chess!=null&&chess.getColor().equals(_chess.getColor())){ iswincount++; } else { break; } } if(iswincount>=5){ iswincount=1; return true; } iswincount=1; return false; } //封装的方法,如果找到返回chess,没有返回null //获取指定坐标的棋子对象 private Chess getChess(int x,int y){ //定义个容器 for (int i=0;i () { @Override public void handle(ActionEvent event) { if (!isWin) return; //怎么删除呢,怎么添加怎么删除,pane.getChildren()是一个单列集合 pane.getChildren().removeIf(new Predicate package com.zht.domain;//真正的面向对象思想 import javafx.scene.paint.Color; public class Chess { private int x; private int y; private Color color; public Chess(){} public Chess(int x,int y,Color color){ this.x=x; this.y=y; this.color=color; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Chess chess = (Chess) o; return x == chess.x && y == chess.y && color.equals(chess.color); } @Override public int hashCode() { return 0; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public String getColor() { return color.toString(); } public void setColor(Color color) { this.color = color; } @Override public String toString() { return "com.Chess{" + "x=" + x + ", y=" + y + ", color=" + color + '}'; } }套接字编程
想要看到更多的代码,请
完整代码请点击此处



