📄 gobang.java
字号:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.sound.midi.*;
import java.io.*;
import java.net.*;
import sun.audio.*;
public class Gobang extends JFrame implements ActionListener
{
static int table[][] = new int[15][15]; //棋盘 数组的每一个元素对应棋盘上的一个交叉点,
//用'0'表示空位、'1'代表玩家的子、'2'代表电脑的子
static double [][][]player=new double[15][15][4]; //玩家棋型表
static double [][][]computer=new double[15][15][4]; //电脑棋型表
static boolean b = false; //值为真时代表该玩家走,为假时该电脑走
static boolean priority=false; //优先权 选择谁先下 true为玩家先 false为电脑先
static int numberOfChess=0; //已经下的棋子数
static int final_x=0,final_y=0; //最终放置的位置
int last_x1 = 0,last_y1 = 0; //最后一次放置的位置
int last_x2 = 0,last_y2 = 0; //倒数第二次放置的位置
static int countJushu=0; //统计局数
static Sequencer sequencer; //与音乐播放有关
AudioStream as; //与音效播放有关
boolean openSound = true; //为真时打开音效,为假时关闭音效
boolean fights = false; //为真时表示认输
DrawTable chessboard = new DrawTable(this); //画棋盘
TimeLabel timeLabel=new TimeLabel(this);
JTextArea jtfTime; //分别用于显示时间标签
JButton jbtRetry,jbtStart,jbtFights;//悔棋、开始和认输按钮
ImageIcon imgComputer = new ImageIcon("zuozhu.jpg");
ImageIcon imgPlayer = new ImageIcon("xiaoying.jpg");
ImageIcon imgWhite = new ImageIcon("white.jpg");
ImageIcon imgBlack = new ImageIcon("black.jpg");
ImageIcon img1 = new ImageIcon("海.jpg");
ImageIcon img2= new ImageIcon("草原.jpg");
ImageIcon img3 = new ImageIcon("天文.jpg");
JLabel label=new JLabel();
JLabel jlbTitle = new JLabel("五子棋");
JLabel jlbComputer,jlbPlayer;
public Gobang()
{
//数据结构初始化
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
{
table[i][j] = 0;
}
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
for(int k=0;k<4;k++)
{
player[i][j][k]=0;
computer[i][j][k]=0;
}
label.setIcon(img1);
//设置菜单
JMenuBar menu=new JMenuBar();
JMenu game=new JMenu("游戏");
JMenuItem startGame=new JMenuItem("开始",'P');
JMenuItem exit=new JMenuItem("退出",'E');
game.add(startGame);
game.add(exit);
game.setLocation(5,10);
game.setSize(15,10);
JMenu help=new JMenu("帮助");
JMenuItem introduce=new JMenuItem("版本信息");
JMenuItem playHelp=new JMenuItem("使用帮助",'U');
help.add(introduce);
help.add(playHelp);
help.setLocation(30,10);
help.setSize(15,10);
JMenu function=new JMenu("功能");
JMenu music= new JMenu("背景音乐");
JMenu soundEffects=new JMenu ("音效");
JMenu backgroundPicture=new JMenu("背景图片");
function.add(music);
function.add(soundEffects);
function.addSeparator(); //分隔符
function.add(backgroundPicture);
JMenuItem openMusic=new JMenuItem("打开音乐");
JMenuItem closeMusic=new JMenuItem("关闭音乐");
JMenuItem openSound=new JMenuItem("打开音效");
JMenuItem closeSound=new JMenuItem("关闭音效");
JMenuItem p1=new JMenuItem("海");
JMenuItem p2=new JMenuItem("草原");
JMenuItem p3=new JMenuItem("天文");
menu.add(game);
menu.add(function);
menu.add(help);
music.add(openMusic);
music.add(closeMusic);
soundEffects.add(openSound);
soundEffects.add(closeSound);
backgroundPicture.add(p1);
backgroundPicture.add(p2);
backgroundPicture.add(p3);
getContentPane().add(menu,BorderLayout.NORTH);
//设置小图片和时间显示
jlbComputer = new JLabel(imgComputer);
jlbComputer.setLocation(20,100);
jlbComputer.setSize(70,120);
jlbPlayer = new JLabel(imgPlayer);
jlbPlayer.setLocation(430,100);
jlbPlayer.setSize(70,120);
jtfTime = new JTextArea();
jtfTime.setLocation(470,300);
jtfTime.setSize(50,70);
jtfTime.setForeground(Color.green);
jtfTime.setBackground(Color.black);
jtfTime.setText("总用时:\n3:00\n倒计时:\n3:00");
getContentPane().add(jlbComputer);
getContentPane().add(jlbPlayer);
getContentPane().add(jtfTime);
//设置并添加棋盘
chessboard.setLocation(110,30);
chessboard.setSize(300,300);
getContentPane().add(chessboard);
//添加功能按钮
jbtRetry = new JButton("悔棋");
jbtRetry.setLocation(95,350);
jbtRetry.setSize(70,40);
jbtStart = new JButton("开始");
jbtStart.setLocation(225,350);
jbtStart.setSize(70,40);
jbtFights = new JButton("认输");
jbtFights.setLocation(355,350);
jbtFights.setSize(70,40);
getContentPane().add(jbtRetry);
getContentPane().add(jbtStart);
getContentPane().add(jbtFights);
//设置事件监听器
startGame.addActionListener(this); //"游戏"各项的事件监听
exit.addActionListener(this);
introduce.addActionListener(this); //"帮助"各项的事件监听
playHelp.addActionListener(this);
//"功能"各项的事件监听
openMusic.addActionListener(this); //背景音乐
closeMusic.addActionListener(this);
openSound.addActionListener(this); //音效
closeSound.addActionListener(this);
p1.addActionListener(this); //背景图片
p2.addActionListener(this);
p3.addActionListener(this);
jbtRetry.addActionListener(this); //功能按钮监听事件
jbtStart.addActionListener(this);
jbtFights.addActionListener(this);
getContentPane().add(label);
//设置窗体
this.setTitle("五子棋");
this.setSize(550,430);
this.setLocation(220,150);
this.setResizable(false); //固定窗体大小
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbtRetry)
{
Graphics g = chessboard.getGraphics();
g.drawImage(null,20*last_x1+5,20*last_y1+5,null);
g.drawImage(null,20*last_x2+5,20*last_y2+5,null);
table[last_x1][last_y1] = 0;
table[last_x2][last_y2] = 0;
repaint();
}
else if(e.getSource() == jbtStart)
{
countJushu++;
if(countJushu%2==0)
priority=true;
else
priority=false;
if(priority==true)
JOptionPane.showMessageDialog(null," 这次您先走"
,"系统提示",JOptionPane.INFORMATION_MESSAGE);
numberOfChess=0; //初始化落子数量
final_x=0; //初始化落子位置
final_y=0;
timeLabel.stop();
timeLabel = new TimeLabel(this);
timeLabel.start();
clear();
repaint();
fights = false;
action();
}
else if(e.getSource() == jbtFights)
{
JOptionPane.showMessageDialog(null," 黑棋认输,白旗胜","提示信息",
JOptionPane.INFORMATION_MESSAGE);
int judge=JOptionPane.showConfirmDialog(null, "胆小鬼,居然认输,还下不了? ","强烈鄙视",
JOptionPane.YES_NO_OPTION);
if(judge==0) //判断选了yes 还是 no
;
else{
JOptionPane.showMessageDialog(null," 游戏将退出!","系统提示",
JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
timeLabel.stopped = true;
fights = true;
return;
}
Object object=e.getSource();
String s=e.getActionCommand();
if(object instanceof JMenuItem)
{
if("开始".equals(s))
{
countJushu++;
if(countJushu%2==0)
priority=true;
else
priority=false;
if(priority==true)
JOptionPane.showMessageDialog(null," 这次您先走"
,"系统提示",JOptionPane.INFORMATION_MESSAGE);
numberOfChess=0; //初始化落子数量
final_x=0; //初始化落子位置
final_y=0;
timeLabel.stop();
timeLabel = new TimeLabel(this);
timeLabel.start();
clear();
repaint();
fights = false;
action();
}
else if("退出".equals(s))
{
System.exit(0);
}
else if("版本信息".equals(s))
{
String s1="一.软件名称及版本号:五子棋游戏1.0版。\n二. 开发人员:王峰,李杰。\n"+
"三. 开发时间:2007.3.25--2007.4.9";
JOptionPane.showMessageDialog(null,s1,
"游戏说明",JOptionPane.INFORMATION_MESSAGE);
}
else if("使用帮助".equals(s))
{
String s2="1.人机对战。第一局电脑先,以后各局玩家与电脑依次先\n"+
"2.点击界面内的<开始>或<游戏>菜单中的<开始>可以开始新游戏\n"+
"3.可以通过<功能>菜单开闭背景音乐、音效以及更换背景图片\n"+
"4.点击界面中的<悔棋>或<认输>玩家可以实现相应功能\n"+
"5.注意时间控制,最长为3分钟,时间到为和棋";
JOptionPane.showMessageDialog(null,s2,
"帮助",JOptionPane.INFORMATION_MESSAGE);
}
else if("打开音乐".equals(s))
musicPlay(); //调用背景音乐播放函数,打开音乐
else if("关闭音乐".equals(s))
sequencer.close(); //关闭音乐
else if("打开音效".equals(s))
{
openSound = true;
}
else if("关闭音效".equals(s))
{
openSound = false;
AudioPlayer.player.stop(as);
}
else if("海".equals(s)){
label.setIcon(img1);
}
else if("草原".equals(s)){
label.setIcon(img2);
}
else if("天文".equals(s)){
label.setIcon(img3);;
}
}
}
public void musicPlay(){ //背景音乐播放
try
{
sequencer = MidiSystem.getSequencer();
sequencer.open();
File myMusic = new File("moonlight_in_town.mid");
Sequence mySequence = MidiSystem.getSequence(myMusic);
sequencer.setSequence(mySequence);
sequencer.start();
}
catch(Exception me)
{
me.printStackTrace();
}
}
public void soundPlay(){ //音效播放
if(!openSound)
{
return;
}
try {
FileInputStream fileau = new FileInputStream("sound.wav");
as = new AudioStream(fileau);
AudioPlayer.player.start(as);
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
public void action() //下棋动作
{
if(!priority)
{
computerAction();
priority = true;
}
else
{
priority = false;
}
chessboard.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(b)
{
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
{
if(Math.abs(e.getX()-(20*i+10))<=3 && Math.abs(e.getY()-(20*j+10))<=3 && table[i][j] == 0)
{
if(isWin()){
return;
}
last_x2 = last_x1;
last_y2 = last_y1;
last_x1 = i;
last_y1 = j;
table[i][j] = 1;
repaint();
soundPlay();
b = false;
if(isWin()){
return;
}
computerAction();
break;
}
}
}
}
});
numberOfChess++;
}
//电脑下棋
public void computerAction()
{
int x=0,y=0;
int heng,shu,pie,na;
if(numberOfChess==0){ //设置电脑的第一个子的位置
while(true){
if((final_x<6||final_x>9)||final_y<6||final_y>9){
final_x=(int)(Math.random()*15);
final_y=(int)(Math.random()*15);
}
else
break;
}
}
else
forecast(); //预测
x=final_x;y=final_y;
last_x2 = last_x1;
last_y2 = last_y1;
last_x1 = x;
last_y1 = y;
table[x][y] = 2;
numberOfChess++;
b = true;
repaint();
soundPlay(); //播放音效
isWin();
}
public void clear() //清零
{
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
{
Graphics g = chessboard.getGraphics();
g.drawImage(null,20*i+5,20*j+5,null);
table[i][j]=0;
}
for(int i=0;i<15;i++)
for(int j=0;j<15;j++)
for(int k=0;k<4;k++)
{
player[i][j][k]=0;
computer[i][j][k]=0;
}
b = true;
}
public boolean isWin() //判断是否胜利
{
if(fights)
{
JOptionPane.showMessageDialog(null," 胜负已分,请重新开始","系统提示",
JOptionPane.WARNING_MESSAGE);
return true;
}
for(int i = 0; i < 15; i++)
for(int j = 0; j < 15; j++)
{
player[i][j][0]=Transverse(i,j,1);
player[i][j][1]=Longitudinal(i,j,1);
player[i][j][2]=Anti_diagonal(i,j,1);
player[i][j][3]=diagonal(i,j,1);
computer[i][j][0]=Transverse(i,j,2);
computer[i][j][1]=Longitudinal(i,j,2);
computer[i][j][2]=Anti_diagonal(i,j,2);
computer[i][j][3]=diagonal(i,j,2);
if(player[i][j][0]>4 || player[i][j][1]>4 || player[i][j][2]>4 || player[i][j][3]>4)
{
JOptionPane.showMessageDialog(null," 恭喜你,你赢了!","系统提示",
JOptionPane.INFORMATION_MESSAGE);
timeLabel.stopped = true;
int judge=JOptionPane.showConfirmDialog(null, "你挺强啊,敢不敢再来一局!","我不服",
JOptionPane.YES_NO_OPTION );
if(judge==0) //判断选了yes 还是 no
;
else{
JOptionPane.showMessageDialog(null," 游戏将退出!","系统提示",
JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
return true;
}
else if(computer[i][j][0]>4||computer[i][j][1]>4||computer[i][j][2]>4
||computer[i][j][3]>4)
{
JOptionPane.showMessageDialog(null," 电脑获胜! ","系统提示",
JOptionPane.INFORMATION_MESSAGE);
timeLabel.stopped = true;
int judge=JOptionPane.showConfirmDialog(null, "臭棋篓子,服不服? 不服再来!","强烈鄙视",
JOptionPane.YES_NO_OPTION);
if(judge==0) //判断选了yes 还是 no
;
else{
JOptionPane.showMessageDialog(null," 游戏将退出!","系统提示",
JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
return true;
}
}
return false;
}
static void forecast(){ //电脑进行预测
double maxOfAll=0.0;
double maxOfCom=0.0; //所有点的最大computer值
double maxOfPla=0.0; //所有点的最大player值
int i=0,j=0,k;
for(i=0;i<15;i++)
for(j=0;j<15;j++)
if(table[i][j]==0)
table[i][j]=4;
int position[][]={{0,0},{0,0}}; //预测电脑和玩家时分别放置的位置
V_fillInBlank(true);
for(i=0;i<15;i++)
for(j=0;j<15;j++)
for(k=0;k<4;k++){
if(computer[i][j][k]>=maxOfCom&&table[i][j]==4)
maxOfCom=computer[i][j][k];
}
for(i=0;i<15;i++)
for(j=0;j<15;j++)
for(k=0;k<4;k++){
if(maxOfCom==computer[i][j][k]&&table[i][j]==4){
position[0][0]=i;
position[0][1]=j;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -