Ejemplo n.º 1
0
        // Parse data into collection of pawns object
        public static ChessPawn Parse(string data)
        {
            string[] tbData = data
                              .Split(';')
                              .Select(x => x.Trim().ToLower())
                              .ToArray();

            ChessPawn pawn;

            try
            {
                var side = ParseSide(tbData[0].ToLower());
                var xPos = ParseCoordinate(tbData[1]);
                var yPos = ParseCoordinate(tbData[2]);
                var type = ParseType(tbData[3]);
                pawn = new ChessPawn(side, (x: xPos, y: yPos), type);
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Oops... :C");
                System.Console.WriteLine($"Type of error: {e.GetType()}");
                System.Console.WriteLine($"Input data: {data}");
                System.Console.WriteLine(e.Message);
                System.Console.WriteLine();
                pawn = null;
            }

            return(pawn);
        }
Ejemplo n.º 2
0
        public ChessPawn GetPossibleMoves(ChessPawn pawn)
        {
            if (pawn.type == ChessPawnType.NormalPawn && pawn.side == 'w')
            {
                pawn.possibleMoves = NormalWhitePawnMoves(pawn.coords);

                return(pawn);
            }

            string methodName = pawn.type + "Moves";

            // get method by using reflection
            var method = typeof(MovementEngine).GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);

            // invoke method, then assign results into current pawn
            pawn.possibleMoves = (IEnumerable <(int x, int y)>)method.Invoke(this, new object[] { pawn.coords });

            return(pawn);
        }
Ejemplo n.º 3
0
        public string GetBoard(ChessPawn pawn)
        {
            // convert enum value (not name like "NormalPawn") to string
            string pawnType = ((char)(pawn.type)).ToString();

            if (pawn.side == 'w')
            {
                pawnType = pawnType.ToUpper();
            }

            // set pawn character
            ReplaceCharInBoard(pawn.coords.x * 4, (9 - pawn.coords.y) * 2, pawnType);

            // set posibble pawn moves
            foreach (var move in pawn.possibleMoves)
            {
                ReplaceCharInBoard(move.x * 4, (9 - move.y) * 2, "X");
            }

            return(getStringOfBoard());
        }