enum QZ
{
Empty,
White,
Black
}
//黑先下
public class w7 : Control
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form f = new Form();
f.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
f.ClientSize = new System.Drawing.Size(560, 550);
w7 w = new w7();
w.Dock = System.Windows.Forms.DockStyle.Fill;
f.Controls.Add(w);
Application.Run(f);
}
/// <summary>
/// 是否正在游戏,结果时为false
/// </summary>
bool IsPlay = true;
/// <summary>
/// 赢还是输
/// </summary>
bool iswin = false;
/// <summary>
/// 鼠标当前的位置
/// </summary>
Point? nowPost = null;
/// <summary>
/// 黑子白字的状态数组
/// </summary>
QZ[,] symbol = new QZ[15, 15];
public w7()
{
//使用双缓冲机制,解决闪屏问题
SetStyle(ControlStyles.UserPaint | ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true);
UpdateStyles();
Size = new System.Drawing.Size(562, 557);
BackColor = System.Drawing.Color.BurlyWood;
TabStop = false;
Paint += w7_Paint;
MouseDown += w7_MouseDown;
MouseMove += w7_MouseMove;
initGame();
}
/// <summary>
/// 画棋盘
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void w7_Paint(object sender, PaintEventArgs e)
{
//绘图图面
Graphics g = e.Graphics;
//画棋盘
//绘笔
Pen p = new Pen(Color.Black);
for (int i = 1; i <= 15; i++)
{
//绘制线
g.DrawLine(p, i * 35, 35 * 1, i * 35, 15 * 35);
}
for (int i = 1; i <= 15; i++)
{
g.DrawLine(p, 1 * 35, i * 35, 15 * 35, i * 35);
}
SolidBrush bb = new SolidBrush(Color.Black);
Rectangle yuan1 = new Rectangle(3 * 35 - 5 + 35, 3 * 35 - 5 + 35, 10, 10);
g.FillEllipse(bb, yuan1);
Rectangle yuan2 = new Rectangle(3 * 35 - 5 + 35, 11 * 35 - 5 + 35, 10, 10);
g.FillEllipse(bb, yuan2);
Rectangle yuan3 = new Rectangle(11 * 35 - 5 + 35, 3 * 35 - 5 + 35, 10, 10);
g.FillEllipse(bb, yuan3);
Rectangle yuan4 = new Rectangle(11 * 35 - 5 + 35, 11 * 35 - 5 + 35, 10, 10);
g.FillEllipse(bb, yuan4);
//画棋子
for (int y = 0; y < 15; y++)
for (int x = 0; x < 15; x++)
if (symbol[y, x] != QZ.Empty)
{
Color c = Color.White;
if (symbol[y, x] == QZ.Black)
c = Color.Black;
SolidBrush b = new SolidBrush(c);
Rectangle yuan = new Rectangle(x * 35 - 11 + 35, y * 35 - 11 + 35, 22, 22);
//填充边框所定义的椭圆的内部
g.FillEllipse(b, yuan);
}
//当前鼠标位置
if (nowPost != null)
{
Color c = Color.Gray;
SolidBrush b = new SolidBrush(c);
Rectangle yuan = new Rectangle(nowPost.Value.X * 35 - 11 + 35, nowPost.Value.Y * 35 - 11 + 35, 22, 22);
//填充边框所定义的椭圆的内部
g.FillEllipse(b, yuan);
}
}
/// <summary>
/// 判断放置棋子的位置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void w7_MouseDown(object sender, MouseEventArgs e)
{
//如果没有开始则不执行
if (IsPlay == false)
{
showMessage();
return;
}
//是否在棋盘内
if (25 > e.X || e.X > 535 || 25 > e.Y || e.Y > 535)
{
//MessageBox.Show("请在棋盘内放棋子");
return;
}
//棋盘坐标
Point qPoint = getQPXY(e.X, e.Y);
//如果这个位置已存在棋
if (symbol[qPoint.Y, qPoint.X] != QZ.Empty)
return;
//放入相应坐标[列,行] = 黑子
symbol[qPoint.Y, qPoint.X] = QZ.Black;
Refresh();
if (check(symbol, qPoint.X, qPoint.Y, QZ.Black))
{
iswin = true;
IsPlay = false;
showMessage();
return;
}
//电脑下
List<Point> points = find4(symbol, QZ.Black);
if (points.Count > 0)
{
Point dPoint = points[new Random().Next(points.Count)];
symbol[dPoint.Y, dPoint.X] = QZ.White;
Refresh();
if (check(symbol, dPoint.X, dPoint.Y, QZ.White))
{
iswin = false;
IsPlay = false;
showMessage();
return;
}
return;
}
}
void showMessage()
{
if (IsPlay) return;
string[] message = { "恭喜获胜", "输了" };
MessageBox.Show(iswin ? message[0] : message[1]);
//清空 数组
DialogResult d = MessageBox.Show("是否重新开始?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (d == DialogResult.Yes)
{
initGame();
Refresh();
}
}
/// <summary>
/// 控件坐标,转换成棋盘坐标
/// </summary>
/// <param name="sx"></param>
/// <param name="sy"></param>
/// <returns></returns>
private Point getQPXY(int x, int y)
{
int qX = (x - 15) / 35;
int qY = (y - 15) / 35;
//看是否进入其它的位置
int xyu = x / 35;
int yyu = y / 35;
if (xyu < 17.5 && yyu < 17.5)
{
}
if ((xyu > 17.5 && yyu > 17.5))
{
qX++;
qY++;
}
if (xyu > 17.5 && yyu < 17.5)
{
qX++;
}
if ((xyu < 17.5 && yyu > 17.5))
{
qY++;
}
return new Point(qX, qY);
}
protected void w7_MouseMove(object sender, MouseEventArgs e)
{
if (!IsPlay)
{
if (nowPost != null)
{
nowPost = null;
Refresh();
}
return;
}
//是否在棋盘内
if (25 > e.X || e.X > 535 || 25 > e.Y || e.Y > 535)
{
//MessageBox.Show("请在棋盘内放棋子");
if (nowPost != null)
{
nowPost = null;
Refresh();
}
return;
}
//棋盘坐标
Point qPoint = getQPXY(e.X, e.Y);
//如果这个位置已存在棋
if (symbol[qPoint.Y, qPoint.X] != QZ.Empty)
{
if (nowPost != null)
{
nowPost = null;
Refresh();
}
return;
}
if (nowPost == null)
nowPost = qPoint;
else if (nowPost.Value.Equals(qPoint))
return;
else nowPost = qPoint;
Refresh();
}
void clear(QZ[,] symbol)
{
for (int i = 0; i < 15; i++)
for (int j = 0; j < 15; j++)
symbol[i, j] = QZ.Empty;
}
void initGame()
{
clear(symbol);
IsPlay = true;
}
/// <summary>
/// 是否在x,y,位置hb棋胜利
/// </summary>
/// <param name="symbol"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="hb"></param>
/// <returns></returns>
bool check(QZ[,] symbol, int x, int y, QZ hb)
{
#region - | init
int minX = x >= 4 ? x - 4 : 0;
int maxX = x >= 10 ? 14 : x + 4;
int minY = y >= 4 ? y - 4 : 0;
int maxY = y >= 10 ? 14 : y + 4;
#endregion - | init
#region -
int count = 0;
for (int i = minX; i <= maxX; i++)
{
if (symbol[y, i] == hb)
{
count++;
if (count == 5)
return true;
}
else count = 0;
}
#endregion -
#region |
count = 0;
for (int i = minY; i <= maxY; i++)
{
if (symbol[i, x] == hb)
{
count++;
if (count == 5)
return true;
}
else count = 0;
}
#endregion |
#region init \
if (x - minX > y - minY)
{
minX = x - (y - minY);
}
if (x - minX < y - minY)
{
minY = y - (x - minX);
}
if (maxX - x > maxY - y)
{
maxX = x + (maxY - y);
}
if (maxX - x < maxY - y)
{
maxY = y + (maxX - x);
}
#endregion init \ /
#region \
count = 0;
for (int i = 0; i <= maxY - minY; i++)
{
if (symbol[minY + i, minX + i] == hb)
{
count++;
if (count == 5)
return true;
}
else count = 0;
}
#endregion \
#region /
minX = x >= 4 ? x - 4 : 0;
maxX = x >= 10 ? 14 : x + 4;
minY = y >= 4 ? y - 4 : 0;
maxY = y >= 10 ? 14 : y + 4;
if (x - minX > maxY - y)
{
minX = x - (maxY - y);
}
if (x - minX < maxY - y)
{
maxY = y + (x - minY);
}
if (maxX - x > y - minY)
{
maxX = x + (y - minY);
}
if (maxX - x < y - minY)
{
minY = y - (maxX - x);
}
count = 0;
for (int i = 0; i <= maxY - minY; i++)
{
if (symbol[minY + i, maxX - i] == hb)
{
count++;
if (count == 5)
return true;
}
else count = 0;
}
#endregion /
return false;
}
/// <summary>
/// AI,找到hb要下的位置,四连
/// </summary>
/// <param name="symbol"></param>
/// <param name="hb"></param>
/// <returns></returns>
List<Point> find4(QZ[,] symbol, QZ hb)
{
List<Point> points = new List<Point>();
#region -
for (int y = 0; y < 15; y++)
for (int x = 0; x < 15 - 4; )
{
bool over = true;
int nowx = -1;
int count = 0;
for (int i = 0; i < 5; i++)
{
if (symbol[y, x + i] == hb)
count++;
else if (symbol[y, x + i] == QZ.Empty)
{
if (nowx == -1)
nowx = x + i;
else
{
nowx = x + i - 1;
over = false;
break;
}
}
else
{
x += i;
over = false;
break;
}
}
if (over && count == 4)
{
points.Add(new Point(nowx, y));
x = nowx + 1;
}
else
x++;
}
#endregion -
#region |
for (int x = 0; x < 15; x++)
for (int y = 0; y < 15 - 4; )
{
bool over = true;
int nowy = -1;
int count = 0;
for (int i = 0; i < 5; i++)
{
if (symbol[y + i, x] == hb)
count++;
else if (symbol[y + i, x] == QZ.Empty)
{
if (nowy == -1)
nowy = y + i;
else
{
nowy = y + i - 1;
over = false;
break;
}
}
else
{
y += i;
over = false;
break;
}
}
if (over && count == 4)
{
points.Add(new Point(x, nowy));
y = nowy + 1;
}
else
y++;
}
#endregion|
#region \
for (int sx = 0; sx < 15 - 4; sx++)
{
int x = sx;
for (int y = 0; y < 15 - 4 && x < 15 - 4; )
{
bool over = true;
int oi = -1;
int count = 0;
for (int i = 0; i < 5; i++)
{
//越界
if (x + i >= 15 || y + i >= 15)
{
y += i;
break;
}
if (symbol[y + i, x + i] == hb)
count++;
else if (symbol[y + i, x + i] == QZ.Empty)
{
if (oi == -1)
{
oi = i;
}
}
else
{
y += i;
x += i;
over = false;
break;
}
}
if (over && count == 4)
{
points.Add(new Point(x + oi, y + oi));
y = oi + 1;
x = oi + 1;
}
else
{
y++;
x++;
}
}
}
for (int sy = 0; sy < 15 - 4; sy++)
{
int y = sy;
for (int x = 0; x < 15 - 4 && y < 15 - 4; )
{
bool over = true;
int oi = -1;
int count = 0;
for (int i = 0; i < 5; i++)
{
//越界
if (x + i >= 15 || y + i >= 15)
{
y += i;
break;
}
if (symbol[y + i, x + i] == hb)
count++;
else if (symbol[y + i, x + i] == QZ.Empty)
{
if (oi == -1)
{
oi = i;
}
}
else
{
y += i;
x += i;
over = false;
break;
}
}
if (over && count == 4)
{
points.Add(new Point(x + oi, y + oi));
y = oi + 1;
x = oi + 1;
}
else
{
y++;
x++;
}
}
}
#endregion \
#region /
for (int sx = 4; sx < 15; sx++)
{
int x = sx;
for (int y = 0; y < 15 - 4 && x > 3; )
{
bool over = true;
int oi = -1;
int count = 0;
for (int i = 0; i < 5; i++)
{
//越界
if (x - i < 0 || y + i >= 15)
{
y += i;
break;
}
if (symbol[y + i, x - i] == hb)
count++;
else if (symbol[y + i, x - i] == QZ.Empty)
{
if (oi == -1)
{
oi = i;
}
}
else
{
y += i;
x -= i;
over = false;
break;
}
}
if (over && count == 4)
{
points.Add(new Point(x - oi, y + oi));
y += oi + 1;
x += -oi - 1;
}
else
{
y++;
x--;
}
}
}
for (int sy = 0; sy < 15 - 4; sy++)
{
int y = sy;
for (int x = 14; x > 0 && y < 15 - 4; )
{
bool over = true;
int oi = -1;
int count = 0;
for (int i = 0; i < 5; i++)
{
//越界
if (x - i < 0 || y + i >= 15)
{
y += i;
break;
}
if (symbol[y + i, x - i] == hb)
count++;
else if (symbol[y + i, x - i] == QZ.Empty)
{
if (oi == -1)
{
oi = i;
}
}
else
{
y += i;
x -= i;
over = false;
break;
}
}
if (over && count == 4)
{
points.Add(new Point(x - oi, y + oi));
y += oi + 1;
x += -oi - 1;
}
else
{
y++;
x--;
}
}
}
#endregion /
return points;
}
}
只能找四目
五子棋
最新推荐文章于 2025-06-18 10:19:35 发布