五子棋实验报告(含代码)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验报告
实验一五子棋游戏
北方工业大学 2013级计算机技术米鹏一、实验原理及方法
五子棋游戏开发借用Visual Studio 2012软件开发平台,选用C#语言进行编写。
整体程序主要分为三部分:界面操作部分、AI逻辑部分和棋子定点分析部分。
1、界面操作部分
界面操作部分代码主要针对图像呈现、对应矩阵存储、下棋过程控制等可见的操作环节进编写。
同时负责整个程序的初始化工作。
图像呈现采用C#中Graphics进行绘制。
棋盘被划分为15行15列,每个划分出的小方格均为30*30的正方形,棋盘可操作的范围规定在(20,20)、(460,460)两点的确定的正方形区域内。
通过鼠标左击来确定下子地点。
程序会根据鼠标鼠标点击的位置进行计算,计算得到时对应矩阵的行列,之后再改变对应矩阵的内容后,在通过行列值乘以小方格边长计算得到在显示区域中的具体位置,再稍加变动后画到显示区域中。
以X点坐标为例,下面是计算X(Column)的流程图:
在对应矩阵存储方面,后面AI逻辑和棋子分析所用到的矩阵都是来源这里。
同时AI 逻辑和棋子分析不能去修改对应矩阵内容。
图像呈现点的位置、重绘的根据都是来源这里。
在下棋过程控制方面采用信号亮的机制,当操作者下过后,根据信号AI会立即计算将要下点的位置同时改变信号亮变量。
当AI下过棋子后,由于信号亮的的限制就等待操作者去下棋,同时改变信号亮变量内容。
AI和操作者的所有下子、修改矩阵、显示棋子的过程都是统一的。
在每一盘游戏开始时程序会对一些重要的变量进行初始化这里包括矩阵、信号亮、第一步棋子颜色、呈现图像等内容进行初始化。
同时AI会在棋盘中央下第一子。
2、AI逻辑部分
AI逻辑部分算是整个程序策略的灵魂。
其中的一些关键性判别的前后关系将影响AI 的下棋的结果。
同时加大和降低AI的难度也是这里。
下面是我设计的策略过程:
从下棋者的考虑角度进行考虑,尽可能保证每一次下子都是有必要的、都是在情理当中的。
我所设计的策略并不是完整,漏洞在与没有考虑三棋子连续的情况。
3、棋子定点分析部分
棋子定点分析部分是这个程序策略的支撑。
分析的正确与否直接影响AI下子是否真的有意义、是否真的可以达到所需目的。
这里的代码也是最复杂。
这里包括了检测是否输赢的五棋子连续的状态、四子状态(再补一子,五子连续)存在与否、每个棋子的上到下、左到右、左上到右下、右上到左下,四个方向上棋子排列情况和可落子情况,同时分析出落子情况的优先级。
对于优先级较高额在AI逻辑部分会优先选择下子。
下面列举在从左到右这个方向上可下子的区域情况流程图:
二、源程序清单:
1、界面操作部分代码:
public partial class Form1 : Form
{
bool isBlack = true;
bool IsAI = true;
int[,] bg = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } , { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; int change = 0;
public Form1(){
InitializeComponent();
this.Width = 497;
}
private void InitPanel(){
int index = 60;
Graphics gs = panel1.CreateGraphics();
Pen myPen = new Pen(Color.Black, 2);
gs.DrawLine(myPen, new Point(30, 30), new Point(30, 450));
gs.DrawLine(myPen, new Point(30, 30), new
Point(450, 30));
gs.DrawLine(myPen, new Point(450, 30), new
Point(450, 450));
gs.DrawLine(myPen, new Point(30, 450), new
Point(450, 450));
myPen.Width = 1;
while (index <= 420){
gs.DrawLine(myPen, new Point(30, index), new
Point(450, index));
gs.DrawLine(myPen, new Point(index, 30), new Point(index, 450));
index += 30;
}
SolidBrush bursh = new SolidBrush(Color.Black); gs.FillEllipse(bursh, 240 - 4, 240 - 4, 8, 8);
for (int i = 0; i < 15; i++){
for (int j = 0; j < 15; j++){
if (bg[i, j] == 1){
gs.DrawImage(Resources.Black, new Point((j + 1) * 30 - 10, (i + 1) * 30 - 10));
}
else if (bg[i, j] == -1){
gs.DrawImage(Resources.White, new Point((j + 1) * 30 - 10, (i + 1) * 30 - 10));
}
}
}
myPen.Dispose();
bursh.Dispose();
gs.Dispose();
gs = null;
bursh = null;
myPen = null;
}
private void PutOn(int row, int colume){
Image m=null;
if (bg[row - 1, colume - 1] == 0){
Graphics gs = panel1.CreateGraphics();
if (isBlack){
m=Resources.Black;
isBlack = false;
change = 1;
}
else{
m=Resources.White;
isBlack = true;
change = -1;
}
gs.DrawImage(m, new Point(colume * 30 - 10, row * 30 - 10));
bg[row - 1, colume - 1] = change;
m.Dispose();
gs.Dispose();
m = null;
gs = null;
IsFiveChessman chess = new sFiveChessman(bg);
if (chess.AllSelect()){
if (IsAI){
MessageBox.Show("AI 胜!");
}
else{
MessageBox.Show("你胜!");
}
}
else if (!IsAI){
IsAI = true;
AI myAI = new AI(bg, 1);
string str = myAI.AIMain();
myAI.DisArraylist();
PutOn(Convert.ToInt32(str.Split('#')[0]) + 1, Convert.ToInt32(str.Split('#')[1]) + 1);
}
}
}
private void Form1_Load(object sender, EventArgs e){
IsAI = true;
isBlack = true;
change = 0;
PutOn(8, 8);
}
private void panel1_Paint(object sender, PaintEventArgs e){
InitPanel();
}
private void panel1_MouseUp(object sender, MouseEventArgs e){
int row = -1;
int colume = -1;
int p = 0;
int q = 0;
if (e.X > 20 && e.X < 460 && e.Y > 20 && e.Y < 460 && e.Button == MouseButtons.Left){
p = e.X % 30;
q = e.X / 30;
if (q < 1 || q > 14){
if (q < 1){q = 1;}
else{q = 15;}
}
else{
if (p >= 15){q = q + 1;}
}
colume = q;
q = e.Y / 30;
p = e.Y % 30;
if (q < 1 || q > 14){
if (q < 1){q = 1;}
else{q = 15;}
}
else{
if (p > 15){q = q + 1;}
}
row = q;
IsAI = false;
this.PutOn(row, colume);
}
}
}
2、AI逻辑部分
class AI : IsFiveChessman{
protected int AIValue = 0;
protected int firstX = -1;
protected int firstY = -1;
ArrayList listAI = new ArrayList();
ArrayList listP = new ArrayList();
public AI(int[,] bg, int value){
base.BG = bg;
AIValue = value;
}
private void SetFirst(int x, int y, int current){
firstX = x;
firstY = y;
BG[x, y] = current;
}
private void BackFirst()
{
BG[firstX, firstY] = 0;
firstX = -1;
firstY = -1;
}
private ArrayList SelectCountOfSame(ArrayList list){ int i = 0;
string tempStr = "";
int tempCount = 0;
for (i = 0; i < list.Count; i++){
tempStr = list[i].ToString();
for (int j = 0; j < list.Count; j++){
if (tempStr == list[j].ToString()){
tempCount++;
}
}
tempStr += "#" + tempCount.ToString();
list[i] = tempStr;
tempCount = 0;
tempStr = "";
}
return list;
}
private ArrayList BetweenSame(ArrayList listA, ArrayList listB){
ArrayList arry = new ArrayList();
for (int i = 0; i < listA.Count; i++){
for (int j = 0; j < listB.Count; j++){
if (listA[i].ToString() == listB[j].ToString()){
if (arry.Contains(listA[i]) == false){
arry.Add(listA[i]);
break;
}
}
}
}
return arry;
}
private string Selects(ArrayList arry, int flag,int counts){ int tempCount = 0;
int i = 0;int j = 0;int k = 0;
int x = 0;int y = 0;
string tempStr = "";
if (arry.Count > 0){
tempStr = "";
tempCount = 0;
for (i = 0; i < arry.Count; i++){
k = 0;
SetFirst(Convert.ToInt32(arry[i].ToString().Split('#' )[1]), Convert.ToInt32(arry[i].ToString().Split('#')[2]), flag*AIValue);
tempStr += this.DirectionA(firstX, firstY,
flag*AIValue);
tempStr += this.DirectionB(firstX, firstY,
flag*AIValue);
tempStr += this.DirectionC(firstX, firstY,
flag*AIValue);
tempStr += this.DirectionD(firstX, firstY,
flag*AIValue);
for (j = 0; j < tempStr.Length; j++){
if (tempStr[j].ToString() == "T" ||
tempStr[j].ToString() == "F"){
k++;
}
}
if (k > tempCount){
x = firstX;
y = firstY;
tempCount = k;
}
BackFirst();
}
if (tempCount > counts){
return x.ToString() + "#" + y.ToString();
}
else{
Random rg = new Random();
i = rg.Next(0, arry.Count - 1);
return arry[i].ToString().Split('#')[1] + "#" + arry[i].ToString().Split('#')[2];
}
}
return "";
}
public string AIMain(){
int i = 0;int j = 0;int k = 0;
string allP = "";
string allAI = "";
string tempStr = "";
ArrayList arry = new ArrayList();
#region AI可放棋子位置allAI
for (i = 0; i < 15; i++){
for (j = 0; j < 15; j++){
if (BG[i, j] == AIValue){
allAI += this.DirectionA(i, j, AIValue);
allAI += this.DirectionB(i, j, AIValue);
allAI += this.DirectionC(i, j, AIValue);
allAI += this.DirectionD(i, j, AIValue);
}
}
}
#endregion
#region 人可放棋子位置allP
for (i = 0; i < 15; i++){
for (j = 0; j < 15; j++){
if (BG[i, j] == -AIValue){
allP += this.DirectionA(i, j, -AIValue);
allP += this.DirectionB(i, j, -AIValue);
allP += this.DirectionC(i, j, -AIValue);
allP += this.DirectionD(i, j, -AIValue);
}
}
}
#endregion
#region AI放子后必赢(五连)
if (allAI.IndexOf("T") >= 0){
int TstartIndexOfAI = allAI.IndexOf("T");
int TendIndexOfAI = allAI.IndexOf("$", TstartIndexOfAI);
tempStr = allAI.Substring(TstartIndexOfAI + 2, TendIndexOfAI - TstartIndexOfAI - 2);
return tempStr.Split('#')[0] + "#" +
tempStr.Split('#')[1];
}
#endregion
#region 人放子后必赢(五连)
if (allP.IndexOf("T") >= 0){
int TstartIndexOfP = allP.IndexOf("T");
int TendIndexOfP = allP.IndexOf("$", TstartIndexOfP);
tempStr = allP.Substring(TstartIndexOfP+2, TendIndexOfP - TstartIndexOfP-2);
return tempStr.Split('#')[0] + "#" +
tempStr.Split('#')[1];
}
#endregion
#region 可放棋子结果统计
string[] temp = allAI.Split('$');
for (k = 0; k < temp.Length; k++){
listAI.Add(temp[k]);
}
temp = allP.Split('$');
for (k = 0; k < temp.Length; k++){
listP.Add(temp[k]);
}
listAI.RemoveAt(listAI.Count - 1);
listP.RemoveAt(listP.Count - 1);
listAI = this.SelectCountOfSame(listAI);
listP = this.SelectCountOfSame(listP);
#endregion
#region AI放子后四连
arry.Clear();
for (i = 0; i < listAI.Count; i++){
if (listAI[i].ToString()[0].ToString() == "F"){ if (arry.Contains(listAI[i]) == false){
arry.Add(listAI[i]);
}
}
}
tempStr = this.Selects(arry, 1,1);
if (tempStr != ""){
arry = null;
return tempStr;
}
#endregion
#region 人放子后四连
arry.Clear();
for (i = 0; i < listP.Count; i++){
if (listP[i].ToString()[0].ToString() == "F"){
if (arry.Contains(listP[i]) == false){
arry.Add(listP[i]);
}
}
}
tempStr = this.Selects(arry, -1,1);
if (tempStr != ""){
arry = null;
return tempStr;
}
#endregion
#region 两子内可以四连,同时影响对手落子arry.Clear();
arry = this.BetweenSame(listAI, listP); tempStr = this.Selects(arry, -1,0);
if (tempStr != ""){
arry = null;
return tempStr;
}
#endregion
#region 在AI区域内随意落子
Random rg3 = new Random();
i = rg3.Next(0, listAI.Count - 1);
return listAI[i].ToString().Split('#')[1] + "#" + listAI[i].ToString().Split('#')[2];
#endregion
}
public void DisArraylist(){
listAI.Clear();
listAI = null;
listP.Clear();
listP = null;
}
}
}
3、棋子定点分析部分
class IsFiveChessman{
protected int[,] BG = new int[15, 15];
public IsFiveChessman() { }
public IsFiveChessman(int[,] bg){
BG = bg;
}
public bool IsExistFull(int x, int y,int count){
int xIndex = x;
int yIndex = y;
int current = BG[x, y];
int continuousCount = 0;
int flag = 1;
// 从左到右
while (yIndex >= 0 && yIndex <= 14){
if (BG[xIndex, yIndex] == current){
continuousCount++;
if (continuousCount == count){
return true;
}
yIndex = yIndex + flag;
if ((yIndex == -1 || yIndex == 15) && flag == 1){
yIndex = y - 1;
flag = -1;
}
continue;
}
else{
if (flag == 1){
yIndex = y - 1;
flag = -1;
}
else{break;}
}
}
//从右上到左下
continuousCount = 0;
xIndex = x;
yIndex = y;
flag = 1;
while ((xIndex <= 14 && yIndex >= 0) && (yIndex <= 14 && xIndex >= 0)){
if (BG[xIndex, yIndex] == current){
continuousCount++;
if (continuousCount == count){
return true;
}
yIndex = yIndex + flag;
xIndex = xIndex - flag;
if ((xIndex == -1 || yIndex == 15 || xIndex == 15 || yIndex == -1) && flag == 1){
yIndex = y - 1;
xIndex = x + 1;
flag = -1;
}
continue;
}
else{
if (flag == 1){
yIndex = y - 1;
xIndex = x + 1;
flag = -1;
}
else{break;}
}
}
// 从上到下
continuousCount = 0;
xIndex = x;
yIndex = y;
flag = 1;
while (xIndex >= 0 && xIndex <= 14){
if (BG[xIndex, yIndex] == current){
continuousCount++;
if (continuousCount == count){
return true;
}
xIndex = xIndex + flag;
if ((xIndex == -1 || xIndex == 15) && flag == 1){
xIndex = x - 1;
flag = -1;
}
continue;
}
else{
if (flag == 1){
xIndex = x - 1;
flag = -1;
}
else{break;}
}
}
//从右下到左上
continuousCount = 0;
xIndex = x;
yIndex = y;
flag = 1;
while ((xIndex <= 14 && yIndex >= 0) && (yIndex <= 14 && xIndex >= 0)){
if (BG[xIndex, yIndex] == current){
continuousCount++;
if (continuousCount == count){
return true;
}
yIndex = yIndex + flag;
xIndex = xIndex + flag;
if ((xIndex == -1 || yIndex == 15 || xIndex == 15 || yIndex == -1) && flag == 1){
yIndex = y - 1;
xIndex = x - 1;
flag = -1;
}
continue;
}
else{
if (flag == 1){
yIndex = y - 1;
xIndex = x - 1;
flag = -1;
}
else{break;}
}
}
return false;
}
public bool IsExistFour(int x, int y, int count){
int xIndex = x;
int yIndex = y;
int current = BG[x, y];
int continuousCount = 0;
int flag = 1;
// 从左到右
while (yIndex >= 0 && yIndex <= 14){
if (continuousCount == count && BG[xIndex, yIndex] == 0 && flag ==-1){
return true;
}
if (BG[xIndex, yIndex] == current){
continuousCount++;
yIndex = yIndex + flag;
if ((yIndex == -1 || yIndex == 15) ){
break;
}
continue;
}
else if (BG[xIndex, yIndex] == 0){
if (flag == 1){
yIndex = y - 1;
flag = -1;
}
else if (continuousCount == count){
return true;
}
else{break;}
}
else{break;}
}
//从右上到左下
continuousCount = 0;
xIndex = x;
yIndex = y;
flag = 1;
while ((xIndex <= 14 && yIndex >= 0) && (yIndex <= 14 && xIndex >= 0)){
if (continuousCount == count && BG[xIndex, yIndex] == 0 && flag == -1){
return true;
}
if (BG[xIndex, yIndex] == current){
continuousCount++;
yIndex = yIndex + flag;
xIndex = xIndex - flag;
if (xIndex == -1 || yIndex == 15 || xIndex == 15 || yIndex == -1){
break;
}
continue;
}
else if (BG[xIndex, yIndex] == 0){
if (flag == 1){
yIndex = y - 1;
xIndex = x + 1;
flag = -1;
}
else if (continuousCount == count){
return true;
}
else{break;}
}
else
{break;}
}
// 从上到下
continuousCount = 0;
xIndex = x;
yIndex = y;
flag = 1;
while (xIndex >= 0 && xIndex <= 14){
if (continuousCount == count && BG[xIndex, yIndex] == 0 && flag == -1){
return true;
}
if (BG[xIndex, yIndex] == current){
continuousCount++;
xIndex = xIndex + flag;
if (xIndex == -1 || xIndex == 15){
break;
}
continue;
}
else if (BG[xIndex, yIndex] == 0){
if (flag == 1){
xIndex = x - 1;
flag = -1;
}
else if (continuousCount == count){
return true;
}
else{break;}
}
else{break;}
}
//从右下到左上
continuousCount = 0;
xIndex = x;
yIndex = y;
flag = 1;
while ((xIndex <= 14 && yIndex >= 0) && (yIndex <= 14 && xIndex >= 0)){
if (continuousCount == count && BG[xIndex, yIndex] == 0 && flag == -1){
return true;}
if (BG[xIndex, yIndex] == current){
continuousCount++;
yIndex = yIndex + flag;
xIndex = xIndex + flag;
if (xIndex == -1 || yIndex == 15 || xIndex == 15 || yIndex == -1){break;}
continue;
}
else if (BG[xIndex, yIndex] == 0){
if (flag == 1){
yIndex = y - 1;
xIndex = x - 1;
flag = -1;
}
else if (continuousCount == count){
return true;
}
else{break;}
}
else{break;}
}
return false;
}
public string DirectionA(int x, int y,int current){
int xIndex = x;
int yIndex = y;
int flag = 1;
string res = "";
while (xIndex >= 0 && xIndex <= 14){
if (BG[xIndex, yIndex] == current){
xIndex = xIndex + flag;
if ((xIndex == -1 || xIndex == 15) && flag == 1){
xIndex = x - 1;
flag = -1;
}
continue;
}
else if (BG[xIndex, yIndex] == 0){
BG[xIndex, yIndex] = current;
if (this.IsExistFull(xIndex, yIndex, 5)){
res += "T" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else if (this.IsExistFour(xIndex, yIndex, 4)){
res += "F" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else{
res += "" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
BG[xIndex, yIndex] = 0;
if (flag == 1){
xIndex = x - 1;
flag = -1;
}
else{break;}
}
else if (BG[xIndex, yIndex] == -current){
if (flag == 1){
xIndex = x - 1;
flag = -1;
}
else{break;}
}
}
return res;
}
public string DirectionC(int x, int y, int current){ int xIndex = x;
int yIndex = y;
int flag = 1;
string res = "";
while (yIndex >= 0 && yIndex <= 14){
if (BG[xIndex, yIndex] ==current){
yIndex = yIndex + flag;
if ((yIndex == -1 || yIndex == 15) && flag == 1){
yIndex = y - 1;
flag = -1;
}
continue;
}
else if (BG[xIndex, yIndex] == 0){
BG[xIndex, yIndex] = current;
if (this.IsExistFull(xIndex, yIndex, 5)){
res += "T" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else if (this.IsExistFour(xIndex, yIndex, 4)){
res += "F" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else{
res += "" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
BG[xIndex, yIndex] = 0;
if (flag == 1){
yIndex = y - 1;
flag = -1;
}
else{break;}
}
else if (BG[xIndex, yIndex] == -current){
if (flag == 1){
yIndex = y - 1;
flag = -1;
}
else{break;}
}
}
return res;
}
public string DirectionB(int x, int y, int current){ int xIndex = x;
int yIndex = y;
int flag = 1;
string res = "";
while (xIndex >= 0 && xIndex <= 14 && yIndex >=0 && yIndex <=14){
if (BG[xIndex, yIndex] == current){
yIndex = yIndex + flag;
xIndex = xIndex - flag;
if ((xIndex == -1 || yIndex == 15 || xIndex == 15 || yIndex == -1) && flag == 1){
yIndex = y - 1;
xIndex = x + 1;
flag = -1;
}
continue;
}
else if (BG[xIndex, yIndex] == 0){
BG[xIndex, yIndex] = current;
if (this.IsExistFull(xIndex, yIndex, 5)){
res += "T" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else if (this.IsExistFour(xIndex, yIndex, 4)){
res += "F" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else{
res += "" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
BG[xIndex, yIndex] = 0;
if (flag == 1){
yIndex = y - 1;
xIndex = x + 1;
flag = -1;
}
else{break;}
}
else if (BG[xIndex, yIndex] == -current){
if (flag == 1){
yIndex = y - 1;
xIndex = x + 1;
flag = -1;
}
else{break;}
}
}
return res;
}
public string DirectionD(int x, int y, int current){ int xIndex = x;
int yIndex = y;
int flag = 1;
string res = "";
while (xIndex >= 0 && xIndex <= 14 && yIndex >= 0 && yIndex <= 14){
if (BG[xIndex, yIndex] == current){
yIndex = yIndex + flag;
xIndex = xIndex + flag;
if ((xIndex == -1 || yIndex == 15 || xIndex == 15 || yIndex == -1) && flag == 1){
yIndex = y - 1;
xIndex = x - 1;
flag = -1;
}
continue;
}
else if (BG[xIndex, yIndex] == 0){
BG[xIndex, yIndex] = current;
if (this.IsExistFull(xIndex, yIndex, 5)){
res += "T" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else if (this.IsExistFour(xIndex, yIndex, 4)){ res += "F" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
else{
res += "" + "#" + xIndex.ToString() + "#" + yIndex.ToString() + "$";
}
BG[xIndex, yIndex] = 0;
if (flag == 1){
yIndex = y - 1;
xIndex = x - 1;
flag = -1;
}
else{break;}
}
else if (BG[xIndex, yIndex] == -current){
if (flag == 1){
yIndex = y - 1;
xIndex = x - 1;
flag = -1;
}
else{break;}
}
}
return res;
}
public bool AllSelect(){
for (int i = 0; i < 15; i++){
for (int j = 0; j < 15; j++){
if (BG[i, j] != 0){
if (IsExistFull(i, j,5)){
return true;
}
}
}
}
return false;
}
}
}
三、实验结果及分析
总的来说程序基本达到实验目的。
体会到了简单的人机博弈、AI逻辑思考的过程。
要求内容基本完成,AI所下棋子都是在情理之中,可以达到人机对弈的目的。
运行速度上没有影响,AI的下棋速度基本瞬间完成。
程序写好后经过多人测试、试玩。
程序运行正常,AI的胜率大约在60%上下。
效果图如下图1所示(黑子为AI):
图 1 战况结果
通过多人试玩也发现了对特殊下法的处理没有进行估计。
网上流传的一些针对AI的特殊下棋法没有在本程序中考虑。
导致在特殊的下棋法下AI没有获胜的可能。
如下图图2所示,一朋友所下情况:(黑子为AI)
图 2 特殊情况
word文档可自由复制编辑。