public static Player Load(out bool newP) { newP = true; Console.Clear(); string[] paths = Directory.GetFiles("saves"); List<Player> players = new List<Player>(); int idCount = 0; BinaryFormatter binForm = new BinaryFormatter(); foreach (string p in paths) { FileStream file= File.Open(p, FileMode.Open); Player player = (Player)binForm.Deserialize(file); file.Close(); players.Add(player); } idCount = players.Count; while (true) { Console.Clear(); Console.WriteLine("Chose a Saved player:"); foreach (Player p in players) { Console.WriteLine(p.id + ": " + p.name); } Console.WriteLine("Please input player name or id(id:# or playername)"); Console.WriteLine("'(C)reate' new save"); string[] data = Console.ReadLine().ToLower().Split(':'); try { if (data[0] == "id") { if (int.TryParse(data[1], out int id)) { foreach (Player p in players) { if (p.id == id) { return p; } return p; } Console.WriteLine("ID number not found"); Console.ReadKey(); } else { Console.WriteLine("ID is not a valid number!"); Console.WriteLine("Any key to continue!"); Console.ReadKey(); } } else if (data[0] == "create" || data[0] == "c") { Player newPlayer = NewStart(idCount); newP = true; return newPlayer; } else { foreach (Player player in players) { if (player.name == data[0]) { return player; } } Console.WriteLine("Name not found"); Console.ReadKey(); } if (data[0] == "e" || data[0] == "exit") { break; } else { Console.WriteLine("Invalid selection"); } } catch (IndexOutOfRangeException) { Console.WriteLine("ID is not a valid number!"); Console.WriteLine("Any key to continue!"); Console.ReadKey(); } } } } }
if (data[0] == "e" || data[0] == "exit") { break; } else { Console.WriteLine("Invalid selection"); }
if (int.TryParse(data[1], out int id)) { foreach (Player p in players) { if (p.id == id) { return p; } return p; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)