Example #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();
                    }
                }
            }
        }
        public static void placePieces(Boolean is_loaded, String loaded_file)
        {
            if (!is_loaded)
            {
                Tuple <Int16, Int16> castleCoords = GameBoard.getPlayerCastle();
                Int16 j = castleCoords.Item1;
                Int16 i = castleCoords.Item2;

                //Logger.log(@"Placing player pieces in random positions.");
                if (i - 1 >= 0)
                {
                    if (GameBoard.overlay[i - 1, j] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i - 1), (short)j);
                        PlayerCharacter      character = new PlayerCharacter('1', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }
                if (i + 1 < GameBoard.overlay.GetLength(0))
                {
                    if (GameBoard.overlay[i + 1, j] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i + 1), (short)j);
                        PlayerCharacter      character = new PlayerCharacter('2', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }
                if (j - 1 >= 0)
                {
                    if (GameBoard.overlay[i, j - 1] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i), (short)(j - 1));
                        PlayerCharacter      character = new PlayerCharacter('3', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }
                if (j + 1 < GameBoard.overlay.GetLength(1))
                {
                    if (GameBoard.overlay[i, j + 1] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i), (short)(j + 1));
                        PlayerCharacter      character = new PlayerCharacter('4', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }

                //diagonals
                if (i - 1 >= 0 && j + 1 < GameBoard.overlay.GetLength(1))
                {
                    if (GameBoard.overlay[i - 1, j + 1] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i - 1), (short)(j + 1));
                        PlayerCharacter      character = new PlayerCharacter('5', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }
                if (i + 1 < GameBoard.overlay.GetLength(0) && j - 1 >= 0)
                {
                    if (GameBoard.overlay[i + 1, j - 1] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i + 1), (short)(j - 1));
                        PlayerCharacter      character = new PlayerCharacter('6', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }
                if (i - 1 >= 0 && j - 1 >= 0)
                {
                    if (GameBoard.overlay[i - 1, j - 1] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i - 1), (short)(j - 1));
                        PlayerCharacter      character = new PlayerCharacter('7', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }
                if (i + 1 < GameBoard.overlay.GetLength(0) && j + 1 < GameBoard.overlay.GetLength(1))
                {
                    if (GameBoard.overlay[i + 1, j + 1] != '@')
                    {
                        Tuple <Int16, Int16> coords    = Tuple.Create((short)(i + 1), (short)(j + 1));
                        PlayerCharacter      character = new PlayerCharacter('8', coords.Item1, coords.Item2, 10, 1, 1);
                        FlameBadge.player_units.Add(character);
                        GameBoard.update(character, coords.Item1, coords.Item2);
                        Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), coords.Item1, coords.Item2, 10, 1, 1));
                    }
                }
            }
            else
            {
                Logger.log(@"Placing player pieces according to loaded save file.");
                try
                {
                    using (StreamReader sr = new StreamReader(loaded_file))
                    {
                        while (sr.Peek() > -1)
                        {
                            String line = sr.ReadLine();
                            if (line.StartsWith("Player"))
                            {
                                for (int i = 0; i < Convert.ToInt16(line.Split()[1]); i++)
                                {
                                    String[]        unit_info = sr.ReadLine().Split();
                                    PlayerCharacter character = new PlayerCharacter(Convert.ToChar(unit_info[0]), Convert.ToInt16(unit_info[1]), Convert.ToInt16(unit_info[2]), Convert.ToInt32(unit_info[3]), Convert.ToInt32(unit_info[4]), Convert.ToInt32(unit_info[5]));
                                    FlameBadge.player_units.Add(character);
                                    GameBoard.update(character, Convert.ToInt16(unit_info[1]), Convert.ToInt16(unit_info[2]));
                                    Logger.log(String.Format(@"Placed {0} at {1}, {2} with health {3}, level {4}, and dpsMod {5}", character.id.ToString(), Convert.ToInt16(unit_info[1]), Convert.ToInt16(unit_info[2]), Convert.ToInt32(unit_info[3]), Convert.ToInt32(unit_info[4]), Convert.ToInt32(unit_info[5])));
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    Logger.log("Could not load player pieces from save file. Quitting...", "error");
                    Environment.Exit(1);
                }
            }
        }
        public override void takeTurn()
        {
            /*Sidebar.announce(String.Format(@"CPU taking turn...", this.id.ToString()), false);
             *
             * Random rand = new Random();
             *
             * // MAGIC NUMBER ALERT!!!
             * // 8 - Move Up          7 - MoveUpLeft
             * // 4 - Move Left        9 - MoveUpRight
             * // 6 - Move Right       1 - MoveDownLeft
             * // 2 - Move Left        3 - MoveDownRight
             * List<Char> moves = new List<Char> {'1', '2', '3', '4', '6', '7', '8', '9'};
             *
             * while (true)
             * {
             *  // The computer is an idiot
             *  Char move = moves[rand.Next(moves.Count)];
             *
             *  if (makeMove(move))
             *  {
             *      Sidebar.announce(String.Format(@"CPU's turn, please wait...", this.id.ToString()), false);
             *      System.Threading.Thread.Sleep(1500);
             *
             *      break;
             *  }
             * }*/

            // EXAMPLE AI CODE (AI object should be declared at a higher level though, not for each individual unit).
            // The 3rd and 4th parameters are base positions. If given with x and y less than 0, the AI assumes no bases exist.
            // The 5th parameter is the AI level, which controls how far into the future the search goes. The level caps at 5 (because any higher takes forever).
            Tuple <Int16, Int16> castleLoc          = GameBoard.getCPUCastle();
            Tuple <int, int>     convertedLoc       = Tuple.Create((int)castleLoc.Item2, (int)castleLoc.Item1);
            Tuple <Int16, Int16> castlePlayerLoc    = GameBoard.getPlayerCastle();
            Tuple <int, int>     convertedPlayerLoc = Tuple.Create((int)castlePlayerLoc.Item2, (int)castlePlayerLoc.Item1);

            AI smartCPU = new AI(FlameBadge.player_units, FlameBadge.cpu_units, convertedPlayerLoc, convertedLoc, 2);

            Tuple <int, int> nextMoveLoc = smartCPU.getNextMove(this);

            if (nextMoveLoc.Item1 < this.xPos && nextMoveLoc.Item2 > this.yPos)
            {
                makeMove('1');
            }
            else if (nextMoveLoc.Item1 == this.xPos && nextMoveLoc.Item2 > this.yPos)
            {
                makeMove('2');
            }
            else if (nextMoveLoc.Item1 > this.xPos && nextMoveLoc.Item2 > this.yPos)
            {
                makeMove('3');
            }
            else if (nextMoveLoc.Item1 < this.xPos && nextMoveLoc.Item2 == this.yPos)
            {
                makeMove('4');
            }
            else if (nextMoveLoc.Item1 > this.xPos && nextMoveLoc.Item2 == this.yPos)
            {
                makeMove('6');
            }
            else if (nextMoveLoc.Item1 < this.xPos && nextMoveLoc.Item2 < this.yPos)
            {
                makeMove('7');
            }
            else if (nextMoveLoc.Item1 == this.xPos && nextMoveLoc.Item2 < this.yPos)
            {
                makeMove('8');
            }
            else if (nextMoveLoc.Item1 > this.xPos && nextMoveLoc.Item2 < this.yPos)
            {
                makeMove('9');
            }
            else
            {
                Logger.log(String.Format(@"Error getting move for cpu unit {0}", this.id), "Error");
            }
        }