示例#1
0
        private void ChessBoardTile_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(ChessPiece)))
            {
                ChessPiece     currentPiece = (ChessPiece)e.Data.GetData(typeof(ChessPiece));
                ChessBoardTile cbtCurrent   = currentPiece.parentTile;
                bool           contains     = currentPiece.movableTiles.Contains(this);
                if (contains)
                {
                    currentPiece.previousKey = currentPiece.parentTile.Key;

                    Cursor                  = Cursors.Hand;
                    cbtCurrent.Piece        = null;
                    currentPiece.parentTile = this;
                    Piece = currentPiece;
                    currentPiece.moveCount++;
                    var player = new System.Media.SoundPlayer("../../effects/sound.wav");
                    player.Play();
                    mainBoard.movementDone(Piece.movementString);
                    currentPiece.parentTile.mainBoard.turn = currentPiece.Color == PieceColor.Black ? PieceColor.White : PieceColor.Black;
                    mainBoard.form.client.Movement(currentPiece);
                    mainBoard.form.getMovements(currentPiece.movementString);
                    mainBoard.form.updateTurn();
                }
                cbtCurrent.BackColor = cbtCurrent.mainColor;
                foreach (ChessBoardTile tile in currentPiece.movableTiles)
                {
                    tile.BackColor = tile.mainColor;
                }
                foreach (ChessBoardTile tile in currentPiece.capturableTiles)
                {
                    tile.BackColor = tile.mainColor;
                }
            }
        }
 private void lookForCapturing(ChessBoardTile cbt)
 {
     if (cbt.Piece != null && cbt.Piece.Color != Color)
     {
         cbt.BackColor = ChessBoardTile.boardCurrent;
         capturableTiles.Add(cbt);
     }
 }
 private bool lookForMovement(ChessBoardTile cbt)
 {
     if (cbt.Piece != null)
     {
         if (Type != PieceType.Pawn)
         {
             lookForCapturing(cbt);
         }
         return(false);
     }
     else
     {
         cbt.BackColor = ChessBoardTile.boardAvailable;
         movableTiles.Add(cbt);
         return(true);
     }
 }
示例#4
0
        public void opponentsMovement(ChessPieceServer piece)
        {
            string previousKey = null;
            string currentKey  = null;

            cb.turn    = cb.turn == ChessPiece.PieceColor.Black ? ChessPiece.PieceColor.White : ChessPiece.PieceColor.Black;
            currentKey = piece.key;
            ChessPiece found = null;

            foreach (var item in cb.boardTiles)
            {
                ChessPiece currentPiece = item.Value.Piece;
                if (currentPiece != null)
                {
                    if (currentPiece.Type == piece.Type && currentPiece.Color == piece.Color && item.Key == piece.previousKey)
                    {
                        found = currentPiece;
                    }
                }
            }
            bool pnlEnable = true;

            if (cb.boardTiles[currentKey].Piece != null)
            {
                if (cb.boardTiles[currentKey].Piece.Type == ChessPiece.PieceType.King)
                {
                    pnlEnable = false;
                }
            }
            cb.boardTiles[currentKey].Piece = found;
            ChessBoardTile cbt = cb.boardTiles[piece.previousKey];

            found.parentTile    = cb.boardTiles[currentKey];
            cbt.Piece           = null;
            cb.pnlBoard.Enabled = pnlEnable;
            getMovements(found.movementString);
            var player = new System.Media.SoundPlayer("../../effects/sound.wav");

            player.Play();
            updateTurn();
        }
示例#5
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;
            }
        }
示例#6
0
 protected bool Equals(ChessBoardTile other)
 {
     return(Key == other.Key);
 }