Beispiel #1
0
        public static String saveGame(Char whose_turn)
        {
            System.Console.Clear();
            System.Console.Write(@"Save name: ");
            String save_name = System.Console.ReadLine();
            String file_name;

            if (save_name == "")
            {
                String timestamp = DateTime.Now.ToString(@"yyyyMMddHHmmffff");
                file_name = FlameBadge.save_dir + timestamp + @".fbsave";
            }
            else
            {
                file_name = FlameBadge.save_dir + save_name + @".fbsave";
                if (File.Exists(file_name))
                {
                    System.Console.WriteLine("Are you sure you want to overwrite save '{0}'? (Y/n)", save_name);
                    Boolean need_answer = true;
                    while (need_answer)
                    {
                        ConsoleKeyInfo answer;
                        answer = System.Console.ReadKey();

                        switch (answer.KeyChar)
                        {
                        case 'n':
                        case 'N':
                            need_answer = false;
                            GameBoard.saveGame(whose_turn);
                            break;

                        case 'y':
                        case 'Y':
                            need_answer = false;
                            File.Delete(file_name);
                            break;

                        default:
                            System.Console.WriteLine(@"Please input either 'y' or 'n': ");
                            break;
                        }
                    }
                }
            }

            try
            {
                using (StreamWriter writer = new StreamWriter(file_name))
                {
                    // write the map
                    for (int i = 0; i < MAP_SIZE; i++)
                    {
                        for (int j = 0; j < MAP_SIZE; j++)
                        {
                            writer.Write(board[i, j]);
                            writer.Write(@" ");
                        }
                        writer.WriteLine();
                    }
                    writer.WriteLine();
                    writer.WriteLine();

                    // write the living player units and their positions, and stats
                    writer.Write(@"Player {0}", FlameBadge.player_units.Count);
                    writer.WriteLine();
                    foreach (var unit in FlameBadge.player_units)
                    {
                        writer.Write(@"{0} {1} {2} {3} {4} {5}", unit.id, unit.xPos, unit.yPos, unit.health, unit.level, unit.dpsMod);
                        writer.WriteLine();
                    }

                    writer.WriteLine();
                    writer.WriteLine();
                    // write the living computer units and their positions, and stats
                    writer.Write(@"Computer {0}", FlameBadge.cpu_units.Count);
                    writer.WriteLine();
                    foreach (var unit in FlameBadge.cpu_units)
                    {
                        writer.Write(@"{0} {1} {2} {3} {4} {5}", unit.id, unit.xPos, unit.yPos, unit.health, unit.level, unit.dpsMod);
                        writer.WriteLine();
                    }

                    writer.WriteLine();
                    writer.WriteLine();
                    // write who of the player units was taking his turn
                    writer.Write(@"TURN {0}", whose_turn);
                }
                System.Console.WriteLine("Game Saved!");
                System.Threading.Thread.Sleep(1000);
                GameBoard.redraw();
                Sidebar.announce(String.Format(@"{0} taking turn...", whose_turn), true);
            }
            catch (Exception e)
            {
                String msg = String.Format(@"The game could not be saved. Reason: " + e);
                Logger.log(msg, "error");
                return(null);
            }

            Logger.log("Game saved.");
            return(file_name);
        }