当前位置:   article > 正文

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

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

实现了基本的玩法。

BoardController.cs

  1. using System;
  2. namespace GomokuGame
  3. {
  4. public class BoardController
  5. {
  6. private static BoardController instance;
  7. private readonly int[,] board;
  8. private const int boardSize = 15;
  9. private BoardController()
  10. {
  11. board = new int[boardSize, boardSize];
  12. }
  13. public static BoardController Instance
  14. {
  15. get
  16. {
  17. if (instance == null)
  18. {
  19. instance = new BoardController();
  20. }
  21. return instance;
  22. }
  23. }
  24. public int[,] GetBoard() => board;
  25. public bool PlacePiece(int x, int y, int player)
  26. {
  27. if (board[x, y] == 0)
  28. {
  29. board[x, y] = player;
  30. return true;
  31. }
  32. return false;
  33. }
  34. public bool CheckWin(int player)
  35. {
  36. for (int i = 0; i < boardSize; i++)
  37. {
  38. for (int j = 0; j < boardSize; j++)
  39. {
  40. if (board[i, j] == player)
  41. {
  42. if (CheckDirection(i, j, 1, 0, player) || // Horizontal
  43. CheckDirection(i, j, 0, 1, player) || // Vertical
  44. CheckDirection(i, j, 1, 1, player) || // Diagonal \
  45. CheckDirection(i, j, 1, -1, player)) // Diagonal /
  46. {
  47. return true;
  48. }
  49. }
  50. }
  51. }
  52. return false;
  53. }
  54. private bool CheckDirection(int startX, int startY, int dx, int dy, int player)
  55. {
  56. int count = 0;
  57. for (int i = 0; i < 5; i++)
  58. {
  59. int x = startX + i * dx;
  60. int y = startY + i * dy;
  61. if (x >= 0 && x < boardSize && y >= 0 && y < boardSize && board[x, y] == player)
  62. {
  63. count++;
  64. }
  65. else
  66. {
  67. break;
  68. }
  69. }
  70. return count == 5;
  71. }
  72. }
  73. }

BoardView.cs

  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. namespace GomokuGame
  5. {
  6. public class BoardView : Panel
  7. {
  8. private const int cellSize = 30;
  9. private const int boardSize = 15;
  10. private int[,] board;
  11. private int currentPlayer;
  12. public BoardView()
  13. {
  14. this.DoubleBuffered = true;
  15. this.Size = new Size(boardSize * cellSize, boardSize * cellSize);
  16. board = BoardController.Instance.GetBoard();
  17. currentPlayer = 1;
  18. this.Paint += BoardView_Paint;
  19. this.MouseClick += BoardView_MouseClick;
  20. }
  21. private void BoardView_Paint(object sender, PaintEventArgs e)
  22. {
  23. Graphics g = e.Graphics;
  24. for (int i = 0; i < boardSize; i++)
  25. {
  26. for (int j = 0; j < boardSize; j++)
  27. {
  28. g.DrawRectangle(Pens.Black, i * cellSize, j * cellSize, cellSize, cellSize);
  29. if (board[i, j] == 1)
  30. {
  31. g.FillEllipse(Brushes.Black, i * cellSize, j * cellSize, cellSize, cellSize);
  32. }
  33. else if (board[i, j] == 2)
  34. {
  35. g.FillEllipse(Brushes.Black, i * cellSize, j * cellSize, cellSize, cellSize);
  36. g.FillEllipse(Brushes.White, i * cellSize + 2, j * cellSize + 2, cellSize - 4, cellSize - 4);
  37. }
  38. }
  39. }
  40. }
  41. private void BoardView_MouseClick(object sender, MouseEventArgs e)
  42. {
  43. int x = e.X / cellSize;
  44. int y = e.Y / cellSize;
  45. if (BoardController.Instance.PlacePiece(x, y, currentPlayer))
  46. {
  47. this.Invalidate();
  48. if (BoardController.Instance.CheckWin(currentPlayer))
  49. {
  50. MessageBox.Show($"Player {currentPlayer} wins!");
  51. }
  52. // 交换玩家
  53. currentPlayer = currentPlayer == 1 ? 2 : 1;
  54. }
  55. }
  56. }
  57. }

