Пример #1
0
 public bool isThreatened(int row, int col, Piece.Colour threatenedBy, Piece[,] board)
 {
     bool threatened = false;
     List<int[]> covered = new List<int[]>();
     List<int[]> filler = new List<int[]>();
     for (int rows = 0; rows < Board.Rows; rows++)
     {
         for (int cols = 0; cols < Board.Cols; cols++)
         {
             if (board[rows, cols].PieceColour == threatenedBy)
             {
                 thm.highlightThreatened(board[rows, cols], rows, cols, covered, filler, board, false);
             }
         }
     }
     foreach (int[] pair in covered)
     {
         if (pair[0] == row && pair[1] == col)
         {
             threatened = true;
             break;
         }
     }
     return threatened;
 }
Пример #2
0
        private void SetPiece(Piece piece, Player player, int letter, int number)
        {
            // if a thread called this, invoke recursion
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() => SetPiece(piece, player, letter, number)));
                return;
            }

            // out of bounds
            if (letter < 0 || letter > 7 || number < 0 || number > 7)
                return; // throw new IndexOutOfRangeException("Chess board letter or number out of range");

            // clear tile
            if (piece == Piece.NONE)
            {
                Board[number][letter].Image = null;
                Board[number][letter].Invalidate();
                return;
            }

            // update our render
            Board[number][letter].Image = graphics.Pieces[player][piece];
            Board[number][letter].Invalidate();
        }
 void rookThreatenHighlight(Piece.Colour colour, int row, int col, List<int[]> threatened, Piece[,] board)
 {
     int i = 1;
     //check down
     while (addThreatened(colour, row, col, i, 0, threatened))
     {
         if (board[row + i, col].Exists)
             break;
         i++;
     }
     //check up
     i = 1;
     while (addThreatened(colour, row, col, -i, 0, threatened))
     {
         if (board[row - i, col].Exists)
             break;
         i++;
     }
     //check right
     i = 1;
     while (addThreatened(colour, row, col, 0, i, threatened))
     {
         if (board[row, col + i].Exists)
             break;
         i++;
     }
     //check left
     i = 1;
     while (addThreatened(colour, row, col, 0, -i, threatened))
     {
         if (board[row, col - i].Exists)
             break;
         i++;
     }
 }
 void bishopThreatenHighlight(string colour, int row, int col, List<int[]> threatened, Piece[,] board)
 {
     int i = 1;
     //check down right
     while (addThreatened(colour, row, col, i, i, threatened))
     {
         if (board[row + i, col + i].Exists)
             break;
         i++;
     }
     //check up right
     i = 1;
     while (addThreatened(colour, row, col, -i, i, threatened))
     {
         if (board[row - i, col + i].Exists)
             break;
         i++;
     }
     //check up left
     i = 1;
     while (addThreatened(colour, row, col, -i, -i, threatened))
     {
         if (board[row - i, col - i].Exists)
             break;
         i++;
     }
     //check down left
     i = 1;
     while (addThreatened(colour, row, col, i, -i, threatened))
     {
         if (board[row + i, col - i].Exists)
             break;
         i++;
     }
 }
Пример #5
0
 public void performEnPassant(int row, int col, Piece[,] Playfield)
 {
     if (row == 2)
         Playfield[3, col] = new Piece();
     if (row == 5)
         Playfield[4, col] = new Piece();
 }
Пример #6
0
 public void checkForCheck(Piece[,] board, Piece.Colour colour)
 {
     if (cf.isThreatened(uf.FindKing(uf.OtherColour(colour), board), colour, board))
         df.Check();
     else
         df.Clear();
 }
Пример #7
0
 void addTaken(Piece piece, Piece[,] takenGrid, int[] takenIndeces)
 {
     takenGrid[takenIndeces[0], takenIndeces[1]] = piece;
     takenIndeces[1]++;
     takenIndeces[0] += takenIndeces[1] / 2;
     takenIndeces[1] %= 2;
 }
Пример #8
0
 public void performEnPassant(int row, int col, Piece[,] Playfield)
 {
     if (row == Board.FirstRow + 2)
         Playfield[Board.FirstRow + 3, col] = new Piece();
     if (row == Board.LastRow - 2)
         Playfield[Board.LastRow - 3, col] = new Piece();
 }
 void pawnThreatenHighlight(Piece piece, int row, int col, List<int[]> threatened, Piece[,] board, List<int[]> enPassant)
 {
     //capturing
     if (piece.PieceColour == Piece.Colour.Black)
     {
         addThreatened(piece.PieceColour, piece.Row, piece.Col, 1, 1, threatened);
         addThreatened(piece.PieceColour, piece.Row, piece.Col, 1, -1, threatened);
     }
     if (piece.PieceColour == Piece.Colour.White)
     {
         addThreatened(piece.PieceColour, piece.Row, piece.Col, -1, 1, threatened);
         addThreatened(piece.PieceColour, piece.Row, piece.Col, -1, -1, threatened);
     }
     //en passant
     if (piece.PieceColour == Piece.Colour.Black)
     {
         addEnPassant(piece.PieceColour, piece.Row, piece.Col, 1, 1, enPassant, board);
         addEnPassant(piece.PieceColour, piece.Row, piece.Col, 1, -1, enPassant, board);
     }
     if (piece.PieceColour == Piece.Colour.White)
     {
         addEnPassant(piece.PieceColour, piece.Row, piece.Col, -1, 1, enPassant, board);
         addEnPassant(piece.PieceColour, piece.Row, piece.Col, -1, -1, enPassant, board);
     }
 }
Пример #10
0
 // Copy-constructor
 public Piece(Piece origin)
 {
     this.empty = origin.empty;
     this.team = origin.team;
     this.type = origin.type;
     this.row = origin.row;
     this.column = origin.column;
 }
Пример #11
0
 public PieceMove(Piece piece, Square target, Piece capturedPiece = null)
 {
     this.Piece = piece;
     this.Source = piece.Square;
     this.Target = target;
     this.CanCapture = true;
     this.CapturedPiece = capturedPiece ?? this.Board[target];
 }
Пример #12
0
 public void TakeTurn(Piece[,] board, Piece[,] WhiteTaken, Piece[,] BlackTaken, int[] whiteTakenIndeces, int[] blackTakenIndeces, int depth)
 {
     topNode = new MoveNode(board);
     think(depth, topNode);
     bool mate = false;
     AvailableMove move = selectMove(allMovesList, board, ref mate, currentColour);
     if (!mate)
         makeMove(move, board, WhiteTaken, BlackTaken, whiteTakenIndeces, blackTakenIndeces);
 }
 void addEnPassant(string colour, int row, int col, int rowOffset, int colOffset, List<int[]> enPassant, Piece[,] board)
 {
     int destinationRow = row + rowOffset;
     int destinationCol = col + colOffset;
     if (destinationRow >= 0 && destinationRow <= 7 && destinationCol >= 0 && destinationCol <= 7 && board[row, col + colOffset].EnPassant)
     {
         enPassant.Add(new int[] { destinationRow, destinationCol });
     }
 }
Пример #14
0
        internal static Image GetSquareImage(Square square, Piece piece = null)
        {
            if (piece == null)
                return boardImageCache["Empty"][square.GetSquareColor()];

            var squareColor = square.GetSquareColor();
            var key = piece.Player + piece.GetType().Name;
            return boardImageCache[key][squareColor];
        }
Пример #15
0
        public List<Tuple<int, int>> Rules(Piece currentPiece, Gameboard gameboard)
        {
            this.moves_dia = get_diagonal.Rules(currentPiece, gameboard);
            this.moves_str = get_straight.Rules(currentPiece, gameboard);

            moves_str = moves_str.Concat(moves_dia).ToList();

            return moves_str;
        }
Пример #16
0
        public void AddPiece(Piece piece, BoardCoordinate moveTarget)
        {
            if (piece == null)
                throw new ArgumentNullException("piece");
            if (!moveTarget.IsCoordinateValidForBoardSize(_boardSize))
                throw new ArgumentException("moveTarget");

            SetPiece(piece, moveTarget);
        }
Пример #17
0
 public Piece(Piece piece)
 {
     Colour = piece.Colour;
     Type = piece.Type;
     Exists = piece.Exists;
     HasMoved = piece.HasMoved;
     EnPassant = piece.EnPassant;
     Row = piece.Row;
     Col = piece.Col;
 }
Пример #18
0
        public List<Tuple<int, int>> Rules(Piece current_piece, Gameboard gameboard)
        {
            this.gameboard = gameboard;
            this.current_piece = current_piece;

            if (current_piece.column > min && current_piece.row > min + 1)
            {
                next_piece = gameboard.getPiece(current_piece.row - 2, current_piece.column - 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row - 2, current_piece.column - 1));   // up left
            }
            if (current_piece.column < max && current_piece.row > min + 1)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row - 2, current_piece.column + 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row -2, current_piece.column + 1)); // up right
            }
            if (current_piece.column < max - 1 && current_piece.row > min)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row - 1, current_piece.column + 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row - 1, current_piece.column + 2)); // right up
            }
            if (current_piece.column < max - 1 && current_piece.row < max)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 1, current_piece.column + 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 1, current_piece.column + 2));  // right down
            }
            if (current_piece.column < max && current_piece.row < max - 1)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 2, current_piece.column + 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 2, current_piece.column + 1));  // down right
            }
            if (current_piece.column > min && current_piece.row < max - 1)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 2, current_piece.column - 1);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 2, current_piece.column - 1));  // down left
            }
            if (current_piece.column > min + 1 && current_piece.row < max)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row + 1, current_piece.column - 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row + 1, current_piece.column - 2));  // left down
            }
            if (current_piece.column > min + 1 && current_piece.row > min)
            {
                Piece next_piece = gameboard.getPiece(current_piece.row - 1, current_piece.column - 2);
                if (next_piece.empty == true || next_piece.team != current_piece.team)
                    valid_destinations.Add(new Tuple<int, int>(current_piece.row - 1, current_piece.column - 2)); // left up
            }
            return valid_destinations;
        }
Пример #19
0
 public Piece.Colour OtherColour(Piece.Colour colour)
 {
     Piece.Colour otherColour;
     if (colour == Piece.Colour.Black)
         otherColour = Piece.Colour.White;
     else if (colour == Piece.Colour.White)
         otherColour = Piece.Colour.Black;
     else
         otherColour = Piece.Colour.Empty;
     return otherColour;
 }
Пример #20
0
 //Methods
 public void AddTaken(Piece piece, Piece[,] whiteTaken, Piece[,] blackTaken, int[] whiteTakenIndeces, int[] blackTakenIndeces)
 {
     if (piece.PieceColour == Piece.Colour.White)
     {
         addTaken(piece, whiteTaken, whiteTakenIndeces);
     }
     if (piece.PieceColour == Piece.Colour.Black)
     {
         addTaken(piece, blackTaken, blackTakenIndeces);
     }
 }
Пример #21
0
 public Promotion(int row, ref Piece.Type promoteTo)
 {
     cvm = new ChessVM();
     InitializeComponent();
     if (row == 0)
     {
         imgWhiteBishop.Visibility = Visibility.Visible;
         imgWhiteKnight.Visibility = Visibility.Visible;
         imgWhiteQueen.Visibility = Visibility.Visible;
         imgWhiteRook.Visibility = Visibility.Visible;
     }
 }
Пример #22
0
        private void up_right_function()
        {
            next_piece = gameboard.getPiece(current_piece.row - 1, current_piece.column + 1);
            while (next_piece.team != current_piece.team)
            {
                valid_destinations.Add(new Tuple<int, int>(next_piece.row, next_piece.column));

                if (next_piece.row - 1 < min || next_piece.column + 1 > max || next_piece.team != (int)team.none)
                    break;
                next_piece = gameboard.getPiece(next_piece.row - 1, next_piece.column + 1);
            }
        }
Пример #23
0
        public void InitializePlayfield(ref bool isWhiteTurn, ref bool isGameDone, 
            List<int[]> highlight, Piece[,] playfield, Piece[,] whiteTaken, 
            Piece[,] blackTaken, ref int[] whiteTakenIndeces, ref int[] blackTakenIndeces)
        {
            //reseting parameters
            df.Clear();
            isWhiteTurn = true;
            isGameDone = false;
            highlight.Clear();
            whiteTakenIndeces = new int[] { 0, 0 };
            blackTakenIndeces = new int[] { 0, 0 };
            for (int row = 0; row < Board.Rows; row++)
            {
                for (int col = 0; col < Board.Cols; col++)
                {
                    //set piece type
                    playfield[row, col] = new Piece() { Row = row, Col = col };
                    if (row == Board.FirstRow || row == Board.LastRow)
                    {
                        if (col == Board.FirstCol || col == Board.LastCol)
                            playfield[row, col].PieceType = Piece.Type.Rook;
                        if (col == Board.FirstCol + 1 || col == Board.LastCol - 1)
                            playfield[row, col].PieceType = Piece.Type.Knight;
                        if (col == Board.FirstCol + 2 || col == Board.LastCol - 2)
                            playfield[row, col].PieceType = Piece.Type.Bishop;
                        if (col == Board.FirstCol + 3)
                            playfield[row, col].PieceType = Piece.Type.Queen;
                        if (col == Board.FirstCol + 4)
                            playfield[row, col].PieceType = Piece.Type.King;
                    }
                    if (row == Board.FirstRow + 1 || row == Board.LastRow - 1)
                        playfield[row, col].PieceType = Piece.Type.Pawn;
                    //set piece color
                    //all pieces in the top two rows are black
                    if (row <= Board.FirstRow + 1)
                        playfield[row, col].PieceColour = Piece.Colour.Black;
                    //all pieces in the bottom two rows are white
                    if (row >= Board.LastRow - 1)
                        playfield[row, col].PieceColour = Piece.Colour.White;
                    //set those pieces to existing
                    if (row <= Board.FirstRow + 1 || row >= Board.LastRow - 1)
                        playfield[row, col].Exists = true;
                }

                //Reset the taken grids
                for (int col = 0; col < 2; col++)
                {
                    whiteTaken[row, col] = new Piece();
                    blackTaken[row, col] = new Piece();
                }
            }
        }
Пример #24
0
        private void check_right_function()
        {
            // Console.WriteLine("inne i check right");
            next_piece = gameboard.getPiece(current_piece.row, current_piece.column + 1);
            while (next_piece.team != current_piece.team)
            {
                valid_destinations.Add(new Tuple<int, int>(next_piece.row, next_piece.column));

                if (next_piece.column + 1 > max || next_piece.team != (int)team.none)
                    break;
                next_piece = gameboard.getPiece(next_piece.row, next_piece.column + 1);
            }
        }
Пример #25
0
        private void check_down_function()
        {
            // Console.WriteLine("inne i check down");
            next_piece = gameboard.getPiece(current_piece.row + 1, current_piece.column);
            while (next_piece.team != current_piece.team)
            {
                valid_destinations.Add(new Tuple<int, int>(next_piece.row, next_piece.column));
                //Console.WriteLine("Added: {0},{1} as valid coordinate.", next_piece.row, next_piece.column);

                if (next_piece.row + 1 > max || next_piece.team != (int)team.none)
                    break;
                next_piece = gameboard.getPiece(next_piece.row + 1, next_piece.column);
            }
        }
Пример #26
0
        public static bool ParseLine(String line, out String message)
        {
            string placingPattern = "^([KQBNRP])([ld])([a-h, A-H][1-8])$";
            string movingPattern = "^([a-h,A-H][1-8])\\s([a-h,A-H][1-8])$";
            string doubleMovePattern = "^([a-h,A-H][1-8])\\s([a-h,A-H][1-8])\\s([a-h,A-H][1-8])\\s([a-h,A-H][1-8])$";

            message = "";
            if (Regex.IsMatch(line, placingPattern))
            {
                char piece = line.ElementAt(0);
                char color = line.ElementAt(1);
                char rank = line.ElementAt(2);
                char file = line.ElementAt(3);
                Piece p = new Piece(piece, color);
                message = string.Concat("Place the ", p.GetColor(), " ", p.GetPieceType(), " at ", rank, file, "\n");
                validPlacements.Add(line);
                return true;
            }
            else if (Regex.IsMatch(line, movingPattern))
            {
                char startingRank = line.ElementAt(0);
                char startingFile = line.ElementAt(1);
                char endingRank = line.ElementAt(3);
                char endingFile = line.ElementAt(4);
                message = string.Concat("Move the piece at ", startingRank, startingFile, " to ", endingRank, endingFile, "\n");
                validMoves.Add(line);
                return true;
            }
            else if (Regex.IsMatch(line, doubleMovePattern))
            {
                char firstStartingRank = line.ElementAt(0);
                char firstStartingFile = line.ElementAt(1);
                char firstEndingRank = line.ElementAt(3);
                char firstEndingFile = line.ElementAt(4);
                char secondStartingRank = line.ElementAt(6);
                char secondStartingFile = line.ElementAt(7);
                char secondEndingRank = line.ElementAt(9);
                char secondEndingFile = line.ElementAt(10);
                message = string.Concat("Move the piece at ", firstStartingRank, firstStartingFile, " to ", firstEndingRank, firstEndingFile, " and the piece at ", secondStartingRank, secondStartingFile, " to ", secondEndingRank, secondEndingFile,"\n");
                validMoves.Add(line);
                return true;
            }
            else if (!Regex.IsMatch(line, "^($)"))
            {
                PrintInfo(line, "This line is invalid input.");
                return false;
            }
            return false;
        }
Пример #27
0
        public Piece[,] MergeBackgrounds(Piece[,] background, Piece[,] pattern)
        {
            Piece[,] merge = new Piece[8, 8];
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (pattern[j, i] != null)
                        merge[j, i] = pattern[j, i];
                    else merge[j, i] = background[j, i];
                }
            }

            return merge;
        }
Пример #28
0
 public string GetImageFile(Piece piece, int row, int col)
 {
     //Change this line to match the project name
     string fileName = "pack://application:,,,/Chess;component/Images/";
     if (piece.Exists)
     {
         fileName += piece.Colour;
         fileName += piece.Type;
     }
     if (row % 2 == col % 2)
         fileName += "Light.png";
     else
         fileName += "Dark.png";
     return fileName;
 }
 bool addMoveable(int row, int col, int rowOffset, int colOffset, List<int[]> moveable, Piece[,] board)
 {
     bool added = false;
     int destinationRow = row + rowOffset;
     int destinationCol = col + colOffset;
     if (uf.IsInBounds(destinationRow, destinationCol))
     {
         if (board[destinationRow, destinationCol].Colour != board[row, col].Colour && !cf.createCheck(row, col, destinationRow, destinationCol, board))
         {
             moveable.Add(new int[] { destinationRow, destinationCol });
             added = true;
         }
     }
     return added;
 }
Пример #30
0
 void pawnMoveHighlight(Piece piece, int row, int col, List<int[]> moveable, Piece[,] board)
 {
     //moving
     if (piece.PieceColour == Piece.Colour.Black && !board[row + 1, col].Exists)
     {
         addMoveable(piece.Row, piece.Col, 1, 0, moveable, board);
         if (!piece.HasMoved && !board[piece.Row + 2, piece.Col].Exists)
             addMoveable(piece.Row, piece.Col, 2, 0, moveable, board);
     }
     if (piece.PieceColour == Piece.Colour.White && !board[row - 1, col].Exists)
     {
         addMoveable(piece.Row, piece.Col, -1, 0, moveable, board);
         if (!piece.HasMoved && !board[piece.Row - 2, piece.Col].Exists)
             addMoveable(piece.Row, piece.Col, -2, 0, moveable, board);
     }
 }
Пример #31
0
 private Texture PieceTexture(Piece piece)
 {
     return(piece.Owner == Piece.Colour.White
                         ? Resources.WhitePieces[piece.Type]
                         : Resources.BlackPieces[piece.Type]);
 }
Пример #32
0
        private bool CanMove(Position position)
        {
            Piece piece = Board.Piece(position);

            return(piece == null || piece.Color != Color);
        }
Пример #33
0
        /*********************************************************************
        * ButtonClick
        * Handles the button click for each space of the board
        *********************************************************************/
        public void ButtonClick(object sender, MouseEventArgs e)
        {
            Space thisButton = ((Space)sender);

            //MessageBox.Show(whiteTeam[0].GetRow().ToString() + " " + whiteTeam[0].GetColumn().ToString());
            if (!gameOver)
            {
                // Check if it was a right mouse click
                if (e.Button == MouseButtons.Right)
                {
                    // if this was the selected space then we unselect it
                    if (selected != null && thisButton.GetPiece() == selected)
                    {
                        selected             = null;
                        thisButton.ForeColor = thisButton.GetPiece().GetTextColor();
                        ClearPossible();
                        return;
                    }
                }
                // left mouse click
                else
                {
                    // if there isn't a piece selected
                    if (selected == null)
                    {
                        // if the place we just clicked on has a piece there
                        if (thisButton.GetPiece() != null)
                        {
                            // if the piece is on our team
                            if (turn == thisButton.GetPiece().GetTeam())
                            {
                                // set this piece as the selected one
                                selected = thisButton.GetPiece();
                                if (e.Button == MouseButtons.Right)
                                {
                                }
                                else
                                {
                                    // calculate all the spots this piece could move to
                                    selected.CalcPossMoves(board);
                                    // if it can't move then we unselect it
                                    if (selected.GetPossMoves().Count == 0)
                                    {
                                        selected = null;
                                    }
                                    // we set the text color to yellow to show which piece is selected
                                    else
                                    {
                                        thisButton.ForeColor = Color.Yellow;
                                        ShowPossibleMoves();
                                    }
                                }
                            }
                        }
                    }
                    // if we already had a selected piece
                    else
                    {
                        int row = thisButton.GetRow();
                        int col = thisButton.GetCol();

                        // if the selected piece can be moved here then we move it
                        if (IsPossible(row, col))
                        {
                            int fromRow = selected.GetRow();
                            int fromCol = selected.GetColumn();
                            MoveTo(fromRow, fromCol, row, col);
                            ClearPossible();
                            gameOver = IsCheckMate();
                            SwitchTurn();
                        }
                    }
                }
            }
        }
Пример #34
0
        private bool canMov(Position pos)
        {
            Piece p = board.piece(pos);

            return(p == null || p.collor != collor);
        }
Пример #35
0
        private void MouseClick(object sender, MouseEventArgs e)
        {
            Piece piece = sender as Piece;

            if (!FirstMouseClick)
            {
                if (piece.PiecePosition < BoardSizeC * 2)
                {
                    Image imagn;
                    if (Global.GlobalCuloare == CuloarePiesa.Negru)
                    {
                        imagn = Resources.BK;
                    }
                    else
                    {
                        imagn = Resources.WK;
                    }
                    pieceIdBoard[piece.PiecePosition].Image      = imagn;
                    pieceIdBoard[piece.PiecePosition].SizeMode   = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
                    pieceIdBoard[piece.PiecePosition].PieceName  = "Wp";
                    pieceIdBoard[piece.PiecePosition].PieceValue = 10;
                    pieceIdBoard[piece.PiecePosition].Enumarare  = ++count;
                    label1.Text     = "Selectați modul în care se va deplasa regele";
                    FirstMouseClick = true;
                    LastX           = piece.Location.X;
                    LastY           = piece.Location.Y;
                }
                else
                {
                    MessageBox.Show("Piesa de start trebuie să fie așezată pe primele 2 rânduri ale tabelei");
                }
            }
            else
            if (e.Button == MouseButtons.Left)
            {
                if (pieceIdBoard[piece.PiecePosition].Enumarare != 0)
                {
                    MessageBox.Show("Nu aveți voie să selectați de 2 ori aceeași căsuță!");
                    return;
                }
                if ((Math.Abs(piece.Location.X - LastX) / 84 == 1 && Math.Abs(piece.Location.Y - LastY) / 84 == 1) ||
                    (Math.Abs(piece.Location.X - LastX) / 84 == 0 && Math.Abs(piece.Location.Y - LastY) / 84 == 1) ||
                    (Math.Abs(piece.Location.X - LastX) / 84 == 1 && Math.Abs(piece.Location.Y - LastY) / 84 == 0))
                {
                    if (Culoarea == Color.Yellow)
                    {
                        pieceIdBoard[piece.PiecePosition].Enumarare = ++count;
                    }
                    else
                    {
                        pieceIdBoard[piece.PiecePosition].Enumarare = --negativecount;
                    }
                    pieceIdBoard[piece.PiecePosition].BackColor = Culoarea;
                    LastX = piece.Location.X;
                    LastY = piece.Location.Y;
                }
                else
                {
                    MessageBox.Show("Căsuța selectată trebuie să fie legată de căsuța precedent selectată", "Eroare");
                }
            }
            else
            if (e.Button == MouseButtons.Right && pornit == 1)
            {
                if (pieceIdBoard[piece.PiecePosition].Capturare != 0 || pieceIdBoard[piece.PiecePosition].Enumarare == 1)
                {
                    MessageBox.Show("Nu aveți voie să selectați de 2 ori aceeași căsuță!");
                    return;
                }
                pieceIdBoard[piece.PiecePosition].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                pieceIdBoard[piece.PiecePosition].BackColor   = Color.FromArgb(255, 213, 0);
                pieceIdBoard[piece.PiecePosition].Capturare   = ++countt;
            }
        }
Пример #36
0
        public static Tuple <bool, bool> checkStraightMovement(Piece piece, int x_new, int y_new)
        {
            // returns 2 data in the variables (move_possible, will kill)

            // ensures that it is a straight line
            bool move_possible = true;
            bool will_kill     = false;

            if ((piece.x != x_new) && (piece.y != y_new))
            {
                move_possible = will_kill = false;
                Tuple <bool, bool> result = new Tuple <bool, bool>(move_possible, will_kill);
                return(result);
            }

            List <Tuple <int, int> > temp_locations = new List <Tuple <int, int> >();

            #region fill temp_locations
            for (int y = piece.y; y <= y_new; y++)
            {
                Tuple <int, int> temp_coords = new Tuple <int, int>(x_new, y);
                temp_locations.Add(temp_coords);
            }
            for (int y = y_new; y <= piece.y; y++)
            {
                Tuple <int, int> temp_coords = new Tuple <int, int>(x_new, y);
                temp_locations.Add(temp_coords);
            }
            for (int x = piece.x; x <= x_new; x++)
            {
                Tuple <int, int> temp_coords = new Tuple <int, int>(x, y_new);
                temp_locations.Add(temp_coords);
            }
            for (int x = x_new; x <= piece.x; x++)
            {
                Tuple <int, int> temp_coords = new Tuple <int, int>(x, y_new);
                temp_locations.Add(temp_coords);
            }
            #endregion

            foreach (var i in temp_locations)
            {
                if (checkPosToPiece(i.Item1, i.Item2))
                {
                    Piece j = (Piece)Board.PosToPiece[i];

                    if ((j.colour != piece.colour) &&
                        (j.x == x_new) && (j.y == y_new))
                    {
                        will_kill = true;
                    }

                    if (
                        ((j.colour == piece.colour) && (j != piece)) ||
                        ((j.colour != piece.colour) && ((j.x != x_new) || (j.y != y_new))))
                    {
                        move_possible = will_kill = false;
                        Tuple <bool, bool> result = new Tuple <bool, bool>(move_possible, will_kill);
                        return(result);
                    }
                }
            }
            Tuple <bool, bool> result1 = new Tuple <bool, bool>(move_possible, will_kill);
            return(result1);
        }
Пример #37
0
        public static bool MateLogic(bool colour)
        {
            authentication.InCheckLogic(colour);
            if (!Board.InCheck)
            {
                Board.tempData = "not in check";
                return(false);
            }
            if (authentication.KingHasLegalMoves(colour))
            {
                Board.tempData = "king can move";
                return(false);
            }
            // I simply can't use the algorithm of finding the temp locations of the piece
            // which is checking the king and see if one of my pieces can reach those locations
            // and hence block, or kill the piece, and then again check for mate, because there might
            // be more than 1 piece checking.
            // So I'll create a list of pieces which are checking the king. Then I'll create their temporary locations,
            // then i'll see if one of my pieces can go to their temporary locations, killing or blocking them, and then again
            // check for InCheck. That way everything will be awesome. I need an overloaded InCheckLogic into which I can send a
            // changed chessboard. Have to think of an efficient way for that.
            List <Tuple <int, int> > tempLocations = new List <Tuple <int, int> >();

            #region filling tempLocations
            foreach (Piece p in Board.PiecesCheckingKing)
            {
                switch (p.identifier)
                {
                case 'p':
                case 'P':
                    // I can only kill pawns, not block them
                    tempLocations.Add(new Tuple <int, int>(p.x, p.y));
                    break;

                case 'R':
                case 'r':
                    // This includes their own position as well as the path the rook takes
                    for (int i = Math.Min(p.x, Board.KingInCheck.x); i <= Math.Max(p.x, Board.KingInCheck.x); i++)
                    {
                        for (int j = Math.Min(p.y, Board.KingInCheck.y); j <= Math.Max(p.y, Board.KingInCheck.y); j++)
                        {
                            tempLocations.Add(new Tuple <int, int>(i, j));
                        }
                    }
                    tempLocations.Remove(new Tuple <int, int>(Board.KingInCheck.x, Board.KingInCheck.y));
                    break;

                case 'b':
                case 'B':
                    // fill in the diagonal locations from piece to king
                    #region filling in temp locations
                    int[] x_pos   = { 0, 0, 0, 0, 0, 0, 0, 0 };
                    int[] y_pos   = { 0, 0, 0, 0, 0, 0, 0, 0 };
                    int   x_pos_i = 0;
                    if (p.x < Board.KingInCheck.x)
                    {
                        for (int i = p.x; i <= Board.KingInCheck.x; i++)
                        {
                            x_pos[x_pos_i] = i;
                            x_pos_i++;
                        }
                    }
                    else
                    {
                        for (int i = p.x; i >= Board.KingInCheck.x; i--)
                        {
                            x_pos[x_pos_i] = i;
                            x_pos_i++;
                        }
                    }
                    int y_pos_i = 0;
                    if (p.y < Board.KingInCheck.y)
                    {
                        for (int i = p.y; i <= Board.KingInCheck.y; i++)
                        {
                            y_pos[y_pos_i] = i;
                            y_pos_i++;
                        }
                    }
                    else
                    {
                        for (int i = p.y; i >= Board.KingInCheck.y; i--)
                        {
                            y_pos[y_pos_i] = i;
                            y_pos_i++;
                        }
                    }
                    #endregion
                    for (int i = 0; i < 8; i++)
                    {
                        if (x_pos[i] == 0)
                        {
                            break;
                        }
                        tempLocations.Add(new Tuple <int, int>(x_pos[i], y_pos[i]));
                    }
                    tempLocations.Remove(new Tuple <int, int>(Board.KingInCheck.x, Board.KingInCheck.y));
                    break;

                case 'N':
                case 'n':
                    // I'll have to kill the knights. Can't block them
                    tempLocations.Add(new Tuple <int, int>(p.x, p.y));
                    break;

                case 'Q':
                case 'q':
                    #region Filling in temporary locations for queen
                    // check if straight kill or diagnol kill
                    if ((p.x == Board.KingInCheck.x) || (p.y == Board.KingInCheck.y))
                    {
                        for (int i = Math.Min(p.x, Board.KingInCheck.x); i <= Math.Max(p.x, Board.KingInCheck.x); i++)
                        {
                            for (int j = Math.Min(p.y, Board.KingInCheck.y); j <= Math.Max(p.y, Board.KingInCheck.y); j++)
                            {
                                tempLocations.Add(new Tuple <int, int>(i, j));
                            }
                        }
                    }
                    else
                    {
                        // diagonal kill
                        int[] xpos  = { 0, 0, 0, 0, 0, 0, 0, 0 };
                        int[] ypos  = { 0, 0, 0, 0, 0, 0, 0, 0 };
                        int   xposi = 0;
                        if (p.x < Board.KingInCheck.x)
                        {
                            for (int i = p.x; i <= Board.KingInCheck.x; i++)
                            {
                                xpos[xposi] = i;
                                xposi++;
                            }
                        }
                        else
                        {
                            for (int i = p.x; i >= Board.KingInCheck.x; i--)
                            {
                                xpos[xposi] = i;
                                xposi++;
                            }
                        }
                        int yposi = 0;
                        if (p.y < Board.KingInCheck.y)
                        {
                            for (int i = p.y; i <= Board.KingInCheck.y; i++)
                            {
                                ypos[yposi] = i;
                                yposi++;
                            }
                        }
                        else
                        {
                            for (int i = p.y; i >= Board.KingInCheck.y; i--)
                            {
                                ypos[yposi] = i;
                                yposi++;
                            }
                        }
                        for (int i = 0; i < 8; i++)
                        {
                            if (xpos[i] == 0)
                            {
                                break;
                            }
                            tempLocations.Add(new Tuple <int, int>(xpos[i], ypos[i]));
                        }
                    }
                    #endregion
                    tempLocations.Remove(new Tuple <int, int>(Board.KingInCheck.x, Board.KingInCheck.y));
                    break;
                }
            }
            #endregion
            // so that changing PosToPiece does not raise exception
            List <Piece> temp_pieces = Board.PosToPiece.Values.Cast <Piece>().ToList();
            foreach (Piece piece in temp_pieces)
            {
                if (piece.colour == colour)
                {
                    foreach (Tuple <int, int> pos in tempLocations)
                    {
                        if (piece.check(pos.Item1, pos.Item2))
                        {
                            Piece original_piece = null;
                            // checks that its not check till now
                            // creating a new representation doesn't work as still piece.check uses the PosToPiece
                            // also instead of modifying all the piece functions with optional arguments,
                            // I'll modify the PosToPiece itself and correct it before the function returns

                            /*
                             * System.Collections.Hashtable tempBoardRepresentation = (System.Collections.Hashtable)Board.PosToPiece.Clone();
                             * tempBoardRepresentation.Remove(new Tuple<int, int>(piece.x, piece.y));
                             * tempBoardRepresentation[new Tuple<int, int>(pos.Item1, pos.Item2)] = piece;
                             * if (authentication.InCheckLogic(tempBoardRepresentation) == false)
                             */
                            Tuple <int, int> actual_coordinates = new Tuple <int, int>(piece.x, piece.y);
                            Board.PosToPiece.Remove(actual_coordinates);

                            // simply doing this would overwrite an already existing piece with a temporary piece,
                            // thus causing a piece to be lost forever. In short -- killing.
                            // Board.PosToPiece[new Tuple<int, int>(pos.Item1, pos.Item2)] = piece;
                            // so
                            if (authentication.checkPosToPiece(pos.Item1, pos.Item2))
                            {
                                if (piece.check(pos.Item1, pos.Item2))
                                {
                                    original_piece = (Piece)Board.PosToPiece[pos];
                                }
                            }

                            Board.PosToPiece[new Tuple <int, int>(pos.Item1, pos.Item2)] = piece;
                            piece.x = pos.Item1;
                            piece.y = pos.Item2;
                            if (authentication.InCheckLogic() == false)
                            {
                                Board.tempData = "SOMEONE CAN BLOCK.!!";
                                //correct the board representation
                                Board.PosToPiece.Remove(new Tuple <int, int>(piece.x, piece.y));
                                Board.PosToPiece[new Tuple <int, int>(actual_coordinates.Item1, actual_coordinates.Item2)] = piece;
                                piece.x = actual_coordinates.Item1;
                                piece.y = actual_coordinates.Item2;
                                //Console.WriteLine("SOMEONE CAN KILL");
                                if (original_piece != null)
                                {
                                    Board.PosToPiece[pos] = original_piece;
                                }
                                return(false);
                            }
                            //correct the board representation
                            Board.PosToPiece.Remove(new Tuple <int, int>(piece.x, piece.y));
                            Board.PosToPiece[new Tuple <int, int>(actual_coordinates.Item1, actual_coordinates.Item2)] = piece;
                            piece.x = actual_coordinates.Item1;
                            piece.y = actual_coordinates.Item2;
                            if (original_piece != null)
                            {
                                Board.PosToPiece[pos] = original_piece;
                            }
                        }
                    }
                }
            }
            Board.tempData = "CHECKMATE..!!";
            return(true);
        }
Пример #38
0
        public static Tuple <bool, bool> checkDiagonalMovement(Piece piece, int x_new, int y_new)
        {
            if (piece.x == x_new || piece.y == y_new)
            {
                return(new Tuple <bool, bool>(false, false));
            }

            if (Math.Abs(((float)piece.x - (float)x_new) / ((float)piece.y - (float)y_new)) != 1)
            {
                return(new Tuple <bool, bool>(false, false));
            }

            List <Tuple <int, int> > tempLocations = new List <Tuple <int, int> >();

            #region filling tempLocations
            if (x_new > piece.x)
            {
                if (y_new > piece.y)
                {
                    for (int i = 1; i <= x_new - piece.x; i++)
                    {
                        tempLocations.Add(new Tuple <int, int>(piece.x + i, piece.y + i));
                    }
                }
                else
                {
                    for (int i = 1; i <= x_new - piece.x; i++)
                    {
                        tempLocations.Add(new Tuple <int, int>(piece.x + i, piece.y - i));
                    }
                }
            }
            else
            {
                if (y_new > piece.y)
                {
                    for (int i = 1; i <= piece.x - x_new; i++)
                    {
                        tempLocations.Add(new Tuple <int, int>(piece.x - i, piece.y + i));
                    }
                }
                else
                {
                    for (int i = 1; i <= piece.x - x_new; i++)
                    {
                        tempLocations.Add(new Tuple <int, int>(piece.x - i, piece.y - i));
                    }
                }
            }
            #endregion

            bool move_possible = true;
            bool will_kill     = false;
            bool early_break   = false;
            Tuple <bool, bool> answer;
            foreach (var i in tempLocations)
            {
                if (checkPosToPiece(i.Item1, i.Item2))
                {
                    var j = (Piece)Board.PosToPiece[i];

                    if ((j.colour != piece.colour) &&
                        (j.x == x_new) && (j.y == y_new))
                    {
                        will_kill = true;
                    }

                    else if ((j.colour == piece.colour) ||
                             (j.colour != piece.colour && (j.x != x_new || j.y != y_new))
                             )
                    {
                        early_break = true;
                        break;
                    }
                }
            }
            if (early_break)
            {
                answer = new Tuple <bool, bool>(false, false);
            }
            else
            {
                answer = new Tuple <bool, bool>(move_possible, will_kill);
            }
            return(answer);
        }
Пример #39
0
        private bool HasEnemy(Position pos)
        {
            Piece p = Board.Piece(pos);

            return(p != null && p.Color != Color);
        }
Пример #40
0
        public override bool canCapture(Piece target)
        {
            var to = target.Position;

            return(Math.Abs(pos.X - to.X) == 1 && to.Y == pos.Y + 1);
        }