Guest User

Untitled

a guest
Jan 19th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class ChessPiece
  5. {
  6. public string Type { get; set; }
  7. public string Color { get; set; }
  8. public int[] Position { get; set; }
  9.  
  10. public ChessPiece(string type, string color, int[] position)
  11. {
  12. Type = type;
  13. Color = color;
  14. Position = position;
  15. }
  16. }
  17.  
  18. class ChessBoard
  19. {
  20. public ChessPiece[,] Board { get; set; }
  21.  
  22. public ChessBoard()
  23. {
  24. Board = new ChessPiece[8, 8];
  25. }
  26.  
  27. public void Initialize()
  28. {
  29. // Initialize white pieces
  30. Board[0, 0] = new ChessPiece("Rook", "White", new int[] { 0, 0 });
  31. Board[0, 1] = new ChessPiece("Knight", "White", new int[] { 0, 1 });
  32. Board[0, 2] = new ChessPiece("Bishop", "White", new int[] { 0, 2 });
  33. Board[0, 3] = new ChessPiece("Queen", "White", new int[] { 0, 3 });
  34. Board[0, 4] = new ChessPiece("King", "White", new int[] { 0, 4 });
  35. Board[0, 5] = new ChessPiece("Bishop", "White", new int[] { 0, 5 });
  36. Board[0, 6] = new ChessPiece("Knight", "White", new int[] { 0, 6 });
  37. Board[0, 7] = new ChessPiece("Rook", "White", new int[] { 0, 7 });
  38. for (int i = 0; i < 8; i++)
  39. {
  40. Board[1, i] = new ChessPiece("Pawn", "White", new int[] { 1, i });
  41. }
  42.  
  43. // Initialize black pieces
  44. Board[7, 0] = new ChessPiece("Rook", "Black", new int[] { 7, 0 });
  45. Board[7, 1] = new ChessPiece("Knight", "Black", new int[] { 7, 1 });
  46. Board[7, 2] = new ChessPiece("Bishop", "Black", new int[] { 7, 2 });
  47. Board[7, 3] = new ChessPiece("Queen", "Black", new int[] { 7, 3 });
  48. Board[7, 4] = new ChessPiece("King", "Black", new int[] { 7, 4 });
  49. Board[7, 5] = new ChessPiece("Bishop", "Black", new int[] { 7, 5 });
  50. Board[7, 6] = new ChessPiece("Knight", "Black", new int[] { 7, 6 });
  51. Board[7, 7] = new ChessPiece("Rook", "Black", new int[] { 7, 7 });
  52. for (int i = 0; i < 8; i++)
  53. {
  54. Board[6, i] = new ChessPiece("Pawn", "Black", new int[] { 6, i });
  55. }
  56. }
  57.  
  58. public void PrintBoard()
  59. {
  60. for (int i = 0; i < 8; i++)
  61. {
  62. for (int j = 0; j < 8; j++)
  63. {
  64. if (Board[i, j] == null)
  65. {
  66. Console.Write("[ ]");
  67. }
  68. else
  69. {
  70. Console.Write("[" + Board[i, j].Type[0] + "]");
  71. }
  72. }
  73. Console.WriteLine();
  74. }
  75. }
  76.  
  77. public bool MovePiece(int[] currentPos, int[] newPos)
  78. {
  79. // Check if the current position has a piece
  80. if (Board[currentPos[0], currentPos[1]] == null)
  81. {
  82. Console.WriteLine("No piece at current position.");
  83. return false;
  84. }
  85. // Check if the new position is within the board
  86. if (newPos[0] < 0 || newPos[0] > 7 || newPos[1] < 0 || newPos[1] > 7)
  87. {
  88. Console.WriteLine("Invalid move.");
  89. return false;
  90. }
  91. // Check if the new position has a piece of the same color
  92. if (Board[newPos[0], newPos[1]] != null && Board[currentPos[0], currentPos[1]].Color == Board[newPos[0], newPos[1]].Color)
  93. {
  94. Console.WriteLine("Cannot move to a square occupied by a piece of the same color.");
  95. return false;
  96. }
  97. // Move the piece
  98. Board[newPos[0], newPos[1]] = Board[currentPos[0], currentPos[1]];
  99. Board[currentPos[0], currentPos[1]] = null;
  100. return true;
  101. }
  102. }
  103.  
  104. class ChessGame
  105. {
  106. static void Main()
  107. {
  108. ChessBoard chessBoard = new ChessBoard();
  109. chessBoard.Initialize();
  110. chessBoard.PrintBoard();
  111. while (true)
  112. {
  113. Console.WriteLine("Enter current position (row column):");
  114. int[] currentPos = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
  115. Console.WriteLine("Enter new position (row column):");
  116. int[] newPos = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
  117. if (chessBoard.MovePiece(currentPos, newPos))
  118. {
  119. chessBoard.PrintBoard();
  120. }
  121. }
  122. }
  123. }
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment