五子棋 C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. namespace MyGobang
  11. {
  12.      
  13.     public partial class Form1 : Form
  14.     { 
  15.         private Point lastMovePoint = new Point(-1, -1);//上一步棋子的坐标
  16.         private int[,] virtualGobangBoard = new int[15, 15];//虚拟棋盘
  17.         private PictureBox[,] chessPicutureBox = new PictureBox[15, 15];//棋子的图片
  18.         private int[, ,] ValueTable = new int[15, 15, 9];//临时棋型表
  19.         private int[, ,] LastValueTable = new int[15, 15, 5];//最终棋型表
  20.         private const int white = 1;
  21.         private const int black = -1;
  22.         private const int noChess = 0;//无棋子的空格
  23.         private int playerChessColor = black;
  24.         private Point MVP=new Point(-1,-1);//最有价值点
  25.         public Form1()
  26.         {
  27.             InitializeComponent();
  28.             InitializeGobangBoard();
  29.             GobangGroupBox.Paint += new PaintEventHandler(GobangGroupBox_Paint);
  30.             GobangGroupBox.MouseMove += new MouseEventHandler(GobangGroupBox_MouseMove);
  31.             GobangGroupBox.MouseClick += new MouseEventHandler(GobangGroupBox_MouseClick);
  32.         }
  33.         private void InitializeGobangBoard()//初始化棋盘
  34.         {
  35.             int i, j;
  36.             GobangGroupBox.Paint += new PaintEventHandler(GobangGroupBox_Paint);//why
  37.             for (i = 0; i < 15; i++)
  38.                 for (j = 0; j < 15; j++)
  39.                 {
  40.                     chessPicutureBox[i, j] = new PictureBox();//实例化数组之后也要实例化数组中的每一个成员
  41.                     chessPicutureBox[i, j].BackColor = Color.Transparent;
  42.                     chessPicutureBox[i, j].Size = new Size(40, 40);
  43.                     chessPicutureBox[i, j].Location = new Point(10 + 40 * i, 10 + 40 * j);
  44.                     chessPicutureBox[i, j].Visible = false;
  45.                     chessPicutureBox[i, j].SizeMode = PictureBoxSizeMode.CenterImage;
  46.                     GobangGroupBox.Controls.Add(chessPicutureBox[i, j]);//为什么
  47.                 }
  48.         }
  49.         private void GobangGroupBox_Paint(object sender, PaintEventArgs e)//绘制棋盘
  50.         {
  51.             int i;
  52.             Graphics gr = e.Graphics;
  53.             Pen myPen = new Pen(Color.Black, 2);
  54.             SolidBrush sb = new SolidBrush(Color.Red);//单色画笔用于填充
  55.             SolidBrush whiteBrush = new SolidBrush(Color.White);
  56.             for (i = 0; i < 15; i++)
  57.             {
  58.                 gr.DrawLine(myPen, 30 + i * 40, 30, 30 + i * 40, 590);//画横线
  59.                 gr.DrawLine(myPen, 30, 30 + i * 40, 590, 30 + i * 40);//画竖线
  60.             }
  61.             gr.FillEllipse(sb, 146, 146, 8, 8);//画小红点
  62.             gr.FillEllipse(sb, 466, 146, 8, 8);
  63.             gr.FillEllipse(sb, 466, 466, 8, 8);
  64.             gr.FillEllipse(sb, 146, 466, 8, 8);
  65.             gr.FillEllipse(sb, 306, 306, 8, 8);
  66.         }
  67.         //棋型表,包括各种棋型的分值
  68.         //H指活棋,d指防守,C指冲棋。比如dH3表示对手的“活3”,C4表示己方的“冲4”
  69.         private enum pattern : int { H4 =16000, dH4=4000, C4=16000, dC4=3750, H3=750, dH3=150,
  70.             C3=50, dC3=50, H2=30, dH2=30, dC2=5, H1=10, C2=5, dH1=10, C1=2, dC1=1};
  71.         private bool CheckIfIndexOutOfRange(int x, int y, int plus, int direction)//检查数组索引是否溢出
  72.         {
  73.             int left;
  74.             switch (direction)
  75.             {
  76.                 case 5://左方向
  77.                     left = x;
  78.                     return (left >= plus);
  79.                 case 1://右方向
  80.                     left = 14 - x;
  81.                     return (left >= plus);
  82.                 case 3://下方向
  83.                     left = 14 - y;
  84.                     return (left >= plus);
  85.                 case 7://上方向
  86.                     left = y;
  87.                     return (left >= plus);
  88.                 case 8://右上方向
  89.                     left = Math.Min(14 - x, y);
  90.                     return (left >= plus);
  91.                 case 4://左下方向
  92.                     left = 14 - Math.Max(14 - x, y);
  93.                     return (left>=plus);
  94.                 case 2://右下
  95.                     left = Math.Min(14-x,14-y);
  96.                     return (left >= plus);
  97.                 case 6://左上
  98.                     left = Math.Min(x, y);
  99.                     return (left >= plus);
  100.                 default:
  101.                     MessageBox.Show("System error.");
  102.                     return false;
  103.             }
  104.         }
  105.         private void AnalyseOneLine()//用于判断一行上一个点的价值,比如水平方向上的一个点有左右
  106.         {                                                    //两个方向,此函数把两个分散方向的价值结合起来
  107.             int x, y, dir;
  108.             for (dir=1;dir<=4;dir++)                    //把ValueTable[x,y,1]和ValueTable[x,y,4]结合成LastValueTable[x,y,1],指水平方向
  109.                 for (y = 0; y < 15; y++)                //把ValueTable[x,y,3]和ValueTable[x,y,7]结合成LastValueTable[x,y,2],指垂直方向    
  110.                         for (x = 0; x < 15; x++)        //把ValueTable[x,y,2]和ValueTable[x,y,6]结合成LastValueTable[x,y,3],指斜右上方的对角线
  111.                         {                               //把ValueTable[x,y,4]和ValueTable[x,y,8]结合成LastValueTable[x,y,3],指斜左上方的对角线
  112.                             LastValueTable[x, y, dir] = noChess;//清零
  113.                             //将ValueTable[x, y, dir]不为零的点赋给ValueTable[x, y, dir]
  114.                             if (ValueTable[x, y, dir] != 0) LastValueTable[x, y, dir] = ValueTable[x, y, dir];
  115.                             //将ValueTable[x, y, dir+4]不为零的点赋给ValueTable[x, y, dir],但可能会覆盖ValueTable[x, y, dir]中的值
  116.                             if (ValueTable[x, y, dir + 4] != 0) LastValueTable[x, y, dir] = ValueTable[x, y, dir + 4];
  117.                             //进攻端            
  118.                             if ((ValueTable[x, y, dir] == (int)pattern.H3 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
  119.                                || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.H3)
  120.                                || (ValueTable[x, y, dir] == (int)pattern.H3 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
  121.                                || (ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
  122.                                || (ValueTable[x, y, dir] == (int)pattern.C3 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
  123.                                || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.C3)
  124.                                || (ValueTable[x, y, dir] == (int)pattern.C3 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
  125.                                || (ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.C3)
  126.                                || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
  127.                                || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
  128.                                || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.C2)
  129.                                || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.C2)) LastValueTable[x, y, 1] = (int)pattern.H4;
  130.                             if ((ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
  131.                                 || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.H1)) LastValueTable[x, y, 1] = (int)pattern.H3;
  132.                             if ((ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.H2)
  133.                                 || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
  134.                                 || (ValueTable[x, y, dir] == (int)pattern.H2 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
  135.                                 || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.C2)) LastValueTable[x, y, 1] = (int)pattern.C3;
  136.                             if ((ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.H1)) LastValueTable[x, y, 1] = (int)pattern.H2;
  137.                             if ((ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.H1)
  138.                                 || (ValueTable[x, y, dir] == (int)pattern.H1 && ValueTable[x, y, dir + 4] == (int)pattern.C1)) LastValueTable[x, y, 1] = (int)pattern.C2;
  139.                             if ((ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.C1)
  140.                                 || (ValueTable[x, y, dir] == (int)pattern.C1 && ValueTable[x, y, dir + 4] == (int)pattern.C2)
  141.                                 || (ValueTable[x, y, dir] == (int)pattern.C2 && ValueTable[x, y, dir + 4] == (int)pattern.C1)) LastValueTable[x, y, 1] = 0;//无价值点
  142.                             //防守端
  143.                             if ((ValueTable[x, y, dir] == (int)pattern.dH3 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
  144.                                 || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dH3)
  145.                                 || (ValueTable[x, y, dir] == (int)pattern.dH3 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
  146.                                 || (ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
  147.                                 || (ValueTable[x, y, dir] == (int)pattern.dC3 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
  148.                                 || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dC3)
  149.                                 || (ValueTable[x, y, dir] == (int)pattern.dC3 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
  150.                                 || (ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dC3)
  151.                                 || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
  152.                                 || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
  153.                                 || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)
  154.                                 || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)) LastValueTable[x, y, 1] = (int)pattern.dH4;
  155.                             if ((ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
  156.                                || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)) LastValueTable[x, y, 1] = (int)pattern.dH3;
  157.                             if ((ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dH2)
  158.                                 || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
  159.                                 || (ValueTable[x, y, dir] == (int)pattern.dH2 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
  160.                                 || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)) LastValueTable[x, y, 1] = (int)pattern.dC3;
  161.                             if ((ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)) LastValueTable[x, y, 1] = (int)pattern.dH2;
  162.                             if ((ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dH1)
  163.                                 || (ValueTable[x, y, dir] == (int)pattern.dH1 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)) LastValueTable[x, y, 1] = (int)pattern.dC2;
  164.                             if ((ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)
  165.                                 || (ValueTable[x, y, dir] == (int)pattern.dC1 && ValueTable[x, y, dir + 4] == (int)pattern.dC2)
  166.                                 || (ValueTable[x, y, dir] == (int)pattern.dC2 && ValueTable[x, y, dir + 4] == (int)pattern.dC1)) LastValueTable[x, y, 1] = 0;//无价值点
  167.                         }
  168.         }
  169.         private void AnalyseLevel(int chessColor)//水平方向上点位价值的判断
  170.         {
  171.             int x, y;
  172.             for (y = 0; y < 15; y++)//右方向上进攻的判断
  173.                 for (x = 0; x < 15; x++)
  174.                 {
  175.                     ValueTable[x, y, 1] = noChess;//棋型表全部清零
  176.                     if (!CheckIfIndexOutOfRange(x, y, 1, 1)) continue;
  177.                     if (13 == x && virtualGobangBoard[14, y] == chessColor && virtualGobangBoard[13, y] == noChess) ValueTable[13, y, 1] = (int)pattern.C1;//距离棋盘边1格位置的判断
  178.                     if (!CheckIfIndexOutOfRange(x, y, 2, 1)) continue;
  179.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x + 1, y])
  180.                     {
  181.                         if (noChess == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.H1;
  182.                         else if (-chessColor == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.C1;
  183.                         else
  184.                         {
  185.                             if (!CheckIfIndexOutOfRange(x, y, 3, 1)) continue;
  186.                             if (chessColor == virtualGobangBoard[x + 2, y] && noChess == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.H2;
  187.                             else if (chessColor == virtualGobangBoard[x + 2, y] && -chessColor == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.C2;
  188.                             else
  189.                             {
  190.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 1)) continue;
  191.                                 if (chessColor == virtualGobangBoard[x + 3, y] && noChess == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.H3;
  192.                                 else if (chessColor == virtualGobangBoard[x + 3, y] && -chessColor == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.C3;
  193.                                 else
  194.                                 {
  195.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 1)) continue;
  196.                                     if (chessColor == virtualGobangBoard[x + 4, y] && noChess == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.H4;
  197.                                     else if (chessColor == virtualGobangBoard[x + 4, y] && -chessColor == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.C4;
  198.                                 }
  199.                             }
  200.                         }
  201.                     }
  202.                 }
  203.             for (y = 0; y < 15; y++)//右方向上防守的判断
  204.                 for (x = 0; x < 15; x++)
  205.                 {
  206.                     if (!CheckIfIndexOutOfRange(x, y, 1, 1)) continue;
  207.                     if (13 == x && virtualGobangBoard[14, y] == -chessColor && virtualGobangBoard[13, y] == noChess) ValueTable[13, y, 1] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  208.                     if (!CheckIfIndexOutOfRange(x, y, 2, 1)) continue;
  209.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x + 1, y])
  210.                     {
  211.                         if (noChess == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.dH1;
  212.                         else if (chessColor == virtualGobangBoard[x + 2, y]) ValueTable[x, y, 1] = (int)pattern.dC1;
  213.                         else
  214.                         {
  215.                             if (!CheckIfIndexOutOfRange(x, y, 3, 1)) continue;
  216.                             if (-chessColor == virtualGobangBoard[x + 2, y] && noChess == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.dH2;
  217.                             else if (-chessColor == virtualGobangBoard[x + 2, y] && chessColor == virtualGobangBoard[x + 3, y]) ValueTable[x, y, 1] = (int)pattern.dC2;
  218.                             else
  219.                             {
  220.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 1)) continue;
  221.                                 if (-chessColor == virtualGobangBoard[x + 3, y] && noChess == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.dH3;
  222.                                 else if (-chessColor == virtualGobangBoard[x + 3, y] && chessColor == virtualGobangBoard[x + 4, y]) ValueTable[x, y, 1] = (int)pattern.dC3;
  223.                                 else
  224.                                 {
  225.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 1)) continue;
  226.                                     //if (-chessColor == virtualGobangBoard[x + 4, y] && noChess == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.dH4;
  227.                                     if (-chessColor == virtualGobangBoard[x + 4, y] && chessColor == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 1] = (int)pattern.dC4;
  228.                                 }
  229.                             }
  230.                         }
  231.                     }
  232.                 }
  233.             for (y = 0; y < 15; y++)//左方向上进攻的判断
  234.                 for (x = 14; x > -1; x--)
  235.                 {
  236.                     ValueTable[x, y, 5] = noChess;//棋型表全部清零
  237.                     if (!CheckIfIndexOutOfRange(x, y, 1, 5)) continue;
  238.                     if (1 == x && virtualGobangBoard[0, y] == chessColor && virtualGobangBoard[1, y] == noChess) ValueTable[1, y, 5] = (int)pattern.C1;//距离棋盘边1格位置的判断
  239.                     if (!CheckIfIndexOutOfRange(x, y, 2, 5)) continue;
  240.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x - 1, y])
  241.                     {
  242.                         if (noChess == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.H1;
  243.                         else if (-chessColor == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.C1;
  244.                         else
  245.                         {
  246.                             if (!CheckIfIndexOutOfRange(x, y, 3, 5)) continue;
  247.                             if (chessColor == virtualGobangBoard[x - 2, y] && noChess == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.H2;
  248.                             else if (chessColor == virtualGobangBoard[x - 2, y] && -chessColor == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.C2;
  249.                             else
  250.                             {
  251.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 5)) continue;
  252.                                 if (chessColor == virtualGobangBoard[x - 3, y] && noChess == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.H3;
  253.                                 else if (chessColor == virtualGobangBoard[x - 3, y] && -chessColor == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.C3;
  254.                                 else
  255.                                 {
  256.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 5)) continue;
  257.                                     if (chessColor == virtualGobangBoard[x - 4, y] && noChess == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.H4;
  258.                                     else if (chessColor == virtualGobangBoard[x - 4, y] && -chessColor == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.C4;
  259.                                 }
  260.                             }
  261.                         }
  262.                     }
  263.                 }
  264.             for (y = 0; y < 15; y++)//左方向上防守的判断
  265.                 for (x = 14; x > -1; x--)
  266.                 {
  267.                     if (!CheckIfIndexOutOfRange(x, y, 1, 5)) continue;
  268.                     if (1 == x && virtualGobangBoard[0, y] == -chessColor && virtualGobangBoard[1, y] == noChess) ValueTable[1, y, 5] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  269.                     if (!CheckIfIndexOutOfRange(x, y, 2, 5)) continue;
  270.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x - 1, y])
  271.                     {
  272.                         if (noChess == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.dH1;
  273.                         else if (chessColor == virtualGobangBoard[x - 2, y]) ValueTable[x, y, 5] = (int)pattern.dC1;
  274.                         else
  275.                         {
  276.                             if (!CheckIfIndexOutOfRange(x, y, 3, 5)) continue;
  277.                             if (-chessColor == virtualGobangBoard[x - 2, y] && noChess == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.dH2;
  278.                             else if (-chessColor == virtualGobangBoard[x - 2, y] && chessColor == virtualGobangBoard[x - 3, y]) ValueTable[x, y, 5] = (int)pattern.dC2;
  279.                             else
  280.                             {
  281.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 5)) continue;
  282.                                 if (-chessColor == virtualGobangBoard[x - 3, y] && noChess == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.dH3;
  283.                                 else if (-chessColor == virtualGobangBoard[x - 3, y] && chessColor == virtualGobangBoard[x - 4, y]) ValueTable[x, y, 5] = (int)pattern.dC3;
  284.                                 else
  285.                                 {
  286.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 5)) continue;
  287.                                     //if (-chessColor == virtualGobangBoard[x - 4, y] && noChess == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.dH4;
  288.                                     if (-chessColor == virtualGobangBoard[x - 4, y] && chessColor == virtualGobangBoard[x - 5, y]) ValueTable[x, y, 5] = (int)pattern.dC4;
  289.                                 }
  290.                             }
  291.                         }
  292.                     }
  293.                 }
  294.             //FileInput();
  295.         }
  296.         private void AnalyseUpright(int chessColor)//垂直方向上点位价值的判断
  297.         {
  298.             int x, y;
  299.             for (x = 0; x < 15; x++)//下方向上进攻的判断
  300.                 for (y = 0; y < 15; y++)
  301.                 {
  302.                     ValueTable[x, y, 3] = noChess;//棋型表全部清零
  303.                     if (!CheckIfIndexOutOfRange(x, y, 1, 3)) continue;
  304.                     if (13 == y && virtualGobangBoard[x, 14] == chessColor && virtualGobangBoard[x, 13] == noChess) ValueTable[x, 13, 3] = (int)pattern.C1;//距离棋盘边1格位置的判断
  305.                     if (!CheckIfIndexOutOfRange(x, y, 2, 3)) continue;
  306.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x , y+1])
  307.                     {
  308.                         if (noChess == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.H1;
  309.                         else if (-chessColor == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.C1;
  310.                         else
  311.                         {
  312.                             if (!CheckIfIndexOutOfRange(x, y, 3, 3)) continue;
  313.                             if (chessColor == virtualGobangBoard[x, y + 2] && noChess == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.H2;
  314.                             else if (chessColor == virtualGobangBoard[x, y + 2] && -chessColor == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.C2;
  315.                             else
  316.                             {
  317.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 3)) continue;
  318.                                 if (chessColor == virtualGobangBoard[x, y + 3] && noChess == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.H3;
  319.                                 else if (chessColor == virtualGobangBoard[x, y + 3] && -chessColor == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.C3;
  320.                                 else
  321.                                 {
  322.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 3)) continue;
  323.                                     if (chessColor == virtualGobangBoard[x, y + 4] && noChess == virtualGobangBoard[x, y + 5]) ValueTable[x, y, 3] = (int)pattern.H4;
  324.                                     else if (chessColor == virtualGobangBoard[x, y + 4] && -chessColor == virtualGobangBoard[x, y + 5]) ValueTable[x, y, 3] = (int)pattern.C4;
  325.                                 }
  326.                             }
  327.                         }
  328.                     }
  329.                 }
  330.             for (x = 0; x < 15; x++)//下方向上防守的判断
  331.                 for (y = 0; y < 15; y++)
  332.                 {
  333.                     if (!CheckIfIndexOutOfRange(x, y, 1, 3)) continue;
  334.                     if (13 == y && virtualGobangBoard[x, 14] == -chessColor && virtualGobangBoard[x, 13] == noChess) ValueTable[x, 13, 3] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  335.                     if (!CheckIfIndexOutOfRange(x, y, 2, 3)) continue;
  336.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x, y + 1])
  337.                     {
  338.                         if (noChess == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.dH1;
  339.                         else if (chessColor == virtualGobangBoard[x, y + 2]) ValueTable[x, y, 3] = (int)pattern.dC1;
  340.                         else
  341.                         {
  342.                             if (!CheckIfIndexOutOfRange(x, y, 3, 3)) continue;
  343.                             if (-chessColor == virtualGobangBoard[x, y + 2] && noChess == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.dH2;
  344.                             else if (-chessColor == virtualGobangBoard[x, y + 2] && chessColor == virtualGobangBoard[x, y + 3]) ValueTable[x, y, 3] = (int)pattern.dC2;
  345.                             else
  346.                             {
  347.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 3)) continue;
  348.                                 if (-chessColor == virtualGobangBoard[x, y + 3] && noChess == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.dH3;
  349.                                 else if (-chessColor == virtualGobangBoard[x, y + 3] && chessColor == virtualGobangBoard[x, y + 4]) ValueTable[x, y, 3] = (int)pattern.dC3;
  350.                                 else
  351.                                 {
  352.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 3)) continue;
  353.                                     //if (-chessColor == virtualGobangBoard[x + 4, y] && noChess == virtualGobangBoard[x + 5, y]) ValueTable[x, y, 3] = (int)pattern.dH4;
  354.                                     if (-chessColor == virtualGobangBoard[x, y + 4] && chessColor == virtualGobangBoard[x, y + 5]) ValueTable[x, y, 3] = (int)pattern.dC4;
  355.                                 }
  356.                             }
  357.                         }
  358.                     }
  359.                 }
  360.             for (x = 0; x < 15; x++)//上方向上进攻的判断
  361.                 for (y = 14; y > -1; y--)
  362.                 {
  363.                     ValueTable[x, y, 7] = noChess;//棋型表全部清零
  364.                     if (!CheckIfIndexOutOfRange(x, y, 1, 7)) continue;
  365.                     if (1 == y && virtualGobangBoard[x, 0] == chessColor && virtualGobangBoard[x, 1] == noChess) ValueTable[x, 1, 7] = (int)pattern.C1;//距离棋盘边1格位置的判断
  366.                     if (!CheckIfIndexOutOfRange(x, y, 2, 7)) continue;
  367.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x, y - 1])
  368.                     {
  369.                         if (noChess == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.H1;
  370.                         else if (-chessColor == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.C1;
  371.                         else
  372.                         {
  373.                             if (!CheckIfIndexOutOfRange(x, y, 3, 7)) continue;
  374.                             if (chessColor == virtualGobangBoard[x, y - 2] && noChess == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.H2;
  375.                             else if (chessColor == virtualGobangBoard[x, y - 2] && -chessColor == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.C2;
  376.                             else
  377.                             {
  378.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 7)) continue;
  379.                                 if (chessColor == virtualGobangBoard[x, y - 3] && noChess == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.H3;
  380.                                 else if (chessColor == virtualGobangBoard[x, y - 3] && -chessColor == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.C3;
  381.                                 else
  382.                                 {
  383.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 7)) continue;
  384.                                     if (chessColor == virtualGobangBoard[x, y - 4] && noChess == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.H4;
  385.                                     else if (chessColor == virtualGobangBoard[x, y - 4] && -chessColor == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.C4;
  386.                                 }
  387.                             }
  388.                         }
  389.                     }
  390.                 }
  391.             for (x = 0; x < 15; x++)//上方向上防守的判断
  392.                 for (y = 14; y > -1; y--)
  393.                 {
  394.                     if (!CheckIfIndexOutOfRange(x, y, 1, 7)) continue;
  395.                     if (1 == y && virtualGobangBoard[x, 0] == -chessColor && virtualGobangBoard[x, 1] == noChess) ValueTable[x, 1, 7] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  396.                     if (!CheckIfIndexOutOfRange(x, y, 2, 7)) continue;
  397.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x, y - 1])
  398.                     {
  399.                         if (noChess == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.dH1;
  400.                         else if (chessColor == virtualGobangBoard[x, y - 2]) ValueTable[x, y, 7] = (int)pattern.dC1;
  401.                         else
  402.                         {
  403.                             if (!CheckIfIndexOutOfRange(x, y, 3, 7)) continue;
  404.                             if (-chessColor == virtualGobangBoard[x, y - 2] && noChess == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.dH2;
  405.                             else if (-chessColor == virtualGobangBoard[x, y - 2] && chessColor == virtualGobangBoard[x, y - 3]) ValueTable[x, y, 7] = (int)pattern.dC2;
  406.                             else
  407.                             {
  408.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 7)) continue;
  409.                                 if (-chessColor == virtualGobangBoard[x, y - 3] && noChess == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.dH3;
  410.                                 else if (-chessColor == virtualGobangBoard[x, y - 3] && chessColor == virtualGobangBoard[x, y - 4]) ValueTable[x, y, 7] = (int)pattern.dC3;
  411.                                 else
  412.                                 {
  413.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 7)) continue;
  414.                                     //if (-chessColor == virtualGobangBoard[x , y- 4] && noChess == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.dH4;
  415.                                     if (-chessColor == virtualGobangBoard[x, y - 4] && chessColor == virtualGobangBoard[x, y - 5]) ValueTable[x, y, 7] = (int)pattern.dC4;
  416.                                 }
  417.                             }
  418.                         }
  419.                     }
  420.                 }
  421.             //FileInput();
  422.         }
  423.         private void AnalyseLeftDownToRightUp(int chessColor)//左下角至右上角的对角线上点位价值的判断
  424.         {
  425.             int x, y, z;
  426.             for (z = 0; z < 29; z++)//右上方向进攻的判断
  427.                 for (x = Math.Max(0, z - 14), y = Math.Min(z, 14); x <= Math.Min(z, 14) ; x++, y--)
  428.                 {
  429.                     ValueTable[x, y, 8] = noChess;//棋型表全部清零
  430.                     if (!CheckIfIndexOutOfRange(x, y, 1, 8)) continue;
  431.                     if (1==Math.Min(14-x,y) && virtualGobangBoard[x+1, y-1] == chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 8] = (int)pattern.C1;//距离棋盘边1格位置的判断
  432.                     if (!CheckIfIndexOutOfRange(x, y, 2, 8)) continue;
  433.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x + 1, y-1])
  434.                     {
  435.                         if (noChess == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.H1;
  436.                         else if (-chessColor == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.C1;
  437.                         else
  438.                         {
  439.                             if (!CheckIfIndexOutOfRange(x, y, 3, 8)) continue;
  440.                             if (chessColor == virtualGobangBoard[x + 2, y-2] && noChess == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.H2;
  441.                             else if (chessColor == virtualGobangBoard[x + 2, y-2] && -chessColor == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.C2;
  442.                             else
  443.                             {
  444.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 8)) continue;
  445.                                 if (chessColor == virtualGobangBoard[x + 3, y-3] && noChess == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.H3;
  446.                                 else if (chessColor == virtualGobangBoard[x + 3, y-3] && -chessColor == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.C3;
  447.                                 else
  448.                                 {
  449.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 8)) continue;
  450.                                     if (chessColor == virtualGobangBoard[x + 4, y-4] && noChess == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.H4;
  451.                                     else if (chessColor == virtualGobangBoard[x + 4, y-4] && -chessColor == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.C4;
  452.                                 }
  453.                             }
  454.                         }
  455.                     }
  456.                 }
  457.             for (z = 0; z < 29; z++)//右上方向防守的判断
  458.                 for (x = Math.Max(0, z - 14), y = Math.Min(z, 14); x <= Math.Min(z, 14); x++, y--)
  459.                 {
  460.                     if (!CheckIfIndexOutOfRange(x, y, 1, 8)) continue;
  461.                     if (1 == Math.Min(14 - x, y) && virtualGobangBoard[x+1, y-1] == -chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 8] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  462.                     if (!CheckIfIndexOutOfRange(x, y, 2, 8)) continue;
  463.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x + 1, y-1])
  464.                     {
  465.                         if (noChess == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.dH1;
  466.                         else if (chessColor == virtualGobangBoard[x + 2, y-2]) ValueTable[x, y, 8] = (int)pattern.dC1;
  467.                         else
  468.                         {
  469.                             if (!CheckIfIndexOutOfRange(x, y, 3, 8)) continue;
  470.                             if (-chessColor == virtualGobangBoard[x + 2, y-2] && noChess == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.dH2;
  471.                             else if (-chessColor == virtualGobangBoard[x + 2, y-2] && chessColor == virtualGobangBoard[x + 3, y-3]) ValueTable[x, y, 8] = (int)pattern.dC2;
  472.                             else
  473.                             {
  474.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 8)) continue;
  475.                                 if (-chessColor == virtualGobangBoard[x + 3, y-3] && noChess == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.dH3;
  476.                                 else if (-chessColor == virtualGobangBoard[x + 3, y-3] && chessColor == virtualGobangBoard[x + 4, y-4]) ValueTable[x, y, 8] = (int)pattern.dC3;
  477.                                 else
  478.                                 {
  479.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 8)) continue;
  480.                                     //if (-chessColor == virtualGobangBoard[x + 4, y-4] && noChess == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.dH4;
  481.                                     if (-chessColor == virtualGobangBoard[x + 4, y-4] && chessColor == virtualGobangBoard[x + 5, y-5]) ValueTable[x, y, 8] = (int)pattern.dC4;
  482.                                 }
  483.                             }
  484.                         }
  485.                     }
  486.                 }
  487.             for (z = 0; z < 29; z++)//左下方向进攻的判断
  488.                 for (x = Math.Min(z, 14), y = Math.Max(z - 14, 0); x >= Math.Max(z - 14, 0); x--, y++)
  489.                 {
  490.                     ValueTable[x, y, 4] = noChess;//棋型表全部清零
  491.                     if (!CheckIfIndexOutOfRange(x, y, 1, 4)) continue;
  492.                     if (1 == 14-Math.Max(14 - x, y) && virtualGobangBoard[x - 1, y + 1] == chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 4] = (int)pattern.C1;//距离棋盘边1格位置的判断
  493.                     if (!CheckIfIndexOutOfRange(x, y, 2, 4)) continue;
  494.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x - 1, y + 1])
  495.                     {
  496.                         if (noChess == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.H1;
  497.                         else if (-chessColor == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.C1;
  498.                         else
  499.                         {
  500.                             if (!CheckIfIndexOutOfRange(x, y, 3, 4)) continue;
  501.                             if (chessColor == virtualGobangBoard[x - 2, y + 2] && noChess == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.H2;
  502.                             else if (chessColor == virtualGobangBoard[x - 2, y + 2] && -chessColor == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.C2;
  503.                             else
  504.                             {
  505.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 4)) continue;
  506.                                 if (chessColor == virtualGobangBoard[x - 3, y + 3] && noChess == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.H3;
  507.                                 else if (chessColor == virtualGobangBoard[x - 3, y +3] && -chessColor == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.C3;
  508.                                 else
  509.                                 {
  510.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 4)) continue;
  511.                                     if (chessColor == virtualGobangBoard[x - 4, y + 4] && noChess == virtualGobangBoard[x - 5, y + 5]) ValueTable[x, y, 4] = (int)pattern.H4;
  512.                                     else if (chessColor == virtualGobangBoard[x - 4, y + 4] && -chessColor == virtualGobangBoard[x - 5, y + 5]) ValueTable[x, y, 4] = (int)pattern.C4;
  513.                                 }
  514.                             }
  515.                         }
  516.                     }
  517.                 }
  518.             for (z = 0; z < 29; z++)//左下方向防守的判断
  519.                 for (x = Math.Min(z, 14), y = Math.Max(z - 14, 0); x>=Math.Max(z - 14, 0); x--, y++)
  520.                 {
  521.                     if (!CheckIfIndexOutOfRange(x, y, 1, 4)) continue;
  522.                     if (1 ==14- Math.Max(14 - x, y) && virtualGobangBoard[x - 1, y + 1] == -chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 4] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  523.                     if (!CheckIfIndexOutOfRange(x, y, 2, 4)) continue;
  524.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x - 1, y + 1])
  525.                     {
  526.                         if (noChess == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.dH1;
  527.                         else if (chessColor == virtualGobangBoard[x - 2, y + 2]) ValueTable[x, y, 4] = (int)pattern.dC1;
  528.                         else
  529.                         {
  530.                             if (!CheckIfIndexOutOfRange(x, y, 3, 4)) continue;
  531.                             if (-chessColor == virtualGobangBoard[x - 2, y + 2] && noChess == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.dH2;
  532.                             else if (-chessColor == virtualGobangBoard[x - 2, y + 2] && chessColor == virtualGobangBoard[x - 3, y + 3]) ValueTable[x, y, 4] = (int)pattern.dC2;
  533.                             else
  534.                             {
  535.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 4)) continue;
  536.                                 if (-chessColor == virtualGobangBoard[x - 3, y + 3] && noChess == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.dH3;
  537.                                 else if (-chessColor == virtualGobangBoard[x - 3, y + 3] && chessColor == virtualGobangBoard[x - 4, y + 4]) ValueTable[x, y, 4] = (int)pattern.dC3;
  538.                                 else
  539.                                 {
  540.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 4)) continue;
  541.                                     //if (-chessColor == virtualGobangBoard[x - 4, y+4] && noChess == virtualGobangBoard[x - 5, y-5]) ValueTable[x, y, 4] = (int)pattern.dH4;
  542.                                     if (-chessColor == virtualGobangBoard[x - 4, y + 4] && chessColor == virtualGobangBoard[x - 5, y + 5]) ValueTable[x, y, 4] = (int)pattern.dC4;
  543.                                 }
  544.                             }
  545.                         }
  546.                     }
  547.                 }
  548.             //FileInput();
  549.         }
  550.         private void AnalyseRightDownToLeftUp(int chessColor)//右下角至左上角的对角线上点点位价值的判断
  551.         {
  552.             int x, y, z;
  553.             for (z = 0; z < 29; z++)
  554.                 for (x = Math.Max(0, z - 14), y = Math.Max(14 - z, 0);x+y<=Math.Min(z,28-z)+14 ;x++, y++ )//右下方向进攻的判断
  555.                 {
  556.                     ValueTable[x, y, 2] = noChess;//棋型表全部清零
  557.                     if (!CheckIfIndexOutOfRange(x, y, 1, 2)) continue;
  558.                     if (1 == Math.Min(14 - x,14 - y) && virtualGobangBoard[x + 1, y + 1] == chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 2] = (int)pattern.C1;//距离棋盘边1格位置的判断
  559.                     if (!CheckIfIndexOutOfRange(x, y, 2, 2)) continue;
  560.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x + 1, y + 1])
  561.                     {
  562.                         if (noChess == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.H1;
  563.                         else if (-chessColor == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.C1;
  564.                         else
  565.                         {
  566.                             if (!CheckIfIndexOutOfRange(x, y, 3, 2)) continue;
  567.                             if (chessColor == virtualGobangBoard[x + 2, y + 2] && noChess == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.H2;
  568.                             else if (chessColor == virtualGobangBoard[x + 2, y + 2] && -chessColor == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.C2;
  569.                             else
  570.                             {
  571.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 2)) continue;
  572.                                 if (chessColor == virtualGobangBoard[x + 3, y + 3] && noChess == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.H3;
  573.                                 else if (chessColor == virtualGobangBoard[x + 3, y + 3] && -chessColor == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.C3;
  574.                                 else
  575.                                 {
  576.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 2)) continue;
  577.                                     if (chessColor == virtualGobangBoard[x + 4, y + 4] && noChess == virtualGobangBoard[x + 5, y + 5]) ValueTable[x, y, 2] = (int)pattern.H4;
  578.                                     else if (chessColor == virtualGobangBoard[x + 4, y + 4] && -chessColor == virtualGobangBoard[x + 5, y + 5]) ValueTable[x, y, 2] = (int)pattern.C4;
  579.                                 }
  580.                             }
  581.                         }
  582.                     }
  583.                 }
  584.             for (z = 0; z < 29; z++)
  585.                 for (x = Math.Max(0, z - 14), y = Math.Max(14 - z, 0); x + y <= Math.Min(z, 28 - z) + 14; x++, y++)//右下方向防守的判断
  586.                 {
  587.                     if (!CheckIfIndexOutOfRange(x, y, 1, 2)) continue;
  588.                     if (1 == Math.Min(14 - x, 14 - y) && virtualGobangBoard[x + 1, y + 1] == -chessColor && virtualGobangBoard[x, y] == noChess) ValueTable[x, y, 2] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  589.                     if (!CheckIfIndexOutOfRange(x, y, 2, 2)) continue;
  590.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x + 1, y + 1])
  591.                     {
  592.                         if (noChess == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.dH1;
  593.                         else if (chessColor == virtualGobangBoard[x + 2, y + 2]) ValueTable[x, y, 2] = (int)pattern.dC1;
  594.                         else
  595.                         {
  596.                             if (!CheckIfIndexOutOfRange(x, y, 3, 2)) continue;
  597.                             if (-chessColor == virtualGobangBoard[x + 2, y + 2] && noChess == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.dH2;
  598.                             else if (-chessColor == virtualGobangBoard[x + 2, y + 2] && chessColor == virtualGobangBoard[x + 3, y + 3]) ValueTable[x, y, 2] = (int)pattern.dC2;
  599.                             else
  600.                             {
  601.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 2)) continue;
  602.                                 if (-chessColor == virtualGobangBoard[x + 3, y + 3] && noChess == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.dH3;
  603.                                 else if (-chessColor == virtualGobangBoard[x + 3, y + 3] && chessColor == virtualGobangBoard[x + 4, y + 4]) ValueTable[x, y, 2] = (int)pattern.dC3;
  604.                                 else
  605.                                 {
  606.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 2)) continue;
  607.                                     //if (-chessColor == virtualGobangBoard[x + 4, y+4] && noChess == virtualGobangBoard[x + 5, y+5]) ValueTable[x, y, 2] = (int)pattern.dH4;
  608.                                     if (-chessColor == virtualGobangBoard[x + 4, y + 4] && chessColor == virtualGobangBoard[x + 5, y + 5]) ValueTable[x, y, 2] = (int)pattern.dC4;
  609.                                 }
  610.                             }
  611.                         }
  612.                     }
  613.                 }
  614.             for (z = 0; z < 29; z++)
  615.                 for (x = Math.Min(z,14), y = Math.Min(28-z,14); x+y>=Math.Abs(14-z); x--, y--)//左上方向进攻的判断
  616.                 {
  617.                    ValueTable[x, y, 6] = noChess;//棋型表全部清零
  618.                     if (!CheckIfIndexOutOfRange(x, y, 1, 6)) continue;
  619.                     if (1 == Math.Min( x,  y) && virtualGobangBoard[x - 1, y - 1] == chessColor && virtualGobangBoard[x, y] == noChess)ValueTable[x, y, 6] = (int)pattern.C1;//距离棋盘边1格位置的判断
  620.                     if (!CheckIfIndexOutOfRange(x, y, 2, 6)) continue;
  621.                     if (noChess == virtualGobangBoard[x, y] && chessColor == virtualGobangBoard[x - 1, y - 1])
  622.                     {
  623.                         if (noChess == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.H1;
  624.                         else if (-chessColor == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.C1;
  625.                         else
  626.                         {
  627.                             if (!CheckIfIndexOutOfRange(x, y, 3, 6)) continue;
  628.                             if (chessColor == virtualGobangBoard[x - 2, y - 2] && noChess == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.H2;
  629.                             else if (chessColor == virtualGobangBoard[x - 2, y - 2] && -chessColor == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.C2;
  630.                             else
  631.                             {
  632.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 6)) continue;
  633.                                 if (chessColor == virtualGobangBoard[x - 3, y - 3] && noChess == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.H3;
  634.                                 else if (chessColor == virtualGobangBoard[x - 3, y - 3] && -chessColor == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.C3;
  635.                                 else
  636.                                 {
  637.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 6)) continue;
  638.                                     if (chessColor == virtualGobangBoard[x - 4, y - 4] && noChess == virtualGobangBoard[x - 5, y - 5])ValueTable[x, y, 6] = (int)pattern.H4;
  639.                                     else if (chessColor == virtualGobangBoard[x - 4, y - 4] && -chessColor == virtualGobangBoard[x - 5, y - 5])ValueTable[x, y, 6] = (int)pattern.C4;
  640.                                 }
  641.                             }
  642.                         }
  643.                     }
  644.                 }
  645.             for (z = 0; z < 29; z++)
  646.                 for (x = Math.Min(z, 14), y = Math.Min(28 - z, 14); x + y >= Math.Abs(14 - z); x--, y--)//左上方向防守的判断
  647.                 {
  648.                     if (!CheckIfIndexOutOfRange(x, y, 1, 6)) continue;
  649.                     if (1 ==Math.Min(x, y) && virtualGobangBoard[x - 1, y - 1] == -chessColor && virtualGobangBoard[x, y] == noChess)ValueTable[x, y, 6] = (int)pattern.dC1;//距离棋盘边1格位置的判断
  650.                     if (!CheckIfIndexOutOfRange(x, y, 2, 6)) continue;
  651.                     if (noChess == virtualGobangBoard[x, y] && -chessColor == virtualGobangBoard[x - 1, y - 1])
  652.                     {
  653.                         if (noChess == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.dH1;
  654.                         else if (chessColor == virtualGobangBoard[x - 2, y - 2])ValueTable[x, y, 6] = (int)pattern.dC1;
  655.                         else
  656.                         {
  657.                             if (!CheckIfIndexOutOfRange(x, y, 3, 6)) continue;
  658.                             if (-chessColor == virtualGobangBoard[x - 2, y - 2] && noChess == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.dH2;
  659.                             else if (-chessColor == virtualGobangBoard[x - 2, y - 2] && chessColor == virtualGobangBoard[x - 3, y - 3])ValueTable[x, y, 6] = (int)pattern.dC2;
  660.                             else
  661.                             {
  662.                                 if (!CheckIfIndexOutOfRange(x, y, 4, 6)) continue;
  663.                                 if (-chessColor == virtualGobangBoard[x - 3, y - 3] && noChess == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.dH3;
  664.                                 else if (-chessColor == virtualGobangBoard[x - 3, y - 3] && chessColor == virtualGobangBoard[x - 4, y - 4])ValueTable[x, y, 6] = (int)pattern.dC3;
  665.                                 else
  666.                                 {
  667.                                     if (!CheckIfIndexOutOfRange(x, y, 5, 6)) continue;
  668.                                     //if (-chessColor == virtualGobangBoard[x - 4, y-4] && noChess == virtualGobangBoard[x - 5, y-5])ValueTable[x, y, 6] = (int)pattern.dH4;
  669.                                     if (-chessColor == virtualGobangBoard[x - 4, y - 4] && chessColor == virtualGobangBoard[x - 5, y - 5])ValueTable[x, y, 6] = (int)pattern.dC4;
  670.                                 }
  671.                             }
  672.                         }
  673.                     }
  674.                 }
  675.            // FileInput();
  676.         }
  677.         private void FileInput()//调试算法所用
  678.         {
  679.         //    int x, y;
  680.         //    StreamWriter right = new StreamWriter("Right.txt", true);
  681.         //    StreamWriter left = new StreamWriter("Left.txt", true);
  682.         //    StreamWriter last = new StreamWriter("Last.txt", true);
  683.         //    StreamWriter up = new StreamWriter("Up.txt", true);
  684.         //    StreamWriter down = new StreamWriter("Down.txt", true);
  685.         //    StreamWriter RightUp = new StreamWriter("RightUp.txt",true);
  686.         //    StreamWriter LeftDown = new StreamWriter("LeftDown.txt", true);
  687.         //    StreamWriter RightDown = new StreamWriter("RightDown.txt", true);
  688.         //    StreamWriter LeftUp = new StreamWriter("LeftUp.txt", true);
  689.         //    for (y = 0; y < 15; y++)
  690.         //    {
  691.         //        for (x = 0; x < 15; x++)
  692.         //        {
  693.         //            right.Write("{0,3}", ValueTable[x, y, 1]);
  694.         //        }
  695.         //        right.Write("/r/n");
  696.         //    }
  697.         //    right.Write("/r/n");
  698.         //    right.Flush();
  699.         //    right.Close();
  700.         //    for (y = 0; y < 15; y++)
  701.         //    {
  702.         //        for (x = 0; x < 15; x++)
  703.         //        {
  704.         //            left.Write("{0,3}", ValueTable[x, y, 5]);
  705.         //        }
  706.         //        left.Write("/r/n");
  707.         //    }
  708.         //    left.Write("/r/n");
  709.         //    left.Flush();
  710.         //    left.Close();
  711.         //    for (y = 0; y < 15; y++)
  712.         //    {
  713.         //        for (x = 0; x < 15; x++)
  714.         //        {
  715.         //            last.Write("{0,3}", LastValueTable[x, y, 1]);
  716.         //        }
  717.         //        last.Write("/r/n");
  718.         //    }
  719.         //    last.Write("/r/n");
  720.         //    last.Flush();
  721.         //    last.Close();
  722.         //    for (y = 0; y < 15; y++)
  723.         //    {
  724.         //        for (x = 0; x < 15; x++)
  725.         //        {
  726.         //            up.Write("{0,3}", ValueTable[x, y, 7]);
  727.         //        }
  728.         //        up.Write("/r/n");
  729.         //    }
  730.         //    up.Write("/r/n");
  731.         //    up.Flush();
  732.         //    up.Close();
  733.         //    for (y = 0; y < 15; y++)
  734.         //    {
  735.         //        for (x = 0; x < 15; x++)
  736.         //        {
  737.         //            down.Write("{0,3}", ValueTable[x, y, 3]);
  738.         //        }
  739.         //        down.Write("/r/n");
  740.         //    }
  741.         //    down.Write("/r/n");
  742.         //    down.Flush();
  743.         //    down.Close();
  744.         //    for (y = 0; y < 15; y++)
  745.         //    {
  746.         //        for (x = 0; x < 15; x++)
  747.         //        {
  748.         //            RightUp.Write("{0,3}", ValueTable[x, y, 8]);
  749.         //        }
  750.         //        RightUp.Write("/r/n");
  751.         //    }
  752.         //    RightUp.Write("/r/n");
  753.         //    RightUp.Flush();
  754.         //    RightUp.Close();
  755.         //    for (y = 0; y < 15; y++)
  756.         //    {
  757.         //        for (x = 0; x < 15; x++)
  758.         //        {
  759.         //            LeftDown.Write("{0,3}", ValueTable[x, y, 4]);
  760.         //        }
  761.         //        LeftDown.Write("/r/n");
  762.         //    }
  763.         //    LeftDown.Write("/r/n");
  764.         //    LeftDown.Flush();
  765.         //    LeftDown.Close();
  766.         //    for (y = 0; y < 15; y++)
  767.         //    {
  768.         //        for (x = 0; x < 15; x++)
  769.         //        {
  770.         //            RightDown.Write("{0,3}", ValueTable[x, y, 2]);
  771.         //        }
  772.         //        RightDown.Write("/r/n");
  773.         //    }
  774.         //    RightDown.Write("/r/n");
  775.         //    RightDown.Flush();
  776.         //    RightDown.Close();
  777.         //    for (y = 0; y < 15; y++)
  778.         //    {
  779.         //        for (x = 0; x < 15; x++)
  780.         //        {
  781.         //            LeftUp.Write("{0,3}", ValueTable[x, y, 6]);
  782.         //        }
  783.         //        LeftUp.Write("/r/n");
  784.         //    }
  785.         //    LeftUp.Write("/r/n");
  786.         //    LeftUp.Flush();
  787.         //    LeftUp.Close();
  788.             int x, y;
  789.             StreamWriter Last = new StreamWriter("Last.txt"true);
  790.             for (y = 0; y < 15; y++)
  791.             {
  792.                 for (x = 0; x < 15; x++)
  793.                 {
  794.                     Last.Write("{0,7}", LastValueTable[x, y, 0]);
  795.                 }
  796.                 Last.Write("/r/n");
  797.             }
  798.             Last.Write("/r/n");
  799.             Last.Flush();
  800.             Last.Close();
  801.         }
  802.         private void SearchTheBestPisition()//寻找水平,垂直,两条对角线分值相加的最大值的点,赋予LastValueTable[x,y,0]
  803.         {
  804.             int i, j, z, max=0;
  805.             MVP.X = -1;
  806.             MVP.Y = -1;
  807.             for (i = 0; i < 15; i++)
  808.                 for (j = 0; j < 15; j++)
  809.                 {
  810.                     LastValueTable[i, j, 0] = 0;//清零
  811.                     for (z = 1; z <= 4; z++)
  812.                     {
  813.                         LastValueTable[i, j, 0] += LastValueTable[i, j, z];
  814.                         if (LastValueTable[i, j, 0] >= max)
  815.                         {
  816.                             max = LastValueTable[i, j, 0];
  817.                             MVP.X = i;
  818.                             MVP.Y = j;
  819.                         }
  820.                     }
  821.                 }
  822.            // FileInput();
  823.         }
  824.         private void ComputerMoveChess(int chessColor)//电脑移动棋子
  825.         {
  826.             AnalyseLevel(chessColor);
  827.             AnalyseUpright(chessColor);
  828.             AnalyseLeftDownToRightUp(chessColor);
  829.             AnalyseRightDownToLeftUp(chessColor);
  830.             AnalyseOneLine();
  831.             SearchTheBestPisition();
  832.             label1.Text = MVP.X.ToString();
  833.             label2.Text = MVP.Y.ToString();
  834.             MoveGobang(MVP.X, MVP.Y , chessColor);
  835.             CheckIfWin(MVP.X, MVP.Y, chessColor);
  836.         }
  837.         private void CheckIfWin(int x, int y, int chessColor)//检查是否五子一线成胜局
  838.         {
  839.             int i, max;
  840.             int up = -1, down = -1, right = -1, left = -1, upRight = -1, downRight = -1, upLeft = -1, downLeft = -1;
  841.             for (i = 0; i <= Math.Min(5, y); i++)
  842.             {
  843.                 if (chessColor == virtualGobangBoard[x, y - i]) up++;
  844.                 if (chessColor != virtualGobangBoard[x, y - i]) break;
  845.             }
  846.             for (i = 0; i <= Math.Min(5, 14 - y); i++)
  847.             {
  848.                 if (chessColor == virtualGobangBoard[x, y + i]) down++;
  849.                 if (chessColor != virtualGobangBoard[x, y + i]) break;
  850.             }
  851.             for (i = 0; i <= Math.Min(5, 14 - x); i++)
  852.             {
  853.                 if (chessColor == virtualGobangBoard[x + i, y]) right++;
  854.                 if (chessColor != virtualGobangBoard[x + i, y]) break;
  855.             }
  856.             for (i = 0; i <= Math.Min(5, x); i++)
  857.             {
  858.                 if (chessColor == virtualGobangBoard[x - i, y]) left++;
  859.                 if (chessColor != virtualGobangBoard[x - i, y]) break;
  860.             }
  861.             for (i = 0; i <= Math.Min(Math.Min(14 - x, y), 5); i++)
  862.             {
  863.                 if (chessColor == virtualGobangBoard[x + i, y - i]) upRight++;
  864.                 if (chessColor != virtualGobangBoard[x + i, y - i]) break;
  865.             }
  866.             for (i = 0; i <= Math.Min(Math.Min(x, y), 5); i++)
  867.             {
  868.                 if (chessColor == virtualGobangBoard[x - i, y - i]) upLeft++;
  869.                 if (chessColor != virtualGobangBoard[x - i, y - i]) break;
  870.             }
  871.             for (i = 0; i <= Math.Min(Math.Min(14 - x, 14 - y), 5); i++)
  872.             {
  873.                 if (chessColor == virtualGobangBoard[x + i, y + i]) downRight++;
  874.                 if (chessColor != virtualGobangBoard[x + i, y + i]) break;
  875.             }
  876.             for (i = 0; i <= Math.Min(Math.Min(x, 14 - y), 5); i++)
  877.             {
  878.                 if (chessColor == virtualGobangBoard[x - i, y + i]) downLeft++;
  879.                 if (chessColor != virtualGobangBoard[x - i, y + i]) break;
  880.             }
  881.             max = Math.Max(Math.Max((up + down), (upRight + downLeft)), Math.Max((left + right), (downRight + upLeft)));
  882.             if (4 <= max)
  883.             {
  884.                 switch (chessColor)
  885.                 {
  886.                     case -1:
  887.                         MessageBox.Show("黑棋赢了!");
  888.                         break;
  889.                     case 1:
  890.                         MessageBox.Show("白棋赢了!");
  891.                         break;
  892.                     default:
  893.                         MessageBox.Show("System Error.");
  894.                         break;
  895.                 }
  896.             }
  897.         }
  898.         private void GobangGroupBox_MouseClick(object sender, MouseEventArgs e)//鼠标点击事件
  899.         {
  900.             int x, y;
  901.             x = (e.X - 10) / 40;
  902.             y = (e.Y - 10) / 40;
  903.             MoveGobang(x, y, playerChessColor);
  904.             CheckIfWin(x, y, playerChessColor);
  905.             ComputerMoveChess(-playerChessColor);
  906.         }
  907.         private void MoveGobang(int x, int y, int chessColor)//将棋子放在棋盘上
  908.         {
  909.             if (-1 == chessColor) chessPicutureBox[x, y].BackgroundImage = global::MyGobang.Properties.Resources.blackstone;
  910.             else chessPicutureBox[x, y].BackgroundImage = global::MyGobang.Properties.Resources.whitestone;
  911.             chessPicutureBox[x, y].Visible = true;
  912.             virtualGobangBoard[x, y] = chessColor;
  913.         }
  914.         private void GobangGroupBox_MouseMove(object sender, MouseEventArgs e)//鼠标在棋盘上移动时显示红色小方框
  915.         {
  916.             int x = -1, y = -1;
  917.             Graphics gr = GobangGroupBox.CreateGraphics();
  918.             Pen myPen = new Pen(Color.Red, 1);
  919.             Pen lastPen = new Pen(Color.BurlyWood, 1);
  920.             if (lastMovePoint.X != -1)
  921.             {
  922.                 gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y - 18, lastMovePoint.X - 8, lastMovePoint.Y - 18);
  923.                 gr.DrawLine(lastPen, lastMovePoint.X + 8, lastMovePoint.Y - 18, lastMovePoint.X + 18, lastMovePoint.Y - 18);
  924.                 gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y + 18, lastMovePoint.X - 8, lastMovePoint.Y + 18);
  925.                 gr.DrawLine(lastPen, lastMovePoint.X + 8, lastMovePoint.Y + 18, lastMovePoint.X + 18, lastMovePoint.Y + 18);
  926.                 gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y - 18, lastMovePoint.X - 18, lastMovePoint.Y - 8);
  927.                 gr.DrawLine(lastPen, lastMovePoint.X - 18, lastMovePoint.Y + 8, lastMovePoint.X - 18, lastMovePoint.Y + 18);
  928.                 gr.DrawLine(lastPen, lastMovePoint.X + 18, lastMovePoint.Y - 18, lastMovePoint.X + 18, lastMovePoint.Y - 8);
  929.                 gr.DrawLine(lastPen, lastMovePoint.X + 18, lastMovePoint.Y + 8, lastMovePoint.X + 18, lastMovePoint.Y + 18);
  930.             }
  931.             if (e.X > 10 && e.X < 600 && e.Y > 10 && e.Y < 600)
  932.             {
  933.                 x = (e.X - 10) / 40 * 40 + 30;
  934.                 y = (e.Y - 10) / 40 * 40 + 30;
  935.                 gr.DrawLine(myPen, x - 18, y - 18, x - 8, y - 18);
  936.                 gr.DrawLine(myPen, x + 8, y - 18, x + 18, y - 18);
  937.                 gr.DrawLine(myPen, x - 18, y + 18, x - 8, y + 18);
  938.                 gr.DrawLine(myPen, x + 8, y + 18, x + 18, y + 18);
  939.                 gr.DrawLine(myPen, x - 18, y - 18, x - 18, y - 8);
  940.                 gr.DrawLine(myPen, x - 18, y + 8, x - 18, y + 18);
  941.                 gr.DrawLine(myPen, x + 18, y - 18, x + 18, y - 8);
  942.                 gr.DrawLine(myPen, x + 18, y + 8, x + 18, y + 18);
  943.                 lastMovePoint.X = x;
  944.                 lastMovePoint.Y = y;
  945.             }
  946.         } 
  947.     }
  948. }

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值