当前位置: 首页 > 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语言】堆~动画超详细解读!
  • java解析json复杂数据补充
  • CSS伪类实现input聚焦时,上层div样式改变
  • 如何跨过robots协议的限制爬取内容?
  • 设计模式9——适配器模式
  • Prometheus+Grafana监控服务器、mysql数据库并配置报警规则推送邮箱
  • WORD、PPT技巧
  • Python文件和数据格式化-课堂练习[python123题库]
  • dmanywhere的docker制作
  • 【博客714】golang使用mmap来优化gc
  • 从零开始学Vue3--环境搭建
  • AUTOMATIC1111/stable-diffusion-webui/stable-diffusion-webui-v1.9.3
  • 【FPGA】Verilog:解码器 | 编码器 | 多路复用器(Mux, Multiplexer)
  • 基于HTML5和CSS3搭建一个Web网页(二)
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • Brief introduction of how to 'Call, Apply and Bind'
  • Centos6.8 使用rpm安装mysql5.7
  • el-input获取焦点 input输入框为空时高亮 el-input值非法时
  • JAVA并发编程--1.基础概念
  • swift基础之_对象 实例方法 对象方法。
  • Travix是如何部署应用程序到Kubernetes上的
  • 阿里云前端周刊 - 第 26 期
  • 飞驰在Mesos的涡轮引擎上
  • 关键词挖掘技术哪家强(一)基于node.js技术开发一个关键字查询工具
  • 看图轻松理解数据结构与算法系列(基于数组的栈)
  • 前嗅ForeSpider采集配置界面介绍
  • 深入浏览器事件循环的本质
  • 微信小程序开发问题汇总
  • 微信小程序--------语音识别(前端自己也能玩)
  • 小而合理的前端理论:rscss和rsjs
  • 小试R空间处理新库sf
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • 阿里云重庆大学大数据训练营落地分享
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​一文看懂数据清洗:缺失值、异常值和重复值的处理
  • !!java web学习笔记(一到五)
  • #我与Java虚拟机的故事#连载09:面试大厂逃不过的JVM
  • #职场发展#其他
  • (12)Linux 常见的三种进程状态
  • (M)unity2D敌人的创建、人物属性设置,遇敌掉血
  • (笔试题)分解质因式
  • (不用互三)AI绘画:科技赋能艺术的崭新时代
  • (第9篇)大数据的的超级应用——数据挖掘-推荐系统
  • (十)Flink Table API 和 SQL 基本概念
  • (贪心) LeetCode 45. 跳跃游戏 II
  • (转)Linux下编译安装log4cxx
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • *ST京蓝入股力合节能 着力绿色智慧城市服务
  • .NET 实现 NTFS 文件系统的硬链接 mklink /J(Junction)
  • .Net 知识杂记
  • .NET未来路在何方?
  • .NET应用UI框架DevExpress XAF v24.1 - 可用性进一步增强
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法