Advertisement
Guest User

jonny-chess (C# Shell App Paste)

a guest
May 27th, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.03 KB | None | 0 0
  1. //Disclaimer: The creator of 'C# Shell (C# Offline Compiler)' is in no way responsible for the code posted by any user.
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6. using System.Text;
  7.  
  8. namespace CSharp_Shell
  9. {
  10.  
  11.     public class Chess
  12.     {
  13.         private List<Piece> pieceList;
  14.         private bool checkMate;
  15.         private bool whiteTurn;
  16.        
  17.         public static void Main()
  18.         {
  19.             var chess = new Chess();
  20.             chess.Init();
  21.             chess.Run();
  22.         }
  23.        
  24.         public void Init()
  25.         {
  26.             InitPieces();
  27.             whiteTurn = true;
  28.         }
  29.        
  30.         public void Run()
  31.         {
  32.             while (!checkMate)
  33.             {
  34.                 bool validMove = false;
  35.                 Piece piece = null;
  36.                 int x = 0, y = 0;
  37.                 string error = null;
  38.                
  39.                 Console.Clear();
  40.                 Console.WriteLine((whiteTurn ? "White" : "Black") + "'s turn");
  41.            
  42.                 while (!validMove)
  43.                 {
  44.                     Console.Clear();
  45.                     PrintBoard();
  46.                     PrintError(error);
  47.                     Console.WriteLine("Enter your move (<piece> <x> <y>):");
  48.                     string move = Console.ReadLine();
  49.            
  50.                     string[] moveParts = move.Split(null);
  51.                     if (moveParts[0].Equals("exit"))
  52.                     {
  53.                         return;
  54.                     }
  55.                     else if (moveParts.Length < 3)
  56.                     {
  57.                         error = "Invalid move, make sure you supply all 3 attributes";
  58.                         continue;
  59.                     }
  60.            
  61.                     piece = GetPiece(moveParts[0]);
  62.                     int.TryParse(moveParts[1], out x);
  63.                     int.TryParse(moveParts[2], out y);
  64.            
  65.                     error = ValidateMove(piece, x, y);
  66.                     validMove = error == null;
  67.                 }
  68.            
  69.                 MovePiece(piece, x-1, y-1);
  70.                 Console.WriteLine($"{piece.GetActualName()} moved to {x}, {y}?");
  71.                 whiteTurn = !whiteTurn;
  72.             }
  73.         }
  74.        
  75.         private void PrintError(string error)
  76.         {
  77.             if (error == null) return;
  78.            
  79.             Console.WriteLine(error);
  80.         }
  81.        
  82.         private string ValidateMove(Piece piece, int x, int y)
  83.         {
  84.             if (piece == null)
  85.             {
  86.                 return "Invalid piece";
  87.             }
  88.             else if (x < 1 || x > 8)
  89.             {
  90.                 return "Invalid x coordinate";
  91.             }
  92.             else if (y < 1 || y > 8)
  93.             {
  94.                 return "Invalid y coordinate";
  95.             }
  96.             else if (whiteTurn != piece.IsWhite())
  97.             {
  98.                 return "That's not your piece!";
  99.             }
  100.             else if (!piece.CanMove(x-1, y-1)) {
  101.                 return "Can't move there!";
  102.             }
  103.             else
  104.             {
  105.                 return null;
  106.             }
  107.         }
  108.        
  109.         private void InitPieces()
  110.         {
  111.             pieceList = new List<Piece>();
  112.             var color = ConsoleColor.Black;
  113.             pieceList.Add(new Piece(PieceType.ROOK, color, 0, 0, 1));
  114.             pieceList.Add(new Piece(PieceType.KNIGHT, color, 1, 0, 1));
  115.             pieceList.Add(new Piece(PieceType.BISHOP, color, 2, 0, 1));
  116.             pieceList.Add(new Piece(PieceType.KING, color, 3, 0));
  117.             pieceList.Add(new Piece(PieceType.QUEEN, color, 4, 0));
  118.             pieceList.Add(new Piece(PieceType.BISHOP, color, 5, 0, 2));
  119.             pieceList.Add(new Piece(PieceType.KNIGHT, color, 6, 0, 2));
  120.             pieceList.Add(new Piece(PieceType.ROOK, color, 7, 0, 2));
  121.             for (int x = 0; x < 8; x++)
  122.             {
  123.                 pieceList.Add(new Piece(PieceType.PAWN, color, x, 1, x+1));
  124.             }
  125.            
  126.             color = ConsoleColor.White;
  127.             for (int x = 0; x < 8; x++)
  128.             {
  129.                 pieceList.Add(new Piece(PieceType.PAWN, color, x, 6, x+1));
  130.             }
  131.             pieceList.Add(new Piece(PieceType.ROOK, color, 0, 7, 1));
  132.             pieceList.Add(new Piece(PieceType.KNIGHT, color, 1, 7, 1));
  133.             pieceList.Add(new Piece(PieceType.BISHOP, color, 2, 7, 1));
  134.             pieceList.Add(new Piece(PieceType.KING, color, 3, 7));
  135.             pieceList.Add(new Piece(PieceType.QUEEN, color, 4, 7));
  136.             pieceList.Add(new Piece(PieceType.BISHOP, color, 5, 7, 2));
  137.             pieceList.Add(new Piece(PieceType.KNIGHT, color, 6, 7, 2));
  138.             pieceList.Add(new Piece(PieceType.ROOK, color, 7, 7, 2));
  139.         }
  140.        
  141.         private void PrintBoard()
  142.         {
  143.             Console.WriteLine("  |1  |2  |3  |4  |5  |6  |7  |8");
  144.             for (int y = 0; y < 8; y++)
  145.             {
  146.                 Console.WriteLine("-----------------------------------");
  147.                 Console.Write(" {0}|", y + 1);
  148.                 StringBuilder rowText = new StringBuilder();
  149.                 for (int x = 0; x < 8; x++)
  150.                 {
  151.                     Piece piece = null;
  152.                     foreach (var p in pieceList)
  153.                     {
  154.                         if (p.X == x && p.Y == y)
  155.                         {
  156.                             piece = p;
  157.                             break;
  158.                         }
  159.                     }
  160.                    
  161.                     if (piece != null)
  162.                     {
  163.                         rowText.Append(piece.ToString());
  164.                     }
  165.                     else
  166.                     {
  167.                         rowText.Append("   ");
  168.                     }
  169.                     rowText.Append("|");
  170.                 }
  171.                
  172.                 Console.WriteLine(rowText.ToString());
  173.             }
  174.             Console.WriteLine("------------------------------------");
  175.             Console.WriteLine();
  176.         }
  177.        
  178.         private Piece GetPiece(string pieceCode)
  179.         {
  180.             if (pieceCode == null || pieceCode.Length < 3) return null;
  181.            
  182.             string colorCode = pieceCode.Substring(0,1);
  183.             string typeCode = pieceCode.Substring(1, 1);
  184.             int sequenceNumber = 0;
  185.             int.TryParse(pieceCode.Substring(2, 1), out sequenceNumber);
  186.             if (sequenceNumber == 0) return null;
  187.            
  188.             foreach (Piece piece in pieceList)
  189.             {
  190.                 if (!piece.GetTypeCode().ToLower().Equals(typeCode.ToLower()))
  191.                 {
  192.                     continue;
  193.                 }
  194.                 else if (!piece.GetColorCode().Equals(colorCode))
  195.                 {
  196.                     continue;
  197.                 }
  198.                 else if (piece.SequenceNumber != sequenceNumber)
  199.                 {
  200.                     continue;
  201.                 }
  202.                 else
  203.                 {
  204.                     return piece;
  205.                 }
  206.             }
  207.             return null;
  208.         }
  209.        
  210.         private void MovePiece(Piece piece, int x, int y)
  211.         {
  212.             piece.SetLocation(x, y);
  213.         }
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement