using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
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)
{
= X;
= Y;
this.Have_chess = Have_chess;
= 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();
(game_types);
= 0;
init_score_dict();
}
private void init_game()
{
switch(())
{
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();
= .chess_bg1;
cur_chess_btn_list.Pop();
}
}
set_down_chess_label();
}
//绘制棋盘
private void draw_chess_grid()
{
var graphics = ();
();//一定要加这句,不然无法显示
var pen = new Pen(, 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);
(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);
(pen, p1, p2);
}
}
//右上方向判断
private int right_up_check(Pos pos,int count)
{
if (count == 5) {
if ( == 2)("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
game_over = true;
}
if ( + 1 <13 && - 1 >0 && != 0)
{
var index = get_pos_index_from_null_chess_list( + 1, - 1);
if (null_chess_pos_list[index].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 ( == 2) ("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
game_over = true;
}
if ( - 1 > 0 && - 1 > 0 && != 0)
{
var index = get_pos_index_from_null_chess_list( - 1, - 1);
if (null_chess_pos_list[index].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 ( == 2) ("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
game_over = true;
}
if ( + 1 < 13 && != 0)
{
var index = get_pos_index_from_null_chess_list( + 1, );
if (null_chess_pos_list[index].Type == ) horizontal_check(null_chess_pos_list[index], count + 1);
}
return count;
}
//竖向方向判断
private int vertical_check(Pos pos, int count)
{
if (count == 5)
{
if ( == 2) ("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
game_over = true;
}
if ( + 1 < 13 && != 0)
{
var index = get_pos_index_from_null_chess_list( , + 1);
if (null_chess_pos_list[index].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();
= new Point(x, y);
= new Size(40, 40);
set_btn_style(button);
(button);
+= new EventHandler(button1_Click);
(button);
}
//窗口坐标转点坐标
private Point location_to_point(Button button)
{
return new Point(( - 30)/50, ( - 30)/50);
}
//点坐标转窗口坐标
private Point point_to_location(Pos pos)
{
return new Point( * 50 +30, * 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 < ; i++)
if (buttons[i]. == x && buttons[i]. == 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(, );
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(, );
if (null_chess_pos_list[index].Have_chess) return;
null_chess_pos_list[index].Have_chess = true;
if (down_chess == 2)
{
= ;
null_chess_pos_list[index].Type = 2;
down_chess = 1;
}
else
{
= ;
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)
{
= ;//样式
= ;//前景
= ;//去背景
= 0;//去边线
= ;//鼠标经过
= ;//鼠标按下
}
//提示当前由哪方下棋
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(,);
null_chess_pos_list[index].Type = 0;
null_chess_pos_list[index].Have_chess = false;
= .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个连子
("22222",122222);
//4个连子
("022220", 12222);
("122220", 5222);
("20222", 5222);
("22022", 5222);
//3个连子
("02220", 2522);
("122200", 1222);
("120220", 1222);
("020221", 1222);
("122020", 1222);
("022021", 1222);
//2个连子
("00220", 522);
("122000", 522);
("120200", 322);
//1个子
("211112", 52222);
("11121", 52222);
("11211", 52222);
("21110", 4222);
("12110", 4222);
("11210", 4222);
("21100",822);
("12100", 822);
("11200", 822);
("21000", 222);
("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) (head_temp_pos.());
else
{
var index = get_pos_index_from_null_chess_list(i, head_temp_pos.Y);
(null_chess_pos_list[index].());
}
}
return ();
}
//竖直方向
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) (head_temp_pos.());
else
{
var index = get_pos_index_from_null_chess_list(head_temp_pos.X,i);
(null_chess_pos_list[index].());
}
}
return ();
}
//左上方向
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) (head_temp_pos.());
else
{
var index = get_pos_index_from_null_chess_list(step, i);
(null_chess_pos_list[index].());
}
step++;
}
return ();
}
//右上方向
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) (head_temp_pos.());
else
{
var index = get_pos_index_from_null_chess_list(i, step);
(null_chess_pos_list[index].());
}
step--;
}
return ();
}
//得到在位置落子时的分数
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() || left_up.Contains(new string(().Reverse().ToArray()))) value += ;
if (right_up.Contains() || right_up.Contains(new string(().Reverse().ToArray()))) value += ;
if (() || (new string(().Reverse().ToArray()))) value += ;
if (() || (new string(().Reverse().ToArray()))) 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 => ).ToDictionary(p =>,o => );
return temp_dict_score.();
}
}
}