Exemplo n.º 1
0
        //Läser data från xml-fil, skapar en representation av schackbräädet och returnerar det.
        public ChessPiece[,] LoadData()
        {
            if (!IsFileLocked())
            {

            ChessPiece[,] board = new ChessPiece[8, 8];

                XDocument doc = XDocument.Load(@".\chessdata\chessdata.xml");

                var data = from item in doc.Descendants("ChessPiece")
                           select new
                           {
                               posx = item.Element("posX").Value,
                               posy = item.Element("posY").Value,
                               color = item.Element("color").Value,
                               type = item.Element("type").Value
                           };

                foreach (var c in data)
                {
                    switch (c.type)
                    {
                        case "Chess2._0.King":
                            ChessPiece king = new King(c.color, int.Parse(c.posx), int.Parse(c.posy));
                            board[int.Parse(c.posx), int.Parse(c.posy)] = king;
                            break;
                        case "Chess2._0.Queen":
                            ChessPiece queen = new Queen(c.color, int.Parse(c.posx), int.Parse(c.posy));
                            board[int.Parse(c.posx), int.Parse(c.posy)] = queen;
                            break;
                        case "Chess2._0.Runner":
                            ChessPiece runner = new Runner(c.color, int.Parse(c.posx), int.Parse(c.posy));
                            board[int.Parse(c.posx), int.Parse(c.posy)] = runner;
                            break;
                        case "Chess2._0.Horse":
                            ChessPiece horse = new Horse(c.color, int.Parse(c.posx), int.Parse(c.posy));
                            board[int.Parse(c.posx), int.Parse(c.posy)] = horse;
                            break;
                        case "Chess2._0.Tower":
                            ChessPiece tower = new Tower(c.color, int.Parse(c.posx), int.Parse(c.posy));
                            board[int.Parse(c.posx), int.Parse(c.posy)] = tower;
                            break;
                        case "Chess2._0.Farmer":
                            ChessPiece farmer = new Farmer(c.color, int.Parse(c.posx), int.Parse(c.posy));
                            board[int.Parse(c.posx), int.Parse(c.posy)] = farmer;
                            break;
                    }
                }

                return board;
            }
            else
            {
                throw new Exception();
            }
        }
Exemplo n.º 2
0
        public ChessBoard(DataStorage dataS)
        {
            ds = dataS;
            //Om det finns ett sparat spel, läs in det
            if (File.Exists(@".\chessdata\chessdata.xml"))
            {
                this.board = ds.LoadData();
                ds.SaveData(board);
            }
            else
            {
                //Skapar vita pjäser
                ChessPiece whiteKing = new King("white", 4, 0);
                ChessPiece whiteQueen = new Queen("white", 3, 0);
                ChessPiece whiteRunner1 = new Runner("white", 2, 0);
                ChessPiece whiteRunner2 = new Runner("white", 5, 0);
                ChessPiece whiteHorse1 = new Horse("white", 1, 0);
                ChessPiece whiteHorse2 = new Horse("white", 6, 0);
                ChessPiece whiteTower1 = new Tower("white", 0, 0);
                ChessPiece whiteTower2 = new Tower("white", 7, 0);
                ChessPiece whiteFarmer1 = new Farmer("white", 0, 1);
                ChessPiece whiteFarmer2 = new Farmer("white", 1, 1);
                ChessPiece whiteFarmer3 = new Farmer("white", 2, 1);
                ChessPiece whiteFarmer4 = new Farmer("white", 3, 1);
                ChessPiece whiteFarmer5 = new Farmer("white", 4, 1);
                ChessPiece whiteFarmer6 = new Farmer("white", 5, 1);
                ChessPiece whiteFarmer7 = new Farmer("white", 6, 1);
                ChessPiece whiteFarmer8 = new Farmer("white", 7, 1);

                //Skapar svarta pjäser
                ChessPiece blackKing = new King("black", 4, 7);
                ChessPiece blackQueen = new Queen("black", 3, 7);
                ChessPiece blackRunner1 = new Runner("black", 2, 7);
                ChessPiece blackRunner2 = new Runner("black", 5, 7);
                ChessPiece blackHorse1 = new Horse("black", 1, 7);
                ChessPiece blackHorse2 = new Horse("black", 6, 7);
                ChessPiece blackTower1 = new Tower("black", 0, 7);
                ChessPiece blackTower2 = new Tower("black", 7, 7);
                ChessPiece blackFarmer1 = new Farmer("black", 0, 6);
                ChessPiece blackFarmer2 = new Farmer("black", 1, 6);
                ChessPiece blackFarmer3 = new Farmer("black", 2, 6);
                ChessPiece blackFarmer4 = new Farmer("black", 3, 6);
                ChessPiece blackFarmer5 = new Farmer("black", 4, 6);
                ChessPiece blackFarmer6 = new Farmer("black", 5, 6);
                ChessPiece blackFarmer7 = new Farmer("black", 6, 6);
                ChessPiece blackFarmer8 = new Farmer("black", 7, 6);

                //Lägg till pjäser i tvådimensionell array som representerar schackbrädet
                board[4, 0] = whiteKing;
                board[3, 0] = whiteQueen;
                board[2, 0] = whiteRunner1;
                board[5, 0] = whiteRunner2;
                board[1, 0] = whiteHorse1;
                board[6, 0] = whiteHorse2;
                board[0, 0] = whiteTower1;
                board[7, 0] = whiteTower2;
                board[0, 1] = whiteFarmer1;
                board[1, 1] = whiteFarmer2;
                board[2, 1] = whiteFarmer3;
                board[3, 1] = whiteFarmer4;
                board[4, 1] = whiteFarmer5;
                board[5, 1] = whiteFarmer6;
                board[6, 1] = whiteFarmer7;
                board[7, 1] = whiteFarmer8;

                board[4, 7] = blackKing;
                board[3, 7] = blackQueen;
                board[2, 7] = blackRunner1;
                board[5, 7] = blackRunner2;
                board[1, 7] = blackHorse1;
                board[6, 7] = blackHorse2;
                board[0, 7] = blackTower1;
                board[7, 7] = blackTower2;
                board[0, 6] = blackFarmer1;
                board[1, 6] = blackFarmer2;
                board[2, 6] = blackFarmer3;
                board[3, 6] = blackFarmer4;
                board[4, 6] = blackFarmer5;
                board[5, 6] = blackFarmer6;
                board[6, 6] = blackFarmer7;
                board[7, 6] = blackFarmer8;

                ds.SaveData(board);
            }
        }