using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace wuziqi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//储存落点列表
List<Point> list1 = new List<Point>(250);
//记录棋盘及上面棋子信息
public int[,] chessboard = new int[15, 15];//0无子1黑色2白色
public int[] x = new int[250];
public int[] y = new int[250];
//判断胜负方法
public bool judgment(int[,] ChessBoard, int judgment)
{
bool win = false;
//遍历棋盘
for (int p = 0; p < 15; p++)
{
for (int q = 0; q < 15; q++)
{
if (ChessBoard[p, q] == judgment)
{
//水平向右
if (p < 11)
{
if (ChessBoard[p + 1, q] == judgment &&
ChessBoard[p + 2, q] == judgment &&
ChessBoard[p + 3, q] == judgment &&
ChessBoard[p + 4, q] == judgment)
{ win = true; }
}
//水平向左
if (p > 3)
{
if (ChessBoard[p - 1, q] == judgment &&
ChessBoard[p - 2, q] == judgment &&
ChessBoard[p - 3, q] == judgment &&
ChessBoard[p - 4, q] == judgment)
{ win = true; }
}
//垂直向上
if (q > 3)
{
if (ChessBoard[p , q-1] == judgment &&
ChessBoard[p , q-2] == judgment &&
ChessBoard[p , q-3] == judgment &&
ChessBoard[p , q-4] == judgment)
{ win = true; }
}
//垂直向下
if (q < 11)
{
if (ChessBoard[p, q + 1] == judgment &&
ChessBoard[p, q + 2] == judgment &&
ChessBoard[p, q + 3] == judgment &&
ChessBoard[p, q + 4] == judgment)
{ win = true; }
}
//斜向右上
if (q < 11 && p < 11)
{
if (ChessBoard[p + 1, q + 1] == judgment &&
ChessBoard[p + 2, q + 2] == judgment &&
ChessBoard[p + 3, q + 3] == judgment &&
ChessBoard[p + 4, q + 4] == judgment)
{ win = true; }
}
//斜向左下
if (p > 3 && q > 3)
{
if (ChessBoard[p - 1, q - 1] == judgment &&
ChessBoard[p - 2, q - 2] == judgment &&
ChessBoard[p - 3, q - 3] == judgment &&
ChessBoard[p - 4, q - 4] == judgment)
{ win = true; }
}
//斜向右下
if (q > 3 && p < 11)
{
if (ChessBoard[p + 1,q - 1]==judgment&&
ChessBoard[p + 2,q - 2]==judgment&&
ChessBoard[p + 3,q - 3]==judgment&&
ChessBoard[p + 4, q - 4] == judgment)
{ win = true; }
}
//斜向左上
if (p > 3 && q < 11)
{
if (ChessBoard[p - 1, q + 1] == judgment &&
ChessBoard[p - 2, q + 2] == judgment &&
ChessBoard[p - 3, q + 3] == judgment &&
ChessBoard[p - 4, q + 4] == judgment)
{ win = true; }
}
}
}
}
return win;
}
//绘制棋盘与落子
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, 1);
SolidBrush sb = new SolidBrush(Color.Black);
SolidBrush sw = new SolidBrush(Color.White);
//棋盘
for (int i = 0; i < 15; i++)
{
g.DrawLine(pen, 45 + i * 35, 45, 45 + i * 35, 45 + 35 * 14);
g.DrawLine(pen, 45, 45 + i * 35, 45 + 14 * 35, 45 + i * 35);
}
//落子
if (fupan == false)
{
for (int i = 0; i < list1.Count; i++)
{
if (i % 2 == 0)
{
label1.Text = "请落白子";
g.FillEllipse(sb, list1[i].X - 10, list1[i].Y - 10, 20, 20);
}
else
{
label1.Text = "请落黑子";
g.FillEllipse(sw, list1[i].X - 10, list1[i].Y - 10, 20, 20);
}
}
}
else
{
for (int i = 0; i < list2.Count; i++)
{
if (i % 2 == 0)
{
label1.Text = "黑子";
g.FillEllipse(sb, list2[i].X - 10, list1[i].Y - 10, 20, 20);
}
else
{
label1.Text = "白子";
g.FillEllipse(sw, list2[i].X - 10, list1[i].Y - 10, 20, 20);
}
}
}
}
//落子调整
public Point adjust(Point p)
{
Point adjustpoint = new Point(45 + (int)((p.X - 45) / 35.0 + 0.5) * 35,
45 + (int)((p.Y - 45) / 35.0 + 0.5) * 35);
return adjustpoint;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
//鼠标落子判断与储存
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
//判断是否超过边界
if (45 <= e.X && e.X <= 45 + 35 * 14 && 45 <= e.Y && 45 + 35 * 14 >= e.Y)
{
Point p = new Point(e.X, e.Y);
//重复落子警告
if (list1.Contains(adjust(p)))
{
MessageBox.Show("不能重复落子!", "警告", MessageBoxButtons.OK);
}
else
{
Point p2 = adjust(p);
timer2.Start();
a = 100;
list1.Add(p2);
x[list1.Count - 1] = (p2.X - 45) / 35;
y[list1.Count - 1] = (p2.Y - 45) / 35;
if (list1.Count % 2 == 1)
chessboard[x[list1.Count - 1], y[list1.Count - 1]] = 1;
else
chessboard[x[list1.Count - 1], y[list1.Count - 1]] = 2;
//刷新paint
pictureBox1.Invalidate();
//胜负检测
if (list1.Count > 0 && judgment(chessboard,
chessboard[x[list1.Count - 1], y[list1.Count - 1]]))
{
timer2.Stop();
if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 1)
{
MessageBox.Show("黑子获胜");
}
else if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 2)
{
MessageBox.Show("白子获胜");
}
}
}
}
}
private void textBox游戏状态_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
//悔棋
private void button悔棋_Click(object sender, EventArgs e)
{
if (list1.Count > 0)
{
chessboard[x[list1.Count - 1], y[list1.Count - 1]] = 0;
list1.RemoveAt(list1.Count - 1);
a = 100;
pictureBox1.Invalidate();
}
else
{
MessageBox.Show("无棋可悔", "提示");
}
}
//复盘
//初始化
bool fupan = false;
List<Point> list2 = new List<Point>(250);
//点击开始复盘
private void button复盘_Click(object sender, EventArgs e)
{
fupan = true;
timer1.Enabled = true;
timer2.Stop();
}
//利用计时器ticker开始复盘
int i = 0;
private void timer1_Tick(object sender, EventArgs e)
{
if (list2.Count < list1.Count)
{
list2.Add(list1[i]);
i++;
pictureBox1.Invalidate();
}
else
{
timer1.Enabled = false;
list2.Clear();
i = 0;
fupan = false;
MessageBox.Show("复盘结束", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
//倒计时
int a=100;
private void timer2_Tick(object sender, EventArgs e)
{
if (a > 0)
{
a--;
textBox时间.Text = (a / 60).ToString().PadLeft(2, '0') +
":" + (a % 60).ToString().PadLeft(2, '0');
}
else
{
timer2.Stop();
if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 1)
{
MessageBox.Show("白子超时,黑子获胜");
}
else if (chessboard[x[list1.Count - 1], y[list1.Count - 1]] == 2)
{
MessageBox.Show("黑子超时,白子获胜");
}
}
}
private void textBox思考时间_TextChanged(object sender, EventArgs e)
{
}
//改变背景色
private void 改变棋盘颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
pictureBox1.BackColor = colorDialog1.Color;
}
}
}
五子棋c#
最新推荐文章于 2024-05-16 22:35:53 发布