📄 gobang.java.bak
字号:
}
}
for(i=0;i<15;i++)
for(j=0;j<15;j++){
if(table[i][j]==4)
for(k=0;k<4;k++)
computer[i][j][k]=0;
}
V_fillInBlank(false);
for(i=0;i<15;i++)
for(j=0;j<15;j++)
for(k=0;k<4;k++){
if(computer[i][j][k]>=maxOfPla&&(table[i][j]==4))
maxOfPla=computer[i][j][k];
}
for(i=0;i<15;i++)
for(j=0;j<15;j++)
for(k=0;k<4;k++){
if(maxOfPla==computer[i][j][k]&&table[i][j]==4){
position[1][0]=i;
position[1][1]=j;
break ;
}
}
if(maxOfCom>=maxOfPla){
final_x=position[0][0];
final_y=position[0][1];
maxOfAll=maxOfCom;
}
else if(maxOfCom<maxOfPla){
final_x=position[1][0];
final_y=position[1][1];
maxOfAll=maxOfPla;
}
for(int m=0;m<15;m++)
for(int n=0;n<15;n++)
if(table[m][n]==4)
table[m][n]=0;
}
static void V_fillInBlank(boolean which){ //预添棋型表
boolean w=which;
for(int x=0;x<15;x++)
for(int y=0;y<15;y++){
if(table[x][y]==4){
double temp[]={v_Transverse(x,y,w),v_Longitudinal(x,y,w),
v_Anti_diagonal(x,y,w),v_diagonal(x,y,w)};
int count2=0; //记录已经存在两个连子的个数
int count1=0; //记录已经存在一个连子的个数
for(int i=0;i<4;i++){
if(temp[i]==2)
count2++;
else if(temp[i]==1)
count1++;
}
if(count2>=2){
for(int i=0;i<4;i++)
if(temp[i]==2)
temp[i]+=0.8;
}
if(count2==1&&count1==1){
for(int i=0;i<4;i++)
if(temp[i]>0)
temp[i]+=0.5;
}
count2=0;
count1=0;
computer[x][y][0]=temp[0];
computer[x][y][1]=temp[1];
computer[x][y][2]=temp[2];
computer[x][y][3]=temp[3];
}
}
}
//统计电脑与玩家已落子的各方向的情况
//横向
static int Transverse(int i,int j,int turn){ //turn为1轮玩家,为2轮电脑,为0是无子,为4是电脑预测
int count=0; // 记录相连的棋子个数
if(turn==1){
for(int t=j;t>=0;t--){
if(table[i][t]==1)
count++;
else
break ;
}
if(count>0){
for(int t=j+1;t<15;t++){
if(table[i][t]==1)
count++;
else
break ;
}
}
}
else if(turn==2){
for(int t=j;t>=0;t--){
if(table[i][t]==2)
count++;
else
break ;
}
if(count>0){
for(int t=j+1;t<15;t++){
if(table[i][t]==2)
count++;
else
break ;
}
}
}
else if(turn==0){
for(int t=j-1;t>=0;t--){
if(table[i][t]==2)
count++;
else
break ;
}
if(count>0){
for(int t=j+1;t<15;t++){
if(table[i][t]==2)
count++;
else
break ;
}
}
}
return count;
}
//纵向
static int Longitudinal(int i,int j,int turn){
int count=0; // 记录相连的棋子个数
if(turn==1){
for(int t=i;t>=0;t--){
if(table[t][j]==1)
count++;
else
break ;
}
if(count>0){
for(int t=i+1;t<15;t++){
if(table[t][j]==1)
count++;
else
break ;
}
}
}
else if(turn==2){
for(int t=i;t>=0;t--){
if(table[t][j]==2)
count++;
else
break ;
}
if(count>0){
for(int t=i+1;t<15;t++){
if(table[t][j]==2)
count++;
else
break ;
}
}
}
else if(turn==0){
for(int t=i-1;t>=0;t--){
if(table[t][j]==2)
count++;
else
break ;
}
for(int t=i+1;t<15;t++){
if(table[t][j]==2)
count++;
else
break ;
}
}
return count;
}
//正对角
static int diagonal(int i,int j,int turn){
int count=0; // 记录相连的棋子个数
if(turn==1){
for(int m=0;i-m>=0&&j-m>=0;m++){
if(table[i-m][j-m]==1)
count++;
else
break;
}
if(count>0){
for(int m=1;i+m<15&&j+m<15;m++){
if(table[i+m][j+m]==1)
count++;
else
break;
}
}
}
else if(turn==2){
for(int m=0;i-m>=0&&j-m>=0;m++){
if(table[i-m][j-m]==2)
count++;
else
break;
}
if(count>0){
for(int m=1;i+m<15&&j+m<15;m++){
if(table[i+m][j+m]==2)
count++;
else
break;
}
}
}
else if(turn==0){
for(int m=1;i-m>=0&&j-m>=0;m++){
if(table[i-m][j-m]%2==0)
count++;
else
break;
}
for(int m=1;i+m<15&&j+m<15;m++){
if(table[i+m][j+m]%2==0)
count++;
else
break;
}
}
return count;
}
//反对角
static int Anti_diagonal(int i,int j,int turn){
int count=0; // 记录相连的棋子个数
if(turn==1){
for(int m=0;i-m>=0&&j+m<15;m++){
if(table[i-m][j+m]==1)
count++;
else
break;
}
if(count>0){
for(int m=1;i+m<15&&j-m>=0;m++){
if(table[i+m][j-m]==1)
count++;
else
break ;
}
}
}
else if(turn==2){
for(int m=0;i-m>=0&&j+m<15;m++){
if(table[i-m][j+m]==2)
count++;
else
break;
}
if(count>0){
for(int m=1;i+m<15&&j-m>=0;m++){
if(table[i+m][j-m]==2)
count++;
else
break ;
}
}
}
else if(turn==0){
for(int m=1;i-m>=0&&j+m<15;m++){
if(table[i-m][j+m]==2)
count++;
else
break;
}
for(int m=1;i+m<15&&j-m>=0;m++){
if(table[i+m][j-m]==2)
count++;
else
break ;
}
}
return count;
}
//统计预测时的各方向上的情况
//横向
static double v_Transverse(int i,int j,boolean which) { //which 表示计算电脑还是玩家
int count=0;
int value=(which==true? 2: 1); //决定是对电脑的预判还是对玩家的预判
for(int t=i-1;t>=0;t--){
if(table[t][j]==value)
count++;
else {
if(count>0&&count<4&&table[t][j]!=4)
count-=0.3;
break ;
}
}
for(int t=i+1;t<15;t++){
if(table[t][j]==value)
count++;
else {
if(count>0&&count<4&&table[t][j]!=4)
count-=0.3;
break ;
}
}
return count;
}
//纵向
static double v_Longitudinal(int i,int j,boolean which){
int count=0;
int value=(which==true? 2: 1);
for(int t=j-1;t>=0;t--){
if(table[i][t]==value)
count++;
else {
if(count>0&&count<4&&table[i][t]!=4)
count-=0.3;
break ;
}
}
for(int t=j+1;t<15;t++){
if(table[i][t]==value)
count++;
else {
if(count>0&&count<4&&table[i][t]!=4)
count-=0.3;
break ;
}
}
return count;
}
//撇向
static double v_Anti_diagonal(int i,int j,boolean which){
int count=0;
int value=(which==true? 2: 1);
for(int m=1;i-m>=0&&j+m<15;m++){
if(table[i-m][j+m]==value)
count++;
else {
if(count>0&&count<4&&table[i-m][j+m]!=4)
count-=0.3;
break ;
}
}
for(int m=1;i+m<15&&j-m>=0;m++){
if(table[i+m][j-m]==value)
count++;
else {
if(count>0&&count<4&&table[i+m][j-m]!=4)
count-=0.3;
break ;
}
}
return count;
}
//捺向
static double v_diagonal(int i,int j,boolean which){
int count=0;
int value=(which==true? 2: 1);
for(int m=1;i-m>=0&&j-m>=0;m++){
if(table[i-m][j-m]==value)
count++;
else {
if(count>0&&count<4&&table[i-m][j-m]!=4)
count-=0.3;
break ;
}
}
for(int m=1;i+m<15&&j+m<15;m++){
if(table[i+m][j+m]==value)
count++;
else{
if(count>0&&count<4&&table[i+m][j+m]!=4)
count-=0.3;
break ;
}
}
return count;
}
public static void main(String[] args) //主函数
{
Gobang frame = new Gobang();
frame.musicPlay(); //自动打开音乐
}
}
//画图
class DrawTable extends JPanel
{
ImageIcon imgWhite = new ImageIcon("white.jpg");
ImageIcon imgBlack = new ImageIcon("black.jpg");
Gobang go;
public DrawTable(Gobang go)
{
this.go = go;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(new Color(144,73,5)); //设置棋盘的颜色
for(int i = 0; i < 15; i++)
{
g.setColor(Color.blue);
g.drawLine(10,10+20*i,290,10+20*i);
g.drawLine(10+20*i,10,10+20*i,290);
}
for(int x = 0; x < 15; x++)
for(int y = 0; y < 15; y++)
{
if(go.table[x][y] == 1)
{
g.drawImage(imgBlack.getImage(),20*x+5,20*y+5,null);
}
else if(go.table[x][y] == 2)
{
g.drawImage(imgWhite.getImage(),20*x+5,20*y+5,null);
}
}
}
}
//线程实现显示剩余时间
class TimeLabel extends Thread{
int n=180;
Gobang go;
static boolean stopped;
public TimeLabel(Gobang go){
n = 180;
this.go=go;
this.stopped=false;
}
public void run(){
while(true){
if(stopped){
n=n-1;
break;
}
try{
if(n>=0){
String s = "总用时:\n3:00\n倒计时:\n"+n/60+":"+(n-(n/60)*60);
go.jtfTime.setText(s);
this.sleep(1000);
n--;
}
else{
JOptionPane.showMessageDialog(null,"对不起,时间到了,双方平局!","系统提示",
JOptionPane.INFORMATION_MESSAGE);
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);
}
}
}
catch(Exception e){
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -