C# winform 簡單五子棋 200行程式碼實現人機對戰
1、功能需求
接上篇博文,本文描述簡單人機對戰實現過程,只是簡單實現考慮走一步策略,如果要想實現走多步策略,可以在本文估值演算法的基礎上用極大極小值配合剪枝演算法,實現考慮多步策略,這樣ai會顯得更加聰明,後期如果有時間完善,會更新程式碼。
2、介面設計
參考上一篇博文的介面。
3、演算法描述
其實演算法非常簡單,畢竟ai部分只有200行程式碼,所以應該只能算是實現估值函式,即當前局面走一步時最好的位置,不考慮走多步。我的思路如下:
(1)列舉當前局面棋子可能的落點,並給不同的局面賦值,越重要的局面分數會越高
(2)遍歷每個可落子點
(3)在可落子點落子時,分四個方向去獲得落子前後五個位置並轉換成序列,然後檢測該序列滿足(1)中的情況,並獲得一定分數累加存到字典中
(4)對字典進行排序
(5)取得分數最高的點即為最優落子點
當然,這只是考慮一步,但也有不錯的棋力,如果要考慮多步,請參考前面提到的極大極小值配合剪枝演算法,其實本博文的演算法可以當作極大極小值配合剪枝演算法的估值函式,然後只做迭代就好了。
4、程式碼實現
對上一博文的程式碼有一點小的更新,因此全部貼出程式碼,可能列舉時有考慮不到的情況,可能有些小問題,不過思想是沒有問題的,專案程式碼如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsAppAnimalRegonize
{
public partial class FormChess : Form
{
class Pos
{
public int X { get; set; }
public int Y { get; set; }
public bool Have_chess { get; set; }
public int Type { get; set; }
public Pos(int X, int Y, bool Have_chess, int Type)
{
this.X = X;
this.Y = Y;
this.Have_chess = Have_chess;
this.Type = Type;
}
}
private List<Pos> null_chess_pos_list;
private Stack<Button> cur_chess_btn_list;
private List<Button> buttons;
private int down_chess;
private string[] game_types = {"雙人對戰","人機對戰","聯網對戰" };
private int cur_game_type;
private bool game_over;
public FormChess()
{
InitializeComponent();
comboBox1.Items.AddRange(game_types);
comboBox1.SelectedIndex = 0;
init_score_dict();
}
private void init_game()
{
switch(comboBox1.SelectedItem.ToString())
{
case "雙人對戰":
cur_game_type = 0;
break;
case "人機對戰":
cur_game_type = 1;
break;
case "聯網對戰":
cur_game_type = 2;
break;
}
game_over = false;
//白棋先走,1為白棋2為黑棋
down_chess = 1;
if (buttons == null) buttons = new List<Button>();
if (null_chess_pos_list == null)
{
null_chess_pos_list = new List<Pos>();
for (int x = 0; x < 13; x++)
{
for (int y = 0; y < 13; y++)
{
new_chess_pos(30 + 50 * x, 30 + 50 * y);
var pos = new Pos(x, y, false, 0);
null_chess_pos_list.Add(pos);
}
}
}
else
{
for (int i = 0; i < null_chess_pos_list.Count; i++)
{
null_chess_pos_list[i].Have_chess = false;
null_chess_pos_list[i].Type = 0;
}
}
if (cur_chess_btn_list == null) cur_chess_btn_list = new Stack<Button>();
else
{
while (cur_chess_btn_list.Count > 0)
{
var btn = cur_chess_btn_list.Peek();
btn.BackgroundImage = Properties.Resources.chess_bg1;
cur_chess_btn_list.Pop();
}
}
set_down_chess_label();
}
//繪製棋盤
private void draw_chess_grid()
{
var graphics = this.CreateGraphics();
this.Show();//一定要加這句,不然無法顯示
var pen = new Pen(Brushes.Black, 2.0f);
for (int x = 0; x < 13; x++)
{
var p1 = new Point(50 + x * 50, 50);
var p2 = new Point(50 + x * 50, 50 + 50 * 12);
graphics.DrawLine(pen, p1, p2);
}
for (int y = 0; y < 13; y++)
{
var p1 = new Point(50, 50 + y * 50);
var p2 = new Point(50 + 50 * 12, 50 + y * 50);
graphics.DrawLine(pen, p1, p2);
}
}
//右上方向判斷
private int right_up_check(Pos pos,int count)
{
if (count == 5) {
if (pos.Type == 2)MessageBox.Show("黑方贏棋", "系統提示");
else MessageBox.Show("白方贏棋", "系統提示");
game_over = true;
}
if (pos.X + 1 <13 && pos.Y - 1 >0 && pos.Type != 0)
{
var index = get_pos_index_from_null_chess_list(pos.X + 1, pos.Y - 1);
if (null_chess_pos_list[index].Type == pos.Type) right_up_check(null_chess_pos_list[index],count + 1);
}
return count;
}
//左上方向判斷
private int left_up_check(Pos pos, int count)
{
if (count == 5)
{
if (pos.Type == 2) MessageBox.Show("黑方贏棋", "系統提示");
else MessageBox.Show("白方贏棋", "系統提示");
game_over = true;
}
if (pos.X - 1 > 0 && pos.Y - 1 > 0 && pos.Type != 0)
{
var index = get_pos_index_from_null_chess_list(pos.X - 1, pos.Y - 1);
if (null_chess_pos_list[index].Type == pos.Type ) left_up_check(null_chess_pos_list[index], count + 1);
}
return count;
}
//橫向方向判斷
private int horizontal_check(Pos pos, int count)
{
if (count == 5)
{
if (pos.Type == 2) MessageBox.Show("黑方贏棋", "系統提示");
else MessageBox.Show("白方贏棋", "系統提示");
game_over = true;
}
if (pos.X + 1 < 13 && pos.Type != 0)
{
var index = get_pos_index_from_null_chess_list(pos.X + 1, pos.Y );
if (null_chess_pos_list[index].Type == pos.Type) horizontal_check(null_chess_pos_list[index], count + 1);
}
return count;
}
//豎向方向判斷
private int vertical_check(Pos pos, int count)
{
if (count == 5)
{
if (pos.Type == 2) MessageBox.Show("黑方贏棋", "系統提示");
else MessageBox.Show("白方贏棋", "系統提示");
game_over = true;
}
if (pos.Y + 1 < 13 && pos.Type != 0)
{
var index = get_pos_index_from_null_chess_list(pos.X , pos.Y + 1);
if (null_chess_pos_list[index].Type == pos.Type) vertical_check(null_chess_pos_list[index], count + 1);
}
return count;
}
//判斷贏局
private void check_win()
{
for (int i = 0; i < null_chess_pos_list.Count; i++)
{
left_up_check(null_chess_pos_list[i], 1);
right_up_check(null_chess_pos_list[i], 1);
horizontal_check(null_chess_pos_list[i], 1);
vertical_check(null_chess_pos_list[i], 1);
}
}
//初始生成下棋位置
private void new_chess_pos(int x,int y)
{
var button = new Button();
button.Location = new Point(x, y);
button.Size = new Size(40, 40);
set_btn_style(button);
this.Controls.Add(button);
button.Click += new EventHandler(button1_Click);
buttons.Add(button);
}
//視窗座標轉點座標
private Point location_to_point(Button button)
{
return new Point((button.Location.X - 30)/50, (button.Location.Y - 30)/50);
}
//點座標轉視窗座標
private Point point_to_location(Pos pos)
{
return new Point(pos.X * 50 +30, pos.Y * 50 +30);
}
//根據點座標得到棋子位置索引
private int get_pos_index_from_null_chess_list(int x,int y)
{
for (int i = 0; i < null_chess_pos_list.Count; i++)
if (null_chess_pos_list[i].X == x && null_chess_pos_list[i].Y == y)return i;
return -1;
}
//根據視窗座標得到索引
private int get_btn_index_from_button_list(int x, int y)
{
for (int i = 0; i < buttons.Count; i++)
if (buttons[i].Location.X == x && buttons[i].Location.Y == y) return i;
return -1;
}
//ai走棋
private void ai_move_chess()
{
Pos pos = get_best_pos();
var point = point_to_location(pos);
var index = get_btn_index_from_button_list(point.X, point.Y);
move_chess(buttons[index]);
}
//走棋
private void move_chess(Button button)
{
var point = location_to_point(button);
var index = get_pos_index_from_null_chess_list(point.X, point.Y);
if (null_chess_pos_list[index].Have_chess) return;
null_chess_pos_list[index].Have_chess = true;
if (down_chess == 2)
{
button.BackgroundImage = Properties.Resources.black;
null_chess_pos_list[index].Type = 2;
down_chess = 1;
}
else
{
button.BackgroundImage = Properties.Resources.white;
null_chess_pos_list[index].Type = 1;
down_chess = 2;
}
set_down_chess_label();
//新增到下棋棧列
cur_chess_btn_list.Push(button);
check_win();
}
// 設定透明按鈕樣式
private void set_btn_style(Button btn)
{
btn.FlatStyle = FlatStyle.Flat;//樣式
btn.ForeColor = Color.Transparent;//前景
btn.BackColor = Color.Transparent;//去背景
btn.FlatAppearance.BorderSize = 0;//去邊線
btn.FlatAppearance.MouseOverBackColor = Color.Transparent;//滑鼠經過
btn.FlatAppearance.MouseDownBackColor = Color.Transparent;//滑鼠按下
}
//提示當前由哪方下棋
private void set_down_chess_label()
{
if (down_chess == 2) label_game_type.Text = "黑方下棋";
else label_game_type.Text = "白方下棋";
}
//悔棋
private void back_chess()
{
if (cur_chess_btn_list==null) return;
if (cur_chess_btn_list.Count == 0) return;
var btn = cur_chess_btn_list.Peek();
var p = location_to_point(btn);
var index = get_pos_index_from_null_chess_list(p.X,p.Y);
null_chess_pos_list[index].Type = 0;
null_chess_pos_list[index].Have_chess = false;
btn.BackgroundImage = Properties.Resources.chess_bg1;
cur_chess_btn_list.Pop();
if (down_chess == 2) down_chess = 1;
else down_chess = 2;
set_down_chess_label();
}
//下棋點選事件
private void button1_Click(object sender, EventArgs e)
{
if (game_over) return;
switch (cur_game_type)
{
case 0:
move_chess((Button)sender);
break;
case 1:
move_chess((Button)sender);
ai_move_chess();
break;
case 2:
break;
}
}
//開始遊戲
private void button_start_Click(object sender, EventArgs e)
{
init_game();
}
//悔棋
private void button_back_Click(object sender, EventArgs e)
{
if (game_over) return;
switch (cur_game_type)
{
case 0:
back_chess();
break;
case 1:
back_chess();
back_chess();
break;
case 2:
break;
}
}
//重置遊戲
private void button_reset_Click(object sender, EventArgs e)
{
init_game();
}
//ai部分程式碼
private Dictionary<string, int> dict = new Dictionary<string, int>();//儲存列舉分數的字典
private void init_score_dict()
{
//5個連子
dict.Add("22222",122222);
//4個連子
dict.Add("022220", 12222);
dict.Add("122220", 5222);
dict.Add("20222", 5222);
dict.Add("22022", 5222);
//3個連子
dict.Add("02220", 2522);
dict.Add("122200", 1222);
dict.Add("120220", 1222);
dict.Add("020221", 1222);
dict.Add("122020", 1222);
dict.Add("022021", 1222);
//2個連子
dict.Add("00220", 522);
dict.Add("122000", 522);
dict.Add("120200", 322);
//1個子
dict.Add("211112", 52222);
dict.Add("11121", 52222);
dict.Add("11211", 52222);
dict.Add("21110", 4222);
dict.Add("12110", 4222);
dict.Add("11210", 4222);
dict.Add("21100",822);
dict.Add("12100", 822);
dict.Add("11200", 822);
dict.Add("21000", 222);
dict.Add("12000", 222);
}
//水平方向,傳入的pos均為當前要估值的pos
private string get_horizontal_pos_str(Pos head_temp_pos)
{
int head_x = head_temp_pos.X,trail_x = head_temp_pos.X,head_count =0,trail_count =0;
while (head_count<5 && head_x - 1>0)
{
head_count++;
head_x--;
}
while (trail_count < 5 && trail_x + 1 < 13)
{
trail_count++;
trail_x++;
}
StringBuilder sb = new StringBuilder();
for (int i = head_temp_pos.X - head_count; i <= head_temp_pos.X+trail_count; i++)
{
if (i == head_temp_pos.X) sb.Append(head_temp_pos.Type.ToString());
else
{
var index = get_pos_index_from_null_chess_list(i, head_temp_pos.Y);
sb.Append(null_chess_pos_list[index].Type.ToString());
}
}
return sb.ToString();
}
//豎直方向
private string get_vertical_pos_str(Pos head_temp_pos)
{
int head_y = head_temp_pos.Y, trail_y = head_temp_pos.Y, head_count = 0, trail_count = 0;
while (head_count < 5 && head_y - 1 > 0)
{
head_count++;
head_y--;
}
while (trail_count < 5 && trail_y + 1 < 13)
{
trail_count++;
trail_y++;
}
StringBuilder sb = new StringBuilder();
for (int i = head_temp_pos.Y - head_count; i <= head_temp_pos.Y + trail_count; i++)
{
if (i == head_temp_pos.Y) sb.Append(head_temp_pos.Type.ToString());
else
{
var index = get_pos_index_from_null_chess_list(head_temp_pos.X,i);
sb.Append(null_chess_pos_list[index].Type.ToString());
}
}
return sb.ToString();
}
//左上方向
private string get_left_up_pos_str(Pos head_temp_pos)
{
int head_x=head_temp_pos.X,head_y = head_temp_pos.Y, trail_x = head_temp_pos.X, trail_y = head_temp_pos.Y, head_count = 0, trail_count = 0;
while (head_count < 5 && head_y - 1 > 0 && head_x - 1>0)
{
head_count++;
head_y--;
head_x--;
}
while (trail_count < 5 && trail_y + 1 < 13 && trail_x + 1 <13)
{
trail_count++;
trail_y++;
trail_x++;
}
var step = head_temp_pos.X - head_count;
StringBuilder sb = new StringBuilder();
for (int i = head_temp_pos.Y - head_count; i <= head_temp_pos.Y + trail_count; i++)
{
if (i == head_temp_pos.Y) sb.Append(head_temp_pos.Type.ToString());
else
{
var index = get_pos_index_from_null_chess_list(step, i);
sb.Append(null_chess_pos_list[index].Type.ToString());
}
step++;
}
return sb.ToString();
}
//右上方向
private string get_right_up_pos_str(Pos head_temp_pos)
{
int head_x = head_temp_pos.X, head_y = head_temp_pos.Y, trail_x = head_temp_pos.X, trail_y = head_temp_pos.Y, head_count = 0, trail_count = 0;
while (head_count < 5 && head_y + 1 < 13 && head_x - 1 > 0)
{
head_count++;
head_y++;
head_x--;
}
while (trail_count < 5 && trail_y - 1 > 0 && trail_x + 1 < 13)
{
trail_count++;
trail_y--;
trail_x++;
}
var step = head_temp_pos.Y + head_count;
StringBuilder sb = new StringBuilder();
for (int i = head_temp_pos.X - head_count; i <= head_temp_pos.X + trail_count; i++)
{
if (i == head_temp_pos.X) sb.Append(head_temp_pos.Type.ToString());
else
{
var index = get_pos_index_from_null_chess_list(i, step);
sb.Append(null_chess_pos_list[index].Type.ToString());
}
step--;
}
return sb.ToString();
}
//得到在位置落子時的分數
private int get_pos_score(Pos pos)
{
int value = 0;
string left_up = get_left_up_pos_str(pos);
string right_up = get_right_up_pos_str(pos);
string horizontal = get_horizontal_pos_str(pos);
string vertical = get_vertical_pos_str(pos);
foreach (KeyValuePair<string,int> pair in dict)
{
if (left_up.Contains(pair.Key) || left_up.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
if (right_up.Contains(pair.Key) || right_up.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
if (horizontal.Contains(pair.Key) || horizontal.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
if (vertical.Contains(pair.Key) || vertical.Contains(new string(pair.Key.ToCharArray().Reverse().ToArray()))) value += pair.Value;
}
return value;
}
//得到最好的落子位置
private Pos get_best_pos()
{
var dict_score = new Dictionary<Pos, int>();
for (int i = 0; i < null_chess_pos_list.Count; i++)
{
if (null_chess_pos_list[i].Have_chess == false)
{
Pos pos = new Pos(null_chess_pos_list[i].X, null_chess_pos_list[i].Y, null_chess_pos_list[i].Have_chess,2);
dict_score.Add(null_chess_pos_list[i], get_pos_score(pos));
}
}
var temp_dict_score = dict_score.OrderByDescending(o => o.Value).ToDictionary(p =>p.Key,o => o.Value);
return temp_dict_score.Keys.First();
}
}
}
5、測試
由於只考慮一步,想要很強的棋力是不可能的,但是也不算特別白痴,給它贏它還是很“樂意”的,好奇的童鞋可以拷貝程式碼執行試試看看,本博文的思想思想僅供參考。