📄 heibaiqi.java
字号:
import javax.swing.*;
import java.awt.event.*;
import java.net.URL;
import java.awt.*;
import java.util.*;
import javax.swing.filechooser.FileFilter;
import java.io.*;
public class HeiBaiQi
{
public static void main(String[] args)
{
HeiBaiQiFrame xx=new HeiBaiQiFrame(8);
xx.setSize(400,450);
xx.setResizable(false);
xx.setVisible(true);
xx.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class FileName extends FileFilter implements Serializable
{
public boolean accept(File f)
{
return f.getName().toLowerCase().endsWith(".hbq")||f.isDirectory();
}
public String getDescription()
{
return "HBQ File";
}
}
class HeiBaiQiFrame extends JFrame implements Serializable
{
Object[] options={"你持黑先下","电脑持黑先下"};
RunThread one;
Thread athread;
ArrayList<HeiBaiButton> blackarray,whitearray;
HeiBaiButton[][] thebutton=null;
Caculate caculate=null;
JPanel bottomPanel;
JPanel testPanel;
public static int m=8;
private static boolean AI=false;
private static boolean flag;
JMenuBar bar;
JMenu game,about;
JMenuItem ai,people,save,load;
JLabel text;
javax.swing.Timer timeshow;
JFileChooser chooser;
ReadAndWrite readandwrite=null;
public HeiBaiQiFrame(int m)
{
this.m=m;
blackarray=new ArrayList<HeiBaiButton>();
whitearray=new ArrayList<HeiBaiButton>();
thebutton=new HeiBaiButton[m+1][m+1];
caculate=new Caculate(thebutton,blackarray,whitearray,m);
chooser=new JFileChooser();
bar=new JMenuBar();
game=new JMenu("游戏");
ai=new JMenuItem("人机对战");
people=new JMenuItem("双人对战");
save=new JMenuItem("保存");
save.addActionListener(new FileSaveListener());
load =new JMenuItem("载入");
load.addActionListener(new FileOpenListener());
game.add(ai);
game.add(people);
game.add(save);
game.add(load);
bar.add(game);
setJMenuBar(bar);
bottomPanel=new JPanel();
text=new JLabel();
bottomPanel.add(text);
class AIListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
blackarray.clear();
whitearray.clear();
if(one!=null)
{
one.time1=0;
one.time2=0;
}
one.AI=true;
one.sequence=true;
int select=JOptionPane.showOptionDialog(null,"开始方式","黑白棋",
JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE,null,
options,options[1]);
if(select!=-1)
{
one.flag=(select!=0);
init();
one.AIbegin();
}
else return;
}
};
class PeopleListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
blackarray.clear();
whitearray.clear();
if(one!=null)
{
one.time1=0;
one.time2=0;
}
one.AI=false;
one.flag=true;
init();
one.begin();
}
};
ai.addActionListener(new AIListener());
people.addActionListener(new PeopleListener());
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
thebutton[i][j]=new HeiBaiButton(thebutton,blackarray,whitearray,(RunThread)one,caculate,m);
thebutton[i][j].setPose(i,j);
}
testPanel=new JPanel(new GridLayout(m,m));
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
testPanel.add(thebutton[i][j]);
}
for(int i=1;i<=m;i++) //标记清空
for(int j=1;j<=m;j++)
thebutton[i][j].setState(HeiBaiButton.blank);
add(testPanel,BorderLayout.CENTER);
add(bottomPanel,BorderLayout.SOUTH);
one=new RunThread(whitearray,blackarray,thebutton,caculate,m,flag,AI,text);
athread=new Thread(one);
athread.start();
}
class ReadAndWrite implements Serializable
{
HeiBaiButton[][] thebutton;
ArrayList<HeiBaiButton> blackarray,whitearray;
int time1;
int time2;
boolean flag;
boolean AI;
public ReadAndWrite(HeiBaiButton[][] thebutton,ArrayList<HeiBaiButton> blackarray,ArrayList<HeiBaiButton> whitearray,int time1,int time2,boolean flag,boolean AI)
{
this.thebutton=thebutton;
this.blackarray=blackarray;
this.whitearray=whitearray;
this.time1=time1;
this.time2=time2;
this.flag=flag;
this.AI=AI;
}
}
private class FileSaveListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
readandwrite=new ReadAndWrite(thebutton,whitearray,blackarray,one.time1,one.time2,flag,AI);
chooser.setCurrentDirectory(new File("."));
chooser.addChoosableFileFilter(new FileName());
chooser.setSelectedFile(new File(new String("default.hbq")));
int result=chooser.showSaveDialog(HeiBaiQiFrame.this);
if(result==JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
saveFile(name);
}
}
}
private class FileOpenListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
chooser.setCurrentDirectory(new File("."));
chooser.addChoosableFileFilter(new FileName());
int result=chooser.showOpenDialog(HeiBaiQiFrame.this);
if(result==JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getPath();
openFile(name);
}
}
}
public void init()
{
for(int i=1;i<=m;i++)
for(int j=1;j<=m;j++)
{
thebutton[i][j].one=one;
}
if(flag)
{
text.setText("白方用时");
text.setEnabled(false);
}
else
{
text.setText("黑方用时");
text.setEnabled(false);
}
for(int i=1;i<=m;i++) //标记清空
for(int j=1;j<=m;j++)
{
thebutton[i][j].clearList();
thebutton[i][j].setState(HeiBaiButton.blank);
}
thebutton[m/2][m/2].setState(HeiBaiButton.black);
thebutton[m/2][m/2+1].setState(HeiBaiButton.white);
thebutton[m/2+1][m/2].setState(HeiBaiButton.white);
thebutton[m/2+1][m/2+1].setState(HeiBaiButton.black);
blackarray.add(thebutton[m/2][m/2]); //初始,加入白棋和黑棋
blackarray.add(thebutton[m/2+1][m/2+1]);
whitearray.add(thebutton[m/2][m/2+1]);
whitearray.add(thebutton[m/2+1][m/2]);
/*
//System.out.println(caculate);
//System.out.println(one);
//System.out.println(thebutton);
//System.out.println(caculate.thebutton);
//System.out.println(one.caculate);
//System.out.println(caculate.blackarray);
*/
//new Thread(one).start();
//one.begin();
/*
for(HeiBaiButton abutton:blackarray) //黑方棋子重新显示
{
abutton.setState(HeiBaiButton.black);
}
for(HeiBaiButton abutton:whitearray) //白方棋子重新显示
{
abutton.setState(HeiBaiButton.white);
}
*/
}
public void openFile(String filename)
{
try
{
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File(filename)));
readandwrite=(ReadAndWrite)in.readObject();
thebutton=readandwrite.thebutton;
blackarray=readandwrite.blackarray;
whitearray=readandwrite.whitearray;
one.time1=readandwrite.time1;
one.time2=readandwrite.time2;
flag=readandwrite.flag;
AI=readandwrite.AI;
one.begin();
}
catch(Exception ex)
{
return;
}
}
public void saveFile(String filename)
{
try
{
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(new File(filename)));
out.writeObject(readandwrite);
}
catch(Exception ex)
{
return;
}
}
}
class HeiBaiButton extends JButton implements ActionListener
{
HeiBaiButton[][] thebutton;
RunThread one;
Caculate caculate;
int m;
public int i,j,state;
ArrayList<HeiBaiButton> arraylist,blackarray,whitearray;
public static final int white=1;
public static final int black=2;
public static final int whiting=3;
public static final int blacking=4;
public static final int blank=5;
public static ImageIcon image1=null;
public static ImageIcon image2=null;
public static ImageIcon image3=null;
public static ImageIcon image4=null;
public static ImageIcon image5=null;
public HeiBaiButton(HeiBaiButton[][] thebutton,ArrayList<HeiBaiButton> blackarray,ArrayList<HeiBaiButton> whitearray,RunThread one,Caculate caculate,int m)
{
this.thebutton=thebutton;
this.blackarray=blackarray;
this.whitearray=whitearray;
this.one=one;
this.caculate=caculate;
this.m=m;
arraylist=new ArrayList<HeiBaiButton>();
addActionListener(this);
image1=new ImageIcon(getClass().getResource("1.jpg"));
image2=new ImageIcon(getClass().getResource("2.jpg"));
image3=new ImageIcon(getClass().getResource("3.jpg"));
image4=new ImageIcon(getClass().getResource("4.jpg"));
image5=new ImageIcon(getClass().getResource("5.jpg"));
setState(HeiBaiButton.blank);
}
public void actionPerformed(ActionEvent e)
{
if(((HeiBaiButton)e.getSource()).state()==HeiBaiButton.blacking)
{
HeiBaiButton temp=(HeiBaiButton)e.getSource();
caculate.changeToBlack(temp,whitearray,blackarray);
for(int i=1;i<=m;i++) //标记清空
for(int j=1;j<=m;j++)
{
thebutton[i][j].clearList();
thebutton[i][j].setState(HeiBaiButton.blank);
}
for(HeiBaiButton abutton:blackarray) //黑方棋子重新显示
{
abutton.setState(HeiBaiButton.black);
}
for(HeiBaiButton abutton:whitearray) //白方棋子重新显示
{
abutton.setState(HeiBaiButton.white);
}
//System.out.println("black opponent using time:"+one.time1+"minute");
one.begin();
}
if(((HeiBaiButton)e.getSource()).state()==HeiBaiButton.whiting)
{
HeiBaiButton temp=(HeiBaiButton)e.getSource();
caculate.changeToWhite(temp,whitearray,blackarray);
for(int i=1;i<=m;i++) //标记清空
for(int j=1;j<=m;j++)
{
thebutton[i][j].clearList();
thebutton[i][j].setState(HeiBaiButton.blank);
}
for(HeiBaiButton abutton:whitearray) //白方棋子重新显示
{
abutton.setState(HeiBaiButton.white);
}
for(HeiBaiButton abutton:blackarray) //黑方棋子重新显示
{
abutton.setState(HeiBaiButton.black);
}
//System.out.println("white opponent using time:"+one.time2+"minute");
one.begin();
}
if(((HeiBaiButton)e.getSource()).state()==HeiBaiButton.blank)
{
}
}
public void setPose(int i,int j)
{
this.i=i;
this.j=j;
}
public int state()
{
return state;
}
public void setState(int i)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -