当前位置: 首页 > news >正文

C# Winform实现五子棋游戏(代完善)

实现了基本的玩法。

BoardController.cs

using System;namespace GomokuGame
{public class BoardController{private static BoardController instance;private readonly int[,] board;private const int boardSize = 15;private BoardController(){board = new int[boardSize, boardSize];}public static BoardController Instance{get{if (instance == null){instance = new BoardController();}return instance;}}public int[,] GetBoard() => board;public bool PlacePiece(int x, int y, int player){if (board[x, y] == 0){board[x, y] = player;return true;}return false;}public bool CheckWin(int player){for (int i = 0; i < boardSize; i++){for (int j = 0; j < boardSize; j++){if (board[i, j] == player){if (CheckDirection(i, j, 1, 0, player) || // HorizontalCheckDirection(i, j, 0, 1, player) || // VerticalCheckDirection(i, j, 1, 1, player) || // Diagonal \CheckDirection(i, j, 1, -1, player))  // Diagonal /{return true;}}}}return false;}private bool CheckDirection(int startX, int startY, int dx, int dy, int player){int count = 0;for (int i = 0; i < 5; i++){int x = startX + i * dx;int y = startY + i * dy;if (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x, y] == player){count++;}else{break;}}return count == 5;}}
}

BoardView.cs

using System;
using System.Drawing;
using System.Windows.Forms;namespace GomokuGame
{public class BoardView : Panel{private const int cellSize = 30;private const int boardSize = 15;private int[,] board;private int currentPlayer;public BoardView(){this.DoubleBuffered = true;this.Size = new Size(boardSize * cellSize, boardSize * cellSize);board = BoardController.Instance.GetBoard();currentPlayer = 1;this.Paint += BoardView_Paint;this.MouseClick += BoardView_MouseClick;}private void BoardView_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;for (int i = 0; i < boardSize; i++){for (int j = 0; j < boardSize; j++){g.DrawRectangle(Pens.Black, i * cellSize, j * cellSize, cellSize, cellSize);if (board[i, j] == 1){g.FillEllipse(Brushes.Black, i * cellSize, j * cellSize, cellSize, cellSize);}else if (board[i, j] == 2){g.FillEllipse(Brushes.Black, i * cellSize, j * cellSize, cellSize, cellSize);g.FillEllipse(Brushes.White, i * cellSize + 2, j * cellSize + 2, cellSize - 4, cellSize - 4);}}}}private void BoardView_MouseClick(object sender, MouseEventArgs e){int x = e.X / cellSize;int y = e.Y / cellSize;if (BoardController.Instance.PlacePiece(x, y, currentPlayer)){this.Invalidate();if (BoardController.Instance.CheckWin(currentPlayer)){MessageBox.Show($"Player {currentPlayer} wins!");}// 交换玩家currentPlayer = currentPlayer == 1 ? 2 : 1;}}}
}

Form1.cs

using System;
using System.Windows.Forms;namespace GomokuGame
{public partial class Form1 : Form{public Form1(){InitializeComponent();InitializeGame();}private void InitializeGame(){BoardView boardView = new BoardView();boardView.Dock = DockStyle.Fill;this.Controls.Add(boardView);IGameStrategy strategy = new PvPStrategy();strategy.Execute();}}
}

Form1.Designer.cs

using System;
using System.Windows.Forms;namespace GomokuGame
{partial class Form1 :  Form{private System.ComponentModel.IContainer components = null;protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}private void InitializeComponent(){this.SuspendLayout();// // Form1// this.ClientSize = new System.Drawing.Size(800, 450);this.Name = "Form1";this.ResumeLayout(false);}}
}

GameStrategy.cs

using System;public interface IGameStrategy
{void Execute();
}public class PvPStrategy : IGameStrategy
{public void Execute(){Console.WriteLine("Player vs Player mode.");}
}public class PvAIStrategy : IGameStrategy
{public void Execute(){Console.WriteLine("Player vs AI mode.");}
}

PieceFactory.cs

using System;public abstract class Piece
{public abstract void Place(int x, int y);
}public class BlackPiece : Piece
{public override void Place(int x, int y){Console.WriteLine($"Placed black piece at ({x}, {y})");}
}public class WhitePiece : Piece
{public override void Place(int x, int y){Console.WriteLine($"Placed white piece at ({x}, {y})");}
}public class PieceFactory
{public static Piece CreatePiece(int player){return player == 1 ? new BlackPiece() : (Piece)new WhitePiece();}
}

PlacePieceCommand.cs

using GomokuGame;
public interface ICommand
{void Execute();
}public class PlacePieceCommand : ICommand
{private readonly int x;private readonly int y;private readonly int player;public PlacePieceCommand(int x, int y, int player){this.x = x;this.y = y;this.player = player;}public void Execute(){BoardController.Instance.PlacePiece(x, y, player);PieceFactory.CreatePiece(player).Place(x, y);}
}

Program.cs

using System;
using System.Windows.Forms;namespace GomokuGame
{static class Program{[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new Form1());}}
}

完整代码下载:https://download.csdn.net/download/exlink2012/89317787

相关文章:

C# Winform实现五子棋游戏(代完善)

实现了基本的玩法。 BoardController.cs using System;namespace GomokuGame {public class BoardController{private static BoardController instance;private readonly int[,] board;private const int boardSize 15;private BoardController(){board new int[boardSize…...

文档档案管理系统整体建设方案书(实际项目原件word2024)

1.系统概述 1.1.需求描述 1.2.需求分析 1.3.重难点分析 1.4.重难点解决措施 2.系统架构设计 2.1.系统架构图 2.2.关键技术 数据备份技术 3.系统功能设计 3.1.功能清单列表 3.2.基础数据管理 3.3.位置管理 3.4.文档使用 3.5.文档管理 软件全套资料包获取方式①&#xff1a;软件项…...

React与Vue的区别?

一、区别: 1. 语法 Vue采用自己特有的模板语法&#xff1b; React是单向的&#xff0c;采用jsx语法创建react元素。 2.监听数据变化的实现原理不同 Vue2.0 通过Object.defineproperty()方法的getter/setter属性, 实现数据劫持, 每次修改完数据会触发diff算法(双端对比) …...

leetcode 2115.从给定原材料中找到所有可以做出的菜

思路&#xff1a;拓补排序&#xff0c;哈希表 在思路上其实很好发现&#xff0c;我们需要有一个明确的做菜顺序&#xff0c;也就是说需要定下来我们根据原材料先做哪些菜&#xff0c;然后做完该做的菜之后&#xff0c;后来又能做哪些菜。 你也发现了&#xff0c;这个顺序其实…...

Opencompass模型评测教程

模型评测 模型评测非常关键&#xff0c;目前主流的方法主要可以概括为主观评测和客观评测&#xff0c;主观评测又可以分为两种形式&#xff1a;人工判断或者和模型竞技场。客观评测一般采用评测数据集的形式进行模型评测。本教程使用Opencompass工具进行对Internlm2-7b模型进行…...

什么是安全测试,如何进行安全测试?

什么是安全测试&#xff1f; 概述 安全测试是一种旨在识别系统、网络或应用程序中的安全漏洞的测试方法。其目标是确保系统能够抵御恶意攻击&#xff0c;保护数据的机密性、完整性和可用性。安全测试通常包括漏洞扫描、渗透测试、代码审计和安全评估等多个方面。 安全测试的…...

ros的pcl库中对于自己定义的消息,调用pcl库时总是报错 c++

首先定义自己的消息类型 struct CustomPoint { // 定义点类型结构PCL_ADD_POINT4D; // 该点类型有4个元素float intensity 0.0;uint32_t zone;uint32_t ring;uint32_t sector;EIGEN_MAKE_ALIGNED_OPERATOR_NEW // 确保new操作符对齐操作 } EIGEN_ALIGN16; // 强制SSE对齐POIN…...

DataFrame—数据汇总6

文章最前&#xff1a; 我是Octopus&#xff0c;这个名字来源于我的中文名--章鱼&#xff1b;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github &#xff1b;这博客是记录我学习的点点滴滴&#xff0c;如果您对 Python、Java、AI、算法有兴趣&#xff0c;可以关注我的…...

Java入门基础学习笔记41——实体类

实体JavaBean/实体类&#xff1a; 就是一种特殊形式的类。 1&#xff09;这个类中的成员变量都要私有&#xff0c;并且要对外提供相应的getXXX&#xff0c;setXXX的方法。 2&#xff09;类中必须要有一个公共的无参的构造器。其他的构造器可写可不写。 右键菜单中&#xff0…...

【Linux】信号之信号的保存和处理详解

&#x1f916;个人主页&#xff1a;晚风相伴-CSDN博客 &#x1f496;如果觉得内容对你有帮助的话&#xff0c;还请给博主一键三连&#xff08;点赞&#x1f49c;、收藏&#x1f9e1;、关注&#x1f49a;&#xff09;吧 &#x1f64f;如果内容有误或者有写的不好的地方的话&…...

基于Django的图书管理系统

文章目录 前言一、页面展示1.登录2.前端页面3.后端页面 二、项目上传&#xff08;1&#xff09;导入数据库&#xff08;2&#xff09;导入项目&#xff08;3&#xff09;数据库密码修改&#xff08;4&#xff09;进入网站 总结 前言 本网站调用Django编写了图书管理网站&#…...

js实现元素根据鼠标滚轮滚动向左右上下滑动着从模糊到清楚显示出来

html代码 <div ref{test} id"animatedElement" className"not-animated"> <div style{{width:"100px",height:"50px",backgroundColor:"red"}}> </div> </div> JS代码 const te…...

yocto学习

bitbake命令单独编译u-boot&#xff1a; $ bitbake -c compile -f u-boot-imx $ bitbake -c deploy -f u-boot-imx //部署编译生成的u-boot镜像到deploy bitbake命令单独编译kernel&#xff1a; bitbake -c compile -f linux-imx //编译内核 bitbake -c deploy -f linux-imx /…...

【IC设计】牛客网-序列检测习题总结

文章目录 状态机基础知识VL25 输入序列连续的序列检测VL26 含有无关项的序列检测VL27 不重叠序列检测VL28 输入序列不连续的序列检测参考资料 状态机基础知识 VL25 输入序列连续的序列检测 timescale 1ns/1ns module sequence_detect(input clk,input rst_n,input a,output re…...

python爬虫登录到海康相机管理页面

简述 1.最近接到个任务是在管理页面更改相机的某个参数&#xff0c;下载官方的sdk貌似没有提供这个接口&#xff0c;所以只能自己写爬虫登录发请求了。 1.主要步骤 1.1 发送get请求获取到salt&#xff0c;sessionID&#xff0c;challenge等信息 http://admin:123456192.168.…...

9.Docker网络

文章目录 1、Docker网络简介2、常用基本命令3、网络模式对比举例3.1、bridge模式3.2、host模式3.3、none模式3.4、container模式3.5、自定义网络 1、Docker网络简介 作用&#xff1a; 容器间的互联和通信以及端口映射容器IP变动时候可以通过服务名直接进行网络通信而不受到影…...

Windows VS2022 C语言使用 sqlite3.dll 访问 SQLite数据库

今天接到一个学生C语言访问SQLite数据库的的需求: 第一步,SQLite Download Page下载 sqlite3.dll 库 下载解压,发现只有两个文件: 于是使用x64 Native Tools Command Prompt 终端 生成 sqlite3.lib 和 sqlite3.exp文件 LIB -def:sqlite3.def -out:sqlite3.lib -machin…...

java库和包的概念

在Java中&#xff0c;"库"和"包"是两个不同的概念&#xff0c;但它们之间存在着密切的关联。 库&#xff08;Library&#xff09; 定义&#xff1a;库是一组已经编写好的代码和资源&#xff0c;用于解决特定的问题或提供特定的功能。它可以包含一个或多个…...

mysql内存结构

一&#xff1a;逻辑存储结构&#xff1a;表空间->段->区->页->行、 表空间&#xff1a;一个mysql实例对应多个表空间&#xff0c;用于存储记录&#xff0c;索引等数据。 段&#xff1a;分为数据段&#xff0c;索引段&#xff0c;回滚段。innoDB是索引组织表&…...

Python | Leetcode Python题解之第111题二叉树的最小深度

题目&#xff1a; 题解&#xff1a; class Solution:def minDepth(self, root: TreeNode) -> int:if not root:return 0que collections.deque([(root, 1)])while que:node, depth que.popleft()if not node.left and not node.right:return depthif node.left:que.appen…...

别再只用Service了!ROS1 Action通信保姆级教程:从导航进度条到任务取消,手把手教你实现带反馈的机器人任务

别再只用Service了&#xff01;ROS1 Action通信保姆级教程&#xff1a;从导航进度条到任务取消&#xff0c;手把手教你实现带反馈的机器人任务当你的机器人正在执行一个长达10分钟的导航任务时&#xff0c;突然发现目标点设置错误&#xff0c;这时候如果只能干等着任务完成或者…...

用Python和MNE库玩转BCI Competition IV 2a脑电数据集:从数据加载到可视化全流程

用Python和MNE库玩转BCI Competition IV 2a脑电数据集&#xff1a;从数据加载到可视化全流程当你第一次接触脑电信号处理时&#xff0c;面对原始数据文件可能会感到无从下手。BCI Competition IV 2a数据集作为脑机接口领域的经典基准数据&#xff0c;包含了9名受试者四种运动想…...

告别网盘客户端!用Alist+RaiDrive把百度云盘变成电脑本地文件夹(保姆级图文教程)

用AlistRaiDrive实现网盘本地化管理的终极方案 你是否厌倦了电脑上安装多个网盘客户端&#xff0c;不仅占用系统资源&#xff0c;操作还繁琐割裂&#xff1f;每次上传下载文件都要在不同客户端间切换&#xff0c;效率低下。现在&#xff0c;通过Alist和RaiDrive的组合&#xf…...

App Inventor蓝牙调试避坑指南:从连接失败到数据乱码,一次讲清所有常见问题

App Inventor蓝牙调试避坑指南&#xff1a;从连接失败到数据乱码的实战解决方案在移动应用开发领域&#xff0c;蓝牙通信一直是实现设备间短距离数据交换的核心技术之一。对于使用App Inventor的开发者而言&#xff0c;蓝牙模块提供了无需复杂编码即可实现无线通信的便捷途径。…...

基于Arduino的模块化DIY智能时钟:从RTC到RGB LED的完整实现

1. 项目概述&#xff1a;打造一台高度可定制的DIY RGB LED时钟如果你和我一样&#xff0c;对市面上千篇一律的电子钟感到审美疲劳&#xff0c;同时又对Arduino和电子DIY充满热情&#xff0c;那么这个项目可能就是为你准备的。我们不是在简单地组装一个套件&#xff0c;而是在亲…...

HarmonyOS ArkTS DateUtil 日期增减与日历计算完整指南

文章目录 背景一、引言二、日期增减方法详解使用示例 三、日历计算方法详解四、Demo 演示&#xff1a;日期增减结果展示五、Demo 演示&#xff1a;月历视图完整实现六、日历视图关键点解析为什么要填充前置空格&#xff1f;getLastDayOfMonth 的实现技巧 七、小结 背景 近期发现…...

2026论文降AI怎么挑?亲测好用工具附免费降AI指南

“您的论文AIGC率为42%&#xff0c;超出学校30%的合格线&#xff0c;请修改后重新提交。”赶毕业论文的同学这段时间估计没少收到这样的提醒。2026年知网、万方、维普等主流平台的AI检测算法持续迭代&#xff0c;把AI生成内容改到符合学校要求&#xff0c;已经成了毕业生的刚需…...

METSO A413248自动化系统

METSO A413248 自动化系统模块产品特点&#xff1a; 品牌归属&#xff1a;芬兰METSO&#xff08;美卓&#xff09;工业自动化系统原装备件。 产品类型&#xff1a;工业级自动化控制模块/接口模块。 核心功能&#xff1a;用于控制信号处理、数据采集及系统集成。 系统兼容&am…...

styled-theming 性能优化:如何避免主题切换时的性能瓶颈

styled-theming 性能优化&#xff1a;如何避免主题切换时的性能瓶颈 【免费下载链接】styled-theming Create themes for your app using styled-components 项目地址: https://gitcode.com/gh_mirrors/st/styled-theming styled-theming 是一个专为 styled-components …...

终极歌词同步神器LRCGET:5分钟为你的音乐库添加完美歌词

终极歌词同步神器LRCGET&#xff1a;5分钟为你的音乐库添加完美歌词 【免费下载链接】lrcget Utility for mass-downloading LRC synced lyrics for your offline music library. 项目地址: https://gitcode.com/gh_mirrors/lr/lrcget 你是否厌倦了在听歌时手动搜索歌词…...