private void ColorAvailableMovments(PictureBox clickedPicture) { TableLayoutPanelCellPosition tableLayoutPanelCellPosition = BoardLayoutPanel.GetPositionFromControl(clickedPicture); Piece selectedPiece = Board.GetPiece(ConverTFromCellPositionToPostion(tableLayoutPanelCellPosition)); if (selectedPiece == null) { MessageBox.Show("this move unavailable for some reason :( ."); return; } if (((selectedPiece.Color == PieceColor.Black) && (CurrentTurn == Turn.White)) || ((selectedPiece.Color == PieceColor.White) && (CurrentTurn == Turn.Black))) { return; } clickedPicture.BackColor = Color.Yellow; foreach (Position item in selectedPiece.AvailabePosisions) { TableLayoutPanelCellPosition cellPosision = ConvertFromPostionToCellPosition(item); PictureBox PicBox = BoardLayoutPanel.GetControlFromPosition(cellPosision.Column, cellPosision.Row) as PictureBox; Piece PieceItem = Board.GetPiece(item); if (PieceItem.Color != PieceColor.Unknown) { PicBox.BackColor = Color.Red; } else { PicBox.BackColor = Color.ForestGreen; } AvailablePiecesToMove.Add(PicBox); } }
private void pictureBox1_Click(object sender, EventArgs e) { PictureBox clickedPicture = sender as PictureBox; if (clickedPicture != null) { // ignor clicking on pieces that refer to empty piece with no color if ((clickedPicture.BackColor == Color.Transparent) && (clickedPicture.Image == null)) { return; } if (firstClicked == null) { ColorAvailableMovments(clickedPicture); if (clickedPicture.BackColor == Color.Transparent) { return; } firstClicked = clickedPicture; return; } // igonr clicking on pieces that not colored with green ! if (clickedPicture.BackColor == Color.ForestGreen) { // detect if user click on same first clicked piece, if yes then he need to cancel this moving if (firstClicked == clickedPicture) { foreach (PictureBox control in AvailablePiecesToMove) { control.BackColor = Color.Transparent; } firstClicked.BackColor = Color.Transparent; firstClicked = null; return; } } if (!(AvailablePiecesToMove.Contains(clickedPicture))) { foreach (PictureBox control in AvailablePiecesToMove) { control.BackColor = Color.Transparent; } firstClicked.BackColor = Color.Transparent; firstClicked = null; return; } else { MovePiece(BoardLayoutPanel.GetPositionFromControl(firstClicked), BoardLayoutPanel.GetPositionFromControl(clickedPicture)); firstClicked = null; } } }