То, что я пытаюсь сделать, - это просто завершить битву, когда ХП игрока или монстра достигнет 0. В прежнем состоянии цикл продолжает работать даже после того, как числа сталкиваются с отрицаниями. Я не могу понять, является ли это сам цикл или какая-то проблема со способом, которым я пытаюсь переключиться, чтобы создать экземпляр нового объекта класса игрока, когда игрок выбирает свой класс.
Код для создания класса игрока:
using System;
using static System.Console;
namespace _350_final_project
{
public class Story
{
public Player choosenClass; //player's choosen class
Encounter newEncounter = new Encounter();
public Player ChooseClass(string choice) //to run and allow player to pick from 3 different classes by intantiate 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 choosen 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 choosen to lurk in the shadows as a theif \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 choosen 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 destorying 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 maing 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. Wich 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 surrondings you suddenly " +
"notice your not alone", directionDecision);
newEncounter.StartBattle(playerClass, choosenClass);
}
}
}
Код для запуска битвы:
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 barrell");
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' choosen 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();
}
}
}
}
}