Ejemplo n.º 1
0
        private static void ChessMove(ChessGame game, BoardInternal game2, string move, Validation validate)
        {
            MoveModel mm = validate.GetMoveCoordsAndPiece(game, move);

            if (mm.srcval == "  ")
            {
                Message.Append("ERROR No piece to move from that square! :" + move + " ");
            }
            else if (!validate.IsWhiteOrBlackToMove(game, move))
            {
                if (game.Moves.Count % 2 == 0)
                {
                    Message.Append("ERROR White's move! :" + move + " ");
                }
                else
                {
                    Message.Append("ERROR Black's move! :" + move + " ");
                }
            }
            else if (validate.IsPieceMoveValid(game, move) && validate.IsClearPath(game, game2, move) && !validate.DoesMovePutSelfInCheck(move))
            {
                if (validate.StorePieceMovePiece(game, move))
                {
                    game.Moves.Add(move);
                }
            }
            else
            {
                Message.Append("ERROR Invalid move :" + move + " ");
            }
        }
Ejemplo n.º 2
0
        public MoveModel GetMoveCoordsAndPiece(ChessGame game, string move)
        {
            MoveModel mm  = new MoveModel();
            var       src = move.Substring(0, 2);
            var       dst = move.Substring(2, 2);

            mm.xsrc = (int)Convert.ToChar(src.Substring(0, 1));
            mm.ysrc = (int)Convert.ToChar(src.Substring(1, 1));
            mm.xdst = (int)Convert.ToChar(dst.Substring(0, 1));
            mm.ydst = (int)Convert.ToChar(dst.Substring(1, 1));

            mm.srcval = (GetVal(game.Board, src));
            mm.dstval = (GetVal(game.Board, dst));
            return(mm);
        }
Ejemplo n.º 3
0
        public bool IsWhiteOrBlackToMove(ChessGame game, string move)
        {
            MoveModel mm = GetMoveCoordsAndPiece(game, move);

            //blacks turn?
            if (" r n b q k p".Contains(mm.srcval))
            {
                if (game.Moves.Count % 2 == 0)
                {
                    return(false);
                }
            }
            //whites turn?
            if (" R N B Q K P".Contains(mm.srcval))
            {
                if (game.Moves.Count % 2 != 0)
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        public bool IsPieceMoveValid(ChessGame game, string move)
        {
            MoveModel mm = GetMoveCoordsAndPiece(game, move);

            //If castling
            if ((move == "0") || (move == "00"))
            {
                if (IsCastlingValid(game, move))
                {
                    return(true);
                }
                return(false);
            }

            switch (mm.srcval)
            {
            case " r":
            case " R":
                if (!IsMovingLikeARook(mm.xsrc, mm.ysrc, mm.xdst, mm.ydst))
                {
                    return(false);
                }
                break;

            case " n":
            case " N":
                if (!IsMovingLikeAKnight(mm.xsrc, mm.ysrc, mm.xdst, mm.ydst))
                {
                    return(false);
                }
                break;

            case " b":
            case " B":
                if (!IsMovingLikeABishop(mm.xsrc, mm.ysrc, mm.xdst, mm.ydst))
                {
                    return(false);
                }
                break;

            case " q":
            case " Q":
                if (!IsMovingLikeAQueen(mm.xsrc, mm.ysrc, mm.xdst, mm.ydst))
                {
                    return(false);
                }
                break;

            case " k":
            case " K":
                if (!IsMovingLikeAKing(mm.xsrc, mm.ysrc, mm.xdst, mm.ydst))
                {
                    return(false);
                }
                break;

            case " p":
                if (!IsMovingLikeABlackPawn(mm.dstval, mm.xsrc, mm.ysrc, mm.xdst, mm.ydst))
                {
                    return(false);
                }
                break;

            case " P":
                if (!IsMovingLikeAWhitePawn(mm.dstval, mm.xsrc, mm.ysrc, mm.xdst, mm.ydst))
                {
                    return(false);
                }
                break;

            default:
                break;
            }
            return(true);
        }