用winform写一个五子棋游戏
时间: 2024-05-08 13:21:43 浏览: 151
好的,以下是用 Winform 写的五子棋游戏示例代码:
1. 首先,需要设计游戏界面。可以在窗体上添加一个 PictureBox 控件作为游戏棋盘,再添加一些按钮控件用于开始游戏、重新开始、悔棋等操作。
2. 在 PictureBox 的 Paint 事件中绘制游戏棋盘,可以使用 Graphics 绘图类。
3. 在 PictureBox 的 MouseClick 事件中处理玩家下棋,可以根据鼠标点击的位置计算出对应的棋盘坐标,然后在棋盘上绘制棋子。
4. 设计一个 Chess 类表示棋子,包括颜色、位置等属性。
5. 实现游戏规则,判断是否有玩家胜利或者平局。
下面是示例代码:
```
public partial class Form1 : Form
{
private const int ChessboardSize = 15; // 棋盘大小
private const int ChessSize = 30; // 棋子大小
private const int ChessboardMargin = 20; // 棋盘边距
private const int ChessboardPadding = 15; // 棋盘线条粗细
private Chess[,] chessboard = new Chess[ChessboardSize, ChessboardSize]; // 棋盘
private bool isBlackTurn = true; // 黑棋先手
public Form1()
{
InitializeComponent();
ResetChessboard();
}
// 绘制棋盘
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen = new Pen(Color.Black, ChessboardPadding);
Brush brush = Brushes.Black;
// 绘制横线
for (int i = 0; i < ChessboardSize; i++)
{
int y = ChessboardMargin + i * ChessSize;
g.DrawLine(pen, ChessboardMargin, y, ChessboardMargin + ChessSize * (ChessboardSize - 1), y);
}
// 绘制竖线
for (int i = 0; i < ChessboardSize; i++)
{
int x = ChessboardMargin + i * ChessSize;
g.DrawLine(pen, x, ChessboardMargin, x, ChessboardMargin + ChessSize * (ChessboardSize - 1));
}
// 绘制棋子
foreach (Chess chess in chessboard)
{
if (chess != null)
{
brush = chess.IsBlack ? Brushes.Black : Brushes.White;
g.FillEllipse(brush, ChessboardMargin + chess.X * ChessSize - ChessSize / 2, ChessboardMargin + chess.Y * ChessSize - ChessSize / 2, ChessSize, ChessSize);
}
}
}
// 处理玩家下棋
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
int x = (e.X - ChessboardMargin + ChessSize / 2) / ChessSize;
int y = (e.Y - ChessboardMargin + ChessSize / 2) / ChessSize;
if (x < 0 || x >= ChessboardSize || y < 0 || y >= ChessboardSize) return;
if (chessboard[x, y] != null) return;
Chess chess = new Chess(isBlackTurn, x, y);
chessboard[x, y] = chess;
isBlackTurn = !isBlackTurn;
pictureBox1.Invalidate();
if (CheckWin(chess))
{
string message = (chess.IsBlack ? "黑棋" : "白棋") + "胜利!";
MessageBox.Show(message);
ResetChessboard();
}
else if (CheckDraw())
{
MessageBox.Show("平局!");
ResetChessboard();
}
}
// 判断是否有玩家胜利
private bool CheckWin(Chess chess)
{
int x = chess.X;
int y = chess.Y;
// 判断横向
int count = 1;
for (int i = x - 1; i >= 0 && chessboard[i, y] != null && chessboard[i, y].IsBlack == chess.IsBlack; i--)
{
count++;
}
for (int i = x + 1; i < ChessboardSize && chessboard[i, y] != null && chessboard[i, y].IsBlack == chess.IsBlack; i++)
{
count++;
}
if (count >= 5) return true;
// 判断纵向
count = 1;
for (int i = y - 1; i >= 0 && chessboard[x, i] != null && chessboard[x, i].IsBlack == chess.IsBlack; i--)
{
count++;
}
for (int i = y + 1; i < ChessboardSize && chessboard[x, i] != null && chessboard[x, i].IsBlack == chess.IsBlack; i++)
{
count++;
}
if (count >= 5) return true;
// 判断左斜向
count = 1;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && chessboard[i, j] != null && chessboard[i, j].IsBlack == chess.IsBlack; i--, j--)
{
count++;
}
for (int i = x + 1, j = y + 1; i < ChessboardSize && j < ChessboardSize && chessboard[i, j] != null && chessboard[i, j].IsBlack == chess.IsBlack; i++, j++)
{
count++;
}
if (count >= 5) return true;
// 判断右斜向
count = 1;
for (int i = x + 1, j = y - 1; i < ChessboardSize && j >= 0 && chessboard[i, j] != null && chessboard[i, j].IsBlack == chess.IsBlack; i++, j--)
{
count++;
}
for (int i = x - 1, j = y + 1; i >= 0 && j < ChessboardSize && chessboard[i, j] != null && chessboard[i, j].IsBlack == chess.IsBlack; i--, j++)
{
count++;
}
if (count >= 5) return true;
return false;
}
// 判断是否平局
private bool CheckDraw()
{
foreach (Chess chess in chessboard)
{
if (chess == null) return false;
}
return true;
}
// 重新开始游戏
private void btnRestart_Click(object sender, EventArgs e)
{
ResetChessboard();
}
// 悔棋
private void btnUndo_Click(object sender, EventArgs e)
{
// TODO: 实现悔棋
}
// 重置棋盘
private void ResetChessboard()
{
for (int i = 0; i < ChessboardSize; i++)
{
for (int j = 0; j < ChessboardSize; j++)
{
chessboard[i, j] = null;
}
}
isBlackTurn = true;
pictureBox1.Invalidate();
}
}
// 棋子类
public class Chess
{
public bool IsBlack { get; set; }
public int X { get; set; }
public int Y { get; set; }
public Chess(bool isBlack, int x, int y)
{
IsBlack = isBlack;
X = x;
Y = y;
}
}
```
这是一个简单的五子棋游戏示例,可以根据需求进行扩展和优化。
阅读全文