Computer.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:4k
源码类别:
C#编程
开发平台:
Others
- using System;
- namespace FiveStones
- {
- /// <summary>
- /// Computer 的摘要说明。
- /// 电脑类。计算电脑下棋的位置
- /// </summary>
- public class Computer
- {
- private bool mflag; //区别电脑是下黑棋还是下白棋
- private int x; //电脑下子的位置
- private int y; //电脑下子的位置
- public Computer(bool flag)
- {
- mflag = flag;
- }
- public int X
- {
- get
- {
- return x;
- }
- }
- public int Y
- {
- get
- {
- return y;
- }
- }
- /// <summary>
- /// 电脑下棋
- /// </summary>
- /// <param name="arrchessboard"></param>
- public void Down(int[,] arrchessboard)
- {
- //权值数组
- int [,] qz = new int[15,15];
- //基本思路:先计算每个点的权值,在权值最高的位置下棋
- for (int i=0;i<15;i++)
- {
- for(int j=0;j<15;j++)
- {
- if (arrchessboard[i,j] < 2)
- {
- qz[i,j] = -1; //当已有子时标注为-1
- }
- else
- {
- qz[i,j] = Check(i,j,arrchessboard);
- }
- }
- }
- //找出权值最大的点
- MaxQZ(qz);
- //先不考虑智能问题,只管下棋的地方无子
- //Random r = new Random();
- //x = r.Next(0,14);
- //y = r.Next(0,14);
- }
- /// <summary>
- /// 找出权值最大点
- /// </summary>
- /// <param name="qz"></param>
- private void MaxQZ(int [,] qz)
- {
- int max = 0;
- for (int i=0;i<15;i++)
- {
- for(int j=0;j<15;j++)
- {
- if (qz[i,j]>max)
- {
- x = i;
- y = j;
- max = qz[i,j];
- }
- }
- }
- }
- /// <summary>
- /// 检查m,n点的权值
- /// </summary>
- /// <param name="m"></param>
- /// <param name="n"></param>
- /// <param name="arrchessboard"></param>
- /// <returns></returns>
- private int Check(int m, int n, int[,] arrchessboard)
- {
- int qz = 0;
- //找自己的取胜点(1000)
- int w1 = 100000;
- //找对手的取胜点(80)
- int w2 = 50000;
- //找自己的三个相连的点(60)
- int w3 = 10000;
- //找对手的三个相连的点(40)
- int w4 = 5000;
- //找自己的两个相连的点(20)
- int w5 = 1000;
- //找对手的两个相连的点(10)
- int w6 = 500;
- //找自己的相连的点(5)
- int w7 = 100;
- //找对方的相连的点(5)
- int w8 = 50;
- //找自己的失败点
- int w9 = -999999;
- int[] arrf = new int[4];
- //如果该位置下我方的子
- if (mflag)
- {
- //我方黑子
- arrchessboard[m,n] = 0;
- }
- else
- {
- //我方白子
- arrchessboard[m,n] = 1;
- }
- arrf[0] = Rule.Xnum(m,n,arrchessboard);
- arrf[1] = Rule.Ynum(m,n,arrchessboard);
- arrf[2] = Rule.YXnum(m,n,arrchessboard);
- arrf[3] = Rule.XYnum(m,n,arrchessboard);
- //中心点权值加1
- if (m==7 && n==7){qz+=1;}
- for (int i=0;i<4;i++)
- {
- if (Math.Abs(arrf[i]) == 5)
- {
- qz += w1;
- }
- if (arrf[i] == 4)
- {
- qz += w3;
- }
- if (arrf[i] == 3)
- {
- qz += w5;
- }
- if (arrf[i] == 2)
- {
- qz += w7;
- }
- //如果我方为黑棋,还要检查失败点
- if (mflag)
- {
- if (Rule.IsFail(arrf, arrchessboard[m,n])>0)
- {
- qz += w9;
- }
- }
- }
- //如果该位置下对方的子
- if (mflag)
- {
- //对方白子
- arrchessboard[m,n] = 1;
- }
- else
- {
- //对方黑子
- arrchessboard[m,n] = 0;
- }
- arrf[0] = Rule.Xnum(m,n,arrchessboard);
- arrf[1] = Rule.Ynum(m,n,arrchessboard);
- arrf[2] = Rule.YXnum(m,n,arrchessboard);
- arrf[3] = Rule.XYnum(m,n,arrchessboard);
- for (int i=0;i<4;i++)
- {
- if (Math.Abs(arrf[i]) == 5)
- {
- qz += w2;
- }
- if (arrf[i] == 4)
- {
- qz += w4;
- }
- if (arrf[i] == 3)
- {
- qz += w6;
- }
- if (arrf[i] == 2)
- {
- qz += w8;
- }
- }
- //数组好像是引用传递,探测完后恢复到默认值
- arrchessboard[m,n] = 2;
- return qz;
- }
- }
- }