Esempio n. 1
0
        public void RebuiltBoard()
        {
            var jsonDoc = JsonDocument.Parse(GameState);

            board = JsonConvert.DeserializeObject <Board>(GameState);
            var         myString = jsonDoc.RootElement.GetProperty("Tiles"); // Get a string from a JsonElement
            var         list     = myString.EnumerateObject();
            List <Tile> tiles    = new List <Tile>();
            var         dict     = new Dictionary <int, Tile>();

            foreach (var item in list)
            {
                var keyID = Convert.ToInt32(item.Name);
                var empty = item.Value.GetProperty("IsEmpty").GetBoolean();
                var Tile  = new Tile()
                {
                    Description = item.Value.GetProperty("Description").GetString(),
                    Id          = item.Value.GetProperty("Id").GetInt32(),
                    IsEmpty     = empty,
                    Piece       = null,
                    Xpos        = item.Value.GetProperty("Xpos").GetInt32(),
                    Ypos        = item.Value.GetProperty("Ypos").GetInt32()
                };
                if (!empty)
                {
                    var pieceX       = item.Value.GetProperty("Piece");
                    var color        = pieceX.GetProperty("Color").GetInt32() == 0 ? Color.White : Color.Black;
                    var currentPos   = pieceX.GetProperty("CurrentPos");
                    var img          = currentPos.GetProperty("Image").GetString();
                    var newPIeceData = new PieceData()
                    {
                        Description = currentPos.GetProperty("Description").GetString(),
                        PieceName   = currentPos.GetProperty("PieceName").GetString(),
                        Xpos        = currentPos.GetProperty("Xpos").GetInt32(),
                        Ypos        = currentPos.GetProperty("Ypos").GetInt32(),
                        Image       = img
                    };
                    // var isPawnfistMove = newPIeceData.PieceName == "pawn" ? pieceX.GetProperty("isFirstMove").GetBoolean() : pieceX.GetProperty("isFirstMove").GetBoolean();
                    var moveCount = pieceX.GetProperty("MoveCount").GetInt32();
                    switch (newPIeceData.PieceName)
                    {
                    case "rook":
                        var rook = new Rook(color, newPIeceData);
                        Tile.Piece           = rook;
                        Tile.Piece.MoveCount = moveCount;
                        break;

                    case "pawn":
                        var pawn = new Pawn(color, newPIeceData);
                        Tile.Piece           = pawn;
                        Tile.Piece.MoveCount = moveCount;
                        break;

                    case "bishop":
                        var bishop = new Bishop(color, newPIeceData);
                        Tile.Piece           = bishop;
                        Tile.Piece.MoveCount = moveCount;
                        break;

                    case "king":
                        var king = new King(color, newPIeceData);
                        Tile.Piece           = king;
                        Tile.Piece.MoveCount = moveCount;
                        break;

                    case "queen":
                        var queen = new Queen(color, newPIeceData);
                        Tile.Piece           = queen;
                        Tile.Piece.MoveCount = moveCount;
                        break;

                    case "horse":
                        var horse = new Horse(color, newPIeceData);
                        Tile.Piece           = horse;
                        Tile.Piece.MoveCount = moveCount;
                        break;
                    }
                }
                board.Tiles[keyID] = Tile;
            }
        }
Esempio n. 2
0
        public void RebuiltPiecesMoved(string data)
        {
            var jsonDoc   = JsonDocument.Parse(data);
            var lists     = jsonDoc.RootElement.EnumerateArray();
            var datalist  = new List <Tile>();
            var superList = new List <List <Tile> >();

            foreach (var list in lists)
            {
                var list2 = list.EnumerateArray();
                foreach (var pieceX in list2)
                {
                    JsonElement test;
                    var         element = pieceX.TryGetProperty("Piece", out test);
                    var         flag    = test.GetRawText();
                    if (element && !string.IsNullOrEmpty(flag) && flag != "null")
                    {
                        var Tile = new Tile()
                        {
                            Description = pieceX.GetProperty("Description").GetString(),
                            Id          = pieceX.GetProperty("Id").GetInt32(),
                            IsEmpty     = false,
                            Piece       = null,
                            Xpos        = pieceX.GetProperty("Xpos").GetInt32(),
                            Ypos        = pieceX.GetProperty("Ypos").GetInt32()
                        };

                        var color        = test.GetProperty("Color").GetInt32() == 0 ? Color.White : Color.Black;
                        var currentPos   = test.GetProperty("CurrentPos");
                        var img          = currentPos.GetProperty("Image").GetString();
                        var newPIeceData = new PieceData()
                        {
                            Description = currentPos.GetProperty("Description").GetString(),
                            PieceName   = currentPos.GetProperty("PieceName").GetString(),
                            Xpos        = currentPos.GetProperty("Xpos").GetInt32(),
                            Ypos        = currentPos.GetProperty("Ypos").GetInt32(),
                            Image       = img
                        };
                        var moveCount = test.GetProperty("MoveCount").GetInt32();
                        switch (newPIeceData.PieceName)
                        {
                        case "rook":
                            var rook = new Rook(color, newPIeceData);
                            rook.MoveCount = moveCount;
                            Tile.Piece     = rook;
                            break;

                        case "pawn":
                            var pawn = new Pawn(color, newPIeceData);
                            pawn.MoveCount = moveCount;
                            Tile.Piece     = pawn;
                            break;

                        case "bishop":
                            var bishop = new Bishop(color, newPIeceData);
                            bishop.MoveCount = moveCount;
                            Tile.Piece       = bishop;
                            break;

                        case "king":
                            var king = new King(color, newPIeceData);
                            king.MoveCount = moveCount;
                            Tile.Piece     = king;
                            break;

                        case "queen":
                            var queen = new Queen(color, newPIeceData);
                            queen.MoveCount = moveCount;
                            Tile.Piece      = queen;
                            break;

                        case "horse":
                            var horse = new Horse(color, newPIeceData);
                            horse.MoveCount = moveCount;
                            Tile.Piece      = horse;
                            break;
                        }
                        datalist.Add(Tile);
                    }
                }

                superList.Add(datalist);
            }
        }