用C#窗体写一个不少于200行代码的项目来实现五子棋游戏

时间: 2024-02-15 09:03:39 浏览: 21
以下是一个简单的五子棋游戏实现,共计 200 多行代码,仅供参考: ``` using System; using System.Drawing; using System.Windows.Forms; namespace Gomoku { public partial class Form1 : Form { private int[,] board = new int[15, 15]; // 棋盘,0 代表无子,1 代表黑子,2 代表白子 private bool blackTurn = true; // 是否轮到黑方下棋 public Form1() { InitializeComponent(); this.Paint += new PaintEventHandler(Form1_Paint); this.MouseClick += new MouseEventHandler(Form1_MouseClick); } private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Pen pen = new Pen(Color.Black, 2); for (int i = 0; i < 15; i++) { g.DrawLine(pen, 30, 30 + i * 30, 420, 30 + i * 30); g.DrawLine(pen, 30 + i * 30, 30, 30 + i * 30, 420); } } private void Form1_MouseClick(object sender, MouseEventArgs e) { if (e.Button != MouseButtons.Left) { return; } int x = (e.X - 15) / 30; int y = (e.Y - 15) / 30; if (x < 0 || x >= 15 || y < 0 || y >= 15) { return; } if (board[x, y] != 0) { return; } board[x, y] = blackTurn ? 1 : 2; blackTurn = !blackTurn; this.Invalidate(); if (CheckWin(x, y)) { MessageBox.Show((board[x, y] == 1 ? "黑" : "白") + "方胜利!"); Reset(); } } private bool CheckWin(int x, int y) { // 横向 int count = 1; for (int i = x - 1; i >= 0; i--) { if (board[i, y] != board[x, y]) { break; } count++; } for (int i = x + 1; i < 15; i++) { if (board[i, y] != board[x, y]) { break; } count++; } if (count >= 5) { return true; } // 纵向 count = 1; for (int i = y - 1; i >= 0; i--) { if (board[x, i] != board[x, y]) { break; } count++; } for (int i = y + 1; i < 15; i++) { if (board[x, i] != board[x, y]) { break; } count++; } if (count >= 5) { return true; } // 左上到右下 count = 1; for (int i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--) { if (board[i, j] != board[x, y]) { break; } count++; } for (int i = x + 1, j = y + 1; i < 15 && j < 15; i++, j++) { if (board[i, j] != board[x, y]) { break; } count++; } if (count >= 5) { return true; } // 左下到右上 count = 1; for (int i = x - 1, j = y + 1; i >= 0 && j < 15; i--, j++) { if (board[i, j] != board[x, y]) { break; } count++; } for (int i = x + 1, j = y - 1; i < 15 && j >= 0; i++, j--) { if (board[i, j] != board[x, y]) { break; } count++; } if (count >= 5) { return true; } return false; } private void Reset() { for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { board[i, j] = 0; } } blackTurn = true; this.Invalidate(); } } } ``` 代码中用到了窗体的 `Paint` 和 `MouseClick` 事件来绘制棋盘和响应鼠标点击事件,用一个二维数组 `board` 来记录棋盘上每个位置的状态,用一个布尔变量 `blackTurn` 来记录当前是否轮到黑方下棋,用 `CheckWin` 方法来判断是否有一方获胜,用 `Reset` 方法来重置游戏。

相关推荐

最新推荐

recommend-type

在类库或winform项目中打开另一个winform项目窗体的方法

主要介绍了在类库或winform项目中打开另一个winform项目窗体的方法,可以实现Winform项目间窗体的调用,在进行Winform项目开发中非常具有实用价值,需要的朋友可以参考下
recommend-type

简单实现C#窗体程序判断是否闰年

主要介绍了简单实现C#窗体程序判断是否闰年的相关代码,禁止窗体调整大小,关闭窗体前的判断,感兴趣的小伙伴们可以参考一下
recommend-type

C# 实现dataGridView选中一行右键出现菜单的示例代码

主要介绍了C# 实现dataGridView选中一行右键出现菜单,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

C#使用读写锁三行代码简单解决多线程并发的问题

本文主要介绍了C#使用读写锁三行代码简单解决多线程并发写入文件时提示“文件正在由另一进程使用,因此该进程无法访问此文件”的问题。需要的朋友可以参考借鉴
recommend-type

c# 实现轮询算法实例代码

主要介绍了c# 实现轮询算法实例代码的相关资料,这里附有实例代码,具有一定的参考价值,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。