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 int game_type;//当前哪方下棋
private string[] game_types = {"双人对战","人机对战","联网对战" };
public FormChess()
{
InitializeComponent();
(game_types);
= 0;
}
private void init_game()
{
//黑棋先走
game_type = 0;
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, -1);//-1代表此位置没有棋子,0代表黑棋,1代表白棋
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 = -1;
}
}
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_game_type_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 void right_up_check(Pos pos,int count)
{
if (count == 5) {
if ( == 0)("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
init_game();
return;
}
if( + 1 <13 && - 1 >0 && != -1)
{
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);
}
}
//左上方向判断
private void left_up_check(Pos pos, int count)
{
if (count == 5)
{
if ( == 0) ("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
init_game();
return ;
}
if ( - 1 > 0 && - 1 > 0 && != -1)
{
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);
}
}
//横向方向判断
private void horizontal_check(Pos pos, int count)
{
if (count == 5)
{
if ( == 0) ("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
init_game();
return ;
}
if ( + 1 < 13 && != -1)
{
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);
}
}
//竖向方向判断
private void vertical_check(Pos pos, int count)
{
if (count == 5)
{
if ( == 0) ("黑方赢棋", "系统提示");
else ("白方赢棋", "系统提示");
init_game();
return ;
}
if ( + 1 < 13 && != -1)
{
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);
}
}
//判断赢局
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);
}
//窗口坐标转点坐标
private Point location_to_point(Button button)
{
return new Point(( - 30)/50, ( - 30)/50);
}
//根据点坐标得到索引
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 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 (game_type == 0)
{
= ;
null_chess_pos_list[index].Type = 0;
game_type = 1;
}
else
{
= ;
null_chess_pos_list[index].Type = 1;
game_type = 0;
}
set_game_type_label();
//添加到下棋栈列
cur_chess_btn_list.Push(button);
check_win();
}
// 设置透明按钮样式
private void set_btn_style(Button btn)
{
= ;//样式
= ;//前景
= ;//去背景
= 0;//去边线
= ;//鼠标经过
= ;//鼠标按下
}
//提示当前由哪方下棋
private void set_game_type_label()
{
if (game_type == 0) 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 = -1;
null_chess_pos_list[index].Have_chess = false;
= .chess_bg1;
cur_chess_btn_list.Pop();
if (game_type == 0) game_type = 1;
else game_type = 0;
set_game_type_label();
}
//下棋点击事件
private void button1_Click(object sender, EventArgs e)
{
move_chess((Button)sender);
}
private void button_start_Click(object sender, EventArgs e)
{
switch (())
{
case "双人对战":
init_game();
break;
case "人机对战":
break;
case "联网对战":
break;
}
}
//悔棋
private void button_back_Click(object sender, EventArgs e)
{
back_chess();
}
//重置游戏
private void button_reset_Click(object sender, EventArgs e)
{
switch (())
{
case "双人对战":
init_game();
break;
case "人机对战":
break;
case "联网对战":
break;
}
}
}
}