Ejemplo n.º 1
0
        public bool sendGameState(CurrentGameState state)
        {
            DataContractJsonSerializer js = new DataContractJsonSerializer(typeof(CurrentGameState));
            MemoryStream stream = new MemoryStream();
            js.WriteObject(stream, state);
            stream.Position = 0;
            StreamReader reader = new StreamReader(stream);
            string json = reader.ReadToEnd();

            makeHttpRequest("PUT", json);

            return true;
        }
Ejemplo n.º 2
0
        public bool checkmate(ChessPiece.Color opponentColor)
        {
            // Assume inCheck() has been called,
            // therefore all piece moves have been calculated

            //Cache current state
            mCurrentState = toCurrentGameState();
            ChessPiece kingPiece = chessPieces[(opponentColor == ChessPiece.Color.BLACK ? "black" : "white") + "_king"];
            Vector2 kingPos = kingPiece.getPosition();

            // If king has a move that is not CAN_TAKE or CAN_MOVE
            // Check if any of kings moves do not result in check
            int[] cardinalDir = new int[16]{-1,1,  0,1,  1,1,
                                            -1,0,        1,0,
                                            -1,-1, 0,-1, 1,-1 };

            for (int i = 0; i < 8; ++i)
            {
                //System.Diagnostics.Debug.WriteLine("Check # " + i);

                Vector2 potentialMove = kingPos + new Vector2(cardinalDir[2 * i], cardinalDir[2 * i + 1]);
                //System.Diagnostics.Debug.WriteLine(potentialMove);
                try
                {

                    //setSelected(potentialMove);
                    kingPiece.makeMove(potentialMove, chessPieces);
                    if (!inCheck(opponentColor))
                    {
                        // Valid move out of check exists
                        //System.Diagnostics.Debug.WriteLine("CheckMate Check - Valid Move :)");
                        resetTurn();
                        return false;
                    }

                }
                catch (Exception ex)
                {
                    // Not valid move
                    //System.Diagnostics.Debug.WriteLine("CheckMate Check - Exception");
                }
                // Reset and try new move
                resetTurn();
                //System.Diagnostics.Debug.WriteLine("CheckMate Check - Invalid Move");
                //System.Diagnostics.Debug.WriteLine("");
            }
            // All moves have been checked, king is stuck.

            inCheck(opponentColor);
            if (checkingPieces.Count > 1) {
                // If checked by two pieces & can't move, checkmate
                return true;
            }

            // But can he be saved without being exposed to new attack?

            ChessPiece checkingPiece = checkingPieces[0];
            int x = (int)checkingPiece.getPosition().X,
                y = (int)checkingPiece.getPosition().Y,
                xDelta = x - (int)kingPos.X,
                yDelta = y - (int)kingPos.Y,
                dist = (xDelta == 0 ? Math.Abs(yDelta) : Math.Abs(xDelta));
            xDelta /= Math.Abs(xDelta);
            yDelta /= Math.Abs(yDelta);

            foreach (KeyValuePair<string, ChessPiece> entry in chessPieces)
            {
                // Only check pieces in play AND opponents color AND not the king
                if ( (!entry.Value.isTaken()) &&
                     (entry.Value.getPlayer() == opponentColor) &&
                     (entry.Value.getType() == ChessPiece.Piece.KING) )
                {
                    ChessBoard.BoardSquare[,] moves = entry.Value.getMoves();
                    // Check if piece can be taken or blocked if sliding

                    if (moves[x, y] == ChessBoard.BoardSquare.CAN_TAKE)
                    {
                        // Checking piece can be taken
                        entry.Value.makeMove(new Vector2(x, y), chessPieces);
                        if (!inCheck(opponentColor))
                        {
                            // Move does not result in another check
                            //System.Diagnostics.Debug.WriteLine("CheckMate Check - Potential Taking Piece");
                            //System.Diagnostics.Debug.WriteLine("CheckMate Check - " + entry.Value.getPosition());

                            resetTurn();
                            return false;
                        }
                        else
                        {
                            // Move results in check
                            resetTurn();
                        }

                    }

                    if (checkingPieces[0].getType() != ChessPiece.Piece.PAWN &&
                        checkingPieces[0].getType() != ChessPiece.Piece.KNIGHT)
                    {
                        // Check spots between king and checking piece

                        for (int i = 1; i < dist; ++i)
                        {
                            int blockX = (int)kingPos.X + i * xDelta,
                                blockY = (int)kingPos.Y + i * yDelta;

                            if (moves[blockX, blockY] == ChessBoard.BoardSquare.CAN_MOVE)
                            {
                                // Checking piece can be blocked
                                entry.Value.makeMove(new Vector2(blockX, blockY), chessPieces);
                                if (!inCheck(opponentColor))
                                {
                                    // Move does not result in another check
                                    //System.Diagnostics.Debug.WriteLine("CheckMate Check - Potential Blocking Piece");
                                    //System.Diagnostics.Debug.WriteLine("CheckMate Check - " + entry.Value.getPosition());

                                    resetTurn();
                                    return false;
                                }
                                else
                                {
                                    // Move results in check
                                    resetTurn();
                                }
                            }
                            // Else cannot block checking piece
                        }
                    }
                    // Else piece is not a blockable piece
                }
            }

            // Otherwise, in checkmate
            return true;
        }
