现在的位置: 首页 > 综合 > 正文

智能五子棋基本思路

2011年08月27日 ⁄ 综合 ⁄ 共 5880字 ⁄ 字号 评论关闭

前些天闲时写的,在学数据结构的时拿来练手的.没技术含量,最有技术含量的AI部分,我是看别人(园子里叫二十四生的)的算法改的.刚弄了一下午小程序弄不过去,头疼,现无聊的紧,闲着发着玩.当消遣
主要发下AI核心算法.有兴趣的同学用VB,VC.VC#都可以一起做着玩.保持对编程的兴趣.其它没了.

一,说下五子棋的原理:5子成一线即赢.
所以可以这样做,当一子落下时,判断该子是否成5子,下面的代码说的很清楚了

        /// <summary>当一棋子落子时检查该色子是否胜利
        
/// 当一棋子落子时检查该色子是否胜利
        
/// </summary>
        
/// <param name="chessPoint">落子位置</param>
        
/// <param name="color">棋子色</param>
        
/// <returns>bool</returns>
        
/// 把棋子分8个方向计算
        
/// 2    3     4
        
/// 1    x,y   5
        
/// 8     7    6

        public bool CheckeWin(ChessPoint chessPoint, CheckerColor color)  //检查是否胜利
        {
            
int X = chessPoint.X;
            
int Y = chessPoint.Y;

            
int times = 1;
            
int tempX = X - 1;
            
int tempY = Y;
            
//////////检查 1 方向//////////
            while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 1, color))
                
{
                    times
++;
                    tempX
--;
                    
continue;
                }

                
else
                    
break;
            }

            
////////检查 5  方向////////
            tempX = X + 1;
            
while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 5, color))
                
{
                    times
++;
                    tempX
++;
                    
continue;
                }

                
else
                    
break;
            }

            
if (times >= 5)
                
return true;
            
////////////////////1-5 方向检查完毕////////////////////////
            times = 1;
            tempX 
= X;
            tempY 
= Y - 1;
            
//////检查 3 方向////////
            while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 3, color))
                
{
                    times
++;
                    tempY
--;
                    
continue;
                }

                
else
                    
break;
            }

            
//////////检查 7 方向////////////
            tempY = Y + 1;
            
while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 7, color))
                
{
                    times
++;
                    tempY
++;
                    
continue;
                }

                
else
                    
break;
            }

            
if (times >= 5)
                
return true;
            
/////////////////////3-7 方向检查完毕////////////////////////////
            times = 1;
            tempX 
= X - 1;
            tempY 
= Y - 1;
            
///////////检查 2 方向////////////
            while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 2, color))
                
{
                    times
++;
                    tempX
--;
                    tempY
--;
                    
continue;
                }

                
else
                    
break;
            }

            tempX 
= X + 1;
            tempY 
= Y + 1;
            
/////////检查 6 方向/////////
            while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 6, color))
                
{
                    times
++;
                    tempX
++;
                    tempY
++;
                    
continue;
                }

                
else
                    
break;
            }

            
if (times >= 5)
                
return true;
            
////////////////////////2-6方向检查完毕////////////////////////////
            times = 1;
            tempX 
= X + 1;
            tempY 
= Y - 1;
            
///////////检查 4 方向////////////
            while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 4, color))
                
{
                    times
++;
                    tempX
++;
                    tempY
--;
                    
continue;
                }

                
else
                    
break;
            }

            tempX 
= X - 1;
            tempY 
= Y + 1;
            
///////////检查 8 方向/////////////
            while (true)
            
{
                
if (NextHasChecker(tempX, tempY, 8, color))
                
{
                    times
++;
                    tempX
--;
                    tempY
++;
                    
continue;
                }

                
else
                    
break;
            }

            
if (times >= 5)
                
return true;

            
return false;   //默认返回False

        }

       
检查1.2.3.4.5.6.7.8 方向是否有该色的子

抱歉!评论已关闭.