示例#1
0
        public ChessUtils.CheckState CetCheckState()
        {
            ChessUtils.CheckState result          = ChessUtils.CheckState.NoCheck;
            List <Button>         attackedSquares = logic.UpdateAttackedSquares(checkBoard);

            foreach (Button square in attackedSquares)
            {
                if (square.Tag.ToString().Contains("King"))
                {
                    result = ChessUtils.CheckState.Check;
                }
            }

            // Get all available moves for me
            List <Button> allMoves      = logic.GetAllAvailableMovesForColor(logic.myColor, checkBoard);
            int           allMovesCount = allMoves.Count();

            // if we are in check
            if (result == ChessUtils.CheckState.Check)
            {
                // Get all available moves while in check
                List <Button> checkedMoves = logic.GetAllPossibleCheckMovesForColor(logic.myColor, checkBoard);

                // if this number of moves is 0, we have lost the game
                if (checkedMoves.Count() == 0)
                {
                    // Send loss condition, send win condition for opponent
                    return(ChessUtils.CheckState.Checkmate);
                }

                // Otherwise, we are just in check
                else
                {
                    return(ChessUtils.CheckState.Check);
                }
            }

            // if we are not in check, we still need to look for stalemate
            if (allMoves.Count() == 0)
            {
                return(ChessUtils.CheckState.Stalemate);
            }

            // If none of the above happens, there is no check state
            return(ChessUtils.CheckState.NoCheck);
        }
        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);
        }
示例#3
0
        private void gameWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (!gameWorker.CancellationPending)
            {
                if (opponentName == "")
                {
                    byte[] data = new byte[65535];
                    try
                    {
                        stream.Read(data, 0, data.Length);
                    }
                    catch (Exception)
                    {
                        continue;
                    }

                    string    json   = ASCIIEncoding.ASCII.GetString(data).Replace("\0", "");
                    TCPSignal signal = (TCPSignal)JsonConvert.DeserializeObject(json, typeof(TCPSignal));
                    if (signal.SignalType == Signal.StartSession)
                    {
                        // Host
                        Invoke(new Action(() =>
                        {
                            moveLogic.gameStarted = true;
                            //TODO add timers, etc
                            opponentName = enemyUsername.Text = signal.NewSession.GuestPlayer;
                        }));
                    }
                }
                else
                {
                    // TODO: Perform server move logic, timers, etc.

                    // Guest
                    Invoke(new Action(() =>
                    {
                        enemyUsername.Text = opponentName;
                        if (moveLogic.myTurn)
                        {
                            timer.Start();
                        }
                    }));
                    byte[] data = new byte[65535];

                    try
                    {
                        stream.Read(data, 0, data.Length);
                    }
                    catch (Exception)
                    {
                        continue;
                    }
                    string    json   = ASCIIEncoding.ASCII.GetString(data).Replace("\0", "");
                    TCPSignal signal = (TCPSignal)JsonConvert.DeserializeObject(json, typeof(TCPSignal));

                    if (signal.SignalType == Signal.MakeAMove)
                    {
                        Invoke(new Action(() =>
                        {
                            Coordinates destination = signal.PlayerMove.Destination;
                            Piece pieceMoved        = signal.PlayerMove.Source;

                            string pieceTag = pieceMoved.Name;
                            Coordinates c   = pieceMoved.Coordinates;

                            Button originSquare = moveLogic.GetButtonOn(c.X, c.Y, board);
                            Button newSquare    = moveLogic.GetButtonOn(destination.X, destination.Y, board);

                            originSquare.Tag   = "NoPiece";
                            originSquare.Image = null;
                            newSquare.Tag      = pieceTag;
                            newSquare.Image    = ChessUtils.Settings.Image.GetImageForTag(pieceTag);

                            // Update check logic...
                            checkLogic = new CheckLogic(board);
                            ChessUtils.CheckState checkState = checkLogic.CetCheckState();

                            if (checkState == ChessUtils.CheckState.Check)
                            {
                                checkLabel.Show();
                            }

                            else if (checkState == ChessUtils.CheckState.NoCheck)
                            {
                                checkLabel.Hide();
                            }

                            else if (checkState == ChessUtils.CheckState.Checkmate)
                            {
                                // I lost, send the lose signal
                                // They won, send the win signal
                            }

                            else if (checkState == ChessUtils.CheckState.Stalemate)
                            {
                                // We tied, send tie signal for both
                            }

                            // It is now my turn
                            moveLogic.myTurn = !moveLogic.myTurn;

                            if (totalTimeRemaining > originalTimeRemaining)
                            {
                                turnTimeRemaining = originalTimeRemaining;
                            }
                            else
                            {
                                turnTimeRemaining = totalTimeRemaining;
                            }
                            myTimeRemaining.Text      = ChessUtils.ConvertSecondsToTimeString(turnTimeRemaining);
                            myTotalTimeRemaining.Text = ChessUtils.ConvertSecondsToTimeString(totalTimeRemaining);
                        }));
                    }
                }
            }
        }