Ejemplo n.º 3
0
        public void loadState(CurrentGameState state)
        {
            if (state != null)
            {
                mCurrentState = state;

                chessPieces["black_pawn1"].setPosition(new Vector2((float)state.black.pawn1.x, (float)state.black.pawn1.y));
                chessPieces["black_pawn2"].setPosition(new Vector2((float)state.black.pawn2.x, (float)state.black.pawn2.y));
                chessPieces["black_pawn3"].setPosition(new Vector2((float)state.black.pawn3.x, (float)state.black.pawn3.y));
                chessPieces["black_pawn4"].setPosition(new Vector2((float)state.black.pawn4.x, (float)state.black.pawn4.y));
                chessPieces["black_pawn5"].setPosition(new Vector2((float)state.black.pawn5.x, (float)state.black.pawn5.y));
                chessPieces["black_pawn6"].setPosition(new Vector2((float)state.black.pawn6.x, (float)state.black.pawn6.y));
                chessPieces["black_pawn7"].setPosition(new Vector2((float)state.black.pawn7.x, (float)state.black.pawn7.y));
                chessPieces["black_pawn8"].setPosition(new Vector2((float)state.black.pawn8.x, (float)state.black.pawn8.y));
                chessPieces["black_rook1"].setPosition(new Vector2((float)state.black.rook1.x, (float)state.black.rook1.y));
                chessPieces["black_rook2"].setPosition(new Vector2((float)state.black.rook2.x, (float)state.black.rook2.y));
                chessPieces["black_bishop1"].setPosition(new Vector2((float)state.black.bishop1.x, (float)state.black.bishop1.y));
                chessPieces["black_bishop2"].setPosition(new Vector2((float)state.black.bishop2.x, (float)state.black.bishop2.y));
                chessPieces["black_knight1"].setPosition(new Vector2((float)state.black.knight1.x, (float)state.black.knight1.y));
                chessPieces["black_knight2"].setPosition(new Vector2((float)state.black.knight2.x, (float)state.black.knight2.y));
                chessPieces["black_queen"].setPosition(new Vector2((float)state.black.queen.x, (float)state.black.queen.y));
                chessPieces["black_king"].setPosition(new Vector2((float)state.black.king.x, (float)state.black.king.y));

                chessPieces["black_pawn1"].setMasqueradesAs(state.black.pawn1.masquerading_as);
                chessPieces["black_pawn2"].setMasqueradesAs(state.black.pawn2.masquerading_as);
                chessPieces["black_pawn3"].setMasqueradesAs(state.black.pawn3.masquerading_as);
                chessPieces["black_pawn4"].setMasqueradesAs(state.black.pawn4.masquerading_as);
                chessPieces["black_pawn5"].setMasqueradesAs(state.black.pawn5.masquerading_as);
                chessPieces["black_pawn6"].setMasqueradesAs(state.black.pawn6.masquerading_as);
                chessPieces["black_pawn7"].setMasqueradesAs(state.black.pawn7.masquerading_as);
                chessPieces["black_pawn8"].setMasqueradesAs(state.black.pawn8.masquerading_as);
                chessPieces["black_rook1"].setMasqueradesAs(state.black.rook1.masquerading_as);
                chessPieces["black_rook2"].setMasqueradesAs(state.black.rook2.masquerading_as);
                chessPieces["black_bishop1"].setMasqueradesAs(state.black.bishop1.masquerading_as);
                chessPieces["black_bishop2"].setMasqueradesAs(state.black.bishop2.masquerading_as);
                chessPieces["black_knight1"].setMasqueradesAs(state.black.knight1.masquerading_as);
                chessPieces["black_knight2"].setMasqueradesAs(state.black.knight2.masquerading_as);
                chessPieces["black_queen"].setMasqueradesAs(state.black.queen.masquerading_as);
                chessPieces["black_king"].setMasqueradesAs(state.black.king.masquerading_as);

                chessPieces["white_pawn1"].setPosition(new Vector2((float)state.white.pawn1.x, (float)state.white.pawn1.y));
                chessPieces["white_pawn2"].setPosition(new Vector2((float)state.white.pawn2.x, (float)state.white.pawn2.y));
                chessPieces["white_pawn2"].setPosition(new Vector2((float)state.white.pawn2.x, (float)state.white.pawn2.y));
                chessPieces["white_pawn3"].setPosition(new Vector2((float)state.white.pawn3.x, (float)state.white.pawn3.y));
                chessPieces["white_pawn4"].setPosition(new Vector2((float)state.white.pawn4.x, (float)state.white.pawn4.y));
                chessPieces["white_pawn5"].setPosition(new Vector2((float)state.white.pawn5.x, (float)state.white.pawn5.y));
                chessPieces["white_pawn6"].setPosition(new Vector2((float)state.white.pawn6.x, (float)state.white.pawn6.y));
                chessPieces["white_pawn7"].setPosition(new Vector2((float)state.white.pawn7.x, (float)state.white.pawn7.y));
                chessPieces["white_pawn8"].setPosition(new Vector2((float)state.white.pawn8.x, (float)state.white.pawn8.y));
                chessPieces["white_rook1"].setPosition(new Vector2((float)state.white.rook1.x, (float)state.white.rook1.y));
                chessPieces["white_rook2"].setPosition(new Vector2((float)state.white.rook2.x, (float)state.white.rook2.y));
                chessPieces["white_bishop1"].setPosition(new Vector2((float)state.white.bishop1.x, (float)state.white.bishop1.y));
                chessPieces["white_bishop2"].setPosition(new Vector2((float)state.white.bishop2.x, (float)state.white.bishop2.y));
                chessPieces["white_knight1"].setPosition(new Vector2((float)state.white.knight1.x, (float)state.white.knight1.y));
                chessPieces["white_knight2"].setPosition(new Vector2((float)state.white.knight2.x, (float)state.white.knight2.y));
                chessPieces["white_queen"].setPosition(new Vector2((float)state.white.queen.x, (float)state.white.queen.y));
                chessPieces["white_king"].setPosition(new Vector2((float)state.white.king.x, (float)state.white.king.y));

                chessPieces["white_pawn1"].setMasqueradesAs(state.white.pawn1.masquerading_as);
                chessPieces["white_pawn2"].setMasqueradesAs(state.white.pawn2.masquerading_as);
                chessPieces["white_pawn3"].setMasqueradesAs(state.white.pawn3.masquerading_as);
                chessPieces["white_pawn4"].setMasqueradesAs(state.white.pawn4.masquerading_as);
                chessPieces["white_pawn5"].setMasqueradesAs(state.white.pawn5.masquerading_as);
                chessPieces["white_pawn6"].setMasqueradesAs(state.white.pawn6.masquerading_as);
                chessPieces["white_pawn7"].setMasqueradesAs(state.white.pawn7.masquerading_as);
                chessPieces["white_pawn8"].setMasqueradesAs(state.white.pawn8.masquerading_as);
                chessPieces["white_rook1"].setMasqueradesAs(state.white.rook1.masquerading_as);
                chessPieces["white_rook2"].setMasqueradesAs(state.white.rook2.masquerading_as);
                chessPieces["white_bishop1"].setMasqueradesAs(state.white.bishop1.masquerading_as);
                chessPieces["white_bishop2"].setMasqueradesAs(state.white.bishop2.masquerading_as);
                chessPieces["white_knight1"].setMasqueradesAs(state.white.knight1.masquerading_as);
                chessPieces["white_knight2"].setMasqueradesAs(state.white.knight2.masquerading_as);
                chessPieces["white_queen"].setMasqueradesAs(state.white.queen.masquerading_as);
                chessPieces["white_king"].setMasqueradesAs(state.white.king.masquerading_as);

                mMyColor = GameStateManager.getInstance().getCurrentPlayer();
                mSelectedPiece = null;
                mMoveMade = false;
                setPieceMoves();
            }
        }
Ejemplo n.º 4
0
 public void setGameState(CurrentGameState _instance)
 {
     currentState = _instance;
 }
Ejemplo n.º 5
0
 public void setGameState(CurrentGameState _instance)
 {
     currentState = _instance;
 }