javaFX实现五子棋小游戏

 更新时间:2020年07月27日 09:11:18   作者:As_Yan_Do  
这篇文章主要为大家详细介绍了javaFX实现五子棋小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

javaFX实现五子棋游戏,供大家参考,具体内容如下

做课程设计的时候做到这个,分享出来大家参考一下吧,图片为游戏运行过程
最下的代码就是整个实现整个需求的

package Version3;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class Version3 extends Application {

 private char winer = ' ';//胜者
 TextField tf = new TextField();
 private char whoseTurn = (int)(Math.random() * 2) == 0 ? 'X' : 'O';//随机回合
 private int numberOfClick = 0;
 @Override
 public void start(Stage primaryStage) {
 Button bt = new Button("New game"); //按钮
 //TextField tf = new TextField();
 
 Cell [] cell = new Cell[9];
 for(int i=0; i<9 ;i++){
 cell[i] = new Cell(2,1);
 }
 
 GridPane gpane = new GridPane();
 int num =0;
 for(int i=0; i<3 ;i++){
 for(int j=0; j<3 ;j++){
 gpane.add(cell[num],j,i);
 num++;
 }
 }
 
 tf.setEditable(false);//文本不可编辑
 
 BorderPane pane = new BorderPane();
 pane.setTop(bt);
 pane.setAlignment(bt,Pos.CENTER);
 pane.setCenter(gpane);
 pane.setBottom(tf);
 
 //按钮事件 重新开始游戏
 bt.setOnAction(e ->{
 gpane.getChildren().clear();
 
 for (int i = 0; i < 9; i++) {
  cell[i] = new Cell(2,1);
  
 }
 
 int k = 0;
 for (int i = 0; i < 3; i++) {
  for (int j = 0; j < 3; j++) {
  gpane.add(cell[k], j, i);
  k++;
  }
 }
 
 whoseTurn = (int)(Math.random() * 2) == 0 ? 'X' : 'O';
 tf.setText(whoseTurn == 'X' ? "X's turn" : "O's turn");
  
 setListenerForCells(cell);//调用单元格的侦听器
 
 winer = ' ';
 });
 
 // 给底部文本设置初始情况
 tf.setText(whoseTurn == 'X' ? "X's turn" : "O's turn");
 
 // 给每个面板设置一个监听器
 setListenerForCells(cell);
 
 Scene scene = new Scene(pane,495,550);
 primaryStage.setTitle("version3");
 primaryStage.setScene(scene);
 primaryStage.show();
 }

 public static void main(String[] args) {
 launch(args);
 }
 
 public void judgeWhoWin(Cell[] cell){
 // 判断行
 for (int i = 0; i < 3; i++) {
  if (cell[i * 3].contain == 'X'&& cell[i * 3 + 1].contain == 'X'&& cell[i * 3 + 2].contain == 'X') {
  winer = 'X';
  } else if (cell[i * 3].contain == 'O'&& cell[i * 3 + 1].contain == 'O'&& cell[i * 3 + 2].contain == 'O') {
  winer = 'O';
  }
 }
 
 // 判断列
 for (int i = 0; i < 3; i++) {
  if (cell[i].contain == 'X'&& cell[i + 3].contain == 'X'&& cell[i + 6].contain == 'X') {
  winer = 'X';
  } else if (cell[i].contain == 'O'&& cell[i + 3].contain == 'O'&& cell[i + 6].contain == 'O') {
  winer = 'O';
  }
 }
 
 // 判断主、副对角线
 if (cell[0].contain == 'X' && cell[4].contain == 'X' && cell[8].contain == 'X'||
  cell[2].contain == 'X' && cell[4].contain == 'X' && cell[6].contain == 'X') {
  winer = 'X';
 } else if (cell[0].contain == 'O' && cell[4].contain == 'O' && cell[8].contain == 'O'||
  cell[2].contain == 'O' && cell[4].contain == 'O' && cell[6].contain == 'O') {
  winer = 'O';
 }
 }

 //点击鼠标 在#字表格里面显示一个随机位置的的X or O
 public void setListenerForCells(Cell[] cell){
 numberOfClick = 0;//点击次数清零
 for (int i = 0; i < cell.length; i++) {
 Cell temp = cell[i];
 
 temp.setOnMouseClicked(e -> {
 if (winer == ' ') {
  if (whoseTurn == 'X' 
  && e.getButton() == MouseButton.PRIMARY 
  && temp.editable) {
  temp.setContain(1,1);
  
  temp.editable = false;//不可编辑
  
  winer = ' ';
  whoseTurn = 'O';//下一次换回和
  
  judgeWhoWin(cell);//判断输赢
  if(winer == ' '){
  numberOfClick++;
  if(numberOfClick == 9){
  tf.setText("the game is a draw");
  }
  else{
  tf.setText(whoseTurn + "'s turn");
  }
  }
  else{
  tf.setText("Game is over, and the winner is "+ winer);
  }
  
  } else if (whoseTurn == 'O' 
  && e.getButton() == MouseButton.PRIMARY 
  && temp.editable) {
  temp.setContain(1,2);
  
  temp.editable = false;//不可编辑
  
  winer = ' ';
  whoseTurn = 'X';//下一次换回和
  
  judgeWhoWin(cell);//判断输赢
  if(winer == ' '){
  numberOfClick++;
  if(numberOfClick == 9){
  tf.setText("the game is a draw");
  }
  else{
  tf.setText(whoseTurn + "'s turn");
  }
  }
  else{
  tf.setText("Game is over, and the winner is "+ winer);
  } 
  }
 }
 });  
 }
}


class Cell extends BorderPane{
 public char contain =' ';
 int num1 = 0,num2 = 0;
 public boolean editable = true;

 public Cell(int num1,int num2){
 super.setPadding(new Insets(5));
 super.setStyle("-fx-border-color: black");
 super.setPrefSize(2000,2000);
 this.setContain(num1,num2);
 }
 
 public void setContain(int num1,int num2){
 if(num1==1 && editable){
 if(num2==1){
  //构建X面板
  Line line1 = new Line(0,0,150,150);
  Line line2 = new Line(150,0,0,150);
  StackPane pane1 = new StackPane();
  pane1.getChildren().addAll(line1,line2); 
  super.setCenter(pane1);
  
  contain = 'X';
 }
 
 else if(num2==2 && editable){
  ///构建O面板
  Circle circle = new Circle(75);//半径为75 
  circle.setFill(Color.WHITE);//填充为白色
  circle.setStroke(Color.BLACK);//边框为黑色
  StackPane pane2 = new StackPane();
  pane2.getChildren().add(circle);
  super.setCenter(pane2);
  
  contain = 'O';
 } 
 }
 }
 }
}

