Esempio n. 1
0
        public ChessData.ChessPiece GetPieceAtCoordinate(ChessData.Coordinate coordinate)
        {
            var request = new Dictionary <string, string>();

            request.Add("row", coordinate.Row.ToString());
            request.Add("column", coordinate.Column.ToString());
            var parsed = JSON.Parse(GetResponse("get_piece_at_coordinate", request));
            var color  = (ChessData.ChessPieceColor) int.Parse(parsed["color"]);
            var type   = (ChessData.ChessPieceType) int.Parse(parsed["type"]);

            return(new ChessData.ChessPiece(color, type));
        }
Esempio n. 2
0
        /*public ChessData.ChessMove GetPreviousMove()
         * {
         *  return ParseMove(GetResponse("get_previous_move", null));
         * }*/

        public IEnumerable <ChessData.ChessMove> GetPossibleMoves(ChessData.Coordinate coordinate)
        {
            var request = new Dictionary <string, string>();

            request.Add("row", coordinate.Row.ToString());
            request.Add("column", coordinate.Column.ToString());
            var parsed = JSON.Parse(GetResponse("get_piece_at_coordinate", request));
            var list   = new LinkedList <ChessData.ChessMove>();

            foreach (var move in parsed["moves"].AsArray)
            {
                list.AddLast(ParseMove(move.Value));
            }
            return(list);
        }
Esempio n. 3
0
        private ChessData.ChessMove ParseMove(string json)
        {
            var parsed            = JSON.Parse(json);
            var pieceType         = (ChessData.ChessPieceType) int.Parse(parsed["chess_piece_type"]);
            var isCapture         = bool.Parse(parsed["is_capture"]);
            var isPromotioToQueen = bool.Parse(parsed["is_promotion_to_queen"]);
            var drawOfferExtended = bool.Parse(parsed["draw_offer_extended"]);
            var isCheck           = bool.Parse(parsed["is_check"]);
            var isCheckMate       = bool.Parse(parsed["is_check_mate"]);
            var kingsideCastle    = bool.Parse(parsed["kingsideCastle"]);
            var queensideCastle   = bool.Parse(parsed["queenside_castle"]);
            var fromRow           = int.Parse(parsed["from"]["row"]);
            var fromColumn        = int.Parse(parsed["from"]["column"]);
            var fromCoord         = new ChessData.Coordinate(fromRow, fromColumn);
            var toRow             = int.Parse(parsed["to"]["row"]);
            var toColumn          = int.Parse(parsed["to"]["column"]);
            var toCoord           = new ChessData.Coordinate(toRow, toColumn);
            var move = new ChessData.ChessMove(fromCoord, toCoord, pieceType, isCapture,
                                               isPromotioToQueen, drawOfferExtended, isCheck, isCheckMate,
                                               kingsideCastle, queensideCastle);

            return(move);
        }
        void RefreshBoard(GameState.MatchState matchState, ChessData.Coordinate?selected)
        {
            CurrentMatchState = matchState;
            StatusText.text   = string.Format("{0} ({1})",
                                              matchState.IsSelfTurn() ? "Your Turn" : "Their Turn",
                                              matchState.Board.GetTurnColor() == ChessData.ChessPieceColor.White ? "white" : "black");

            for (int r = 0; r < 8; r++)
            {
                for (int c = 0; c < 8; c++)
                {
                    foreach (Transform child in TransformGrid[r, c])
                    {
                        Destroy(child.gameObject);
                    }
                }
            }

            if (matchState.Board.PreviousMove.IsCheckMate)
            {
                TitleText.text  = "Game Finished";
                StatusText.text = string.Format("{0} won!", matchState.IsSelfTurn() ? "They" : "You");
                return;
            }

            IEnumerable <ChessData.ChessMove> moves;
            var moveDestinations = new HashSet <ChessData.Coordinate>();

            if (selected.HasValue)
            {
                moves = matchState.Board.GetPossibleMoves(selected.Value);
                //TODO: server.GetPossibleMoves(board_id, selected.value)
            }
            else
            {
                moves = new HashSet <ChessData.ChessMove>();
            }
            foreach (var move in moves)
            {
                moveDestinations.Add(move.To);
            }

            // Fill in pieces, except those that are move destinations
            for (int c = 0; c < 8; c++)
            {
                for (int r = 0; r < 8; r++)
                {
                    var  coordinate           = new ChessData.Coordinate(r, c);
                    bool isSelectedCoordinate = selected.HasValue && selected.Value.Equals(coordinate);
                    bool isMoveDestination    = moveDestinations.Contains(coordinate);

                    ChessData.ChessPiece piece = matchState.Board.GetPieceAtCoordinate(coordinate);
                    // pieces that are move destinations are handled later because clicking on them has movement behavior.
                    if (piece.Type != ChessData.ChessPieceType.None && !isMoveDestination)
                    {
                        Color pieceHighlight = isSelectedCoordinate ? Color.green : Color.white;
                        Image pieceImage     = prefabToPieceImage(piece, pieceHighlight);
                        pieceImage.transform.SetParent(TransformGrid[r, c], false);
                        // Only allow player to make moves if it is their turn. Games against local
                        // player allow both players to make a move on the same device.
                        if (matchState.IsSelfTurn() || matchState.Opponent.IsLocalOpponent())
                        {
                            pieceImage.GetComponent <Button>().onClick.AddListener(delegate
                            {
                                if (selected == null)
                                {
                                    RefreshBoard(matchState, coordinate);
                                }
                                else if (selected.Value.Equals(coordinate))
                                {
                                    RefreshBoard(matchState, null);
                                }
                                else
                                {
                                    RefreshBoard(matchState, coordinate);
                                }
                            });
                        }
                    }
                }
            }

            // Fill in pieces that are move destinations
            foreach (var move in moves)
            {
                bool putsUserInCheck = matchState.Board.TryApplyMove(move);
                var  newMatchState   = new GameState.MatchState(
                    matchState.Opponent,
                    matchState.Board,
                    //TODO: TryApplyMove(move, board_id)
                    matchState.SelfIsWhite,
                    matchState.Identifier);

                if (!putsUserInCheck)
                {
                    bool isOpponentPiece = matchState.Board.GetPieceAtCoordinate(move.To).Type != ChessData.ChessPieceType.None;
                    //TODO: GetPieceAtCoordinate(board_id, coord)
                    Color pieceHighlight = isOpponentPiece ? Color.red : Color.blue;
                    var   fromPiece      = matchState.Board.GetPieceAtCoordinate(move.From);
                    Image pieceImage     = prefabToPieceImage(fromPiece, pieceHighlight);
                    pieceImage.transform.SetParent(TransformGrid[move.To.Row, move.To.Column], false);

                    // Only allow player to make moves if it is their turn. Games against local
                    // player allow both players to make a move on the same device.
                    if (matchState.IsSelfTurn() || matchState.Opponent.IsLocalOpponent())
                    {
                        pieceImage.GetComponent <Button>().onClick.AddListener(delegate
                        {
                            // GameManager will handler whether or not we are saving a local or multiplayer
                            // game, and will notify us of the updated state via our
                            // CurrentMatchStateAvailableHandler
                            StatusText.text = "Saving Match...";
                            GameManager.Instance.UpdateMatchState(newMatchState);
                        });
                    }
                }
            }
        }