示例#1
0
        public FlameBadge()
        {
            // Set up saves directory

            if (!Directory.Exists(save_dir))
            {
                Directory.CreateDirectory(save_dir);
            }

            // Check if any save files exist
            // If they don't we won't bother offering a load game option
            DirectoryInfo dir       = new DirectoryInfo(save_dir);
            Boolean       is_loaded = false;

            if (dir.GetFiles().Length != 0)
            {
                is_loaded = _offerContinue();
            }

            String loaded_file = "";

            if (is_loaded)
            {
                loaded_file = _getSavedGame(dir);
            }

            if (loaded_file == "")
            {
                is_loaded = false;
            }
            else
            {
                loaded_file = save_dir + loaded_file;
            }

            // if we loaded we need to know whose turn it was
            Char curr_turn = '0';

            if (is_loaded)
            {
                curr_turn = _getTurn(loaded_file);
            }

            // Draw the game board.
            GameBoard game_board = new GameBoard(is_loaded ? loaded_file : null);

            // Put the pieces on the board.
            PlayerCharacter.placePieces(is_loaded, loaded_file);
            EnemyCharacter.placePieces(is_loaded, loaded_file);

            // mainloop
            while (true)
            {
                for (int i = 0; i < player_units.Count; i++)
                {
                    // if we loaded a game, skip over everyone until the rightful
                    // unit goes
                    if (curr_turn != '0')
                    {
                        if (player_units[i].id != curr_turn)
                        {
                            continue;
                        }
                        else
                        {
                            curr_turn = '0';
                        }
                    }
                    player_units[i].takeTurn();
                    List <Character> victims = GameBoard.getAttackableUnits(player_units[i].xPos, player_units[i].yPos, cpu_units.Cast <Character>().ToList());
                    if (victims.Count > 0)
                    {
                        //pass in true to signify player
                        GameBoard.attack(player_units[i].id, victims[0].id, true);
                        GameBoard.redraw();
                        if (cpu_units.Count == 0)
                        {
                            FlameBadge.hasEnded = true;
                        }
                    }
                    Tuple <Int16, Int16> enemyCastle = GameBoard.getCPUCastle();
                    if ((int)enemyCastle.Item2 == (int)player_units[i].xPos && (int)enemyCastle.Item1 == (int)player_units[i].yPos)
                    {
                        FlameBadge.hasEnded = true;
                    }
                    if (FlameBadge.hasEnded)
                    {
                        _endGame();
                    }
                }

                for (int i = 0; i < cpu_units.Count; i++)
                {
                    cpu_units[i].takeTurn();
                    List <Character> victims = GameBoard.getAttackableUnits(cpu_units[i].xPos, cpu_units[i].yPos, player_units.Cast <Character>().ToList());
                    if (victims.Count > 0)
                    {
                        //pass in false to signify AI
                        GameBoard.attack(cpu_units[i].id, victims[0].id, false);
                        GameBoard.redraw();
                        if (player_units.Count == 0)
                        {
                            FlameBadge.hasEnded = true;
                        }
                    }
                    Tuple <Int16, Int16> enemyCastle = GameBoard.getPlayerCastle();
                    if ((int)enemyCastle.Item2 == (int)cpu_units[i].xPos && (int)enemyCastle.Item1 == (int)cpu_units[i].yPos)
                    {
                        FlameBadge.hasEnded   = true;
                        FlameBadge.cpuCapture = true;
                    }
                    if (FlameBadge.hasEnded)
                    {
                        _endGame();
                    }
                }
            }
        }
示例#2
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);
        }