void initFromSave() { isLoading = true; Console.WriteLine("Initiating from saved file"); if(File.Exists(xmlStorage.getPath())){ this.shipList.Clear(); GameBoard.Clear(); for (int y = 0; y < boardSize; ++y) { List<Node> tmpList = new List<Node>(); for (int x = 0; x < boardSize; ++x) { Node n = new Node(false, null, x); //create a node to insert in the inner List tmpList.Add(n); } GameBoard.Add(tmpList); //add the inner List to the outer List } XDocument db = XDocument.Load(xmlStorage.getPath()); var shipList = from ship in db.Root.Element("Player") .Element(this.player) .Element("Ships") .Elements("Ship") select ship; var hitList = from node in db.Root.Element("Player") .Element(this.player) .Element("Hits") .Elements("Node") select node; ai.loadKnownBoard(); //load the internal AI board int startX, startY, endX, endY, size, hits; //Place all the loaded ships onto the board, and set hits on hit ships foreach (XElement ship in shipList) { startX = Int32.Parse(ship.Element("startPos").Element("X").Value); startY = Int32.Parse(ship.Element("startPos").Element("Y").Value); endX = Int32.Parse(ship.Element("endPos").Element("X").Value); endY = Int32.Parse(ship.Element("endPos").Element("Y").Value); size = Int32.Parse(ship.Element("size").Value); hits = Int32.Parse(ship.Element("hits").Value); Tuple<int, int> startPos = new Tuple<int, int>(startX, startY); Tuple<int, int> endPos = new Tuple<int, int>(endX, endY); Ship s = new Ship(); placeShip(startPos, endPos, s); s.setHits(hits); this.shipList.Add(s); } int hitX, hitY; //Place hit markers foreach (XElement hit in hitList) { hitX = Int32.Parse(hit.Element("X").Value); hitY = Int32.Parse(hit.Element("Y").Value); this.GameBoard[hitY][hitX].setHit(); } } isLoading = false; Console.WriteLine("Initiating complete"); }
void init() { //init the gameboard Console.WriteLine("Initializing the gameboard"); for (int y = 0; y < boardSize; ++y) { List<Node> tmpList = new List<Node>(); for (int x = 0; x < boardSize; ++x) { Node n = new Node(false, null, x); //create a node to insert in the inner List tmpList.Add(n); } GameBoard.Add(tmpList); //add the inner List to the outer List } Console.WriteLine("Gameboard initialized"); //init shiplist initShipList(); }