Example #1
0
        private void button5_Click(object sender, EventArgs e)
        {
            String data = textBox2.Text;

            if (data.Length != 64)
            {
                textBox2.Text = "Invalid Data";
                return;
            }
            Board.BoardState = data;
            userControl11.ChessBoardState = data;
            Board.PosToPiece = new System.Collections.Hashtable();
            for (int i = 0; i < 64; i++)
            {
                switch (data[i])
                {
                case 'P':
                    WhitePawn wp = new WhitePawn((i % 8) + 1, (i / 8) + 1, 'P');
                    Board.PosToPiece[new Tuple <int, int>(wp.x, wp.y)] = wp;
                    break;

                case 'p':
                    BlackPawn bp = new BlackPawn((i % 8) + 1, (i / 8) + 1, 'p');
                    Board.PosToPiece[new Tuple <int, int>(bp.x, bp.y)] = bp;
                    break;

                case 'R':
                    WhiteRook wr = new WhiteRook((i % 8) + 1, (i / 8) + 1, 'R');
                    Board.PosToPiece[new Tuple <int, int>(wr.x, wr.y)] = wr;
                    break;

                case 'r':
                    BlackRook br = new BlackRook((i % 8) + 1, (i / 8) + 1, 'r');
                    Board.PosToPiece[new Tuple <int, int>(br.x, br.y)] = br;
                    break;

                case 'B':
                    WhiteBishop wb = new WhiteBishop((i % 8) + 1, (i / 8) + 1, 'B');
                    Board.PosToPiece[new Tuple <int, int>(wb.x, wb.y)] = wb;
                    break;

                case 'b':
                    BlackBishop bb = new BlackBishop((i % 8) + 1, (i / 8) + 1, 'b');
                    Board.PosToPiece[new Tuple <int, int>(bb.x, bb.y)] = bb;
                    break;

                case 'N':
                    WhiteKnight wn = new WhiteKnight((i % 8) + 1, (i / 8) + 1, 'N');
                    Board.PosToPiece[new Tuple <int, int>(wn.x, wn.y)] = wn;
                    break;

                case 'n':
                    BlackKnight bn = new BlackKnight((i % 8) + 1, (i / 8) + 1, 'n');
                    Board.PosToPiece[new Tuple <int, int>(bn.x, bn.y)] = bn;
                    break;

                case 'K':
                    WhiteKing wk = new WhiteKing((i % 8) + 1, (i / 8) + 1, 'K');
                    Board.PosToPiece[new Tuple <int, int>(wk.x, wk.y)] = wk;
                    break;

                case 'k':
                    BlackKing bk = new BlackKing((i % 8) + 1, (i / 8) + 1, 'k');
                    Board.PosToPiece[new Tuple <int, int>(bk.x, bk.y)] = bk;
                    break;

                case 'Q':
                    WhiteQueen wq = new WhiteQueen((i % 8) + 1, (i / 8) + 1, 'Q');
                    Board.PosToPiece[new Tuple <int, int>(wq.x, wq.y)] = wq;
                    break;

                case 'q':
                    BlackQueen bq = new BlackQueen((i % 8) + 1, (i / 8) + 1, 'q');
                    Board.PosToPiece[new Tuple <int, int>(bq.x, bq.y)] = bq;
                    break;
                }
            }
        }
