亲宝软件园·资讯

展开

javaFX五子棋 javaFX实现五子棋小游戏

As_Yan_Do 人气:1

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经典小游戏汇总

加载全部内容

相关教程
猜你喜欢
用户评论