Exemple #1
0
        void panelClick(object sender, EventArgs e)
        {
            Panel panel = sender as Panel;

            redrawBoard();

            Point panelLocation = findPanel(panel);

            if (panelLocation == null)
            {
                Console.WriteLine("ERROR_EMPTY PANELLOC");
                return;
            }
            //check if square is highlighted and if is, moves selected piece
            if (potentialMoves != null)
            {
                if (potentialMoves.Contains(panelLocation))
                {
                    movePiece(panelLocation);
                    potentialMoves.Clear();
                }
            }

            //check if has unselected piece
            ChessPiece chessPiece = findChessPiece(panelLocation);

            if (chessPiece == null)
            {
                Console.WriteLine("Empty panel");
                selectedPiece = null;
                if (potentialMoves != null)
                {
                    potentialMoves.Clear();
                }
                return;
            }
            selectedPiece = chessPiece;

            //colors in selected panel and calculates potential moves
            if (chessPiece.getColor() == this.whiteTurn)
            {
                if (panelLocation.Y % 2 == 0)
                {
                    chessPanels[panelLocation.X, panelLocation.Y].BackColor = panelLocation.X % 2 != 0 ? Color.Blue : Color.LightBlue;
                }
                else
                {
                    chessPanels[panelLocation.X, panelLocation.Y].BackColor = panelLocation.X % 2 != 0 ? Color.LightBlue : Color.Blue;
                }
                potentialMoves = chessPiece.CalculateMoves(chessPieces);
            }
            else
            { //clears potential moves if wrong color is selected
                if (potentialMoves != null)
                {
                    potentialMoves.Clear();
                }
            }
            // colors in potential panels
            if (potentialMoves != null)
            {
                foreach (Point point in potentialMoves)
                {
                    Console.WriteLine(point);
                    if (point.Y % 2 == 0)
                    {
                        chessPanels[point.X, point.Y].BackColor = point.X % 2 != 0 ? Color.Blue : Color.LightBlue;
                    }
                    else
                    {
                        chessPanels[point.X, point.Y].BackColor = point.X % 2 != 0 ? Color.LightBlue : Color.Blue;
                    }
                }
            }
        }