📄 fivezigames.java
字号:
/*本五子棋是在把棋盘设计好,让棋子都落在一系列整数点位置,同时跟踪每一个棋子在
在棋盘位子,用来判断输赢
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.*;
public class fivezigames {
public static void main(String args[])
{
MouseFrame frame =new MouseFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class MouseFrame extends JFrame
{
public MouseFrame()
{
setTitle("五子棋");
setSize(700,700);
//Toolkit tk=Toolkit.getDefaultToolkit();
//Image img=tk.getImae("11.gif");
//setIconImage(img);
//Button people_to_computer=new Button("人机对弈");
//Button people_to_people=new Button("人人对弈");
//add(people_to_computer);
//add(people_to_people);
GamePanell panel=new GamePanell();
Container contentPane=getContentPane();
contentPane.add(panel);
//contentPane.add(people_to_people);
}}
class GamePanell extends JPanel implements ActionListener
{ Button people_to_computer=new Button("人机对弈");
Button people_to_people=new Button("人人对弈");
public GamePanell()
{ setLayout(null);
add(people_to_computer);
people_to_computer.setBounds(600,600,50,20);
people_to_computer.addActionListener(this);
add(people_to_people);
people_to_people.setBounds(600,620,50,20);
people_to_people.addActionListener(this);
}
public void actionPerformed(ActionEvent d)
{
if(d.getSource()==people_to_people)//激发新游戏
{
GamePanel a=new GamePanel();
add(a);
}
}
}
class GamePanel extends JPanel implements MouseListener,ActionListener
{
private int oo=0;
private int tt=0;
private int bzslnum=0,hzslnum=0;
private int num1=0,num2=0;
private int currentx,currenty;//当前下子的坐标
private int blackx[]=new int[400];//黑方落子的横坐标数组
private int blacky[]=new int[400];//黑方落子的纵坐标数组
private int whitex[]=new int[400];//白方落子的横坐标数组
private int whitey[]=new int[400];//白方落子的纵坐标数组
private int location[][]=new int [20][20];//用来记录当前位置是否有点存在
Label dangqian=new Label("现在下棋者");
Label bifen=new Label("比分(白方:黑方)");
Label shengli=new Label("胜利方");
Label jushu=new Label("局数统计");
TextField dq=new TextField(30);//当前下棋者
TextField sl=new TextField(30);//胜利者
TextField js=new TextField(10);//总局数
TextField bf=new TextField(10);//比分
Button kaishi=new Button("新游戏");
Button tuichu=new Button("退出");
Button huiqi=new Button("悔棋");
private int w=0;//w=0时候是白方下棋,w=1是黑方下棋
public GamePanel()
{ setLayout(null);//继承了Component的方法,设置此容器的布局管理器
addMouseListener(this);//设置鼠标监视器
add(dangqian);
dangqian.setBounds(410,20,80,20);
add(bifen);
bifen.setBounds(410,40,90,20);
add(shengli);
shengli.setBounds(410,60,40,20);
add(jushu);
jushu.setBounds(410,80,60,20);
add(dq);
dq.setBounds(500,20,100,20);
dq.setEditable(false);
dq.setText("白方下(按左键)");
add(bf);
bf.setBounds(501,40,100,20);
bf.setText("0:0");//开始比分1:1
bf.setEditable(false);
add(sl);
sl.setBounds(501,60,100,20);
sl.setEditable(false);
add(js);
js.setBounds(501,80,100,20);
js.setText("1");
js.setEditable(false);
add(huiqi);
huiqi.setBounds(100,500,100,20);
huiqi.addActionListener(this);
add(kaishi);
kaishi.setBounds(200,500,100,20);
kaishi.addActionListener(this);
add(tuichu);
tuichu.setBounds(300,500,100,20);
tuichu.addActionListener(this);
}
public void paint(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.red);
//画出棋盘
for(int x=20;x<=400;x+=20)
{
g.drawLine(x,20,x,400);
}
for(int y=20;y<=400;y+=20)
{
g.drawLine(20,y,400,y);
}
for(int i=0;i<num1;i++)//画出白子
{
g.setColor(Color.WHITE);
g.fillOval(whitex[i]-6,whitey[i]-6,14,14);
}
for(int i=0;i<num2;i++) //画出黑子
{
g.setColor(Color.black);
g.fillOval(blackx[i]-6,blacky[i]-6,14,14);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==tuichu)
{
System.exit(0);}
if(e.getSource()==kaishi)
{
//重新开始时数组元素都清除
for(int i=0;i<400;i++)
{
whitex[i]=-6;
whitey[i]=-6;
blackx[i]=-6;
blacky[i]=-6;
}
//重新开始时棋盘对应的二维数组清零
for(int i=0;i<20;i++)
{
for(int j=0;j<20;j++)
{
location[i][j]=0;
}
}
//总局数加1
int n=Integer.parseInt(js.getText());
n++;
String s=String.valueOf(n);
js.setText(s);
//比分重新设置
String s1=String.valueOf(bzslnum),s2=String.valueOf(hzslnum);
bf.setText(s1+":"+s2);
w=0;//让白子先下
dq.setText("白棋下子(用左键)");
sl.setText("");
repaint();
}
//悔子
if(e.getSource()==huiqi&&w==1&&num1>0)//判断是白方悔子
{
w=0;
location[(whitex[num1-1]-20)/20][(whitey[num1-1]-20)/20]=0;//将刚下的白子对应的二维数组元素设置为无子
num1--;
dq.setText("白棋下子(用左键)");
oo=1;
repaint();
}
if(e.getSource()==huiqi&&w==0&&num2>0&&oo!=1)//判断是黑方悔子
{
w=1;
location[(blackx[num2-1]-20)/20][(blacky[num2-1]-20)/20]=0;//将刚下的黑子对应的二维数组元素设置为无子
num2--;
dq.setText("黑棋下子(用右键)");
repaint();
}
} //实现接口中其他函数 当然这里可以用适配器
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mousePressed(MouseEvent e)
{
if(e.getModifiers()==InputEvent.BUTTON1_MASK&&w==0) //按下鼠标左键,白方落子
{
int x=0;
int y=0;
//落在棋盘之内
if(e.getX()>=10&&e.getX()<=409&&e.getY()>=10&&e.getY()<=409)
{
currentx=(int)((e.getX()+10)/20*20);//鼠标点击左键的横坐标
currenty=(int)((e.getY()+10)/20*20);//鼠标点击左键的纵坐标
x=(currentx-20)/20;//计算出白子的横坐标
y=(currenty-20)/20;//计算出白子的纵坐标
if(location[x][y]==0)//当前的坐标无子时可以落子
{whitex[num1]=currentx;
whitey[num1]=currenty;
location[x][y]=1;//有白子赋1
num1++;
//状态变量为1,轮到黑方落子
w=1;
oo=0;
panduan baizi=new panduan();
if(baizi.ying(location,x,y,1)==1)
{
//白方胜利
sl.setText("白子胜利");
repaint();
String output="恭喜白方胜利";
JOptionPane.showConfirmDialog(this,output,"本局结果",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
bzslnum++;
w=-1;//当前局结束
}
dq.setText("黑棋下子(用右键)");
repaint();
}
//else
//flag3=1;
repaint();
}
}
else if(e.getModifiers()==InputEvent.BUTTON3_MASK&&w==1)
{
int x=0;
int y=0;
//落在棋盘之内
if(e.getX()>=10&&e.getX()<=409&&e.getY()>=10&&e.getY()<=409)
{
currentx=(short)((e.getX()+10)/20*20);//鼠标点击右键的横坐标
currenty=(short)((e.getY()+10)/20*20);//鼠标点击右键的纵坐标
x=(currentx-20)/20;//计算出黑子的横坐标
y=(currenty-20)/20;//计算出黑子的纵坐标
if(location[x][y]==0)//当前的坐标无子时可以下子
{blackx[num2]=currentx;
blacky[num2]=currenty;
num2++;
location[x][y]=2;//有黑子赋数字2
//状态变量为0,轮到黑方落子
w=0;
panduan heizi=new panduan();
if(heizi.ying(location,x,y,2)==2)
{
sl.setText("黑方胜利");
hzslnum++;
String output="恭喜黑方胜利";
repaint();
JOptionPane.showConfirmDialog(this,output,"本局结果",JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE);
w=-6;
}
dq.setText("白方下子(用左键)");
repaint();
}
repaint();
}
}
}
}
class panduan//由于程序中多次要判断那方是否达到胜利的条件 所以专门设计一类来判断
{
public int ying( int b[][],int x,int y,int zileixing) //返回1白子胜利,返回2黑子胜利,这里的b就是记录先前棋盘中各颗棋子的位置的二维数组
{
if(zileixing==1)//判断白子是到达胜利的条件
{
int a=1;
for(int i=x+1;i<20;i++)//横向向右检查
{
if(b[i][y]==1)
a++;
else if(b[i][y]!=1)
break;
}
for(int i=x-1;i>=0;i--)//横向向左检查
{
if(b[i][y]==1)
a++;//注意 这里a积累了上面横向向右检查的值,这种情况也是满足赢的条件 以下原理一样
else if(b[i][y]!=1)
break;
}
if(a>=5)//横向方向上满足条件,所以返回1
return 1;
a=1;//返回初值
for(int i=y+1;i<20;i++)//竖直向下方向检查
{
if(b[x][i]==1)
a++;
else if(b[x][i]!=1)
break;
}
for(int i=y-1;i>=0;i--)//竖直向上方向检查
{
if(b[x][i]==1)
a++;
else if(b[x][i]!=1)
break;
}
if(a>=5)//竖直方向上就满足条件了,所以返回1
return 1;
a=1;//返回初值
for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--)//在第二象限检查
{
if(b[i][j]==1)
a++;
else if(b[i][j]!=1)
break;
}
for(int i=x+1,j=y+1;i<20&&j<20;i++,j++)//在第四象限检查
{
if(b[i][j]==1)
a++;
else if(b[i][j]!=1)
break;
}
if(a>=5)//在反主对角线上满足条件,所以返回1
return 1;
a=1;//返回初值
for(int i=x+1,j=y-1;i<20&&j>=0;i++,j--)//在第一象限的检查
{
if(b[i][j]==1)
a++;
else if(b[i][j]!=1)
break;
}
for(int i=x-1,j=y+1;i>=0&&j<20;i--,j++)//在第三象限的检查
{
if(b[i][j]==1)
a++;
else if(b[i][j]!=1)
break;
}
if(a>=5)//在主对角线方向满足条件
return 1;
}
else if(zileixing==2)//判断黑子是否达到赢的条件
{
{
int c=1;
for(int i=x+1;i<20;i++)//横向向右检查
{
if(b[i][y]==2)
c++;
else if(b[i][y]!=2)
break;
}
for(int i=x-1;i>=0;i--)//横向向左检查
{
if(b[i][y]==2)
c++;//注意 这里a积累了上面横向向右检查的值,这种情况也是满足赢的条件 以下原理一样
else if(b[i][y]!=2)
break;
}
if(c>=5)//横向方向上满足条件,所以返回2
return 2;
c=1;//返回初值
for(int i=y+1;i<20;i++)//竖直向下方向检查
{
if(b[x][i]==2)
c++;
else if(b[x][i]!=2)
break;
}
for(int i=y-1;i>=0;i--)//竖直向上方向检查
{
if(b[x][i]==2)
c++;
else if(b[x][i]!=2)
break;
}
if(c>=5)//竖直方向上就满足条件了,所以返回2
return 2;
c=1;//返回初值
for(int i=x-1,j=y-1;i>=0&&j>=0;i--,j--)//在第二象限检查
{
if(b[i][j]==2)
c++;
else if(b[i][j]!=2)
break;
}
for(int i=x+1,j=y+1;i<20&&j<20;i++,j++)//在第四象限检查
{
if(b[i][j]==2)
c++;
else if(b[i][j]!=2)
break;
}
if(c>=5)//在反主对角线上满足条件,所以返回2
return 2;
c=1;//返回初值
for(int i=x-1,j=y+1;i>=0&&j<20;i--,j++)//在第三现象检查
{
if(b[i][j]==2)
c++;
else if(b[i][j]!=2)
break;
}
for(int i=x+1,j=y-1;i<20&&j>=0;i++,j--)//在第一现象检查
{
if(b[i][j]==2)
c++;
else if(b[i][j]!=2)
break;
}
if(c>=5)//在主对角方向满足条件
return 2;
}
}
return 0;//白子黑子都没有达到赢的条件时候就返回0
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -