和上次一样
完全是纯代码 无需添加任何控件
粘贴上就能运行
但是有一个前提 贴代码前先往窗体的Microsoft.VisualBasic.PowerPacks那个选项卡拖一个圆进去然后删掉
这样就成功添加引用了
不然可是运行不了的哦
下面上代码
完全是纯代码 无需添加任何控件
粘贴上就能运行
但是有一个前提 贴代码前先往窗体的Microsoft.VisualBasic.PowerPacks那个选项卡拖一个圆进去然后删掉
这样就成功添加引用了
不然可是运行不了的哦
下面上代码
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic.PowerPacks;namespace 五子棋
{
public partial class Form1 : Form
{
int[,] ai = new int[8, 8];//存数据的
int bo = 0;//设置谁下
ShapeContainer sh = new ShapeContainer();//画棋盘的容器
ShapeContainer si = new ShapeContainer();//画棋子的容器 这一步非常重要 如果不弄两个容器 那么棋子将被棋盘覆盖掉
const int f = 30, f2 = 40;//设置常量
public Form1()
{
Text = "五子棋";
StartPosition = FormStartPosition.CenterScreen;
Size = new Size(380, 400);
Controls.Add(sh);
Controls.Add(si);
KeyPreview = true;
MouseDown += new MouseEventHandler(dian);//布局
for (int y = 1, x = 1, x2 = 8; y < 9; y++)//这个循环用来画棋盘
{
LineShape a = new LineShape();
LineShape b = new LineShape();
a.Tag = b.Tag = 1;
b.Y1 = a.X1 = x * f2;
b.X1 = a.Y1 = y * f2;
b.Y2 = a.X2 = x2 * f2;
b.X2 = a.Y2 = y * f2;
a.Enabled = b.Enabled = false;//这一步很重要
sh.Shapes.AddRange(new Shape[] { a, b });
}
}
private void dian(object sender, MouseEventArgs e)//鼠标点击事件
{
int d = dingwei(e.Location);
if (d != 0)
{
bo++;
OvalShape a = new OvalShape();
a.Size = new Size(f, f);
a.Location = new Point(d/10*f2-f/2, d%10*f2-f/2);
a.BackStyle = BackStyle.Opaque;
if (bo%2==0)
a.BackColor = Color.White;
else
a.BackColor = Color.Black;
si.Shapes.Add(a);
}
if (panduan() != 0)
{
MessageBox.Show(panduan().ToString().Replace("1", "黑").Replace("2", "白") + "子胜利");
Close();
}
}
int dingwei(Point p)//定位判断
{
for (int a = 1; a < 9; a++)
for (int b = 1; b < 9; b++)
if (Math.Abs(p.X - a * f2) < 10 && Math.Abs(p.Y - b * f2) < 10)
{
ai[a-1, b-1] = bo%2+1;
return a * 10 + b;
}
return 0;
}
int panduan()//判断胜负
{
int a, b,a1,b1,x,y;
for (x = 0; x < 8; x++)//直线判断
{
a = b = a1 = b1 = 0 ;
for (y = 0; y < 8; y++)
{
a = k(ai[x, y],1,a);
b = k(ai[x, y], 2,b);
a1 = k(ai[y, x], 1,a1);
b1 = k(ai[y, x], 2,b1);
if (a >= 5||a1>=5)
return 1;
if (b >= 5||b1>=5)
return 2;
}
}
int[] bi = { 40, 50, 60, 70, 71, 72, 73 };
for (int i = 0; i < 7; i++)//斜线判断
{
a = b = 0;
for (x = bi[i] / 10, y = bi[i] % 10; x >= 0 && y <= 7; x--, y++)
{
a = k(ai[x, y], 1, a);
b = k(ai[x, y], 2, b);
if (a >= 5)
return 1;
if (b >= 5)
return 2;
}
a = b = 0;
for (y = 7-bi[i] / 10, x = bi[i] % 10; x <= 7 && y <= 7; x++, y++)
{
a = k(ai[x, y], 1, a);
b = k(ai[x, y], 2, b);
if (a >= 5)
return 1;
if (b >= 5)
return 2;
}
}
return 0;
}
int k(int a, int b,int c)//为了简化代码而做的,无实际意义
{
if (a == b)
return c + 1;
return 0;
}
}
}
http://hi.baidu.com/%E1%DC%D8%A1%C0%D6%B6%F9/blog