Example #2
0
        public static void AnalyseData(String data)
        {
            Board.ContinueWithNormalMovement = true;
            Board.InAnalyseData = true;
            // String.Compare is not useful as I'll have to run the check again if it comes out to be false;
            bool move_occured = false;

            int[] old_positions = { -1, -1 };
            int[] new_positions = { -1, -1 };
            for (int i = 0, j = 0, k = 0; i < data.Length; i++)
            {
                if (data[i] != Board.BoardState[i])
                {
                    if (data[i] == 'x')
                    {
                        // This would obviously be the old position of the moved piece
                        move_occured     = true;
                        old_positions[j] = i;
                        j++;
                    }
                    else
                    {
                        // This would be the new position. It could have been an empty place or a kill might have occured.
                        move_occured     = true;
                        new_positions[k] = i;
                        k++;
                    }
                }
            }


            if (move_occured)
            {
                #region castling
                if (Board.CastlingMode)
                {
                    Tuple <int, int> old_coordinate = new Tuple <int, int>(((old_positions[0] % 8) + 1), (old_positions[0] / 8 + 1));
                    WhiteRook        piece          = (WhiteRook)Board.PosToPiece[old_coordinate];
                    int extent = Math.Abs(old_coordinate.Item1 - 4) - 1;
                    if (piece.castlingPossible && piece.identifier == 'R')
                    {
                        // check till the edge of king. That means that all the places are empty..!!
                        if (piece.check((piece.x == 1) ? 3: 5, 1))
                        {
                            // handling the rook
                            Board.PosToPiece.Remove(old_coordinate);
                            piece.x = (extent == 2) ? 3 : 5;
                            piece.y = 1;
                            piece.castlingPossible = false;
                            Board.PosToPiece[new Tuple <int, int> (piece.x, piece.y)] = piece;
                            WhiteKing KingPiece = (WhiteKing)Board.PosToPiece[new Tuple <int, int>(4, 1)];
                            KingPiece.x = (extent == 2) ? 2 : 6;
                            KingPiece.castlingPossible = false;
                            Board.PosToPiece.Remove(new Tuple <int, int>(4, 1));
                            Board.PosToPiece[new Tuple <int, int> (KingPiece.x, KingPiece.y)] = KingPiece;
                            Board.BoardState = data;
                        }
                    }
                    else if (piece.castlingPossible && piece.identifier == 'r')
                    {
                        // check till the edge of king. That means that all the places are empty..!!
                        if (piece.check((piece.x == 1) ? 3 : 5, 1))
                        {
                            // handling the rook
                            Board.PosToPiece.Remove(old_coordinate);
                            piece.x = (extent == 2) ? 3 : 5;
                            piece.y = 8;
                            piece.castlingPossible = false;
                            Board.PosToPiece[new Tuple <int, int>(piece.x, piece.y)] = piece;
                            BlackKing KingPiece = (BlackKing)Board.PosToPiece[new Tuple <int, int>(4, 8)];
                            KingPiece.x = (extent == 2) ? 2 : 6;
                            KingPiece.castlingPossible = false;
                            Board.PosToPiece.Remove(new Tuple <int, int>(4, 8));
                            Board.PosToPiece[new Tuple <int, int>(KingPiece.x, KingPiece.y)] = KingPiece;
                            Board.BoardState = data;
                        }
                    }
                    Board.n_passantPawn = null;
                    Board.CastlingMode  = false;
                }
                #endregion

                if (Board.ClickableGame)
                {
                    Tuple <int, int> old_coordinates = new Tuple <int, int>((old_positions[0] % 8) + 1, (old_positions[0] / 8) + 1);
                    Piece            piece           = (Piece)Board.PosToPiece[old_coordinates];
                    if (authentication.checkN_PassantKill(piece))
                    {
                        Board.PosToPiece.Remove(new Tuple <int, int>(Board.n_passantPawn.x, Board.n_passantPawn.y));
                        Board.PosToPiece.Remove(old_coordinates);
                        piece.x = (new_positions[0] % 8) + 1;
                        piece.y = (new_positions[0] / 8) + 1;
                        Board.PosToPiece[new Tuple <int, int>(piece.x, piece.y)] = piece;
                        Board.GenerateBoardState();
                        Board.ContinueWithNormalMovement = false;
                        Board.n_passantPawn = null;
                    }
                }

                #region NormalMovement
                // normal movement or killing move
                if (((old_positions[1] == -1 && new_positions[1] == -1) == true) && Board.ContinueWithNormalMovement)
                {
                    Board.n_passantPawn = null;
                    Tuple <int, int> old_coordinate = new Tuple <int, int>(((old_positions[0] % 8) + 1), ((old_positions[0] / 8) + 1));
                    Piece            piece          = (Piece)Board.PosToPiece[old_coordinate];

                    if (((piece is WhitePawn) && (piece.y == 7)) ||
                        ((piece is BlackPawn) && (piece.y == 2)))
                    {
                        PawnPromotion(data, old_positions, new_positions);
                    }

                    else if (piece.check((new_positions[0] % 8) + 1, (new_positions[0] / 8) + 1))
                    {
                        Board.BoardState = data;
                        Board.PosToPiece.Remove(old_coordinate);
                        piece.x = (new_positions[0] % 8) + 1;
                        piece.y = (new_positions[0] / 8) + 1;
                        Board.PosToPiece[new Tuple <int, int>(piece.x, piece.y)] = piece;
                    }
                }
                #endregion

                #region N_Passant Movement
                //N_passantMovement
                else if ((new_positions[1] == -1 && old_positions[1] != -1))
                {
                    Tuple <int, int> old_coordinates;
                    if (Board.n_passantPawn == null)
                    {
                        return;
                    }
                    if (
                        (((old_positions[0] % 8) + 1) == Board.n_passantPawn.x) &&
                        (((old_positions[0] / 8) + 1) == Board.n_passantPawn.y))
                    {
                        old_coordinates = new Tuple <int, int>((old_positions[1] % 8) + 1, old_positions[1] / 8 + 1);
                    }
                    else
                    {
                        old_coordinates = new Tuple <int, int>((old_positions[0] % 8) + 1, old_positions[0] / 8 + 1);
                    }
                    Piece piece = (Piece)Board.PosToPiece[old_coordinates];
                    if (authentication.checkN_PassantKill(piece))
                    {
                        Board.BoardState = data;
                        Board.PosToPiece.Remove(new Tuple <int, int> (Board.n_passantPawn.x, Board.n_passantPawn.y));
                        Board.PosToPiece.Remove(old_coordinates);
                        piece.x = (new_positions[0] % 8) + 1;
                        piece.y = (new_positions[0] / 8) + 1;
                        Board.PosToPiece[new Tuple <int, int>(piece.x, piece.y)] = piece;
                    }
                }



                #endregion
            }
            Board.InAnalyseData = false;
            //don't keep it hardcoded.
            authentication.MateLogic(false);
        }
