C#五子棋
时间: 2023-07-12 13:06:35 浏览: 82
下面是一个简单的 C# 五子棋游戏示例,你可以参考它来开始编写你自己的五子棋游戏。这个示例使用了 Windows Forms 应用程序。
```
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FiveInARow
{
public partial class Form1 : Form
{
private int[,] board = new int[15, 15]; // 棋盘
private int player = 1; // 玩家,1 表示黑色,2 表示白色
private bool gameOver = false; // 游戏结束标志
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.BackColor = Color.Orange;
this.ClientSize = new Size(600, 600);
this.Paint += new PaintEventHandler(DrawBoard);
this.MouseClick += new MouseEventHandler(PlayChess);
}
private void DrawBoard(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.Orange);
for (int i = 0; i < 15; i++)
{
g.DrawLine(Pens.Black, 50, 50 + i * 40, 570, 50 + i * 40);
g.DrawLine(Pens.Black, 50 + i * 40, 50, 50 + i * 40, 570);
}
}
private void PlayChess(object sender, MouseEventArgs e)
{
if (gameOver)
{
return;
}
int x = e.X / 40;
int y = e.Y / 40;
if (board[x, y] != 0)
{
return;
}
board[x, y] = player;
Graphics g = this.CreateGraphics();
g.FillEllipse(player == 1 ? Brushes.Black : Brushes.White,
x * 40 - 16, y * 40 - 16, 32, 32);
if (CheckWin(x, y))
{
gameOver = true;
MessageBox.Show("Player " + player + " Wins!");
}
else
{
player = player == 1 ? 2 : 1;
}
}
private bool CheckWin(int x, int y)
{
int count = 1;
int i, j;
// 水平方向
for (i = x - 1; i >= 0 && board[i, y] == player; i--)
{
count++;
}
for (i = x + 1; i < 15 && board[i, y] == player; i++)
{
count++;
}
if (count >= 5) return true;
// 垂直方向
count = 1;
for (j = y - 1; j >= 0 && board[x, j] == player; j--)
{
count++;
}
for (j = y + 1; j < 15 && board[x, j] == player; j++)
{
count++;
}
if (count >= 5) return true;
// 左上到右下方向
count = 1;
for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i, j] == player; i--, j--)
{
count++;
}
for (i = x + 1, j = y + 1; i < 15 && j < 15 && board[i, j] == player; i++, j++)
{
count++;
}
if (count >= 5) return true;
// 右上到左下方向
count = 1;
for (i = x + 1, j = y - 1; i < 15 && j >= 0 && board[i, j] == player; i++, j--)
{
count++;
}
for (i = x - 1, j = y + 1; i >= 0 && j < 15 && board[i, j] == player; i--, j++)
{
count++;
}
if (count >= 5) return true;
return false;
}
}
}
```
这个示例实现了基本的五子棋游戏逻辑,包括绘制棋盘、落子、判断胜负等功能。当然,还有很多可以改进和扩展的地方,比如增加 AI 对手、添加音效、美化界面等等。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)