public void WriteNode(StreamWriter sw, int iLevel) { sw.Write("{0}({1},", new string(' ', iLevel * 4), Question); if (anYes != null) { sw.WriteLine(); anYes.WriteNode(sw, iLevel + 1); } sw.Write(","); if (anNo != null) { sw.WriteLine(); anNo.WriteNode(sw, iLevel + 1); } sw.Write(")"); }
static void Main() { string strFilename = "GuessTheAnimal.txt"; AnimalNode anRoot; try { StreamReader sr = new StreamReader(strFilename); anRoot = AnimalNode.ReadNode(sr); sr.Close(); } catch { anRoot = new AnimalNode(); anRoot.Question = "cat"; } do { // Start playing. Console.Write("Think of an animal and press Enter."); Console.ReadLine(); AskQuestion(ref anRoot); // Save the file. StreamWriter sw = new StreamWriter(strFilename); anRoot.WriteNode(sw, 0); sw.Close(); // Possibly play again. Console.Write("Would you like to play again? "); }while (GetYesNo()); }