Search code examples
c#switch-statementdo-while

Passing and changing an object's parameters into a do while loop


What I'm trying to do is simply have the battle end when either the player or monster's HP has reached 0. As it stands the loop continue to run even after the numbers run into the negatives. I can't figure out if it's the loop itself or some issue with the way I'm trying to switch to instantiate a new player class object when the player chooses their class.

Code to create player class:

using System;
using static System.Console;
namespace _350_final_project
{
    public class Story
    {
        public Player choosenClass; //player's chosen class
        Encounter newEncounter = new Encounter();

        public Player ChooseClass(string choice)    //to run and allow player to pick from 3 different classes by instantiate an instance depending on their answer
        {
            switch (choice.ToLower())
            {
                case "warrior":
                    choosenClass = new Player()
                    {
                        maxPlayerHealth = 150,
                        currentPlayerHealth = 150,
                        playerDamage = 35,
                        playerDef = 120,
                        playerStrength = 95,
                        playerAgility = 30,
                        playerExperience = 0
                    };
                    WriteLine("You have chosen the path of the Warrior. Live by the sword. Die by the sword! \n");
                    return choosenClass;
                case "theif":
                    choosenClass = new Player()
                    {
                        maxPlayerHealth = 85,
                        currentPlayerHealth = 85,
                        playerDamage = 27,
                        playerDef = 43,
                        playerStrength = 20,
                        playerAgility = 125,
                        playerExperience = 0
                    };
                    WriteLine("You have chosen to lurk in the shadows as a thief \n");
                    return choosenClass;
                case "gunner":
                    choosenClass = new Player()
                    {
                        maxPlayerHealth = 70,
                        currentPlayerHealth = 70,
                        playerDamage = 40,
                        playerDef = 75,
                        playerStrength = 60,
                        playerAgility = 110,
                        playerExperience = 0
                    };
                    WriteLine("You have chosen the way of the gunner, shoot with your mind not your eye \n");
                    return choosenClass;
                default:
                    return null;
            }
        }

        public void Beginning(string playerName, string playerClass)

        {

            WriteLine("Valiant {0}, It is time to begin your journey", playerName);
            WriteLine("We begin in the hall of heroes, the queen has tasked you with destroying the dragon \n" +
                      " that resides at the tower on the edge of the kingdom. To bein your journey pick a direction in which to go. \n" +
                      "Will you venture north, east or west?");
            string directionDecision = ReadLine();

            WriteLine("You head {0}, steadily making your way toward {1}", directionDecision, Randomize.RandomizeLocation().Name);   //pick a random location
            WriteLine("After several long miles you come across a fork in the road with no signs pointing you to your destination. \n " +
                      "Your map is useless and due to your illpreparaedness supplies are running short. Which direction will you go? \n" +
                      "North, East, or West?:");
            directionDecision = ReadLine();

            WriteLine("Setting your sights to the {0} you soldier on. As you take a look at your surroundings you suddenly " +
                      "notice your not alone", directionDecision);
            newEncounter.StartBattle(playerClass, choosenClass);
        }
    }
}

Code to run a battle:

using System;
using static System.Console;
using System.Threading;
namespace _350_final_project
{
    public class Encounter
    {
        static int encounterCounter = Randomize.NumberGenerator(1, 10) % 2;

        //public static void GenerateEncounter (string playerClass,Player classChoosen) //determine if the player will or will not find a monster upon choosing a direction
        //{
        //    if (encounterCounter == 0)
        //    {
        //        StartBattle(playerClass, classChoosen);
        //    }
        //}

        public void StartBattle(string playerClass, Player choosenClass) //
        {
            Monster currentMonster = Randomize.RandomizeMonster(); //current monster the player has encountered
            string action;
            WriteLine("AMBUSH! A {0} appears to challenge you adventurer!", currentMonster.Name);

            switch (playerClass)
            {
                case "warrior":
                    WriteLine("You quickly draw your greatsword and stand at the ready");
                    action = "You drag your sword in a great arc!";
                    EncounterBattle(currentMonster, choosenClass, action);
                    break;
                case "theif":
                    WriteLine("You swiftly load your gun, eyes glaring over the barrel");
                    action = "You fire your weapon with extreme accuracy!";
                    EncounterBattle(currentMonster, choosenClass, action);
                    break;
                case "gunner":
                    WriteLine("You pull both daggers from your cloak and being to toss them from palm to palm");
                    action = "You dash forward, daggers a silver blur!";
                    EncounterBattle(currentMonster, choosenClass, action);
                    break;
                default:
                    break;
            }

        }

        public void EncounterBattle(Monster monster, Player choosenClass, string action)    //method to run when an encounter starts
        {
            while (monster.CurrentHp > monster.MonsterDamageTaken(choosenClass.playerDamage) || 
                   choosenClass.CurrentPlayerHealth > choosenClass.DamageTaken(monster.MaxDamage)) //while the monster's health or players health is greater than the amount of damage taken continue the battle
            {
                int monsterHitMissCounter = Randomize.NumberGenerator(1, 10) % 2;   //determine if the monster's attack will hit or miss

                WriteLine("{0}{1}", monster.Name, monster.Attack);
                Thread.Sleep(1000);

                if (monsterHitMissCounter != 0)
                {
                    WriteLine("{0}'s attack hits, you take {1} damage", monster.Name, monster.MaxDamage);   //display monster's name and damage taken

                    WriteLine("Your health is now {0}", choosenClass.DamageTaken(monster.MaxDamage));   //display monster's remaiing health
                    ReadKey();
                }

                else
                {
                    Thread.Sleep(2000); //Pause for two seconds
                    WriteLine("\n {0} missed!", monster.Name);
                    ReadKey();
                }
                int playerHitMissCounter = Randomize.NumberGenerator(1, 10) % 2;    //determine if the player's attack will hit or miss
                if (playerHitMissCounter != 0)
                {
                    Thread.Sleep(2000);
                    WriteLine("Your attacked missed!");
                    ReadKey();
                } else
                    
                {
                    Thread.Sleep(1000);
                    WriteLine("You {0}, {1} takes {2} damage", action, monster.Name, choosenClass.playerDamage);    //display the player's class' chosen prep action and amount of damage taken

                    WriteLine("{0}'s current health is {1}", monster.Name, monster.MonsterDamageTaken(choosenClass.playerDamage)); //display player's remaining health
                    ReadKey();
                }
            }
        }
    }
}

Solution

  • The while condition looks wrong. It should loop as long as the monster's and player's current health is greater than 0. Also, you need to decrement their health inside the loop.