Beispiel #1
0
        private void createBoard()
        {
            int x, y;

            x = 0;
            y = 0;
            UserViewModel blackPlayer = form.session.players[PieceColor.Black];
            UserViewModel whitePlayer = form.session.players[PieceColor.White];

            if (blackPlayer.UserID == form.client.CurrentUser.UserID)
            {
                currentColor = PieceColor.Black;
            }
            else
            {
                currentColor = PieceColor.White;
            }
            if (currentColor == PieceColor.Black)
            {
                for (int i = 0; i < BlackKeyNumber.Length; i++)
                {
                    Label lbl = new Label
                    {
                        Text      = BlackKeyNumber[i].ToString(),
                        Size      = new Size(20, 60),
                        TextAlign = ContentAlignment.MiddleCenter,
                        Location  = new Point(x, y),
                        BackColor = Color.FromArgb(49, 46, 43),
                        ForeColor = Color.FromArgb(151, 150, 148)
                    };
                    lbl.Font = new Font(lbl.Font, FontStyle.Bold);
                    pnlBoard.Controls.Add(lbl);
                    y += 59;
                }
                x = 20;
                for (int i = 0; i < BlackKeyLetter.Length; i++)
                {
                    Label lbl = new Label
                    {
                        Text      = BlackKeyLetter[i].ToString(),
                        Size      = new Size(60, 20),
                        TextAlign = ContentAlignment.MiddleCenter,
                        Location  = new Point(x, y),
                        BackColor = Color.FromArgb(49, 46, 43),
                        ForeColor = Color.FromArgb(151, 150, 148)
                    };
                    lbl.Font = new Font(lbl.Font, FontStyle.Bold);
                    pnlBoard.Controls.Add(lbl);
                    x += 59;
                }
            }
            else
            {
                for (int i = 0; i < WhiteKeyNumber.Length; i++)
                {
                    Label lbl = new Label
                    {
                        Text      = WhiteKeyNumber[i].ToString(),
                        Size      = new Size(20, 60),
                        TextAlign = ContentAlignment.MiddleCenter,
                        Location  = new Point(x, y),
                        BackColor = Color.FromArgb(49, 46, 43),
                        ForeColor = Color.FromArgb(151, 150, 148)
                    };
                    lbl.Font = new Font(lbl.Font, FontStyle.Bold);
                    pnlBoard.Controls.Add(lbl);
                    y += 59;
                }
                x = 20;
                for (int i = 0; i < WhiteKeyLetter.Length; i++)
                {
                    Label lbl = new Label
                    {
                        Text      = WhiteKeyLetter[i].ToString(),
                        Size      = new Size(60, 20),
                        TextAlign = ContentAlignment.MiddleCenter,
                        Location  = new Point(x, y),
                        BackColor = Color.FromArgb(49, 46, 43),
                        ForeColor = Color.FromArgb(151, 150, 148)
                    };
                    lbl.Font = new Font(lbl.Font, FontStyle.Bold);
                    pnlBoard.Controls.Add(lbl);
                    x += 59;
                }
            }
            y = 0;
            for (int i = 0; i < 8; i++)
            {
                x = 20;
                for (int j = 0; j < 8; j++)
                {
                    Color tileColor = (j + i) % 2 != 0
                        ? ChessBoardTile.boardDark : ChessBoardTile.boardLight;
                    string key = currentColor == PieceColor.Black ?
                                 BlackKeyLetter[j].ToString() + BlackKeyNumber[i].ToString()
                        : WhiteKeyLetter[j].ToString() + WhiteKeyNumber[i].ToString();
                    ChessBoardTile chessBoardTile = new ChessBoardTile(x, y, tileColor, key.ToUpper());
                    chessBoardTile.mainBoard = this;
                    if (i == 0 || i == 1 || i == 6 || i == 7)
                    {
                        PieceColor pieceColor;
                        if (currentColor == PieceColor.Black)
                        {
                            pieceColor = (i == 6 || i == 7) ? PieceColor.Black : PieceColor.White;
                        }
                        else
                        {
                            pieceColor = (i == 0 || i == 1) ? PieceColor.Black : PieceColor.White;
                        }
                        PieceType pieceType;
                        if (currentColor == PieceColor.White)
                        {
                            pieceType = (i == 1 || i == 6) ? PieceType.Pawn :
                                        (j <= 4) ? (PieceType)j : (PieceType)(7 - j);
                        }
                        else
                        {
                            pieceType = (i == 1 || i == 6) ? PieceType.Pawn :
                                        (j <= 4) ? j < 3 ? (PieceType)j : j == 3 ? PieceType.King : PieceType.Queen
                                : (PieceType)(7 - j);
                        }
                        ChessPiece chessPiece = new ChessPiece(pieceColor, pieceType);
                        chessPiece.parentTile = chessBoardTile;
                        chessBoardTile.Piece  = chessPiece;
                    }
                    pnlBoard.Controls.Add(chessBoardTile);
                    boardTiles.Add(key, chessBoardTile);
                    x += 59;
                }
                y += 59;
            }
        }
