📄 wuziqi.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class wuziqi extends JFrame implements MouseListener,WindowListener,ActionListener
{
static final int width=19,height=19,p=1020;
int board[][]=new int[19][19];
boolean ptable[][][]=new boolean[width][height][p],ctable[][][]=new boolean[width][height][p];
int cgrades[][]=new int[width][height],pgrades[][]=new int[width][height],cgrade,pgrade;
int win[][]=new int[2][p];
int pcount,ccount;
boolean player,computer,start,over,pwin,cwin,tie;
int i,j,k,m,n,count;
int mat,nat,mde,nde;
String str;
private Dialog dialog;
private Label dlable;
private Label tishi;
static final int fwidth=600,fheight=660;
int fstart,fend;
Button button=new Button("重新开局");
public wuziqi()
{
super("五子棋");
pcount=0;
ccount=0;
computer=false;
start=true;
player=false;
over=false;
pwin=false;
cwin=false;
tie=false;
for(i=0;i<width;i++)
{
for(j=0;j<height;j++)
{
board[i][j]=2;
}
}
count=0;
for(i=0;i<19;i++)
{
for(j=0;j<15;j++)
{
for(k=0;k<5;k++)
{
ptable[j+k][i][count]=true;
ctable[j+k][i][count]=true;
}
count++;
}
}
for(i=0;i<19;i++)
{
for(j=0;j<15;j++)
{
for(k=0;k<5;k++)
{
ptable[i][j+k][count]=true;
ctable[i][j+k][count]=true;
}
count++;
}
}
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
for(k=0;k<5;k++)
{
ptable[j+k][i+k][count]=true;
ctable[j+k][i+k][count]=true;
}
count++;
}
}
for(i=0;i<15;i++)
{
for(j=18;j>=4;j--)
{
for(k=0;k<5;k++)
{
ptable[j-k][i+k][count]=true;
ctable[j-k][i+k][count]=true;
}
count++;
}
}
if((int)(Math.random()*100)%2==1)
{
computer=true;
}
else
{
player=true;
}
count=0;
for(i=0;i<2;i++)
{
for(j=0;j<1020;j++)
{
win[i][j]=0;
}
}
this.setSize(600,660);
this.setLocation(100,100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new FlowLayout());
this.setResizable(false);
this.addMouseListener(this);
dialog=new Dialog(this,"提示",true);
dialog.setSize(240,80);
dialog.setLocation(300,50);
dlable=new Label("",Label.CENTER);
dialog.add(dlable);
dialog.addWindowListener(this);
tishi=new Label("",Label.CENTER);
this.add(tishi);
tishi.setSize(60,35);
tishi.setLocation(0,0);
this.add(button);
button.setLocation(0,0);
button.addActionListener(this);
this.setVisible(true);
}
public void comturn()
{
cgrade=-1;
pgrade=-1;
for(i=0;i<width;i++)
{
for(j=0;j<height;j++)
{
pgrades[i][j]=0;
if(board[i][j]==2)
{
for(k=0;k<p;k++)
{
if(ptable[i][j][k])
{
switch(win[0][k])
{
case 1:
pgrades[i][j]+=5;
break;
case 2:
pgrades[i][j]+=500;
break;
case 3:
pgrades[i][j]+=5000;
break;
case 4:
pgrades[i][j]+=50000;
break;
}
}
}
}
}
}
for(i=0;i<width;i++)
{
for(j=0;j<height;j++)
{
cgrades[i][j]=0;
if(board[i][j]==2)
{
for(k=0;k<p;k++)
{
if(ctable[i][j][k])
{
switch(win[1][k])
{
case 1:
cgrades[i][j]+=5;
break;
case 2:
cgrades[i][j]+=500;
break;
case 3:
cgrades[i][j]+=5000;
break;
case 4:
cgrades[i][j]+=50000;
break;
}
}
}
}
}
}
if(start)
{
if(board[9][9]==2)
{
m=9;
n=9;
}
else
{
m=8;
n=10;
}
start=false;
}
else
{
for(i=0;i<width;i++)
{
for(j=0;j<height;j++)
{
if(board[i][j]==2)
{
if(cgrades[i][j]>=cgrade)
{
cgrade=cgrades[i][j];
mat=i;
nat=j;
}
if(pgrades[i][j]>pgrade)
{
pgrade=pgrades[i][j];
mde=i;
nde=j;
}
}
}
}
if(cgrade>pgrade)
{
m=mat;
n=nat;
}
else
{
m=mde;
n=nde;
}
}
cgrade=0;
pgrade=0;
board[m][n]=1;
ccount++;
if(ccount+pcount==361)
{
tie=true;
over=true;
}
for(i=0;i<p;i++)
{
if(ctable[m][n][i] && win[1][i]!=7)
{
win[1][i]++;
}
if(ptable[m][n][i])
{
ptable[m][n][i]=false;
win[0][i]=7;
}
}
player=true;
computer=false;
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(new Color(150,100,0));
g.fillRect(0,0,fwidth,fheight);
g.setColor(Color.black);
fstart=30;
fend=600-30;
int i,j;
for(i=fstart;i<=fend;i+=30)
{
g.drawLine(fstart,i+30,fend,i+30);
}
fstart=30;
fend=600-30;
for(i=fstart;i<=fend;i+=30)
{
g.drawLine(i,fstart+30,i,fend+30);
}
if(!over)
{
if(computer)
{
tishi.setText("电脑下子");
comturn();
}
for(i=0;i<=1;i++)
{
for(j=0;j<p;j++)
{
if(win[i][j]==5)
{
if(i==0)
{
pwin=true;
over=true;
break;
}
else
{
cwin=true;
over=true;
break;
}
}
}
if(over)
{
break;
}
}
tishi.setText("该你下了");
}
for(i=0;i<width;i++)
{
for(j=0;j<height;j++)
{
if(board[i][j]==0)
{
g.setColor(Color.white);
g.fillOval(i*30+23,j*30+53,14,14);
}
else
{
if(board[i][j]==1)
{
g.setColor(Color.black);
g.fillOval(i*30+23,j*30+53,14,14);
}
}
}
}
if(pwin)
{
over=true;
dlable.setText("白子胜利");
dialog.setVisible(true);
}
if(cwin)
{
over=true;
dlable.setText("黑子胜利");
dialog.setVisible(true);
}
if(tie)
{
over=true;
dlable.setText("和局");
dialog.setVisible(true);
}
}
public void mouseClicked(MouseEvent e)
{
int m=(e.getX()-15)/30,n=(e.getY()-45)/30;
if(!over)
if(player)
{
if(board[m][n]==2)
{
board[m][n]=0;
pcount++;
if(ccount+pcount==361)
{
tie=true;
over=true;
}
for(i=0;i<p;i++)
{
if(ptable[m][n][i] && win[0][i]!=7)
{
win[0][i]++;
if(ctable[m][n][i])
{
ctable[m][n][i]=false;
win[1][i]=7;
}
}
}
player=false;
computer=true;
}
repaint();
}
}
public void mouseEntered(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void windowClosing(WindowEvent e)
{
if(e.getSource()==dialog)
{
dialog.setVisible(false);
}
else
{
System.exit(0);
}
}
public void windowOpened(WindowEvent e)
{
}
public void windowActivated(WindowEvent e)
{
repaint();
}
public void windowDeactivated(WindowEvent e)
{
}
public void windowClosed(WindowEvent e)
{
repaint();
}
public void windowIconified(WindowEvent e)
{
}
public void windowDeiconified(WindowEvent e)
{
}
public void actionPerformed(ActionEvent e)
{
reset();
}
public void reset()
{
pcount=0;
ccount=0;
computer=false;
start=true;
player=false;
over=false;
pwin=false;
cwin=false;
tie=false;
for(i=0;i<width;i++)
{
for(j=0;j<height;j++)
{
board[i][j]=2;
}
}
count=0;
for(i=0;i<19;i++)
{
for(j=0;j<15;j++)
{
for(k=0;k<5;k++)
{
ptable[j+k][i][count]=true;
ctable[j+k][i][count]=true;
}
count++;
}
}
for(i=0;i<19;i++)
{
for(j=0;j<15;j++)
{
for(k=0;k<5;k++)
{
ptable[i][j+k][count]=true;
ctable[i][j+k][count]=true;
}
count++;
}
}
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
for(k=0;k<5;k++)
{
ptable[j+k][i+k][count]=true;
ctable[j+k][i+k][count]=true;
}
count++;
}
}
for(i=0;i<15;i++)
{
for(j=18;j>=4;j--)
{
for(k=0;k<5;k++)
{
ptable[j-k][i+k][count]=true;
ctable[j-k][i+k][count]=true;
}
count++;
}
}
if((int)(Math.random()*100)%2==1)
{
computer=true;
}
else
{
player=true;
}
count=0;
for(i=0;i<2;i++)
{
for(j=0;j<1020;j++)
{
win[i][j]=0;
}
}
repaint();
}
public static void main(String args[])
{
wuziqi wzqf=new wuziqi();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -