Exemplo n.º 1
0
        public List <Button> GetPossibleCheckedMovesForPiece(Button button, Button[,] board)
        {
            // Get all possible moves for piece, even if in check
            List <Button> locations = GetPossibleMovesForPiece(button, board);
            Coordinates   c         = ChessUtils.Settings.GetCoordinatesOfButton(button);
            CheckLogic    checkLogic;
            Piece         piece = GetPieceOnSquare(c.X, c.Y, board);

            // Go through and remove moves that would put the king in check
            foreach (Button location in locations)
            {
                // Get coordinates of new location
                Coordinates c2 = ChessUtils.Settings.GetCoordinatesOfButton(button);

                // Copy the board over to my new board...
                Button[,] newBoard = CopyBoard(board);

                Button source      = newBoard[c.X, c.Y];
                Button destination = newBoard[c2.X, c2.Y];

                // Get the check state if I move the piece to the new location
                MovePieceToNewSquare(source, destination, newBoard);

                // Check logic for updated board
                checkLogic = new CheckLogic(newBoard);
                ChessUtils.CheckState checkState = checkLogic.GetCustomGameCheckState();

                // Determine whether or not to keep this location
                if (checkState == ChessUtils.CheckState.Check)
                {
                    locations.Remove(location);
                }
            }

            return(locations);
        }
        private void saveAndClose_Click(object sender, EventArgs e)
        {
            bool isTurnSelected  = false;
            bool isNameEmpty     = true;
            bool hasBothKings    = false;
            bool areKingsInCheck = true;

            /// Verify form has all valid fields and no kings are in check (turn times are automatically taken care of)
            // Check if the custom game has a name
            isNameEmpty = gameNameBox.Text == "";
            if (isNameEmpty)
            {
                ShowErrorMessage("You must enter a name for this game mode.");
                return;
            }

            // Check if the turns are selected
            isTurnSelected = whiteToMove.Checked || blackToMove.Checked;
            if (!isTurnSelected)
            {
                ShowErrorMessage("You must first select which color moves first on game startup.");
                return;
            }

            // Check to make sure there are both black and white kings on the board
            bool hasWhiteKing = false, hasBlackKing = false;

            foreach (Button square in board)
            {
                if ((string)square.Tag == TagStrings[(int)PieceEnum.WhiteKing])
                {
                    hasWhiteKing = true;
                    if (hasBlackKing)
                    {
                        // Both kings found, exit loop
                        break;
                    }
                }
                else if ((string)square.Tag == TagStrings[(int)PieceEnum.BlackKing])
                {
                    hasBlackKing = true;
                    if (hasWhiteKing)
                    {
                        // Both kings found, exit loop
                        break;
                    }
                }
            }
            hasBothKings = hasBlackKing && hasWhiteKing;
            if (!hasBothKings)
            {
                string missingKings = "";
                if (!hasBlackKing)
                {
                    missingKings += "You are missing the black king";
                    // Both kings missing
                    if (!hasWhiteKing)
                    {
                        missingKings += " and the white king";
                    }
                }
                else if (!hasWhiteKing)
                {
                    missingKings += "You are missing the white king";
                }
                missingKings += ".";
                ShowErrorMessage(missingKings);
                return;
            }

            //Verify no kings are in check
            CheckLogic logic = new CheckLogic(board);

            areKingsInCheck = logic.GetCustomGameCheckState() == ChessUtils.CheckState.Check;
            if (areKingsInCheck)
            {
                ShowErrorMessage("One of your kings is in check!");
                return;
            }

            // Add the settings to the database
            List <Piece> pieces = new List <Piece>();

            foreach (Button square in board)
            {
                Piece       piece = new Piece();
                Coordinates c     = ChessUtils.Settings.GetCoordinatesOfButton(square);
                string      name  = square.Tag.ToString();
                piece.Coordinates = c;
                piece.Name        = name;

                pieces.Add(piece);
            }

            int gameTimerSeconds = IsPlayerTotalTime.Checked ?
                                   ((int)totalMins.Value * 60) + ((int)totalSecs.Value) : 0;

            int moveTimerSeconds = IsPlayerTurnTime.Checked ?
                                   ((int)turnMins.Value * 60) + ((int)turnSecs.Value) : 0;

            CustomGame game = new CustomGame
            {
                Username        = me.Username,
                GameTimer       = gameTimerSeconds,
                MoveTimer       = moveTimerSeconds,
                WhiteMovesFirst = whiteToMove.Checked,
                CustomGameName  = gameNameBox.Text,
                Pieces          = pieces
            };

            bool success = DataApiController <CustomGame> .PostData(ChessUtils.IPAddressWithPort + "AddCustomGame", game);

            if (!success)
            {
                ShowErrorMessage("Error while adding custom game to database.");
            }
            else
            {
                Close();
            }
        }