Resolved help. Double keystrokes and broken selection

SwankyJank

New member
Joined
Jan 7, 2022
Messages
4
Programming Experience
Beginner
hi guys im having a bit of trouble with this chunk of code that ive been working for the last 20 minutes.
my problem is that when i type "a" for my attack strats, it want me to press enter again afterwards but with defence strats its fine, also if i select either attack or defence strats it gives me the opposite. i also have a version of this code where attack and defence have their own methods and that gave me the same results. just need some help with this one :) thanks in advance
C#:
        public static void selscreen()
        {
            Console.Clear();
            Console.WriteLine("Defending or Attacking?");
            Console.WriteLine("Type d or a and press Enter.");
            if (Console.ReadLine() != "a")
            {
                Console.Clear();
                string[] atstrats = new string[] { "as1", "as2", "as3" };
                Random rndat = new Random();
                int indexat = rndat.Next(atstrats.Length);
                Console.WriteLine($"The Strat: {atstrats[indexat]}");
                Console.WriteLine("Press Enter to pick side");
                Console.ReadLine();
                selscreen();
            }
            else _= Console.ReadLine() != "d";
            {
                Console.Clear();
                string[] defstrats = new string[] { "ds1", "ds2", "ds3" };
                Random rnddef = new Random();
                int indexdef = rnddef.Next(defstrats.Length);
                Console.WriteLine($"The Strat: {defstrats[indexdef]}");
                Console.WriteLine("Press Enter to pick side");
                Console.ReadLine();
                selscreen();
            }
            Console.ReadLine();

        }
 
Last edited:
NOTE: the website changes the spacing of my "{ }" brackets when i type it but they are correct in vs
That's because you didn't put your code in code tags. I did that for you. Next time, you can use the </> button on the toolbar.
 
The reason you are seeing that behavior is because this code:
C#:
if (Console.ReadLine() != "a")
{
    Console.Clear();
    string[] atstrats = new string[] { "as1", "as2", "as3" };
    Random rndat = new Random();
    int indexat = rndat.Next(atstrats.Length);
    Console.WriteLine($"The Strat: {atstrats[indexat]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}
else _= Console.ReadLine() != "d";
{
    Console.Clear();
    string[] defstrats = new string[] { "ds1", "ds2", "ds3" };
    Random rnddef = new Random();
    int indexdef = rnddef.Next(defstrats.Length);
    Console.WriteLine($"The Strat: {defstrats[indexdef]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}

is actually seen by the compiler as:
C#:
if (Console.ReadLine() != "a")
{
    Console.Clear();
    string[] atstrats = new string[] { "as1", "as2", "as3" };
    Random rndat = new Random();
    int indexat = rndat.Next(atstrats.Length);
    Console.WriteLine($"The Strat: {atstrats[indexat]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}
else
{
    _= Console.ReadLine() != "d";
}

{
    Console.Clear();
    string[] defstrats = new string[] { "ds1", "ds2", "ds3" };
    Random rnddef = new Random();
    int indexdef = rnddef.Next(defstrats.Length);
    Console.WriteLine($"The Strat: {defstrats[indexdef]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}

I doubt that is your intended result.
 
The reason you are seeing that behavior is because this code:
C#:
if (Console.ReadLine() != "a")
{
    Console.Clear();
    string[] atstrats = new string[] { "as1", "as2", "as3" };
    Random rndat = new Random();
    int indexat = rndat.Next(atstrats.Length);
    Console.WriteLine($"The Strat: {atstrats[indexat]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}
else _= Console.ReadLine() != "d";
{
    Console.Clear();
    string[] defstrats = new string[] { "ds1", "ds2", "ds3" };
    Random rnddef = new Random();
    int indexdef = rnddef.Next(defstrats.Length);
    Console.WriteLine($"The Strat: {defstrats[indexdef]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}

is actually seen by the compiler as:
C#:
if (Console.ReadLine() != "a")
{
    Console.Clear();
    string[] atstrats = new string[] { "as1", "as2", "as3" };
    Random rndat = new Random();
    int indexat = rndat.Next(atstrats.Length);
    Console.WriteLine($"The Strat: {atstrats[indexat]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}
else
{
    _= Console.ReadLine() != "d";
}

{
    Console.Clear();
    string[] defstrats = new string[] { "ds1", "ds2", "ds3" };
    Random rnddef = new Random();
    int indexdef = rnddef.Next(defstrats.Length);
    Console.WriteLine($"The Strat: {defstrats[indexdef]}");
    Console.WriteLine("Press Enter to pick side");
    Console.ReadLine();
    selscreen();
}

I doubt that is your intended result.
I did see the difference and i did 2 if statements instead and also 2 if statements to select methods as well and in both cases i get the same behaviour, would it work properly if i separated them into classes?
 
A better approach would be this pseudo-code:
C#:
var random = new Random();
var atstrats = new string[] { "as1", "as2", "as3" };
var defstrats = new string[] { "ds1", "ds2", "ds3" };

bool quit = false;
do
{
    Console.Write("Enter 'attack', 'defend' or 'quit': ");
    var input = Console.ReadLine();
    string[] arrayChoice = null;
    switch(input)
    {
    case "attack":
        arrayChoice = atstrats;
        break;

    case "defense":
        arrayChoice = defstrats;
        break;

    case "quit";
        quit = true;
        break;

    default:
        Console.WriteLine("Invalid choice. Try again.");
        break;
    }

    if (arrayChoice != null)
    {
        int indexdef = rnddef.Next(arrayChoice.Length);
        Console.WriteLine($"The Strat: {defstrats[indexdef]}");
    }
} while (!quit);

This fixes several things:
  • Random should only be instantiated once.
  • User's choice is only requested once per iteration of the loop.
  • Duplicate code is eliminated. The only thing that varied between your choices was the array.
  • Looping is used instead of recursively calling yourself.
 
also if i select either attack or defence strats it gives me the opposite
That's because you were using Console.ReadLine() != "a" instead of Console.ReadLine() == "a".
 
Thanks for the help, i ended up figuring it all out by using == instead , intelli sense just kept telling me it was supposed to be != But i ended up ignoring intelli. Also the extra key bind issue got fixed when i fixed the if statements. Cheers for bearing with me, i only started c# 2 days ago lol
 
Glad you figured it out. Marking this thread as resolved.
 
Back
Top Bottom