Member Avatar for lithium112

Hello,

Below is a simple gun simulator for the console, but everytime I hit spacebar, I have to hit it twice before the next line will pop up in the console. This started happening after I inserted the last 'if' statement in order to reload the gun. Does anyone know what's going on? Can't seem to figure it out. Thanks for any help!

static void Main(string[] args)
        {
             Console.WriteLine("Press Spacebar to fire the gun. You start with 8 bullets in the mag.\n");
             int i = 8;

            while (Console.ReadKey(true).Key == ConsoleKey.Spacebar)
            {



                    if (i > 0)
                    {
                        i = i - 1;
                        Console.WriteLine("\nYou now have {0} bullets in the barrel.\n", i);
                    }

                    if (i == 0)
                    {
                        Console.WriteLine("\nYou have 0 bullets in your pistol! Hit 'r' to reload!\n");
                        Console.WriteLine("All you hear is a click.\n");
                    }

                    if (Console.ReadKey(true).Key == ConsoleKey.R)
                    {
                        i = 8;
                        Console.WriteLine("You now have 8 bullets in your mag.");
                    }

            }  

        }

Recommended Answers

All 11 Replies

Member Avatar for lithium112

Just to clarify, if I take the below code out, it will work. But I want a way to actually reload that works.

 if (Console.ReadKey(true).Key == ConsoleKey.R)
                    {
                        i = 8;
                        Console.WriteLine("You now have 8 bullets in your mag.");
                    }

The problem is the mutliple readkey() method call, you have to have a storage for you readkey()...
try this

static void Main(string[] args)
        {
            Console.WriteLine("Press Spacebar to fire the gun. You start with 8 bullets in the mag.\n");
            int i = 8;
            while (true)

            {
                ConsoleKeyInfo info = Console.ReadKey();
                if (info.Key==ConsoleKey.Spacebar)
                {
                    i = i - 1;
                    Console.WriteLine("\nYou now have {0} bullets in the barrel.\n", i);
                }
                if (i == 0)
                {
                    Console.WriteLine("\nYou have 0 bullets in your pistol! Hit 'r' to reload!\n");
                    Console.WriteLine("All you hear is a click.\n");
                }
                if (info.Key == ConsoleKey.R)
                {
                    i = 8;
                    Console.WriteLine("You now have 8 bullets in your mag.");
                }
            }
        }
commented: Great Solution +0
Member Avatar for lithium112

Thank you very much! I was able to successfully implement that into my code. I also changed a few things up so that I wouldn't keep getting negative numbers when I got below 0 by nesting an if statement inside of the first if statement. I appreciate the help!

commented: everything's ok +3

There is a bug in that code having to do with the output when you have 1 bullet and hit spacebar.

Member Avatar for lithium112

Here's my full release of the code if you're interested. I'm not having any issues with this: It was a fun little project. I'd be interested in knowing if you're still having issues with the code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GunSimulation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press Spacebar to fire the gun. You start with 8 bullets in the mag.\n");
            int i = 8;
            while (true)
            {
                ConsoleKeyInfo info = Console.ReadKey();
                    if (info.Key==ConsoleKey.Spacebar)
                    {
                        if (i > 0)
                        {
                            i = i - 1;
                            Console.WriteLine("\nYou now have {0} bullets in your mag.\n", i); 
                        }
                    }
                    if (i <= 0)
                    {
                        Console.WriteLine("\nYou have 0 bullets in your pistol! Hit 'r' to reload!\n");
                        Console.WriteLine("All you hear is a click.\n");
                    }
                    if (info.Key==ConsoleKey.R)
                    {
                        i = 8;
                        Console.WriteLine("\nYou now have 8 bullets in your mag.\n");
                    }   
            }     
        }
    }
}

You have the same bug. What is the output when I have 1 bullet left and press the spacebar?

Member Avatar for lithium112

It should be "You have 0 bullets in your pistol! Hit 'r' to reload! All you hear is a click." What is the bug that you are encountering? For some reason it's working perfect for me.

Member Avatar for lithium112

The only thing I can think of at the moment, is that I added a reference to the program environment. I'm using VS2013. Right clicking on the project name I add a reference. The specific reference is System.Windows.Forms. If you didn't add that already, add that and see if that works. I doubt that is it since it'll work even without it, but it's worth a shot.

Well i don't know the exact definition of a bug altough i should know but if he doesn't consider that as a bug then it is not a bug...

@lithium112 --> try this code and your previous code and you will see the difference

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GunSimulation
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press Spacebar to fire the gun. You start with 8 bullets in the mag.\n");
            int i = 8;
            while (true)
            {
                 ConsoleKeyInfo info = Console.ReadKey();
                if (info.Key == ConsoleKey.Spacebar)
                {
                    if (i > 0)
                    {
                        if (i <= 1)
                        {
                            Console.WriteLine("\nYou have 0 bullets in your pistol! Hit 'r' to reload!\n");
                            Console.WriteLine("All you hear is a click.\n");
                        }
                        else
                        {
                            i = i - 1;
                            Console.WriteLine("\nYou now have {0} bullets in your mag.\n", i);
                        }

                    }

                }

                if (info.Key == ConsoleKey.R)
                {
                    i = 8;
                    Console.WriteLine("\nYou now have 8 bullets in your mag.\n");
                }
            }
        }
    }
}

If you have 1 bullet left, why would it say "you have 0 bullets left all you here is a click". If you had one bullet in a gun and pulled the trigger, would it just go "click"?

And the last code has 3 lines that are pretty much identical, they should be taken out and made into 1 line.

Member Avatar for lithium112

Ah, I see what you are saying. I'll have to fix that. Your input is very much appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.