javafx逻辑实现五子棋基本功能(悔棋,重新开始,保存棋谱,复盘,人人对战,单机模式
生活随笔
收集整理的這篇文章主要介紹了
javafx逻辑实现五子棋基本功能(悔棋,重新开始,保存棋谱,复盘,人人对战,单机模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
javafx邏輯實現五子棋基本功能(悔棋,重新開始,保存棋譜,復盤,人人對戰,單機模式)
做這個項目,本身目的僅僅是想應用學過的知識做個小項目,想知道它們在實際開發中應該如何應用,順便幫我對幾個月來的學習的知識更深入的了解。等我學完了數據庫,再做個更大的項目,應該不成問題。所以,這篇文章適合學完Java基礎的人學習,這也是我對自己Java學習第一階段做的總結。
這是關于棋子文件上傳代碼
saveButton.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic 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<count;i++){Chess chess=chesses[i];bw.write(chess.getX()+","+chess.getY()+","+chess.getColor());bw.newLine();bw.flush();}} catch (Exception e) {System.out.println("保存失敗");}finally {try {bw.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println(file);}});復盤代碼
replay.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {//首先應該在點擊復盤的時候清空所有棋子//怎么刪除呢,怎么添加怎么刪除,pane.getChildren()是一個單列集合pane.getChildren().removeIf(new Predicate<Object>() {@Overridepublic boolean test(Object obj) {return obj instanceof Circle;}});//清空了,為什么會是白子先走,而且有些地方仍舊顯示有棋子isBLACK=true;chesses =new Chess[100];count=0;isWin=false;FileChooser fileChooser=new FileChooser();file=fileChooser.showOpenDialog(stage);if (file!=null) {//關閉畫板鼠標點擊事件pane.setOnMouseClicked(new EventHandler<MouseEvent>() {@Overridepublic void handle(MouseEvent event) {return;}});Button up = up();pane.getChildren().add(up);Button down = down();pane.getChildren().add(down);movingdown(down);movingup(up);}elsereturn;}});單機模式完整代碼
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<MouseEvent>(){//當鼠標點擊畫板,就會執行該方法@Overridepublic 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<count;i++){Chess chess=chesses[i];if (chess.getX()==x&&chess.getY()==y){return chess;}}return null;}private boolean isHas(int x,int y){//定義個容器for (int i=0;i<count;i++){Chess chess=chesses[i];if (chess.getX()==x&&chess.getY()==y){return true;}}return false;}File file=null;private Pane getPane() {//創建畫板對象Pane pane=new Pane();//創建線條對象for (int i=1;i<16;i++){Line colline=new Line(50*i,50,50*i,750);Line rowline=new Line(50,50*i,750,50*i);//將線條放進畫板里pane.getChildren().add(colline);pane.getChildren().add(rowline);}//這里是重新開始按鈕Button startButton = getButton();startButton.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {if (!isWin)return;//怎么刪除呢,怎么添加怎么刪除,pane.getChildren()是一個單列集合pane.getChildren().removeIf(new Predicate<Object>() {@Overridepublic boolean test(Object obj) {return obj instanceof Circle;}});//清空了,為什么會是白子先走,而且有些地方仍舊顯示有棋子isBLACK=true;chesses =new Chess[100];count=0;isWin=false;}});pane.getChildren().add(startButton);//獲得悔棋的按鈕對象//悔棋,這里要理解迭代器Button backButton=backButton();backButton.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {if (isWin)return;Iterator it=pane.getChildren().iterator();Object obj=null;while (it.hasNext()){obj=it.next();}if (obj instanceof Circle){it.remove();}if (isBLACK==false)isBLACK=true;elseisBLACK=false;chesses =new Chess[100];count=0;isWin=false;}});pane.getChildren().add(backButton);//獲取保存棋譜的按鈕對象Button saveButton=getsaveButton();saveButton.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic 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<count;i++){Chess chess=chesses[i];bw.write(chess.getX()+","+chess.getY()+","+chess.getColor());bw.newLine();bw.flush();}} catch (Exception e) {System.out.println("保存失敗");}finally {try {bw.close();} catch (IOException e) {e.printStackTrace();}}}System.out.println(file);}});pane.getChildren().add(saveButton);//獲取復盤按鈕的對象Button replay=replayButton();replay.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {//首先應該在點擊復盤的時候清空所有棋子//怎么刪除呢,怎么添加怎么刪除,pane.getChildren()是一個單列集合pane.getChildren().removeIf(new Predicate<Object>() {@Overridepublic boolean test(Object obj) {return obj instanceof Circle;}});//清空了,為什么會是白子先走,而且有些地方仍舊顯示有棋子isBLACK=true;chesses =new Chess[100];count=0;isWin=false;FileChooser fileChooser=new FileChooser();file=fileChooser.showOpenDialog(stage);if (file!=null) {//關閉畫板鼠標點擊事件pane.setOnMouseClicked(new EventHandler<MouseEvent>() {@Overridepublic void handle(MouseEvent event) {return;}});Button up = up();pane.getChildren().add(up);Button down = down();pane.getChildren().add(down);movingdown(down);movingup(up);}elsereturn;}});pane.getChildren().add(replay);//獲取時間對象/* 例:如果想在javafx程序中加一個計時器,那么必須用Platform.runLater(new Runnable() {@Overridepublic void run() {********此處將要執行在javafx上執行的代碼寫進來}});*/Label label=new Label();label.setLayoutX(250);label.setLayoutY(0);Timer timer=new Timer();timer.schedule(new TimerTask() {@Overridepublic void run() {//獲取當前時間LocalDateTime localDateTime=LocalDateTime.now();DateTimeFormatter pattern=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");String time=localDateTime.format(pattern);Platform.runLater(new Runnable() {@Overridepublic void run() {label.setText(time);}});}},0,1000);pane.getChildren().add(label);return pane;}public void movingup(Button up){up.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {if (isWin)return;Iterator it = pane.getChildren().iterator();Object obj = null;while (it.hasNext()) {obj = it.next();}if (obj instanceof Circle) {it.remove();}if (!isBLACK)isBLACK = true;elseisBLACK = false;chesses = new Chess[100];count = 0;isWin = false;}});}public void movingdown(Button down) {try {scanner = new Scanner(new FileInputStream(file));down.setOnAction(new EventHandler<ActionEvent>() {@Overridepublic void handle(ActionEvent event) {Circle circle=null;String line=null;if ((line = scanner.nextLine()) != null) {String[] strs = line.split(",");if (strs[2].equals("0x000000ff"))circle = new Circle(Integer.parseInt(strs[0]), Integer.parseInt(strs[1]), 15, Color.BLACK);elsecircle = new Circle(Integer.parseInt(strs[0]), Integer.parseInt(strs[1]), 15, Color.WHITE);pane.getChildren().add(circle);}}});} catch (FileNotFoundException e) {System.out.println("已經到頭啦!");}}private Button down(){Button up=new Button(">");up.setPrefSize(40,30);up.setLayoutX(780);up.setLayoutY(560);return up;}private Button up(){Button up=new Button("<");up.setPrefSize(40,30);up.setLayoutX(780);up.setLayoutY(500);return up;}private Button replayButton(){Button replay=new Button("復盤");replay.setPrefSize(100,50);replay.setLayoutX(500);replay.setLayoutY(780);return replay;}private Button getsaveButton(){Button saveButton=new Button("保存棋譜");saveButton.setPrefSize(100,50);saveButton.setLayoutX(350);saveButton.setLayoutY(780);return saveButton;}private Button backButton() {Button backButton=new Button("悔棋");backButton.setPrefSize(100,50);backButton.setLayoutX(200);backButton.setLayoutY(780);return backButton;}private Button getButton() {Button startButton=new Button("再來一局");startButton.setPrefSize(100,50);startButton.setLayoutX(50);startButton.setLayoutY(780);return startButton;}} /* class Pane{public final void setOnMouseClicked(EventHandler<? super MouseEvent> value){} }*/單機版實體類
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;}@Overridepublic 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);}@Overridepublic 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;}@Overridepublic String toString() {return "com.Chess{" +"x=" + x +", y=" + y +", color=" + color +'}';} }套接字編程
想要看到更多的代碼,請
完整代碼請點擊此處
總結
以上是生活随笔為你收集整理的javafx逻辑实现五子棋基本功能(悔棋,重新开始,保存棋谱,复盘,人人对战,单机模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 男儿有泪不轻弹,只是未遇感动事
- 下一篇: 教你如何用python俘获女神芳心