利用JavaFX设计一个井字棋游戏,其中包括了能够与玩家对战的AI。AI的实现相比五子棋来说要简单得多,可以保证AI在后手情况下绝对不会输,具体实现如下:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package 井字棋;

import javafx.application.Application;

import javafx.scene.Scene;

import javafx.stage.Stage;

import javafx.scene.layout.GridPane;

import javafx.scene.shape.Circle;

import javafx.scene.shape.Line;

import javafx.scene.control.Button;

import javafx.event.EventHandler;

import javafx.event.ActionEvent;

import javafx.animation.Timeline;

import javafx.util.Duration;

import javafx.animation.KeyFrame;

import javafx.scene.control.TextArea;

/**

*

* @author Midori

*/

public class 井字棋 extends Application{

private static int a[][]=new int[3][3];

private static int step=1;

private static int last=5;

private static boolean win=false;

private static boolean draw=false;

private static int wincount=0;

private static int drawcount=0;

private static String s="<( ̄︶ ̄)↗[GO!]"+"\n"+"[^ェ^] よろしく!";

@Override

public void start(Stage primaryStage){

GridPane pane=new GridPane();

pane.setMinSize(500, 300);

primaryStage.setResizable(false);

Button bt1=new Button();

bt1.setMinSize(100,100);

Button bt2=new Button();

bt2.setMinSize(100,100);

Button bt3=new Button();

bt3.setMinSize(100,100);

Button bt4=new Button();

bt4.setMinSize(100,100);

Button bt5=new Button();

bt5.setMinSize(100,100);

Button bt6=new Button();

bt6.setMinSize(100,100);

Button bt7=new Button();

bt7.setMinSize(100,100);

Button bt8=new Button();

bt8.setMinSize(100,100);

Button bt9=new Button();

bt9.setMinSize(100,100);

pane.add(bt1,0,0);

pane.add(bt2,0,1);

pane.add(bt3,0,2);

pane.add(bt4,1,0);

pane.add(bt5,1,1);

pane.add(bt6,1,2);

pane.add(bt7,2,0);

pane.add(bt8,2,1);

pane.add(bt9,2,2);

Button bt=new Button("Again");

pane.add(bt,5,1);

Scene scene=new Scene(pane);

EventHandler<ActionEvent>eventHandler=e1->{

bt.setOnMousePressed(e->{

for(int i=0;i<3;i++){

for(int j=0;j<3;j++)

a[i][j]=0;

}

step=1;

last=5;

win=false;

draw=false;

s="<( ̄︶ ̄)↗[GO!]"+"\n"+"[^ェ^] よろしく!";

});

if(last==0){

draw=true;

last=-1;

drawcount++;

}

if(win){

last=-1;

s="你咋又输了~"+"\n"+"( ̄_, ̄ )";

}

if(draw){

s="欸~平局~~"+"\n"+"╮(╯-╰)╭";

}

TextArea t2=new TextArea();

t2.setText(s);

t2.setMaxHeight(80);

pane.add(t2,5,2);

TextArea t1=new TextArea();

t1.setText("你已经输了"+wincount+"局!"+"\n"+"你已经平了"+drawcount+"局!"+"\n"+"你还没赢过~");

t1.setMaxHeight(80);

pane.add(t1,5,0);

bt1.setText(Switch(a[0][0]));

bt2.setText(Switch(a[0][1]));

bt3.setText(Switch(a[0][2]));

bt4.setText(Switch(a[1][0]));

bt5.setText(Switch(a[1][1]));

bt6.setText(Switch(a[1][2]));

bt7.setText(Switch(a[2][0]));

bt8.setText(Switch(a[2][1]));

bt9.setText(Switch(a[2][2]));

bt1.setOnMousePressed(e->{

if(step>0&&a[0][0]==0&&last>0){

a[0][0]=1;

step--;

last--;

Play(1);

step++;

}

});

bt2.setOnMousePressed(e->{

if(step>0&&a[0][1]==0&&last>0){

a[0][1]=1;

step--;

last--;

Play(2);

step++;

}

});

bt3.setOnMousePressed(e->{

if(step>0&&a[0][2]==0&&last>0){

a[0][2]=1;

step--;

last--;

Play(3);

step++;

}

});

bt4.setOnMousePressed(e->{

if(step>0&&a[1][0]==0&&last>0){

a[1][0]=1;

step--;

last--;

Play(4);

step++;

}

});

bt5.setOnMousePressed(e->{

if(step>0&&a[1][1]==0&&last>0){

a[1][1]=1;

step--;

last--;

Play(5);

step++;

}

});

bt6.setOnMousePressed(e->{

if(step>0&&a[1][2]==0&&last>0){

a[1][2]=1;

step--;

last--;

Play(6);

step++;

}

});

bt7.setOnMousePressed(e->{

if(step>0&&a[2][0]==0&&last>0){

a[2][0]=1;

step--;

last--;

Play(7);

step++;

}

});

bt8.setOnMousePressed(e->{

if(step>0&&a[2][1]==0&&last>0){

a[2][1]=1;

step--;

last--;

Play(8);

step++;

}

});

bt9.setOnMousePressed(e->{

if(step>0&&a[2][2]==0&&last>0){

a[2][2]=1;

step--;

last--;

Play(9);

step++;

}

});

};

Timeline animation=new Timeline(

new KeyFrame(Duration.millis(100),eventHandler)

);

animation.setCycleCount(Timeline.INDEFINITE);

animation.play();

primaryStage.setScene(scene);

primaryStage.setTitle("井字棋");

primaryStage.show();

}

public String Switch(int i){

String s="";

switch(i){

case 0:

s="  ";

break;

case -1:

s="O";

break;

case 1:

s="X";

}return s;

}

public static void main(String[] args) {

Application.launch(args);

}

public void Reset(){

}

public void Play(int i){

if(last==4)

if(i!=5)

a[1][1]=-1;

else

a[0][0]=-1;

else{

if(a[0][0]!=1&&a[0][1]!=1&&a[0][2]!=1&&a[0][0]+a[0][1]+a[0][2]<-1){

for(int j=0;j<3;j++)

if(a[0][j]==0)

a[0][j]=-1;

win=true;wincount++;

}

else if(a[1][0]!=1&&a[1][1]!=1&&a[1][2]!=1&&a[1][0]+a[1][1]+a[1][2]<-1){

for(int j=0;j<3;j++)

if(a[1][j]==0)

a[1][j]=-1;

win=true;wincount++;

}

else if(a[2][0]!=1&&a[2][1]!=1&&a[2][2]!=1&&a[2][0]+a[2][1]+a[2][2]<-1){

for(int j=0;j<3;j++)

if(a[2][j]==0)

a[2][j]=-1;

win=true;wincount++;

}

else if(a[0][0]!=1&&a[1][0]!=1&&a[2][0]!=1&&a[0][0]+a[1][0]+a[2][0]<-1){

for(int j=0;j<3;j++)

if(a[j][0]==0)

a[j][0]=-1;

win=true;wincount++;

}

else if(a[0][1]!=1&&a[1][1]!=1&&a[2][1]!=1&&a[0][1]+a[1][1]+a[2][1]<-1){

for(int j=0;j<3;j++)

if(a[j][1]==0)

a[j][1]=-1;

win=true;wincount++;

}

else if(a[0][2]!=1&&a[1][2]!=1&&a[2][2]!=1&&a[0][2]+a[1][2]+a[2][2]<-1){

for(int j=0;j<3;j++)

if(a[j][2]==0)

a[j][2]=-1;

win=true;wincount++;

}else if(a[0][0]!=1&&a[1][1]!=1&&a[2][2]!=1&&a[0][0]+a[1][1]+a[2][2]<-1){

for(int j=0;j<3;j++)

if(a[j][j]==0)

a[j][j]=-1;

win=true;wincount++;

}

else if(a[2][0]!=1&&a[1][1]!=1&&a[0][2]!=1&&a[2][0]+a[1][1]+a[0][2]<-1){

for(int j=0;j<3;j++)

if(a[j][2-j]==0)

a[j][2-j]=-1;

win=true;wincount++;

}

else if(a[0][0]!=-1&&a[0][1]!=-1&&a[0][2]!=-1&&a[0][0]+a[0][1]+a[0][2]>1){

for(int j=0;j<3;j++)

if(a[0][j]==0)

a[0][j]=-1;

}

else if(a[1][0]!=-1&&a[1][1]!=-1&&a[1][2]!=-1&&a[1][0]+a[1][1]+a[1][2]>1){

for(int j=0;j<3;j++)

if(a[1][j]==0)

a[1][j]=-1;

}

else if(a[2][0]!=-1&&a[2][1]!=-1&&a[2][2]!=-1&&a[2][0]+a[2][1]+a[2][2]>1){

for(int j=0;j<3;j++)

if(a[2][j]==0)

a[2][j]=-1;

}

else if(a[0][0]!=-1&&a[1][0]!=-1&&a[2][0]!=-1&&a[0][0]+a[1][0]+a[2][0]>1){

for(int j=0;j<3;j++)

if(a[j][0]==0)

a[j][0]=-1;

}

else if(a[0][1]!=-1&&a[1][1]!=-1&&a[2][1]!=-1&&a[0][1]+a[1][1]+a[2][1]>1){

for(int j=0;j<3;j++)

if(a[j][1]==0)

a[j][1]=-1;

}

else if(a[0][2]!=-1&&a[1][2]!=-1&&a[2][2]!=-1&&a[0][2]+a[1][2]+a[2][2]>1){

for(int j=0;j<3;j++)

if(a[j][2]==0)

a[j][2]=-1;

}else if(a[0][0]!=-1&&a[1][1]!=-1&&a[2][2]!=-1&&a[0][0]+a[1][1]+a[2][2]>1){

for(int j=0;j<3;j++)

if(a[j][j]==0)

a[j][j]=-1;

}

else if(a[2][0]!=-1&&a[1][1]!=-1&&a[0][2]!=-1&&a[2][0]+a[1][1]+a[0][2]>1){

for(int j=0;j<3;j++)

if(a[j][2-j]==0)

a[j][2-j]=-1;

}else{

if(a[0][0]==0)

a[0][0]=-1;

else if(a[2][0]==0)

a[2][0]=-1;

else if(a[0][2]==0)

a[0][2]=-1;

else if(a[2][2]==0)

a[2][2]=-1;

else if(a[0][1]==0)

a[0][1]=-1;

else if(a[1][0]==0)

a[1][0]=-1;

else if(a[1][2]==0)

a[1][2]=-1;

else

a[2][1]=-1;

}

}

}

}

