示例#1
0
        private void CellClicked(object sender, EventArgs e)
        {
            MouseButtons button = ((MouseEventArgs)e).Button;

            // translate window coordinates to table-cell coordinates
            Point click = tableLayoutPanel1.PointToClient(MousePosition);
            int windowX = click.X;
            int windowY = click.Y;

            // TODO: Make grid accurate by making up for the difference the coordinate system makes.
            int cellX = windowX / ((tableLayoutPanel1.Width - CoordinateSystemPixelSize) / chessboard.Width);
            int cellY = windowY / ((tableLayoutPanel1.Height - CoordinateSystemPixelSize) / chessboard.Height);

            Coordinate clickTarget = new Coordinate(cellX, cellY);

            // handle click
            switch (button)
            {
                // Deselects all squares marked, (de)selects a piece, or makes a move with a selected piece.
                case MouseButtons.Left:
                    ResetAllTableStyling();

                    Piece piece = chessboard[clickTarget];

                    // deselect piece selected
                    if (fromPosition is not null && piece?.Color == chessboard.CurrentTeamTurn && piece != selectedPiece)
                    {
                        DeselectPiece(fromPosition.Value.File, fromPosition.Value.Rank);
                        UpdateBoard();
                        fromPosition = null;
                    }

                    if (fromPosition is null)
                    {
                        // return if piece is null or has wrong color.
                        if (piece?.Color != chessboard.CurrentTeamTurn)
                        {
                            return;
                        }

                        // only allow selection of local players
                        if (chessboard.CurrentTeamTurn == TeamColor.Black && !blackLocal ||
                            chessboard.CurrentTeamTurn == TeamColor.White && !whiteLocal)
                        {
                            return;
                        }

                        fromPosition = clickTarget;
                        SelectPiece(cellX, cellY);
                        selectedPiece = piece;
                        
                        foreach (var move in piece.GetMoves(chessboard))
                        {
                            if (move.Moves[0].Destination is null)
                            {
                                continue;
                            }

                            Coordinate guardedSquare = move.Moves[0].Destination.Value;

                            // TODO: Patrick fixer brættet, cirka her omkring
                            TilePictureControl cellImageControl = GetCell(guardedSquare.File, guardedSquare.Rank);
                            Image cellImage = cellImageControl.Image;
                            Color backgroundColor = cellImageControl.BackColor;

                            bool isMoveValid = gamemode.ValidateMove(move, chessboard);

                            if (move.Captures)
                            {
                                backgroundColor = isMoveValid ? 
                                    CaptureTileAvailableColor : 
                                    CaptureTileUnavailableColor;
                            }
                            else
                            {
                                cellImage = isMoveValid ? 
                                    Properties.Resources.MuligtTrkBrikTilgængelig :
                                    Properties.Resources.MuligtTrkBrikUtilgængelig;
                            }

                            cellImageControl.Image = cellImage;
                            cellImageControl.BackColor = backgroundColor;
                        }
                    }
                    else
                    {
                        // select target
                        if (clickTarget != fromPosition)
                        {
                            MakeLocalMove(fromPosition.Value, clickTarget);
                        }

                        UpdateBoard();

                        DeselectPiece(cellX, cellY);
                        selectedPiece = null;
                        fromPosition = null;
                    }
                    break;
                case MouseButtons.None:
                    break;
                    // Mark square.
                case MouseButtons.Right:
                    // do not change color if square is already colored.
                    if (recentMoveFrom?.File == cellX && recentMoveFrom?.Rank == cellY || 
                        recentMoveTo?.File == cellX && recentMoveTo?.Rank == cellY)
                    {
                        break;
                    }

                    if (GetCell(cellX, cellY).BackColor == MarkedSquareColor)
                    {
                        ResetTileColor(cellX, cellY);
                    }
                    else
                    {
                        GetCell(cellX, cellY).BackColor = MarkedSquareColor;
                    }
                    break;
                case MouseButtons.Middle:
                    break;
                case MouseButtons.XButton1:
                    break;
                case MouseButtons.XButton2:
                    break;
                default:
                    break;
            }
        }
示例#2
0
        /// <summary>
        /// Instantiates UI controls, creates the visual representation of the chessboard.
        /// </summary>
        public void InstantiateUIBoard()
        {
            tableLayoutPanel1.ColumnCount = chessboard.Width + 1;
            tableLayoutPanel1.ColumnStyles.Clear();
            int i;
            for (i = 0; i < chessboard.Width; i++)
            {
                // set size to any percent, doesnt matter
                tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 1));
            }

            tableLayoutPanel1.RowCount = chessboard.Height + 1;
            tableLayoutPanel1.RowStyles.Clear();
            for (i = 0; i < chessboard.Height; i++)
            {
                tableLayoutPanel1.RowStyles.Add(new ColumnStyle(SizeType.Percent, 1));
            }

            // Coordinate row and column
            tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, CoordinateSystemPixelSize));
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, CoordinateSystemPixelSize));

            boardcells = new TilePictureControl[chessboard.Width, chessboard.Height];

            for (int y = 0; y < chessboard.Height; y++)
            {
                for (int x = 0; x < chessboard.Width; x++)
                {
                    TilePictureControl box = new TilePictureControl();

                    box.Click += CellClicked;

                    boardcells[x, y] = box;

                    tableLayoutPanel1.Controls.Add(box, x, y);
                }
            }

            Font labelFont = new Font("ariel", 15, FontStyle.Bold);
            // Instantiate coordinates
            for (int x = 0; x < tableLayoutPanel1.ColumnCount - 1; x++)
            {
                Label label = new Label
                {
                    Text = ((char)(65 + x)).ToString(),
                    TextAlign = ContentAlignment.MiddleCenter,
                    Dock = DockStyle.Fill,
                    Font = labelFont
                };

                tableLayoutPanel1.Controls.Add(label, x, tableLayoutPanel1.RowCount - 1);
            }
            for (int y = 0; y < tableLayoutPanel1.RowCount - 1; y++)
            {
                Label label = new Label
                {
                    Text =  (y + 1).ToString(),
                    TextAlign = ContentAlignment.MiddleCenter,
                    Dock = DockStyle.Fill,
                    Font = labelFont
                };

                tableLayoutPanel1.Controls.Add(label, tableLayoutPanel1.ColumnCount - 1, y);
            }

            ResetAllTableStyling();
        }