Example #3
0
        public Board()
        {
            #region addingPawns

            for (int i = 1; i <= 8; i++)
            {
                wp = new WhitePawn(i, 2, 'P');
                addToHashTables(wp);
                bp = new BlackPawn(i, 7, 'p');
                addToHashTables(bp);
            }


            #endregion

            #region Adding Pieces manually

            wr = new WhiteRook(1, 1, 'R');
            addToHashTables(wr);

            wb = new WhiteBishop(3, 1, 'B');
            addToHashTables(wb);

            wn = new WhiteKnight(2, 1, 'N');
            addToHashTables(wn);

            wk = new WhiteKing(4, 1, 'K');
            addToHashTables(wk);

            wq = new WhiteQueen(5, 1, 'Q');
            addToHashTables(wq);

            wr = new WhiteRook(8, 1, 'R');
            addToHashTables(wr);

            wb = new WhiteBishop(6, 1, 'B');
            addToHashTables(wb);

            wn = new WhiteKnight(7, 1, 'N');
            addToHashTables(wn);


            br = new BlackRook(1, 8, 'r');
            addToHashTables(br);

            bb = new BlackBishop(3, 8, 'b');
            addToHashTables(bb);

            bn = new BlackKnight(2, 8, 'n');
            addToHashTables(bn);

            bk = new BlackKing(4, 8, 'k');
            addToHashTables(bk);

            bq = new BlackQueen(5, 8, 'q');
            addToHashTables(bq);

            br = new BlackRook(8, 8, 'r');
            addToHashTables(br);

            bb = new BlackBishop(6, 8, 'b');
            addToHashTables(bb);

            bn = new BlackKnight(7, 8, 'n');
            addToHashTables(bn);


            #endregion

            GenerateBoardState();
        }