C#實現(xiàn)AI五子棋游戲的示例代碼
文章描述
關(guān)于簡單的介紹,這篇就不贅述了,主要還是來寫一下實際的人工下棋操作以及對應(yīng)的機(jī)器操作的算法處理。
還是先大致說一下算法實現(xiàn)方式,我們之前寫的五子棋大部分可能主要是基于機(jī)器算法做一個攔截操作,即判斷橫向、豎向、斜向、反斜向的棋子的數(shù)量去直接進(jìn)行攔截。但是這一篇中主要是使用了一個分配權(quán)重的算法,根據(jù)權(quán)重來匹配我是要去攔截你,還是保持自己的勝利。這個權(quán)重可以根據(jù)自己的需求適當(dāng)調(diào)整(我也是瞎寫的)。
萬變不離其宗,無論什么算法,肯定到最后都是根據(jù)五子棋的玩法,去解析橫豎斜的勝率來進(jìn)行權(quán)衡。
開發(fā)環(huán)境
.NET Framework版本:4.5
開發(fā)工具
Visual Studio 2013
實現(xiàn)代碼
/// <summary> /// 轉(zhuǎn)換棋子的繪制位置 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> private Point GetChessPostion(int x, int y) { return new Point((int)(x * cellSize.Width) - chessSize.Width / 2, (int)(y * cellSize.Height) - chessSize.Height / 2); } /// <summary> /// 判斷勝負(fù) /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="type"></param> private void IsWin(int x, int y, bool type) { for (int w = 0; w < winSum; w++) { if (wins[x, y, w] == 1) { if (!type) { userWin[w]++; aiWin[w] = 6; if (userWin[w] == 5) { graphics.DrawString("贏", new Font("黑體", 11.0f), new SolidBrush(Color.Red), GetChessPostion(x, y)); if (MessageBox.Show("你贏了,是否重新開始?") == DialogResult.OK) { Reset(); } break; } } else { aiWin[w]++; userWin[w] = 6; if (aiWin[w] == 5) { graphics.DrawString("贏", new Font("黑體", 11.0f), new SolidBrush(Color.Red), GetChessPostion(x, y)); if (MessageBox.Show("你輸了,是否重新開始?") == DialogResult.OK) { Reset(); } break; } } } } isUserPlay = !isUserPlay; }
/// <summary> /// 人工下棋操作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void panel_board_Click(object sender, EventArgs e) { if (!isUserPlay) { return; } MouseEventArgs mouse = e as MouseEventArgs; if (mouse.X < cellSize.Width || mouse.X > boardSize.Width - cellSize.Width) { return; } if (mouse.Y < cellSize.Height || mouse.Y > boardSize.Height - cellSize.Height) { return; } int x = Convert.ToInt32(Math.Round((decimal)mouse.X / (decimal)cellSize.Width, MidpointRounding.AwayFromZero)); int y = Convert.ToInt32(Math.Round((decimal)mouse.Y / (decimal)cellSize.Width, MidpointRounding.AwayFromZero)); Point chessPoint = GetChessPostion(x, y); if (!chessList.Exists(s => s.point == chessPoint)) { chessList.Add(new ChessModel { point = chessPoint, type = true }); graphics.DrawImage(Properties.Resources.黑棋子, chessPoint.X, chessPoint.Y, chessSize.Width, chessSize.Height); IsWin(x, y, false); SetStatus(x, y, false); if (!isUserPlay) { AIChess(); } } }
/// <summary> /// AI下棋操作 /// </summary> private void AIChess() { int[,] userScore = new int[xCellCount, yCellCount]; int[,] aiScore = new int[xCellCount, yCellCount]; int max = 0; Point aiChess = new Point(); for (int x = 0; x < xCellCount; x++) { for (int y = 0; y < yCellCount; y++) { if (!chessList.Exists(s => s.point == GetChessPostion(x, y))) { for (int w = 0; w < winSum; w++) { if (wins[x, y, w] == 1) { if (userWin[w] == 1) { userScore[x, y] += 100; } if (userWin[w] == 2) { userScore[x, y] += 400; } if (userWin[w] == 3) { userScore[x, y] += 3000; } if (userWin[w] == 4) { userScore[x, y] += 20000; } if (aiWin[w] == 1) { aiScore[x, y] += 200; } if (aiWin[w] == 2) { aiScore[x, y] += 500; } if (aiWin[w] == 3) { aiScore[x, y] += 3400; } if (aiWin[w] == 4) { aiScore[x, y] += 30000; } } } if (userScore[x, y] > max) { max = userScore[x, y]; aiChess.X = x; aiChess.Y = y; } else if (userScore[x, y] == max) { if (aiScore[x, y] > aiScore[x, y]) { aiChess.X = x; aiChess.Y = y; } } if (aiScore[x, y] > max) { max = aiScore[x, y]; aiChess.X = x; aiChess.Y = y; } else if (aiScore[x, y] == max) { if (userScore[x, y] > userScore[x, y]) { aiChess.X = x; aiChess.Y = y; } } } } } Point chessPoint = GetChessPostion(aiChess.X, aiChess.Y); chessList.Add(new ChessModel() { point = chessPoint, type = false }); graphics.DrawImage(Properties.Resources.白棋子, chessPoint.X, chessPoint.Y, chessSize.Width, chessSize.Height); IsWin(aiChess.X, aiChess.Y, true); SetStatus(aiChess.X, aiChess.Y, true); }
實現(xiàn)效果
代碼解析:自己看代碼吧,看懂了就拿來優(yōu)化下,看不懂就直接下載下來玩玩(除了一個思路引導(dǎo)外,好像也沒什么用)
到此這篇關(guān)于C#實現(xiàn)AI五子棋游戲的示例代碼的文章就介紹到這了,更多相關(guān)C#五子棋游戲內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!