更多有趣的经典小游戏实现专题,分享给大家:

C++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

JavaScript经典游戏 玩不停

javascript经典小游戏汇总

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Mybatis中resultMap标签和sql标签的设置方式

    Mybatis中resultMap标签和sql标签的设置方式

    这篇文章主要介绍了Mybatis中resultMap标签和sql标签的设置方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • idea使用带provide修饰依赖导致ClassNotFound

    idea使用带provide修饰依赖导致ClassNotFound

    程序打包到Linux上运行时,若Linux上也有这些依赖,为了在Linux上运行时避免依赖冲突,可以使用provide修饰,本文主要介绍了idea使用带provide修饰依赖导致ClassNotFound,下面就来介绍一下解决方法,感兴趣的可以了解一下
    2024-01-01
  • 详解Java序列化如何破坏单例模式

    详解Java序列化如何破坏单例模式

    这篇文章主要为大家详细介绍了Java序列化是如何破坏单例模式的,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以学习一下
    2023-12-12
  • Java中Spring WebSocket详解

    Java中Spring WebSocket详解

    本篇文章主要通过代码给大家详细分析了Java中Spring WebSocket的用法,需要的读者们参考学习下吧。
    2017-12-12
  • MyBatis中的关联关系配置与多表查询的操作代码

    MyBatis中的关联关系配置与多表查询的操作代码

    本文介绍了在MyBatis中配置和使用一对多和多对多关系的方法,通过合理的实体类设计、Mapper接口和XML文件的配置,我们可以方便地进行多表查询,并丰富了应用程序的功能和灵活性,需要的朋友可以参考下
    2023-09-09
  • Java多线程之线程安全问题详细解析

    Java多线程之线程安全问题详细解析

    这篇文章主要给大家介绍了关于Java多线程之线程安全问题的相关资料,Java多线程中线程安全问题是一个常见的问题,因为多个线程可能同时访问共享的资源,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • 解决java启动时报线程占用报错:Exception in thread “Thread-14“ java.net.BindException: Address already in use: bind

    解决java启动时报线程占用报错:Exception in thread “Thread-14“ java.ne

    这篇文章主要给大家介绍了关于解决java启动时报线程占用:Exception in thread “Thread-14“ java.net.BindException: Address already in use: bind的相关资料,文中将解决的办法介绍的非常详细,需要的朋友可以参考下
    2023-04-04
  • Java中FileWriter类的常用方法说明

    Java中FileWriter类的常用方法说明

    这篇文章主要介绍了Java中FileWriter类的常用方法说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • java字符串格式化(String类format方法)

    java字符串格式化(String类format方法)

    这篇文章主要介绍了java字符串格式化(String类format方法),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • SpringBoot结合mockito测试实战

    SpringBoot结合mockito测试实战

    与集成测试将系统作为一个整体测试不同,单元测试更应该专注于某个类。所以当被测试类与外部类有依赖的时候,尤其是与数据库相关的这种费时且有状态的类,很难做单元测试。但好在可以通过“Mockito”这种仿真框架来模拟这些比较费时的类,从而专注于测试某个类内部的逻辑
    2022-11-11

最新评论