Form1.cs

  1. using System;
  2. using System.Windows.Forms;
  3. namespace GomokuGame
  4. {
  5. public partial class Form1 : Form
  6. {
  7. public Form1()
  8. {
  9. InitializeComponent();
  10. InitializeGame();
  11. }
  12. private void InitializeGame()
  13. {
  14. BoardView boardView = new BoardView();
  15. boardView.Dock = DockStyle.Fill;
  16. this.Controls.Add(boardView);
  17. IGameStrategy strategy = new PvPStrategy();
  18. strategy.Execute();
  19. }
  20. }
  21. }

Form1.Designer.cs

  1. using System;
  2. using System.Windows.Forms;
  3. namespace GomokuGame
  4. {
  5. partial class Form1 : Form
  6. {
  7. private System.ComponentModel.IContainer components = null;
  8. protected override void Dispose(bool disposing)
  9. {
  10. if (disposing && (components != null))
  11. {
  12. components.Dispose();
  13. }
  14. base.Dispose(disposing);
  15. }
  16. private void InitializeComponent()
  17. {
  18. this.SuspendLayout();
  19. //
  20. // Form1
  21. //
  22. this.ClientSize = new System.Drawing.Size(800, 450);
  23. this.Name = "Form1";
  24. this.ResumeLayout(false);
  25. }
  26. }
  27. }

GameStrategy.cs

  1. using System;
  2. public interface IGameStrategy
  3. {
  4. void Execute();
  5. }
  6. public class PvPStrategy : IGameStrategy
  7. {
  8. public void Execute()
  9. {
  10. Console.WriteLine("Player vs Player mode.");
  11. }
  12. }
  13. public class PvAIStrategy : IGameStrategy
  14. {
  15. public void Execute()
  16. {
  17. Console.WriteLine("Player vs AI mode.");
  18. }
  19. }

PieceFactory.cs

  1. using System;
  2. public abstract class Piece
  3. {
  4. public abstract void Place(int x, int y);
  5. }
  6. public class BlackPiece : Piece
  7. {
  8. public override void Place(int x, int y)
  9. {
  10. Console.WriteLine($"Placed black piece at ({x}, {y})");
  11. }
  12. }
  13. public class WhitePiece : Piece
  14. {
  15. public override void Place(int x, int y)
  16. {
  17. Console.WriteLine($"Placed white piece at ({x}, {y})");
  18. }
  19. }
  20. public class PieceFactory
  21. {
  22. public static Piece CreatePiece(int player)
  23. {
  24. return player == 1 ? new BlackPiece() : (Piece)new WhitePiece();
  25. }
  26. }

PlacePieceCommand.cs

  1. using GomokuGame;
  2. public interface ICommand
  3. {
  4. void Execute();
  5. }
  6. public class PlacePieceCommand : ICommand
  7. {
  8. private readonly int x;
  9. private readonly int y;
  10. private readonly int player;
  11. public PlacePieceCommand(int x, int y, int player)
  12. {
  13. this.x = x;
  14. this.y = y;
  15. this.player = player;
  16. }
  17. public void Execute()
  18. {
  19. BoardController.Instance.PlacePiece(x, y, player);
  20. PieceFactory.CreatePiece(player).Place(x, y);
  21. }
  22. }

Program.cs

  1. using System;
  2. using System.Windows.Forms;
  3. namespace GomokuGame
  4. {
  5. static class Program
  6. {
  7. [STAThread]
  8. static void Main()
  9. {
  10. Application.EnableVisualStyles();
  11. Application.SetCompatibleTextRenderingDefault(false);
  12. Application.Run(new Form1());
  13. }
  14. }
  15. }

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/625542
推荐阅读
相关标签
  

闽ICP备14008679号