Esempio n. 1
0
        private void updateValidSquaresForSelectedCB(MyCheckBox cb)
        {
            if (cb.Checked)
            {
                //cb.Image = ResizeImage(Properties.Resources.BlackKnight,  cb.Size.Width/2, cb.Size.Height/2);
                cb.BackColor = MyCheckBox.chessBoardSelectedColor;
            }
            else
            {
                cb.BackColor = cb.checkerBoardColor;
            }
            //Disable All Invalid moves
            Game       currGame = ((Game)gamesListBox.SelectedItem);
            Board      board    = currGame.gameBoard;
            BoardSpace boardPos = cb.getLinkedBoardSpace();

            if (boardPos.piece != null && boardPos.piece.color == currGame.color)
            {
                List <Move> validMoves = boardPos.piece.getAvailableMoves(board);

                foreach (Move mv in validMoves)
                {
                    // Highlight valid Moves
                    bool found = false;
                    for (int row = 1; row <= 8; row++)
                    {
                        for (Char col = 'A'; col <= 'H'; col++)
                        {
                            MyCheckBox itCB = chessBoardList[row - 1, col - 'A'];
                            if (itCB.getLinkedBoardSpace().position.Item2 == mv.toSpace.position.Item2 && itCB.getLinkedBoardSpace().position.Item1 == mv.toSpace.position.Item1)
                            {
                                itCB.BackColor = MyCheckBox.chessBoardValidMoveColor;
                                found          = true;
                                break;;
                            }
                            if (found)
                            {
                                break;
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private void boardSqaureCheckChanged(object sender, EventArgs e)
        {
            MyCheckBox cb = (MyCheckBox)sender;

            List <MyCheckBox> selectedSpaces = getSelectedSquareInChessBoard();

            cb.lastTimeSelected = DateTime.Now;

            if (selectedSpaces.Count > 2)
            {
                if (!cb.Checked)
                {
                    cb.Checked = false;
                }
            }
            else
            {
                if (selectedSpaces.Count == 2)
                {
                    String     move      = "";
                    MyCheckBox fromSpace = null;
                    MyCheckBox toSpace   = null;
                    if (DateTime.Compare(selectedSpaces[0].lastTimeSelected, selectedSpaces[1].lastTimeSelected) < 0)
                    {
                        fromSpace = selectedSpaces[0];
                        toSpace   = selectedSpaces[1];
                    }
                    else
                    {
                        fromSpace = selectedSpaces[1];
                        toSpace   = selectedSpaces[0];
                    }

                    if (toSpace.BackColor == MyCheckBox.chessBoardValidMoveColor)
                    {
                        move = $"{fromSpace.Name.ToLower()}{toSpace.Name.ToLower()}";

                        if (!((Game)gamesListBox.SelectedItem).makeMove(move))
                        {
                            //toSpace.Checked = false;

                            if (toSpace.getLinkedBoardSpace().piece != null && toSpace.getLinkedBoardSpace().piece.color == toSpace.game.color)
                            {
                                // Select this piece instead
                                fromSpace.Checked = false;
                            }
                            else
                            {
                                toSpace.Checked = false;
                            }
                        }
                        else
                        {
                            toSpace.Checked   = false;
                            fromSpace.Checked = false;
                        }
                    }
                    else
                    {
                        if (toSpace.getLinkedBoardSpace().piece != null && toSpace.getLinkedBoardSpace().piece.color == toSpace.game.color)
                        {
                            // Select this piece instead
                            resetCheckBoxDefaultColors(visualOnly: true);
                            fromSpace.Checked = false;
                            toSpace.BackColor = MyCheckBox.chessBoardSelectedColor;
                            toSpace.Checked   = true;
                            updateValidSquaresForSelectedCB(toSpace);
                        }
                        else
                        {
                            //MessageBox.Show($"Move '{move}' is Invalid.");
                            toSpace.Checked = false;
                        }
                    }
                }
                else if (selectedSpaces.Count == 1)
                {
                    updateValidSquaresForSelectedCB(cb);
                }
                else
                {
                    // Non selected Reset Colors
                    resetCheckBoxDefaultColors(visualOnly: true);
                }
            }
        }