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

实现了基本的玩法。

BoardController.cs

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) || // Horizontal
                            CheckDirection(i, j, 0, 1, player) || // Vertical
                            CheckDirection(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

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

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

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

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

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

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

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

相关推荐
相隔一个图书馆的距离3 分钟前
单测(三)Spring Boot 3.5.6 && @MockitoSpyBean / AOP / 循环依赖:静默吞错与运行期 NPE
java·spring boot·后端
软件技术NINI12 分钟前
基于JavaWeb 汽车租聘
java·vue.js·spring boot·后端·python·毕业设计·毕设
yz_aiks14 分钟前
RestTemplate 废弃后迁移 RestClient 实战:Spring Boot 4.2 升级指南
java·spring boot·后端·微服务·restclient
公子小六20 分钟前
基于.NET的Windows窗体编程之WinForms自定义控件(3)
windows·c#·.net·音视频·winforms
wuminyu21 分钟前
JVM规范学习总结-JVM规范基本概念
java·linux·c语言·jvm·c++
时间静止不是简史4 小时前
生产事故排查:Redis 宕机导致补偿队列消费线程死亡,通话录音未上送业务平台
java·redis
2301_32241428047 小时前
基于SpringBoot+Vue的休闲零食超市管理系统的设计与实现59004-计算机毕设 原创(免费领源码+部署教程+开发环境)
java·vue.js·spring boot·redis·mysql·决策树·推荐算法
金銀銅鐵9 小时前
[Java] 用 GUI 展示 EnumSet 增减元素时的内部变化(上)
java·后端·ai编程
福建佰胜张工10 小时前
SMC 薄型气爪 MHF2-16D1 深度解析:平行开闭、低剖面结构在自动化装配中的选型与落地
java·服务器·安全·自动化
catoop10 小时前
API Key 设计与安全生成方案:结构拆解、数据库设计与 Java 实现
java·数据库·安全