So I'm building this RPG turn based game for a class. I'm not trying to get my homework done for me, but this one problem is REALLY spinning me out, I've been at this for like 3 hours and I can't figure out how to fix this problem.
Essentially, I have an update method that controls whos turn it is (either the player or the computer)
public bool Update()
{
// clears console for a fresh start
Console.Clear();
//branch who's turn it is and add a line indicating so
if (playerTurn)
{
//print that it's the player's turn
Console.WriteLine("It's Your Turn to Attack!");
//run the player turn
PlayerTurn();
}
else
{
//print the rivals turn label
Console.WriteLine("It's the Enemy's Turn to Attack!");
}
//run the rivals turn
RivalsTurn();
{
}
//end game check
return EndTurn();
}
Then, I have the player turn method ---
void PlayerTurn()
{
// print instructions to select an attacker
Console.WriteLine($"Select which character will attack!\n(1) To Select {playerArray[0].Name()}\n(2) To Select {playerArray[1].Name()}\n(3) To Select {playerArray[2].Name()}\n(4) to view your teams current status\n(5) To Heal An Ally");
// Loop until an attacker is chosen
while (Attacker == null)
{
// use num 1-3 to select player party member that is the attacker
ConsoleKeyInfo k = Console.ReadKey();
if (k.KeyChar == '1')
{
Attacker = playerArray[0];
Console.WriteLine($"\nYou've chosen to attack with {Attacker.Name()}");
}
else if (k.KeyChar == '2')
{
Attacker = playerArray[1];
Console.WriteLine($"You've chosen to attack with {Attacker.Name()}");
}
else if (k.KeyChar == '3')
{
Attacker = playerArray[2];
Console.WriteLine($"You've chosen to attack with {Attacker.Name()}");
}
else if(k.KeyChar == '4')
{
PrintParties();
continue;
} else if (k.KeyChar == '5') {
HealAlly();
break;
}
// start a new line after user input
Console.WriteLine();
// Data Validation: make sure the key typed is a valid key
if (k.KeyChar < '1' || k.KeyChar > '5')
{
Console.WriteLine("Please Enter (1), (2), or (3) to select your attacking character. Press (4) to view your stats, and (5) to Heal an Ally");
continue;
}
else // convert from key input (1-3) to array element space (0-2)
curSelection = int.Parse(k.KeyChar.ToString()) - 1;
{
}
if (Attacker.GetHP() <= 0)
{
//check to make sure the selected character is alive HP > 0
//print the attackers name
//character's dead choose again
Console.WriteLine($"{Attacker.Name()} is dead! Choose someone who is alive!");
Console.WriteLine("Please Enter (1), (2), or (3) to select your attacking character. Press (4) to view your stats, and (5) to Heal an Ally");
Attacker = null;
}
else
{
Console.WriteLine($"{Attacker.Name()} will attack!");
Thread.Sleep(1000);
}
}
//print instructions for choosing a rival.
Console.WriteLine($"Select which enemy to attack!\n(1) To Attack {enemyArray[0].Name()}\n(2) To Attack {enemyArray[1].Name()}\n(3) To Attack {enemyArray[2].Name()}\n(4) To see the Enemies Current Stats.");
//loop until a defender is choosen
while (Defender == null)
{
// use 1-3 to select player party member that is the attacker
ConsoleKeyInfo k = Console.ReadKey();
if (k.KeyChar == '1')
{
Defender = enemyArray[0];
Console.WriteLine($"\nYou will attack {Defender.Name()}");
} else if(k.KeyChar == '2') {
Defender = enemyArray[1];
Console.WriteLine($"\nYou will attack {Defender.Name()}");
}
else if (k.KeyChar == '3')
{
Defender = enemyArray[2];
Console.WriteLine($"\nYou will attack {Defender.Name()}");
} else if (k.KeyChar == '4')
{
playerTurn = false;
PrintParties();
playerTurn = true;
}
{ }
//add a new line aft er the user input
Console.WriteLine();
// Data Validation: make sure the key typed is a valid key
if (k.KeyChar < '1' || k.KeyChar > '3')
{
// repeat instructions
Console.WriteLine("Select an enemy to attack by pressing either 1, 2, or 3.\n");
// loop again
continue;
}
else // convert from key input (1-3) to array element space (0-2)
curSelection = int.Parse(k.KeyChar.ToString()) - 1; //minus one to use as index
//check to make sure the selected character is alive HP > 0
//print the defenders name
//assign the selected character as the defender
if (Defender.GetHP() <= 0)
{
//print instructions again
Console.WriteLine($"{Defender.Name()} is already dead! Pick another enemy!");
Console.WriteLine("Select an enemy to attack by pressing either 1, 2, or 3.\n");
Defender = null;
} else
{
Console.WriteLine($"{Attacker.Name()} attacks {Defender.Name()}!");
Thread.Sleep(2000);
}
}
//damage the defender by the attacker's Strength value
Defender.ApplyDamage(Attacker.GetStrength());
//change color for rival team
Console.BackgroundColor = Attacker.GetTeamColor();
//print the new rival's health
Console.WriteLine($"{Defender.Name()} was hit by {Attacker.Name()}! {Defender.Name()} now only has {Defender.GetHP()} HP!");
EndTurn();
//change color back for normal
Console.BackgroundColor = ConsoleColor.Black;
//pause for 2 seconds
Thread.Sleep(2000);
//reset attacker/defender for next attack
Attacker = null;
Defender = null;
}
What I am trying to do is when the player selects "5" and runs the HealAlly() Method, I want the program to Take the users next input (who to heal), tell the player whether or not they can heal (if the character is either dead or full health) and then switch turns.
Instead, the code jumps back to the middle of the PlayerTurn method and tries to execute from there, but since the Attacker was never set (the user hit 5 so they didn't pick an Attacker) the game will crash.
Here is the HealAlly() method:
void HealAlly()
{
float healing = 3;
while (Healee == null)
{
Console.WriteLine("\nWhich Ally Do You Want To Heal?");
Console.WriteLine($"(1) for {playerArray[0].Name()}\n(2) for {playerArray[1].Name()}\n(3) for {playerArray[2].Name()}");
// use 1-3 to select player party member that is being healed
ConsoleKeyInfo k = Console.ReadKey();
if (k.KeyChar == '1')
{
Healee = playerArray[0];
Console.WriteLine($"\nYou will heal {Healee.Name()}");
Console.WriteLine("Healing...");
}
else if (k.KeyChar == '2')
{
Healee = playerArray[1];
Console.WriteLine($"\nYou will heal {Healee.Name()}");
Console.WriteLine("Healing...");
}
else if (k.KeyChar == '3')
{
Healee = playerArray[2];
Console.WriteLine($"\nYou will heal {Healee.Name()}");
Thread.Sleep(1000);
Console.WriteLine("Healing...");
}
else if (k.KeyChar > 3 || k.KeyChar < 1)
{
Console.WriteLine("Choose which character you want to heal!");
continue;
}
if (Healee.GetHP() >= 12)
{
Thread.Sleep(1000);
Console.WriteLine($"{Healee.Name()} has full health! You can't heal them!");
EndTurn();
}
else if (Healee.GetHP() <= 0)
{
Thread.Sleep(1000);
Console.WriteLine($"{Healee.Name()} is dead! You can't heal them!");
EndTurn();
}
else
{
Healee.ApplyHealing(healing);
Thread.Sleep(2000);
Console.WriteLine($"{Healee.Name()} has been healed, and now has {Healee.GetHP()} HP!");
EndTurn();
}
I thought that adding the call to EndTurn() at the end of the HealAlly() method would cause the turn to end, but instead all it does is give me the delay message and then shoot back to the middle of the PlayerTurn() method. I'm sure it's because there was no Attacker set, but that's not what I want, because I don't want the player to be able to attack after healing.
Here is the EndTurn() method if that will help
bool EndTurn()
{
//switch turns for next loop
playerTurn = !playerTurn;
// loop through players to see if they're alive and store in a variable counting if they're alive or not
bool playersAlive = true;
for (int i = 0; i < playerArray.Length; i++)
{
if (playerArray[0].GetHP() <= 0 && playerArray[1].GetHP() <= 0 && playerArray[2].GetHP() <= 0)
{
playersAlive = false;
}
}
// same for rivals
bool rivalsAlive = true;
for (int i = 0; i < enemyArray.Length; i++)
{
if (enemyArray[0].GetHP() <= 0 && enemyArray[1].GetHP() <= 0 && enemyArray[2].GetHP() <= 0)
{
rivalsAlive = false;
}
}
// if both have things alive start the next round, pause the game, and return true to continue playing
if (playersAlive && rivalsAlive)
{
Console.WriteLine("Next Round Starts in 5 seconds");
Thread.Sleep(5000);
return true;
} // if only the players have members alive you win
else if (playersAlive)
{
//clear screen for results
Console.Clear();
//print you've won and parties
Console.WriteLine("Congrats you win! Final Standings:");
playerTurn = false;
PrintParties();
playerTurn = true;
PrintParties();
Thread.Sleep(5000);
Console.WriteLine("Thanks For Playing!");
Console.WriteLine("Press Any Key to Play Again");
Console.ReadKey();
Console.Clear();
Init();
return false;
} // only rival members are alive
else
{
//clear screen for results
Console.Clear();
//Print you've lost and parties
Console.WriteLine("You Lose :( Final Standings:");
playerTurn = false;
PrintParties();
playerTurn = true;
PrintParties();
Thread.Sleep(5000);
Console.WriteLine("Thanks For Playing!");
Console.WriteLine("Press Any Key to Play Again");
Console.ReadKey();
Console.Clear();
Init();
return false;
}
}
This is bugging me, can anyone help?
void PlayerTurn()
{
// ...
while (Attacker == null)
{
// ...
if (k.KeyChar == '5') {
HealAlly();
break;
}
}
// ...code that depends on Attacker...
}
The break statement here only causes the flow to break out of the while loop and continue with the code the depends on Attacker. If you want to leave the PlayerTurn() method altogether, replace the break; with a return;
Related
I'm trying to create my own version of "Fleet battle", but I'm stuck at 99.9%. And here is why. After a ship was destroyed, its color should change to red and then a MessageBox "You won" or "You lost" should come. So now, if the AI destroys my last ship, everything is going fine, all the ships are becoming red one by one and then comes this "You lost" message. But in the opposite case - if I win, the AI ships are becoming red, but the last cell of the last ship becomes red after I click "OK" at the MessageBox "You won". I'm using the same algorithm in both cases and wondering what I'm doing wrong with it. Here is the AI part:
// AI hits a ship
if ((arrayOccupiedCellsBattlefield1[rowIndex, columnIndex] == "1") || (arrayOccupiedCellsBattlefield1[rowIndex, columnIndex] == "2") ||
(arrayOccupiedCellsBattlefield1[rowIndex, columnIndex] == "3") || (arrayOccupiedCellsBattlefield1[rowIndex, columnIndex] == "4"))
{
AIHuntingAShip = true;
dataGridView1.Rows[rowIndex].Cells[columnIndex].Value = " X";
arrayShipsHitbyAIMap[rowIndex, columnIndex] = " X"; // AI marks the burning enemy ships
dataGridView1.Rows[rowIndex].Cells[columnIndex].Style.BackColor = Color.Red;
arrayAITakenShots[transformCoordinatesToCellNumber(rowIndex, columnIndex)] = transformCoordinatesToCellNumber(rowIndex, columnIndex);
storeRowIndex = rowIndex;
storeColumnIndex = columnIndex;
arrayShipsHitbyAIXY[detectedShipLength] = transformCoordinatesToCellNumber(rowIndex, columnIndex);
detectedShipLength++;
if (detectedShipLength == int.Parse(arrayOccupiedCellsBattlefield1[rowIndex, columnIndex])) // AI destroys a ship (the array stores the length of the ship in each of the ship's cells)
{
for (int i = 0; i < detectedShipLength; i++) // placing " o"s around the destroyed ship
{
restrictArea(int.Parse(tranformCellNumberToCoordinates(arrayShipsHitbyAIXY[i])[0].ToString()),
int.Parse(tranformCellNumberToCoordinates(arrayShipsHitbyAIXY[i])[1].ToString()),
dataGridView1,
arrayShipsHitbyAIMap,
arrayAITakenShots);
}
detectedShipLength = 0;
AIHuntingAShip = false;
playerShips--;
textBox6.Text = playerShips.ToString();
if (playerShips == 0)
{
MessageBox.Show("You lost!", "Try it again!", MessageBoxButtons.OK);
dataGridView2.Enabled = false;
}
}
and here is my part:
// player hits a ship
if ((arrayOccupiedCellsBattlefield2[e.RowIndex, e.ColumnIndex] != null) && (arrayOccupiedCellsBattlefield2[e.RowIndex, e.ColumnIndex] != " ."))
{
dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = " X";
arrayShipsHitbyPlayerMap[e.RowIndex, e.ColumnIndex] = " X";
dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor = Color.Red;
arrayPlayerTakenShots[transformCoordinatesToCellNumber(e.RowIndex, e.ColumnIndex)] = transformCoordinatesToCellNumber(e.RowIndex, e.ColumnIndex);
int shipIdent = int.Parse(arrayOccupiedCellsBattlefield2[e.RowIndex, e.ColumnIndex][0].ToString()); // ship ID
int playersShipLength = int.Parse(arrayOccupiedCellsBattlefield2[e.RowIndex, e.ColumnIndex][1].ToString()); // ship length
arrayForPlayersShipsXY[shipIdent] += e.RowIndex.ToString() + e.ColumnIndex.ToString() + ";"; // save the coordinates of this particular ship
arrayForPlayerCounters[shipIdent]++; // increase this particular cell counter basing on the "shipIdent"
if (arrayForPlayerCounters[shipIdent] == playersShipLength) // ship destroyed, so mark the area with " o"
{
for (int i = 0; i < playersShipLength; i++)
{
string xy = extractXYForPlayersShip(arrayForPlayersShipsXY[shipIdent])[i].ToString();
int x = int.Parse(xy.ToString()[0].ToString());
int y = int.Parse(xy.ToString()[1].ToString());
restrictArea(x, y, dataGridView2, arrayShipsHitbyPlayerMap, arrayPlayerTakenShots);
}
AIShips--;
textBox7.Text = AIShips.ToString();
}
if (AIShips == 0)
{
dataGridView2.Enabled = false;
MessageBox.Show("You won!", "Congrats!", MessageBoxButtons.OK);
}
}
Does anybody have an idea? Thank you for your help.
The algorithms might seem the same, but they are not. There is a lot of repeated code, and it is hard to make sure they both do the "same thing". Try and break out parts of this code into smaller methods so you can reuse them.
Without seeing everything, I think this is the issue:
AI:
if (...) //Ship is destroyed
{
// ...
if (ships == 0)
{
MessageBox.Show(...);
}
}
Player
if (...) //ship is destroyed
{
// ...
}
if (ships == 0)
{
MessageBox.Show(...);
}
MessageBox.Show is called inside vs. outside the "ship destroyed" if block.
This question is not exactly the best for StackOverflow because the code is fairly messy and not simplified. Check out this link for how to ask good questions. But keep at it!
As you said both algorithms are equal but windows refresh your form after you hit the 'OK' button.
So I suggest refresh it yourself before displaying msgbox.
if (AIShips == 0)
{
dataGridView2.Refresh(); // <-------- +
dataGridView2.Enabled = false;
MessageBox.Show("You won!", "Congrats!", MessageBoxButtons.OK);
}
Your code is a bit messy I'm not sure about the grid view name, If your problem is about the first one, try this: dataGridView1.Refresh();
To give some context for the code, I'm modifying the game "AssaultCube".
So this is a console program. When it launches, you can type stuff in and if you type in "1", it'll start setting the health value to 999 in a loop. However, you can't type more stuff in because the loop isn't over, but in order to end the loop, I need to be able to type "1" to toggle it off. I want to be able to toggle this on and off each time I type in "1". It seems like a simple problem and I've been trying to get this to work for hours with no luck and my brain is fried. Thanks in advance and sorry if I was unclear in my explanation, I'm not good at those :D.
while (true)
{
string Select;
Select = Console.ReadLine();
if (Select == "1") //If the number "1" is typed, do stuff
{
int finalHealth = localPLayer + health; //Add the Base and Health addresses together
if (healthToggle == false)
{
healthToggle = true;
Console.WriteLine("\n[1] Unlimited Health activated\n");
while (healthToggle) //While Health Toggle is TRUE, do stuff
{
vam.WriteInt32((IntPtr)finalHealth, 999); //Set finalHealth to 999 in a loop, making you invincible
Thread.Sleep(100); //Let CPU rest
}
}
else
{
healthToggle = false;
Console.WriteLine("\n[1] Unlimited Health deactivated\n");
vam.WriteInt32((IntPtr)finalHealth, 100); //Set health value back to normal
}
}
Thread.Sleep(100);
}
I agree with 41686d6564, Console.KeyAvailable and Console.ReadKey() are definitely the way to go.
Try this out...
static void Main(string[] args)
{
bool quit = false;
while (!quit)
{
Console.WriteLine("Press Esc to quit, or 1 to start/stop.");
while (!Console.KeyAvailable)
{
System.Threading.Thread.Sleep(100);
}
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Escape)
{
quit = true;
}
else if (cki.Key == ConsoleKey.D1)
{
Console.WriteLine("\n[1] Unlimited Health activated\n");
bool godMode = true;
while (godMode)
{
// ... do something ...
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.ffff") + ": ...something ...");
System.Threading.Thread.Sleep(100);
if (Console.KeyAvailable)
{
cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.D1)
{
godMode = false;
}
}
}
Console.WriteLine("\n[1] Unlimited Health deactivated\n");
}
}
Console.WriteLine("Goodbye!");
Console.Write("Press Enter to Quit");
Console.ReadLine();
}
So I made a "game" in a c# console app project which is basically a hero who must kill a monster and both the hero and the monster have HP and a random amount of damage given and taken (as it should be).
I have a couple tiny problems which don't make sense to me. There is a Regeneration Potion that I have added to the game which obviously adds a random amount of hp between 10-30 to the player.
//Regenerating or not
if (isRegen == false) //if it doesn't
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You've failed to regenerate!");
Console.WriteLine("Enemy: " + monster.name);
Console.WriteLine("Enemy's hp: " + monster.hp);
GetDamage();
}
else //if it does
{
if (myHP + regenAmount > 100 || myHP == 100)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You can't regenerate above 100 hp.");
Game();
}
else
{
if (potionCounter == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You are out of health potions. Cannot regenerate!");
Game();
}
potionCounter--;
myHP += regenAmount;
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("You consumed a Health Potion!");
Console.WriteLine("Your hp was regenerated successfully! HP is raised by " + regenAmount);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Enemy: " + monster.name);
Game();
}
}
Now, as you can see in the code above, I have made an if statement that checks whether the sum of myHP and the regenAmount is higher than 100, a message that says "You can't regenerate above 100 hp" is displayed. Thing is, when I try this, sometimes it does display the message
as presented here,
but eventually it decides to display "You've failed to regenerate!" in here and well the game just goes on and the player gets hit. (which obviously shouldn't happen).
There is even another similar problem with the potionCounter. As presented in the code:
if (potionCounter == 0)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("You are out of health potions. Cannot regenerate!");
Game();
}
Whenever the potion counter reaches 0, it should display a message that says that the user is out of potions. It happens and works, but similarly to the previous problem, it sometimes ignore the if statement and allows the user to either heal and continue on decrementing the potionCounter variable or fail to regenerate and get hit by the monster.
I know some of this code is kinda bad and well that's because im kinda new to programming but im trying my best to develop myself and learn as much as I can, which is why I decided to share it here.
Thank you for reading it and I hope you find the solution :)
Edit: The code for Game():
static void Game()
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(monster.name + "'s hp: " + monster.hp);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Your hp: " + myHP + "\n");
Console.ForegroundColor = ConsoleColor.White;
while(monster.hp > 0 && myHP > 0) //Running until the enemy or the player dies.
{
Console.Write("A - ATTACK (Give damage but always get hit)\nD - DEFEND (50% chance of not getting hit)\nR - REGENERATE (Regenerates HP, but if it fails you get hit) - ");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(potionCounter + " POTIONS LEFT\n");
string input = Console.ReadLine();
//ATTACK
if (input == "a" || input == "A")
{
Console.Clear();
Attack();
}
else if (input == "d" || input == "D")
{
Console.Clear();
Defend();
}
else if (input == "r" || input == "R")
{
Console.Clear();
Regenerate();
}
else
{
Console.Clear();
Game();
}
}
}
This is how I'd structure a simple game like this.
You already seem to have a "Monster" class – good! – but it's elided from this example for simplicity. Similarly, you would probably encapsulate Player as a class to keep track of each player's potions and HP individually.
Either way, the idea is that there's
an infinite loop governed by the "game over" flag
a function to run a single turn – checking whether the game is over, printing the game state, waiting for input, processing it, running AI actions (which, given our monster is a simple ogre of some ilk, is just it bashing your head in)
separate functions for the different events that occur in the game
... and a helper function that lets you change the console color and print a line of text in a single invocation ;-)
I hope this helps! It's not exactly a laser-focused pin-point answer, I know...
class Game
{
private int playerHp = 100;
private int monsterHp = 100;
private int nPotions = 3;
private readonly Random random = new Random();
private bool gameOver = false;
static void ColorPrint(string message, ConsoleColor color)
{
Console.ForegroundColor = color;
Console.WriteLine(message);
}
public void Play()
{
while (!gameOver)
{
RunTurn();
}
}
void RunTurn()
{
CheckGameOver();
if (gameOver) {
return;
}
ColorPrint(String.Format("Monster HP: {0}", monsterHp), ConsoleColor.Red);
ColorPrint(String.Format(" Your HP: {0}", playerHp), ConsoleColor.Green);
ColorPrint(String.Format(" Potions: {0}", nPotions), ConsoleColor.Yellow);
ColorPrint("(A)ttack / (P)otion / anything else to skip your turn?", ConsoleColor.White);
var command = Console.ReadLine().Trim().ToLower();
switch (command)
{
case "a":
DoPlayerAttack();
break;
case "p":
DoPlayerPotion();
break;
default:
ColorPrint("You decide to loiter around.", ConsoleColor.White);
break;
}
DoEnemyAttack();
}
void CheckGameOver() {
if (monsterHp <= 0)
{
gameOver = true;
ColorPrint("The monster is slain!", ConsoleColor.White);
}
if (playerHp <= 0)
{
gameOver = true;
ColorPrint("You are dead. :(", ConsoleColor.White);
}
}
void DoPlayerAttack()
{
var damage = random.Next(1, 10);
ColorPrint(String.Format("You strike the monster for {0} damage.", damage), ConsoleColor.White);
monsterHp -= damage;
}
void DoPlayerPotion()
{
if (nPotions > 0)
{
var heal = random.NextDouble() < 0.7 ? random.Next(5, 15) : 0;
if (heal > 0)
{
ColorPrint(String.Format("You heal for {0} HP.", heal), ConsoleColor.Gray);
}
else
{
ColorPrint("That potion was a dud.", ConsoleColor.Gray);
}
playerHp = Math.Min(100, playerHp + heal);
nPotions--;
}
else
{
ColorPrint("You rummage through your belongings to find no potions.", ConsoleColor.Gray);
}
}
void DoEnemyAttack()
{
var damage = random.Next(1, 10);
ColorPrint(String.Format("The monster nibbles on you for {0} damage.", damage), ConsoleColor.White);
playerHp -= damage;
}
}
Just focusing on this bit of your code for a moment:
if (potionCounter == 0)
{
// ...
}
potionCounter--;
myHP += regenAmount;
Think about what will happen when the potionCounter is zero:
- It will then set potionCounter to -1
- It will give you the HP
You probably meant to put this code inside an else statement.
Additionally, checking if (potionCounter <= 0) would be more robust than testing for zero exactly.
In the beginning i will tell that I'm new with C# and Programming. Trying to learn as much as i can alone.
My first learning project is simple. Moving trough array 10x10 by using "W, S, A, D". It is "game" which is letting user to move his character trough array. The problem is that i want "my character" to move as long as his position will be out of range. So i think i'm trying to make infitive loop which is printing position and lets moving forward. If i'm using for example do{}while(true), position is changing all the time after pressing one key. I'm looking for suggestion how should i construct loop for this kind of "movement".
I'm attaching my code which is responsible for moving character.
int axisx = 0;
int axisy = 0;
var movement = Console.ReadKey();
int[,] level = new int[10, 10];
if (movement.Key.ToString() == "W"){axisy = axisy+1;}
else if (movement.Key.ToString() == "S"){axisy = axisy-1;}
else if (movement.Key.ToString() == "D"){axisx = axisx+1;}
else if (movement.Key.ToString() == "A"){axisx = axisx-1;}
Console.WriteLine("{2} is on position:{0},{1}", axisx, axisy, Player1.getBuilderName());
if (movement.Key == ConsoleKey.Escape)
{
Console.WriteLine("You pressed Escape, Goodbye");
}
You can use a while(true) loop:
int axisx = 0;
int axisy = 0;
while (true)
{
var movement = Console.ReadKey();
if (movement.KeyChar == 'W' || movement.KeyChar == 'w')
{
axisy += 1;
}
else if (movement.KeyChar == 'S' || movement.KeyChar == 's')
{
axisy -= 1;
}
else if (movement.KeyChar == 'D' || movement.KeyChar == 'd')
{
axisx += 1;
}
else if (movement.KeyChar == 'A' || movement.KeyChar == 'a')
{
axisx -= 1;
}
Console.WriteLine("{2} is on position:{0},{1}", axisx, axisy, Player1.getBuilderName());
if (movement.Key == ConsoleKey.Escape)
{
Console.WriteLine("You pressed Escape, Goodbye");
break;
}
}
The break; in the ConsoleKey.Escape block will get you out of the loop.
You can also use movement.KeyChar instead of movement.Key.ToString(), strings are immutable in .NET, so every time you do a ToString() you are creating a new string.
I am creating a C# console application that will be performing an infinite process. How can I get the application to "pause" when the user presses the escape key?
Once the user presses the escape key I want the option to either exit the application or continue the loop right where it left off. I don't want any discontinuity in the process. If I press Esc at step 100 I should be able to pick right back up at step 101.
Here is my method so far:
// Runs the infinite loop application
public static void runLoop()
{
int count = 0;
while (Console.ReadKey().Key!= ConsoleKey.Escape)
{
WriteToConsole("Doing stuff.... Loop#" + count.ToString());
for (int step = 0; step <= int.MaxValue; step++ ) {
WriteToConsole("Performing step #" + step.ToString());
if (step == int.MaxValue)
{
step = 0; // Re-set the loop counter
}
}
count++;
}
WriteToConsole("Do you want to exit? y/n");
exitApplication(ReadFromConsole());
}
Is there any way to check for the user input key in a separate thread then pause the infinite loop when the other thread sees an Esc key-press?
To find out if there is a key available in the loop, you can do this:
while (someLoopCondition)
{
//Do lots of work here
if (Console.KeyAvailable)
{
var consoleKey = Console.ReadKey(true); //true keeps the key from
//being displayed in the console
if (consoleKey.Key == ConsoleKey.Escape)
{
//Pause here, ask a question, whatever.
}
}
}
Console.KeyAvailable returns true if there is a key in the input stream ready to read and it is a non-blocking call so it won't pause to wait for input. You can check if the escape key was pressed and pause or do whatever you want if the condition is true.
Ron, big thank you for your answer. Using Console.KeyAvalible was key to finding the exact solution to my issue. Here is what I did to produce the results I was expecting. I needed to add in a few methods to check if the user wants to break the current operation or start a new loop from the beginning.
public static void runUpdater()
{
int count = 0;
while (true)
{
WriteToConsole("Doing stuff.... Loop# " + count.ToString());
for (int step = 0; step <= int.MaxValue; step++)
{
if (!breakCurrentOperation())
{
WriteToConsole("Performing step #" + step.ToString());
if (step == int.MaxValue)
{
step = 0; // Re-set the loop counter
}
}
else
{
break;
}
}
count++;
if (!startNewOperation())
{
// Noop
}
else
{
break;
}
}
WriteToConsole("\nAre you ready to run the database updater again? y/n");
startApplication(ReadFromConsole());
}
public static bool startNewOperation()
{
WriteToConsole("Do you want go back to the main menu or start a new update process? \nType y to start a new update process or n to go to the main menu.");
string input = ReadFromConsole();
if (input == "y" || input == "Y")
{
return false;
}
else if (input == "n" || input == "N")
{
return true; // Noop - Restart the Loop from the begining
}
else
{
WriteToConsole("Error: Input was not recognized. ");
return startNewOperation(); // Recursivly call method untill user enters a logical input
}
}
public static bool breakCurrentOperation()
{
if (Console.KeyAvailable)
{
var consoleKey = Console.ReadKey(true);
if (consoleKey.Key == ConsoleKey.Escape)
{
WriteToConsole("Do you want to stop the current process? \nType s to stop or c to continue.");
string input = Console.ReadLine();
if (input == "c" || input == "C")
{
return false; // Continue
}
else if (input == "s" || input == "S")
{
return true; // Break the loop
}
else
{
WriteToConsole("Error: Input was not recognized, the current process will now continue. Press Esc to stop the operation.");
}
}
}
return false;
}
Here are the results: