标题:请大神来看看,这个五子棋算法哪里不正确,运行不了,下面是源代码
只看楼主
xyxyl
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-5-27
结帖率:0
已结贴  问题点数:10 回复次数:4 
请大神来看看,这个五子棋算法哪里不正确,运行不了,下面是源代码
package gouwu;

import java.awt.Point;

//定义机器人的能力规范
public interface Alpha{   
   
  //下一步棋
    public Point next();
    //向机器人注入棋盘状态数组
    public void setData(int[][] data);
}

class AlphaPig implements Alpha{
    int[][] data; //棋盘状态数组:0-无子(默认)
   
    public void setData(int[][] data){
        this.data = data;
    }
    //阿尔法猪的策略:顺序寻找,有空位即返回
    public Point next(){
        System.out.println("q1");
        
        //用这个类进行各种判断(包括电脑走哪和输赢判断)
             //棋盘状态数组:0-无子(默认)
            //这个方法用来得出电脑走哪里最合适
                int[][] board={
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
                        {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
                        {0,1,2,2,2,2,2,2,2,2,2,2,2,1,0},
                        {0,1,2,3,3,3,3,3,3,3,3,3,2,1,0},
                        {0,1,2,3,4,4,4,4,4,4,4,3,2,1,0},
                        {0,1,2,3,4,5,5,5,5,5,4,3,2,1,0},
                        {0,1,2,3,4,5,6,6,6,5,4,3,2,1,0},
                        {0,1,2,3,4,5,6,7,6,5,4,3,2,1,0},
                        {0,1,2,3,4,5,6,6,6,5,4,3,2,1,0},
                        {0,1,2,3,4,5,5,5,5,5,4,3,2,1,0},
                        {0,1,2,3,4,4,4,4,4,4,4,3,2,1,0},
                        {0,1,2,3,3,3,3,3,3,3,3,3,2,1,0},
                        {0,1,2,2,2,2,2,2,2,2,2,2,2,1,0},
                        {0,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
                        {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};//初始化棋盘
                int[][][] computer=new int[15][15][5];//电脑棋型评分表(包括横、竖、左斜、右斜四种以及他们的和)
                int[][][] player=new int[15][15][5];//玩家棋型评分表
                int[] result=new int[2];//最终结果(电脑下子坐标)
                int[][] close=new int[2][4];//端点的阻碍情况(2、4指分两种颜色的棋,四个方向,下同)
                int[][] continued=new int[2][4];//连续几个子(两边加起来)
                int[][] continued_1=new int[2][4];//向一边连续几个子
                int[][] continued_2=new int[2][4];//向另一边连续几个子
                int opposite;//对立面颜色代号
                for(int i=0;i<15;i++){
                    for(int j=0;j<15;j++){
                        for(int k=0;k<4;k++){
                            computer[i][j][k]=0;
                            player[i][j][k]=0;//初始化为0
                            }
                        }
                    System.out.println("q2");}     
                for(int i=0;i<2;i++){
                    for(int j=0;j<4;j++){
                        close[i][j]=0;
                        continued[i][j]=0;
                        continued_1[i][j]=0;
                        continued_2[i][j]=0;//初始化为0
                        }
                    System.out.println("q3");}     
                int point=0;
                for(int i=0;i<15;i++){//对棋盘上每个点进行分析
                    for(int j=0;j<15;j++){
                        System.out.println("q4");
                        if(data[i][j]==0){//首先该位置上不能有子(table是用于记录棋盘上的棋子的二维数组,没有棋子为0,电脑为1,玩家为2)
                            System.out.println("q5");
                            for(int side=0;side<2;side++){//分别对黑子和白子进行棋形分析
                                if(side==0){
                                    opposite=2;
                                    }     
                                else{
                                    opposite=1;//根据当前分析的颜色确定其对立面
                                    }     
                                close[side][0]=0;
                                close[side][1]=0;
                                close[side][2]=0;
                                close[side][3]=0;//初始化为0
                                if(i==0 || i==14){//棋盘左右两边缘的情况
                                    close[side][1]+=1;
                                    close[side][2]+=1;
                                    close[side][3]+=1;
                                    }     
                                if(j==0 || j==14){//棋盘上下两边缘的情况
                                    close[side][0]+=1;
                                    if(i!=0 && i!=14){//既在棋盘左右两边又在上下两边即在角落时的情况
                                        close[side][2]+=1;
                                        close[side][3]+=1;
                                        }     
                                    }     
                                for(int k=j+1;k<15;k++){//竖直方向向上的情况
                                    if(data[i][k]==0){//端点为空
                                        continued_1[side][0]=k-j-1;
                                        break;     
                                        }     
                                    else if(data[i][k]==opposite){//端点为对立棋子
                                        continued_1[side][0]=k-j-1;
                                        close[side][0]+=1;     
                                        break;     
                                        }     
                                    if(k==14){//遇到棋盘上边缘
                                        close[side][0]+=1;
                                        }     
                                    }     
                                for(int k=j-1;k>=0;k--){//竖直方向向下的情况
                                    if(data[i][k]==0){
                                        continued_2[side][0]=j-1-k;
                                        break;     
                                        }     
                                    else if(data[i][k]==opposite){
                                        continued_2[side][0]=j-1-k;
                                        close[side][0]+=1;
                                        break;     
                                        }     
                                    if(k==0){
                                        close[side][0]+=1;
                                        }     
                                    }     
                                continued[side][0]=continued_1[side][0]+continued_2[side][0]+1;//竖直方向向上+向下
                                for(int k=i+1;k<15;k++){//水平方向向右的情况
                                    if(data[k][j]==0){
                                        continued_1[side][1]=k-i-1;
                                        break;     
                                        }     
                                    else if(data[k][j]==opposite){
                                        continued_1[side][1]=k-i-1;
                                        close[side][1]+=1;
                                        break;     
                                       
                                    }     
                                    if(k==14){
                                        close[side][1]+=1;
                                        }     
                                    }     
                                for(int k=i-1;k>=0;k--){//水平方向向左的情况
                                    if(data[k][j]==0){
                                        continued_2[side][1]=i-1-k;
                                        break;     
                                        }     
                                    else if(data[k][j]==opposite){
                                       
                                        continued_2[side][1]=i-1-k;
                                        close[side][1]+=1;
                                        break;
                                       
                                    }     
                                    if(k==0){
                                        close[side][1]+=1;
                                        }     
                                    }     
                                continued[side][1]=continued_1[side][1]+continued_2[side][1]+1;
                                for(int k=i+1,p=j+1;k<15&&p<15;k++,p++){//向右上的情况
                                    if(data[k][p]==0){
                                        continued_1[side][2]=k-i-1;
                                        break;     
                                        }     
                                    else if(data[k][p]==opposite){
                                       
                                        continued_1[side][2]=k-i-1;
                                        close[side][2]+=1;     
                                        break;     
                                        }     
                                    if(k==14 || p==0){
                                        close[side][2]+=1;
                                        }     
                                    }     
                                for(int k=i-1,p=j-1;k>=0&&p>=0;k--,p--){//向左下的情况
                                    if(data[k][p]==0){
                                        continued_2[side][2]=i-1-k;
                                        break;     
                                        }     
                                    else if(data[k][p]==opposite){
                                        continued_2[side][2]=i-1-k;
                                        close[side][2]+=1;
                                        break;     
                                        }     
                                    if(k==0 || p==14){
                                        close[side][2]+=1;
                                        }     
                                    }     
                                continued[side][2]=continued_1[side][2]+continued_2[side][2]+1;
                                for(int k=i+1,p=j-1;k<15&&p>=0;k++,p--){//向右下的情况
                                    if(data[k][p]==0){
                                        continued_1[side][3]=k-i-1;
                                        break;     
                                        }     
                                    else if(data[k][p]==opposite){
                                        continued_1[side][3]=k-i-1;
                                        close[side][3]+=1;
                                        break;
                                        }
                                    
                                    if(k==14 || p==0){
                                        close[side][3]+=1;
                                        }     
                                    }     
                                for(int k=i-1,p=j+1;k>=0&&p<15;k--,p++){//向左上的情况
                                    if(data[k][p]==0){     
                                        continued_2[side][3]=i-1-k;     
                                        break;     
                                        }     
                                    else if(data[k][p]==opposite){
                                        continued_2[side][3]=i-1-k;
                                        close[side][3]+=1;
                                        break;     
                                        }     
                                    if(k==0 || p==14){
                                        close[side][3]+=1;
                                        }     
                                    }                        
                                continued[side][3]=continued_1[side][3]+continued_2[side][3]+1;
                                for(int m=0;m<4;m++){//对于四个方向根据连续子数和端点情况给予相应的评分
                                    if(close[side][m]==0){//两端点均无子
                                        if(continued[side][m]==2){//能活2
                                            if(side==0){
                                                computer[i][j][m]=35;
                                                }     
                                            else{     
                                                player[i][j][m]=15;
                                                }     
                                            }     
                                        else if(continued[side][m]==3){//能活3
                                            if(side==0){     
                                                computer[i][j][m]=800;
                                                }     
                                            else{     
                                                player[i][j][m]=400;
                                                }     
                                            }     
                                        else if(continued[side][m]==4){//能活4
                                            if(side==0){
                                                computer[i][j][m]=15000;
                                                }     
                                            else{     
                                                player[i][j][m]=7000;
                                                }     
                                            }     
                                        else if(continued[side][m]>=5){//能活5
                                            if(side==0){     
                                                computer[i][j][m]=800000;
                                                }     
                                            else{     
                                                player[i][j][m]=100000;
                                                }     
                                            }     
                                        }     
                                    else if(close[side][m]==1){//仅一端有子
                                        if(continued[side][m]==2){//能冲2
                                            if(side==0){
                                                computer[i][j][m]=20;
                                                }
                                            else{
                                                player[i][j][m]=5;
                                                }     
                                            }     
                                        else if(continued[side][m]==3){//能冲3
                                            if(side==0){     
                                                computer[i][j][m]=30;
                                                }     
                                            else{     
                                                player[i][j][m]=10;
                                                }     
                                            }     
                                        else if(continued[side][m]==4){//能冲4
                                            if(side==0){
                                                computer[i][j][m]=790;
                                                
                                                }
                                            else{
                                                player[i][j][m]=390;
                                                }     
                                            }     
                                        else if(continued[side][m]>=5){//能冲5
                                            if(side==0){
                                                computer[i][j][m]=800000;
                                                }
                                            else{
                                                player[i][j][m]=100000;
                                                }     
                                            }     
                                        }     
                                    else if(close[side][m]==2){//两端均有子
                                        if(continued[side][m]>=5){//小于5的情况不作考虑,因为不可能成5
                                            if(side==0){
                                                computer[i][j][m]=800000;
                                                }
                                            else{
                                                player[i][j][m]=100000;
                                                }
                                            }     
                                        }     
                                    if(side==0){
                                        computer[i][j][4]+=computer[i][j][m];//将一个点4个方向的评分加起来得到一个总评分
                                        }
                                    else{
                                        player[i][j][4]+=player[i][j][m];
                                        }     
                                    }     
                                }     
                            }     
                        else{//如果该位置有子则给予极小的评分
                            for(int m=0;m<4;m++){
                                computer[i][j][m]=-100000000;
                                computer[i][j][4]+=computer[i][j][m];
                                }     
                            }     
                        if(computer[i][j][4]+player[i][j][4]+board[i][j]>=point){
                            if(computer[i][j][4]+player[i][j][4]+board[i][j]==point){
                                double choice=Math.random();
                                if(choice>=0.5){
                                    point=computer[i][j][4]+player[i][j][4]+board[i][j];
                                    }     
                                }     
                            else{     
                                point=computer[i][j][4]+player[i][j][4]+board[i][j];
                                }     
                            result[0]=i;
                            result[1]=j;
                           
                            }         
                        }     
                    }
                System.out.println("q1");
                return new Point(result[0],result[1]);
                }
   
            /*public static int is_over(){//判断胜负的方法
                int[] continued=new int[4];
                int[] continued_1=new int[4];
                int[] continued_2=new int[4];     
                for(int j=0;j<4;j++){         
                    continued[j]=0;         
                    continued_1[j]=0;     
                    continued_2[j]=0;         
                    }     
                int result=0;     
                int side;         
                int i=main_loop.piece_x;
                int j=main_loop.piece_y;     
                if(main_loop.table[i][j]==1){
                    side=1;         
                    }     
                else{     
                    side=2;
                    }     
                for(int k=j+1;k<15;k++){
                    if(data[i][k]!=side){
                        continued_1[0]=k-j-1;     
                        break;         
                        }     
                    }     
                for(int k=j-1;k>=0;k--){
                    if(data[i][k]!=side){
                        continued_2[0]=j-1-k;     
                        break;         
                        }     
                    }     
                continued[0]=continued_1[0]+continued_2[0]+1;
                for(int k=i+1;k<15;k++){     
                    if(data[k][j]!=side){     
                        continued_1[1]=k-i-1;     
                        break;         
                        }     
                    }     
                for(int k=i-1;k>=0;k--){     
                    if(data[k][j]!=side){     
                        continued_2[1]=i-1-k;     
                        break;     
                        }     
                    }     
                continued[1]=continued_1[1]+continued_2[1]+1;
                for(int k=i+1,p=j+1;k<15&&p<15;k++,p++){     
                    if(data[k][p]!=side){
                        continued_1[2]=k-i-1;     
                        break;             }
                    }
                for(int k=i-1,p=j-1;k>=0&&p>=0;k--,p--){     
                    if(data[k][p]!=side){     
                        continued_2[2]=i-1-k;         
                        break;         
                        }     
                    }     
                continued[2]=continued_1[2]+continued_2[2]+1;
                for(int k=i+1,p=j-1;k<15&&p>=0;k++,p--){
                    if(data[k][p]!=side){     
                        continued_1[3]=k-i-1;         
                        break;         
                        }     
                    }     
                for(int k=i-1,p=j+1;k>=0&&p<15;k--,p++){
                    if(data[k][p]!=side){
                        continued_2[3]=i-1-k;         
                        break;         
                        }     
                    }     
                continued[3]=continued_1[3]+continued_2[3]+1;
                for(int n=0;n<4;n++){     
                    if(continued[n]>=5){//任何位置检测到连续5个以上颜色相同的则游戏结束     
                        result=side;//返回值为赢的一方     
                        }     
                    }     
                if (result==0){//判断和棋     
                    for(int m=0;m<15;m++){     
                        for(int n=0;n<15;n++){     
                            if(data[m][n]==0){     
                                result=3;         
                                }     
                            }     
                        }     
                    }     
                if(result==3){
                    result=0;//即没人赢又没有和棋则返回0
                    }         else if(result==0)
                    {
                        result=3;//和棋则返回3     
                        }     
                return result;     
                } */
            
            }
搜索更多相关主题的帖子: interface 阿尔法 package 五子棋 源代码 
2016-05-31 20:45
grmmylbs
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:54
帖 子:1409
专家分:5845
注 册:2016-2-14
得分:10 
没有定义main函数
2016-06-01 09:42
xyxyl
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-5-27
得分:0 
package gouwu;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Chess5Panel extends JPanel {
   
    int t = 0;
    static int x;
    static int y;
    private int x0=30, y0=30; //棋盘左上角的位置   
    private int N = 9; //N*N的棋盘
    private int k = 60; //方格宽(高)度   
    private int r = 20; //棋子(圆)半径
   
    //棋盘上每个交叉点的状态:0-无子(默认),1-黑子,2-白字
    private int[][] data;
   
   
    //人机对战之机器(阿尔法对象)
    private Alpha alpha;
    public void setAlpha(Alpha alpha){
        alpha.setData( data );
        this.alpha = alpha;
    }
   
    public Chess5Panel() {
        setPreferredSize( new Dimension(600, 600) );
        data = new int[N+1][N+1];
        
   
        addMouseListener( new ChessMouseListener() );
    }
    public void paint(Graphics g){
        for(int i=0; i<=N; i++) {
            g.drawLine(x0, y0+i*k, x0+N*k, y0+i*k);   
        }
        for(int i=0; i<=N; i++) {
            g.drawLine(x0+i*k, y0, x0+i*k, y0+N*k);
        }
        for(int i=0; i<=N; i++){
            for(int j=0; j<=N; j++){
                drawChess(i, j, data[i][j], g);
            }
        }        
    }
    /**
    *  在指定棋盘坐标绘制指定颜色的棋子(圆)
     * @param x 棋盘横坐标
     * @param y 棋盘纵坐标
     * @param color 棋子颜色,1为黑子,2为白子
     * @param g 图形设备上下文
     */
    public void drawChess(int x, int y, int color, Graphics g){
        if(color==1)
            g.fillOval(x0+x*k-r, y0+y*k-r, 2*r, 2*r);
        else if(color==2)
            g.drawOval(x0+x*k-r, y0+y*k-r, 2*r, 2*r);
    }
    /**
     * 内部类,鼠标事件监听器。
     */
    private class ChessMouseListener implements MouseListener {   
        public void mouseClicked(MouseEvent e){
            
            t++;
            x = Math.round( (e.getX()-x0)/(k+0.0f) );
            y = Math.round( (e.getY()-y0)/(k+0.0f) );
            if(data[x][y] == 0){
               
                data[x][y] = 2; //人执黑,先手
               
                Point p = alpha.next();
               
                data[p.x][p.y] = 1; //机执白
               
            }
            repaint(); //重绘
        }
        public void mousePressed(MouseEvent e){}
        public void mouseReleased(MouseEvent e){}
        public void mouseEntered(MouseEvent e){}
        public void mouseExited(MouseEvent e){}
    }
}
2016-06-01 23:11
xyxyl
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-5-27
得分:0 
package gouwu;

import java.awt.*;
import javax.swing.*;

public class Chess5Test {
    public static void main(String[] args) {
        JFrame frame = new JFrame("五子棋");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Chess5Panel c5 = new Chess5Panel();
        c5.setAlpha( new AlphaPig( ) );
        
        frame.getContentPane().add( c5 );        
        frame.pack();
        frame.setVisible(true);
    }
}
2016-06-01 23:12
xyxyl
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2016-5-27
得分:0 
这是全部的代码,麻烦再帮我看看,谢谢了
2016-06-01 23:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-465449-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.104883 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved