利用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
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;
}
}
}
}
下面是图形界面:
/* * 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