Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- class ChessPiece
- {
- public string Type { get; set; }
- public string Color { get; set; }
- public int[] Position { get; set; }
- public ChessPiece(string type, string color, int[] position)
- {
- Type = type;
- Color = color;
- Position = position;
- }
- }
- class ChessBoard
- {
- public ChessPiece[,] Board { get; set; }
- public ChessBoard()
- {
- Board = new ChessPiece[8, 8];
- }
- public void Initialize()
- {
- // Initialize white pieces
- Board[0, 0] = new ChessPiece("Rook", "White", new int[] { 0, 0 });
- Board[0, 1] = new ChessPiece("Knight", "White", new int[] { 0, 1 });
- Board[0, 2] = new ChessPiece("Bishop", "White", new int[] { 0, 2 });
- Board[0, 3] = new ChessPiece("Queen", "White", new int[] { 0, 3 });
- Board[0, 4] = new ChessPiece("King", "White", new int[] { 0, 4 });
- Board[0, 5] = new ChessPiece("Bishop", "White", new int[] { 0, 5 });
- Board[0, 6] = new ChessPiece("Knight", "White", new int[] { 0, 6 });
- Board[0, 7] = new ChessPiece("Rook", "White", new int[] { 0, 7 });
- for (int i = 0; i < 8; i++)
- {
- Board[1, i] = new ChessPiece("Pawn", "White", new int[] { 1, i });
- }
- // Initialize black pieces
- Board[7, 0] = new ChessPiece("Rook", "Black", new int[] { 7, 0 });
- Board[7, 1] = new ChessPiece("Knight", "Black", new int[] { 7, 1 });
- Board[7, 2] = new ChessPiece("Bishop", "Black", new int[] { 7, 2 });
- Board[7, 3] = new ChessPiece("Queen", "Black", new int[] { 7, 3 });
- Board[7, 4] = new ChessPiece("King", "Black", new int[] { 7, 4 });
- Board[7, 5] = new ChessPiece("Bishop", "Black", new int[] { 7, 5 });
- Board[7, 6] = new ChessPiece("Knight", "Black", new int[] { 7, 6 });
- Board[7, 7] = new ChessPiece("Rook", "Black", new int[] { 7, 7 });
- for (int i = 0; i < 8; i++)
- {
- Board[6, i] = new ChessPiece("Pawn", "Black", new int[] { 6, i });
- }
- }
- public void PrintBoard()
- {
- for (int i = 0; i < 8; i++)
- {
- for (int j = 0; j < 8; j++)
- {
- if (Board[i, j] == null)
- {
- Console.Write("[ ]");
- }
- else
- {
- Console.Write("[" + Board[i, j].Type[0] + "]");
- }
- }
- Console.WriteLine();
- }
- }
- public bool MovePiece(int[] currentPos, int[] newPos)
- {
- // Check if the current position has a piece
- if (Board[currentPos[0], currentPos[1]] == null)
- {
- Console.WriteLine("No piece at current position.");
- return false;
- }
- // Check if the new position is within the board
- if (newPos[0] < 0 || newPos[0] > 7 || newPos[1] < 0 || newPos[1] > 7)
- {
- Console.WriteLine("Invalid move.");
- return false;
- }
- // Check if the new position has a piece of the same color
- if (Board[newPos[0], newPos[1]] != null && Board[currentPos[0], currentPos[1]].Color == Board[newPos[0], newPos[1]].Color)
- {
- Console.WriteLine("Cannot move to a square occupied by a piece of the same color.");
- return false;
- }
- // Move the piece
- Board[newPos[0], newPos[1]] = Board[currentPos[0], currentPos[1]];
- Board[currentPos[0], currentPos[1]] = null;
- return true;
- }
- }
- class ChessGame
- {
- static void Main()
- {
- ChessBoard chessBoard = new ChessBoard();
- chessBoard.Initialize();
- chessBoard.PrintBoard();
- while (true)
- {
- Console.WriteLine("Enter current position (row column):");
- int[] currentPos = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
- Console.WriteLine("Enter new position (row column):");
- int[] newPos = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
- if (chessBoard.MovePiece(currentPos, newPos))
- {
- chessBoard.PrintBoard();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment