Esempio n. 1
0
        public override List <ChessPoint> getPossibleMoves(List <ChessPiece> allPieces)
        {
            List <ChessPoint> result = new List <ChessPoint>();

            for (int i = position.X - 1; i <= position.X + 1; i++)
            {
                for (int j = position.Y - 1; j <= position.Y + 1; j++)
                {
                    if (i >= 0 && j >= 0 && i < 8 && j < 8 && !(i == position.X && j == position.Y))
                    {
                        ChessPoint move = new ChessPoint(i, j);
                        result.Add(move);
                    }
                }
            }
            List <ChessPoint> invalidMoves = new List <ChessPoint>();

            foreach (ChessPiece cp in allPieces)
            {
                foreach (ChessPoint possibleMove in result)
                {
                    if (cp.position.X == possibleMove.X && cp.position.Y == possibleMove.Y)
                    {
                        invalidMoves.Add(possibleMove);
                    }
                }
            }
            foreach (ChessPoint invalidMove in invalidMoves)
            {
                result.Remove(invalidMove);
            }
            return(result);
        }
Esempio n. 2
0
        public override List <ChessPoint> getPossibleHits(List <ChessPiece> allPieces)
        {
            List <ChessPoint> allMoves = new List <ChessPoint>();

            for (int i = position.X - 1; i <= position.X + 1; i++)
            {
                for (int j = position.Y - 1; j <= position.Y + 1; j++)
                {
                    if (i >= 0 && j >= 0 && i < 8 && j < 8 && !(i == position.X && j == position.Y))
                    {
                        ChessPoint move = new ChessPoint(i, j);
                        allMoves.Add(move);
                    }
                }
            }
            List <ChessPoint> result = new List <ChessPoint>();

            foreach (ChessPiece cp in allPieces)
            {
                if (isBlack != cp.isBlack && allMoves.Contains(cp.position))
                {
                    result.Add(cp.position);
                }
            }
            return(result);
        }
Esempio n. 3
0
        public override List <ChessPoint> getPossibleMoves(List <ChessPiece> allPieces)
        {
            List <ChessPoint> result = new List <ChessPoint>();

            if (isBlack)
            {
                ChessPoint move1 = new ChessPoint(position.X, position.Y + 1);
                result.Add(move1);
                if (isFrist)
                {
                    ChessPoint move2 = new ChessPoint(position.X, position.Y + 2);
                    result.Add(move2);
                }
            }
            else
            {
                ChessPoint move1 = new ChessPoint(position.X, position.Y - 1);
                result.Add(move1);
                if (isFrist)
                {
                    ChessPoint move2 = new ChessPoint(position.X, position.Y - 2);
                    result.Add(move2);
                }
            }
            List <ChessPoint> invalidMoves = new List <ChessPoint>();

            foreach (ChessPiece cp in allPieces)
            {
                foreach (ChessPoint possibleMove in result)
                {
                    if (cp.position.X == possibleMove.X && cp.position.Y == possibleMove.Y)
                    {
                        invalidMoves.Add(possibleMove);
                    }
                }
            }
            foreach (ChessPoint invalidMove in invalidMoves)
            {
                result.Remove(invalidMove);
            }
            return(result);
        }
Esempio n. 4
0
        private void CellClicked(MouseButtonEventArgs m)
        {
            int tmpX = (int)(m.GetPosition(this).X);
            int tmpY = (int)(m.GetPosition(this).Y);
            int xm   = (int)(tmpY / figureHeight);
            int ym   = (int)(tmpX / figureWidth);

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if ((i + j) % 2 == 0)
                    {
                        borderControls[i, j].Background = new SolidColorBrush(Color.FromRgb(100, 100, 100));
                    }
                    else
                    {
                        borderControls[i, j].Background = new SolidColorBrush(Color.FromRgb(220, 220, 220));
                    }
                }
            }

            if (avaliableMoves != null)
            {
                foreach (ChessPoint cpt in avaliableMoves)
                {
                    if (ym == cpt.X && xm == cpt.Y)
                    {
                        selectedPiece.position = cpt;
                        DrawChessBoard();
                        isBlackMove = !isBlackMove;
                        break;
                    }
                }
            }

            if (avaliableHits != null)
            {
                foreach (ChessPoint cpt in avaliableHits)
                {
                    if (ym == cpt.X && xm == cpt.Y)
                    {
                        ChessPiece pieceToRemove = null;
                        foreach (ChessPiece cpth in chessPieces)
                        {
                            if (cpth.position.X == cpt.X && cpth.position.Y == cpt.Y)
                            {
                                pieceToRemove = cpth;
                            }
                        }
                        chessPieces.Remove(pieceToRemove);
                        selectedPiece.position = cpt;
                        DrawChessBoard();
                        isBlackMove = !isBlackMove;
                        break;
                    }
                }
            }

            bool isValid = false;

            selectedPiece  = null;
            avaliableMoves = null;
            avaliableHits  = null;
            foreach (ChessPiece cp in chessPieces)
            {
                if (ym == cp.position.X && xm == cp.position.Y && cp.isBlack == isBlackMove)
                {
                    isValid       = true;
                    selectedPiece = cp;
                }
            }

            if (isValid)
            {
                selectedPoint = new ChessPoint(ym, xm);
                Console.WriteLine("Selected : " + selectedPiece.position.X + " , " + selectedPiece.position.Y);
                borderControls[xm, ym].Background = Brushes.LimeGreen;

                avaliableMoves = selectedPiece.getPossibleMoves(chessPieces);
                foreach (ChessPoint cpt in avaliableMoves)
                {
                    borderControls[cpt.Y, cpt.X].Background = Brushes.SkyBlue;
                }

                avaliableHits = selectedPiece.getPossibleHits(chessPieces);
                Console.WriteLine("Hits : " + avaliableHits.Count);
                foreach (ChessPoint cpt in avaliableHits)
                {
                    borderControls[cpt.Y, cpt.X].Background = Brushes.Red;
                }
            }
        }
Esempio n. 5
0
 public ChessPiece(int x, int y, bool isBlack)
 {
     position     = new ChessPoint(x, y);
     isFrist      = true;
     this.isBlack = isBlack;
 }