java——博弈算法实现井字棋游戏
通过java语⾔开发了⼀个简单的井字棋游戏。主要有6个类,其中有⼀个是主类(Main.java),⼀个是抽象类(PiecesMove.java)组成。
下⾯对各个类简单介绍⼀下:
TicTicToe.java:
主要负责创建棋盘,管理棋盘。
TicTicToeUI.java:
主要由判断谁是先⼿的对话框、对战界⾯和提⽰谁输谁赢三个对话框组成,负责管理⽤户交互。
PiecesMove.java:
⾥⾯有2个⽅法,move(TicTacToe tict)是抽象⽅法由⼦类实现,isWinning(int[][] cur)判断是否游戏结束。
Computer.java:
负责计算机的落⼦位置计算,核⼼算法为博弈算法。
Player.java:
负责获取⼈落⼦的位置。
Main.java:
负责创建以上类的实例,控制谁先⾛⼦,游戏结束是否继续等逻辑处理。
下⾯贴出源代码:
TicTicToe.java:
/*
* Created by shphuang on 2020/4/4.
* */
import javax.swing.*;
import java.awt.*;
public class TicTacToe {
public static int num =0;//计数⾛的步数
/
/存储棋盘矩阵值
private int[][] chessBd =new int[3][3];
//按钮矩阵
private JButton[][] buttons;
//判断该谁⾛
public boolean PLAYER_MOVE =true;
//实例化Tictactoe
private static TicTacToe tict = null;
public static TicTacToe instanceTict(JButton[][] buttons){ if(tict == null){
tict =new TicTacToe(buttons);
}
return tict;
}
//初始化棋盘
private TicTacToe(JButton[][] buttons){
this.buttons = buttons;
clearBoard();
}
//设置棋盘的值
public void setChessBd(int x ,int y ,int checkBd){
this.chessBd[x][y]= checkBd;
}
/
/得到棋盘
public int[][]getChessBd(){
return chessBd;
}
//得到按钮矩阵
public JButton[][]getButtons(){
return buttons;
}
//清空棋盘
public void clearBoard(){
for(int i=0; i <3; i++){
for(int j=0; j <3; j++){
chessBd[i][j]=0;
}
}
num =0;
}
//将棋盘画出来
public void drawBoard(){
updateUI();
}
//更新ui
private boolean updateUI(){
for(int i =0; i < chessBd.length; i++){
for(int j =0; j < chessBd.length; j++){
switch(chessBd[i][j]){
case-1:
buttons[i][j].setBackground(Color.white);
break;
case0:
buttons[i][j].setBackground(Color.lightGray);
break;
case1:
buttons[i][j].setBackground(Color.black);
break;
}
}
}
}
return false;
}
}
TicTicToeUI.java:
/*
* Created by shphuang on 2020/4/4.
* */
import javax.swing.*;
import java.awt.*;
public class TicTacToeUI {
private JButton[][] buttons =new JButton[3][3];
private JFrame jf;
private JPanel jp;
//实例化TictactoeUI
private static TicTacToeUI tictactoeUI = null;
public static TicTacToeUI instanceTictactoeUI(){
if(tictactoeUI == null){
tictactoeUI =new TicTacToeUI();
}
return tictactoeUI;
}
private TicTacToeUI(){
init();
}
private void init(){
//创建并设置frame
jf =new JFrame();
jf.setTitle("designed by 爱编程的⼤鹏");//设置显⽰窗⼝标题
// 得到显⽰器屏幕的宽⾼
int screenWidth = DefaultToolkit().getScreenSize().width;
int screenHeight = DefaultToolkit().getScreenSize().height;
int width =470;int height =470;
int offset_x = screenWidth/2-width/2;
int offset_y = screenHeight/2-height/2;
jf.setBounds(offset_x,offset_y,width,height);//设置窗⼝显⽰尺⼨
Container c= jf.getContentPane();//获取当前窗⼝的内容窗格
//创建并设置panel
jp =new JPanel();
jp.setBackground(new Color(159,156,157,20));//设置背景⾊
jp.setLayout(new GridLayout(3,3,10,10));//设置布局
//初始化按钮矩阵
for(int i =0; i < buttons.length; i++){
for(int j =0; j < buttons.length; j++){
JButton button =new JButton();
buttons[i][j]= button;
button.setBackground(Color.lightGray);
jp.add(button);
}
}
c.add(jp);//将panel⾯板添加到frame
jf.setVisible(true);//设置窗⼝是否可见
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//置窗⼝是否可以关闭}//初始化TictactoeUI
}//初始化TictactoeUI
//得到JFrame对象
public JFrame getJf(){
return jf;
}
//得到JPanel对象
public JPanel getJp(){
return jp;
}
//返回buttons对象
public JButton[][]getButtons(){
return buttons;
}
public boolean DialogUI(String msg){
try{
Thread.sleep(200);
int temp = JOptionPane.showConfirmDialog(jp,msg,"提⽰",0,1);
if(temp ==0){
return true;
}
}catch(InterruptedException e){简单的java游戏代码
e.printStackTrace();
}
return false;
}
}
PiecesMove.java:
/*
* Created by shphuang on 2020/4/4.
* */
abstract class PiecesMove {
public abstract void move(TicTacToe tict);
//返回0表⽰没有⼈赢,返回-1表⽰⼈赢了,返回1表⽰计算机赢了,返回2表⽰平局
public static int isWinning(int[][] cur){
for(int i =0; i<3; i++)
{
if(cur[i][0]==1&& cur[i][1]==1&& cur[i][2]==1)
return1;
if(cur[i][0]==-1&& cur[i][1]==-1&& cur[i][2]==-1)
return-1;
}
for(int i =0; i<3; i++)
{
if(cur[0][i]==1&& cur[1][i]==1&& cur[2][i]==1)
return1;
if(cur[0][i]==-1&& cur[1][i]==-1&& cur[2][i]==-1)
return-1;
}
if((cur[0][0]==1&& cur[1][1]==1&& cur[2][2]==1)||(cur[2][0]==1&& cur[1][1]==1&& cur[0][2]==1))
return1;
if((cur[0][0]==-1&& cur[1][1]==-1&& cur[2][2]==-1)||(cur[2][0]==-1&& cur[1][1]==-1&& cur[0][2]==-1)) return-1;
if(TicTacToe.num >=9){
return2;
}
return0;
}
}
Computer.java:
/*
* Created by shphuang on 2020/4/4.
* */
public class Computer extends PiecesMove {
//电脑的棋⼦类型
private static final int COMPUTER =1;
private static final int depth =3;
//实例化Player
private static Computer computer = null;
public static Computer instanceComputer(){
if(computer == null){
computer =new Computer();
}
return computer;
}
@Override
public void move(TicTacToe tict){
tict.num +=1;//步数+1
int[][] curQP =ChessBd());
int[] xyVal =getXY(curQP);
ChessBd()[xyVal[0]][xyVal[1]]==0){
tict.setChessBd(xyVal[0], xyVal[1], COMPUTER);
tict.drawBoard();
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论