Beispiel #2
0
        public void AuthorativeMove(Packet ping)
        {
            timerUnlockButtons.Stop();
            var        from    = ping.Content["from"].ToObject <string>();
            var        fromBtn = Board.GetButtonAt(from);
            var        to      = ping.Content["to"].ToObject <string>();
            var        toBtn   = Board.GetButtonAt(to);
            ChessPiece taken   = toBtn.PieceHere;

            if (toBtn.PieceHere != null)
            {
                toBtn.PieceHere.Location = null;
            }
            toBtn.PieceHere = fromBtn.PieceHere;
            if (fromBtn.PieceHere != null)
            {
                fromBtn.PieceHere.Location = toBtn;
            }
            fromBtn.PieceHere        = null;
            toBtn.PieceHere.HasMoved = true;
            if (Main.Game.LastMoves.TryGetValue(toBtn.PieceHere.Opponent, out var mv) && mv != null)
            {
                foreach (var btn in new ChessButton[] { mv.From, mv.To })
                {
                    btn.FlatAppearance.BorderColor = btn.getDefaultColor();
                }
            }
            if (Main.Game.LastMoves.TryGetValue(toBtn.PieceHere.Owner, out mv) && mv != null)
            {
                foreach (var btn in new ChessButton[] { mv.From, mv.To })
                {
                    btn.FlatAppearance.BorderColor = btn.getDefaultColor();
                }
            }
            foreach (var btn in new ChessButton[] { fromBtn, toBtn })
            {
                btn.FlatAppearance.BorderColor = Color.Red;
            }
            Main.Game.LastMoves[toBtn.PieceHere.Owner] = new Move()
            {
                From  = fromBtn,
                To    = toBtn,
                Piece = toBtn.PieceHere,
                Taken = taken
            };
            if (ping.Content.TryGetValue("remove", out var token))
            {
                var ids = token.ToObject <int[]>();
                foreach (var id in ids)
                {
                    var thing = Board.GetPiece(id);
                    if (thing.Location != null)
                    {
                        thing.Location.PieceHere = null;
                    }
                    thing.Location = null;
                }
            }
            if (ping.Content.TryGetValue("castle", out token))
            {
                var cFrom = Board.GetButtonAt(token["from"].ToObject <string>());
                var cTo   = Board.GetButtonAt(token["to"].ToObject <string>());
                var piece = cFrom.PieceHere;
                cFrom.PieceHere = null;
                cTo.PieceHere   = piece;
                piece.Location  = cTo;
            }
            if (ping.Content.TryGetValue("promote", out token))
            {
                var type = token.ToObject <PieceType>();
                toBtn.PieceHere.Type = type;
            }
            Main.Game.Waiting = (PlayerSide)((int)Main.Game.Waiting ^ 0b11);
            Board.Evaluate();
        }