下面是图形界面:

JavaFX 井字棋游戏的更多相关文章

  1. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  2. 井字棋游戏升级版 - TopTicTacToe项目 简介

    一.游戏简介 井字棋是一款世界闻名的游戏,不用我说,你一定知道它的游戏规则. 这款游戏简单易学,玩起来很有意思,不过已经证明出这款游戏如果两个玩家都足够聪明的话, 是很容易无法分出胜负的,即我们得到的 ...

  3. C++井字棋游戏,DOS界面版

    据说有一个能保证不败的算法.明天看看先再写个PVC版的. 正题.今天无聊写了个井字棋游戏,顺便逐渐让自己习惯良好的代码风格,放上来给新手学习学习. jzq2.cpp /* N字棋游戏PVP版,DOS版 ...

  4. Java井字棋游戏

    试着写了一个井字棋游戏,希望各位能给予一些宝贵的建议. 一.棋盘类 package 井字棋游戏; public class ChessBoard { private int number; Perso ...

  5. [C++] 井字棋游戏源码

    TicTac.h #define EX 1 //该点左鼠标 #define OH 2 //该点右鼠标 class CMyApp : public CWinApp { public: virtual B ...

  6. [LeetCode] 348. Design Tic-Tac-Toe 设计井字棋游戏

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

  7. [LeetCode] Design Tic-Tac-Toe 设计井字棋游戏

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

  8. Raptor井字棋游戏

    作为大学第一个小作品,记录一下,也给那些想接触到Raptor游戏的人一个小小的参考QAQ至于Raptor的语法和使用,可以参考一下他的帮助手册,看不懂英文的话可以复制放到翻译上看. 以上是主函数 以下 ...

  9. [Swift]LeetCode348. 设计井字棋游戏 $ Design Tic-Tac-Toe

    Design a Tic-tac-toe game that is played between two players on a n x n grid. You may assume the fol ...

随机推荐

  1. Chrome崩溃的解决办法

    前两天Win10 更新的安全组件,第二天上班来就打开不了Chrome了,打开就是:噢哟,崩溃了! 那是连 setting 页都打不开的啊...好晕,好晕. 我是真的有点崩溃啊,在网上找了好久,什么与百 ...

  2. vue+element table的弹窗组件

    在处理表格编辑相关的需求,是需要做一个弹框进行保存的:或者查看表格数据的详细信息时,也是需要做弹窗: 当然 ,这是类似于这样的 ,当然 element 已经帮我们做好 弹窗这一块 主要 我想记录的是 ...

  3. BeanPostProcessor

    BeanPostProcessor简介 BeanPostProcessor是Spring IOC容器给我们提供的一个扩展接口.接口声明如下: public interface BeanPostProc ...

  4. 关于“100g文件全是数组,取最大的100个数”解决方法汇总

    原题如下: 有一个100G大小的文件里存的全是数字,并且每个数字见用逗号隔开.现在在这一大堆数字中找出100个最大的数出来. 我认为,首先要摸清考官的意图.是想问你os方面的知识,还是算法,或者数据结 ...

  5. MVC发布出现:未能将文件bin\xxx.xml 复制到 obj\Release\PackageTmp\bin\xxx.xml,未能找到文件

    之前写的项目好好的,也可以发布,然后今天要发布MVC项目,一直报错,报下面这个错误 莫名其妙搞了好久,没搜到合理的解决方案,结果就只能瞎搞了. 突然想起了,我前几天犯贱把项目根目录下的bin文件夹和o ...

  6. 一文让你彻底理解准确率,精准率,召回率,真正率,假正率,ROC/AUC

    参考资料:https://zhuanlan.zhihu.com/p/46714763 ROC/AUC作为机器学习的评估指标非常重要,也是面试中经常出现的问题(80%都会问到).其实,理解它并不是非常难 ...

  7. SQLServer --------- 将sql脚本文件导入数据库

    创建数据库方法有两种 第一种通过图形化的操作界面 第二种通过 sql 语句 sql server 如何执行.sql 文件,的原理就是执行sql语句进行创建 打开数据库后找到   最左侧文件 找到需要执 ...

  8. 043 用户注册功能03--Redis安装及完成短信发送功能

    1.Redis安装 (1)下载地址:https://github.com/MicrosoftArchive/redis/releases/tag/win-3.2.100   ( redis官网: ht ...

  9. mysql—增删改查

    MySQL数据库,每条命令后要加:号.不然会认为命令语句未输入完, 若在语句结尾不添加分号时, 命令提示符会以 -> 提示你继续输入(有个别特例, 但加分号是一定不会错的); show data ...

  10. 安装软件时出现这样错误:文件“proe50-1a.bin”无法在“C:\User\ZFTL\Desktop\proe50”定位,请插入正确的磁盘或选择其他文件夹

    把里面的文件改成proe50-1a.bin就可以了.