C
C#16mo ago
<?>

❔ stuck on a code in a console game

so basically the compiler was STUCK on a code and I got stuck on this so I thought it'd be interesting if I came here some people say the Development Environment is messed up, or try using a new IDE I made this boring console game cause I was bored btw (I use unity most of the time but I got bored this time so yes.)
19 Replies
<?>
<?>OP16mo ago
Program01.cs:
using RandomProgram02;

namespace RandomProgram01
{
public class Program
{
public static void Main(string[] args)
{
System.Console.WriteLine("Press M to Start, Press K to Terminate.");
while (true)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);

if (keyInfo.Key == ConsoleKey.M)
{
Wizard wizard = new Wizard();
wizard.Casts(); // Call Wizard.Casts;
continue;
}
else if (keyInfo.Key == ConsoleKey.K)
{
break;
}else {
continue;
}
}
}
}
}
using RandomProgram02;

namespace RandomProgram01
{
public class Program
{
public static void Main(string[] args)
{
System.Console.WriteLine("Press M to Start, Press K to Terminate.");
while (true)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);

if (keyInfo.Key == ConsoleKey.M)
{
Wizard wizard = new Wizard();
wizard.Casts(); // Call Wizard.Casts;
continue;
}
else if (keyInfo.Key == ConsoleKey.K)
{
break;
}else {
continue;
}
}
}
}
}
Program02.cs:
using RandomProgram01;
using System;
using System.Security.Cryptography.X509Certificates;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.ComponentModel;

namespace RandomProgram02
{
public class Wizard {
public static float EXPERIENCE = 0f;
public void Casts () {
int Cast = 4;
string Wizard01FavouriteSpell = "Chounga Bounga!";
if (Cast == 0) {
System.Console.WriteLine("You Have run Out Of Casts!");

while (true) {
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.X)
{
System.Console.WriteLine("You Have Casted" + Wizard01FavouriteSpell);
Cast--;
}
}
}

if (Cast == 3) {
Wizard.EXPERIENCE++;
if (Cast == 2) {
Wizard.EXPERIENCE++;
if (Cast == 1) {
Wizard.EXPERIENCE++;
}
}
}
}
}
}
using RandomProgram01;
using System;
using System.Security.Cryptography.X509Certificates;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.ComponentModel;

namespace RandomProgram02
{
public class Wizard {
public static float EXPERIENCE = 0f;
public void Casts () {
int Cast = 4;
string Wizard01FavouriteSpell = "Chounga Bounga!";
if (Cast == 0) {
System.Console.WriteLine("You Have run Out Of Casts!");

while (true) {
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.X)
{
System.Console.WriteLine("You Have Casted" + Wizard01FavouriteSpell);
Cast--;
}
}
}

if (Cast == 3) {
Wizard.EXPERIENCE++;
if (Cast == 2) {
Wizard.EXPERIENCE++;
if (Cast == 1) {
Wizard.EXPERIENCE++;
}
}
}
}
}
}
what do y'all think?
SinFluxx
SinFluxx16mo ago
you mean stuck in your infinite while loop?
<?>
<?>OP16mo ago
yes. stuck on this: ConsoleKeyInfo keyInfo = Console.ReadKey(true);
SinFluxx
SinFluxx16mo ago
well it's "stuck" because of this: while (true)
<?>
<?>OP16mo ago
what am I supposed to change it to?
SinFluxx
SinFluxx16mo ago
well a while loop carries on looping until the condition inside the parentheses is false, so if you put "true" in there it will loop infinitely (unless there's a return or break statement etc inside the loop) so what you're supposed to do is change it to a condition that at some point becomes false, which depends on what the purpose of the loop is
<?>
<?>OP16mo ago
well look at the while loop.
SinFluxx
SinFluxx16mo ago
Cast--; makes me think you want it to carry on until they run out of spells casts? But then you have that loop inside the if statement where they have 0 casts left already
<?>
<?>OP16mo ago
yes i'll change that ik I'm just focusing on the while loop
SinFluxx
SinFluxx16mo ago
yeah which is why I wanted you to explain it
<?>
<?>OP16mo ago
itself
SinFluxx
SinFluxx16mo ago
ok so what do you want the while loop to do?
<?>
<?>OP16mo ago
well M is gonna exit out of the loop. and if they press other than K/M it will continue looping.
SinFluxx
SinFluxx16mo ago
ok so you need to change it to while (pressed key != K or M)
<?>
<?>OP16mo ago
omg-
SinFluxx
SinFluxx16mo ago
or keep it as while (true) and inside the loop check what key's been pressed, and if it's K or M, break out of the loop
<?>
<?>OP16mo ago
blud, K is gonna terminate the process while M is gonna execute Program02. no errors / warnings in any of the files.
SinFluxx
SinFluxx16mo ago
why would there be? I'm not called blud either so you'd check for what key's been pressed in your loop, if it's K, return/terminate whatever, if it's M call Program02 (then what?), otherwise do whatever
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server