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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/13463.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/13463.shtml
英文地址,請注明出處:http://en.pswp.cn/web/13463.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

uniapp(h5 app) 中 webview和h5通信

1 uniapph5 和h5頁面 通信 h5 window.parent.postMessage(message, *); uniapph5 onload中 window.addEventListener(message, function (e) { // 監聽 message 事件 //console.log(e.origin) console.log(收到的cocos游戲ID,e.data) …

Python實現天氣數據采集

Python實現天氣數據采集 一、需求介紹二、完整代碼一、需求介紹 本次天氣數據采集的需求是獲取每日的最高溫、最低溫、風力、風向、天氣狀況、AQI指數,如圖所示,完整代碼附后: 本次采集的目標網址是2345天氣網: 上圖的URL中,beijing是城市名稱的縮寫,54511即為城市代碼…

數據庫設計步驟and相關注意點

文章目錄 前言數據庫設計的主要步驟1.需求分析2.概念結構設計3.邏輯結構設計4.物理結構模型設計5.數據庫實施和維護給出一些題目理解一下吧~ 總結 前言 學無止境&#xff0c;筆勤不輟。最近筆者狀態不是特別好&#xff0c;一直忙于應付課程作業&#xff0c;于是一直沒有時間更…

科技引領未來:高速公路可視化

高速公路可視化監控系統利用實時視頻、傳感器數據和大數據分析&#xff0c;通過圖撲 HT 可視化展示交通流量、車速、事故和路況信息。交通管理人員可以實時監控、快速響應突發事件&#xff0c;并優化交通信號和指揮方案。這一系統不僅提高了道路安全性和車輛通行效率&#xff0…

vue3結合element-plus之如何優雅的使用表格

背景 表格組件的使用在后臺管理系統中是非常常見的,但是如果每次使用表格我們都去一次一次地從 element-plus 官網去 復制、粘貼和修改成自己想要的表格。 這樣一來也說得過去,但是如果我們靜下來細想不難發現,表格的使用都是大同小異的,每次都去復制粘貼,對于有很多表格…

vue3封裝ElementUI plus Dialog彈窗

因為ElementuiPlus的dialog彈框的初始樣式不太好看,而公司要求又要好看,本來是已經實現了,但是后來想想了發現封裝完dialog的其他功能也要,所以特此記錄一下 方案一 思路:封裝一個組件,將所有新增的參數引入el-dialog 參數中,實現參數共用 新建一個組件,將官網暴露的屬性全部引…

C++開源庫glog使用封裝--自定義日志輸出格式,設置日志保留時間

glog下載和編譯 glog開源地址 https://github.com/google/glog glog靜態庫編譯 cd /home/wangz/3rdParty/hldglog/glogmkdir out mkdir build && cd buildcmake .. -DCMAKE_INSTALL_PREFIX../out -DCMAKE_BUILD_TYPERelease -DBUILD_SHARED_LIBSOFF本文選擇的glo…

網關路由SpringCloudGateway、nacos配置管理(熱更新、動態路由)

文章目錄 前言一、網關路由二、SpringCloudGateway1. 路由過濾2. 網關登錄校驗2.1 鑒權2.2 網關過濾器2.3 登錄校驗2.3.1 JWT2.3.2 登錄校驗過濾器 3. 微服務從網關獲取用戶4. 微服務之間用戶信息傳遞 三、nacos配置管理問題引入3.1 配置共享3.1.1 在Nacos中添加共享配置3.1.2 …

【前端三劍客之HTML】詳解HTML

1. HTML(超文本標記語言) HTML意為超文本標記語言&#xff0c;其可以通過標簽把其他網頁/圖片/視頻等資源引入到當前網頁中&#xff0c;讓網頁最終呈現出來的效果超越了文本.HTML是一種標記語言&#xff0c;其是由一系列標簽組成的. 而且每個標簽都有特定的含義和確定的頁面顯…

Vue 3入門指南

title: Vue 3入門指南 date: 2024/5/23 19:37:34 updated: 2024/5/23 19:37:34 categories: 前端開發 tags: 框架對比環境搭建基礎語法組件開發響應式系統狀態管理路由配置 第1章&#xff1a;Vue 3簡介 1.1 Vue.js的歷史與發展 Vue.js由前谷歌工程師尤雨溪&#xff08;Eva…

Java分支結構詳解

Java分支結構詳解 前言一、if 語句基本語法表示一表示二表示三 代碼示例判定一個數字是奇數還是偶數判定一個數字是正數還是負數判定某一年份是否是閏年 注意要點懸垂 else 問題代碼風格問題分號問題 二、switch 語句基本語法代碼示例根據 day 的值輸出星期 注意事項break 不要…

深入了解 Pandas:對象的缺少值

目錄 前言 第一點&#xff1a;導入模塊 第二點 &#xff1a;發現對象的缺失值 第二點&#xff1a;剔除缺少值 第三點&#xff1a;填補缺失值 總結 前言 在數據處理中&#xff0c;經常會遇到數據中存在缺失值的情況。處理缺失值是數據清洗的一個重要環節&#xff0c;能夠確…

spring常用知識點

1、攔截器和過濾器區別 1. 原理不同&#xff1a; 攔截器是基于java的反射機制&#xff0c;而過濾器采用責任鏈模式是基于函數回調的。 2. 使用范圍不同&#xff1a; 過濾器Filter的使用依賴于Tomcat等容器&#xff0c;導致它只能在web程序中使用 攔截器是一個Sping組件&am…

abs(-2147483648) == 2147483648?

從數學意義上&#xff0c;這是對的。但是&#xff0c;就怕但是。 #include int main() {long long v;v abs(-2147483648);printf("%lld\n", v);return 0; } 輸出: -2147483648 我們從source code中一一解開. /* Return the absolute value of I. */ int abs (…

Mongodb介紹及springboot集成增刪改查

文章目錄 1. MongoDB相關概念1.1 業務應用場景1.2 MongoDB簡介1.3 體系結構1.4 數據模型1.5 MongoDB的特點 2. docker安裝mongodb3. springboot集成3.1 文件結構3.2 增刪改查3.2.1 增加insert3.2.2 保存save3.2.3 更新update3.2.4 查詢3.2.5 刪除 1. MongoDB相關概念 1.1 業務…

Docker-Android安卓模擬器本地部署并實現遠程開發測試

文章目錄 1. 虛擬化環境檢查2. Android 模擬器部署3. Ubuntu安裝Cpolar4. 配置公網地址5. 遠程訪問小結 6. 固定Cpolar公網地址7. 固定地址訪問 本文主要介紹如何在Ubuntu系統使用Docker部署docker-android安卓模擬器&#xff0c;并結合cpolar內網穿透工具實現公網遠程訪問本地…

51建模網AR虛擬試用,讓網購不再只靠想象!

在數字化的浪潮中&#xff0c;網購已成為現代人生活的一部分。然而&#xff0c;傳統的網購模式常常因為無法直接試穿、試用商品&#xff0c;導致買家在收到商品后感到失望&#xff0c;特別是面對大件家居產品時&#xff0c;僅憑屏幕上的圖片和尺寸描述&#xff0c;很難準確地把…

智能AI愈發強大,企業如何防范AI網絡釣魚攻擊

隨著AI技術的快速發展&#xff0c;如ChatGPT等智能化工具在各個領域得到了廣泛應用。然而&#xff0c;這些工具的普及也給網絡安全帶來了新的挑戰。AI模型的自然語言生成功能使得網絡釣魚攻擊更加智能化和隱蔽化&#xff0c;攻擊者能夠利用AI技術生成高度逼真的欺騙性郵件和其他…

深度學習之基于YoloV5人體姿態摔倒識別分析報警系統

歡迎大家點贊、收藏、關注、評論啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代碼。 文章目錄 一項目簡介 二、功能三、系統四. 總結 一項目簡介 一、項目背景與意義 隨著人口老齡化的加劇和人們對健康安全的日益關注&#xff0c;摔倒事件在老年人、幼兒、體育運…

2024-05-23 服務器開發-windows-加載dll動態庫

摘要: 2024-05-23 服務器開發-windows-加載dll動態庫 使用 LoadLibrary HMODULE mdl ::LoadLibrary(L"mylib.dll");if (!mdl){auto err ::GetLastError();std::cout << "ERROR: load VxCfgClient fail, error: " << err << std::endl…