Making a card game for an assignment in school, and I have come accross a problem. In my "Start" method in my static Game class, I initialize some things for the game, where it seems to stop working.
Players[0].MyTurn = true;
for (int i = 0; i < Players.Count; i++) //Initialize decks for all players
{
Game.InitializeDeck(Players[i], Names);
}
for (int i = 0; i < Players.Count; i++) //Flush decks for all players
{
Game.ShuffleDeck(Players[i]);
}
for (int i = 0; i < Players.Count; i++) //Draw 5 cards for all players
{
for (int x = 0; x < 5; x++)
{
Game.DrawCard(Players[i]);
}
}
Thread checkPlayersHealth = new Thread(CheckPlayersHealth);
checkPlayersHealth.Start();
After some debugging, it seems that it gets stuck in the "InitializeDeck" method, that looks like this:
public static void InitializeDeck(Player player, List<string> Names)
{
for (int i = 0; i < 10; i++) //A loop that creates 30 randomly generated cards, with 3 of each card
{
Console.WriteLine("asd");
Random random = Program.random;
int rNameNum1 = random.Next(0, Names.Count); //r for random
int rNameNum2 = random.Next(0, Names.Count);
int rHealth = random.Next(0, 10);
int rPowerNum = random.Next(0, 5);
int rBaseDmg = random.Next(0, 8);
int manaCost = (rHealth + rPowerNum + rBaseDmg) / 2;
for (int x = 0; x < 3; x++)
{
player.Deck.Add(new Card(Names[rNameNum1] + Names[rNameNum2], manaCost, rHealth, rPowerNum, rBaseDmg));
}
}
}
Card constructor:
public Card(string name, int manaCost, int health, int powerNum, int baseDmg)
{
Name = name;
ManaCost = manaCost;
Health = health;
PowerNum = powerNum;
BaseDmg = baseDmg;
int numOfPowers = 4; //Amount of different powers excluding "No power"
if (powerNum <= 0)
{
PowerName = "No power";
}
else if (powerNum == 1)
{
PowerName = "Heal any target";
Power = new Power(false, true, 6, Program.random.Next(1, 4));
}
else if (powerNum == 2)
{
PowerName = "Deal 1-5 Damage to the enemy champion, and heal yourself for 1-5 HP";
Power = new Power(true, true, 5, Program.random.Next(1, 4));
}
else if (powerNum == 3)
{
PowerName = "Sacrifice health from a minion to heal yourself 1-5 HP";
}
else if (powerNum == numOfPowers)
{
PowerName = "Drain HP from yourself to deal damage to a random enemy target";
Power = new Power(true, false, 5, Program.random.Next(1, 4));
}
Thread checkIfDead = new Thread(CheckIfDead);
checkIfDead.Start();
}
It starts with running the loop 3 times, taking around 0.5 seconds each and after the third taking much longer, around 10 seconds for the loop to go through.
Clearly I'm missing something here.
Creating a new Thread with an infinite loop in each for every card (30 cards for each player) wasn't such a good idea >:)
Thread checkIfDead = new Thread(CheckIfDead);
checkIfDead.Start();
public void CheckIfDead()
{
while (true)
{
if (Health <= 0)
{
Dead = true;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.