• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# Bomb Defuse Game

D

Deleted member 613

Guest
C#:
using System;
class MainClass {
 public static void Main (string[] args) {

//Defuse the bomb!


           Console.WriteLine("Choose the correct wire to defuse the bomb!");

           string[] theanswer = { "A", "B", "C", "D" };

           Random Rng = new Random();

           //Creating the wire that defuses the bomb

           int answer2 = Rng.Next(0, 4);

           int answer3;



   
            //Creating the four 'wires' and colouring them.
            Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine("|---|");

            Console.WriteLine("|-1-|");

            Console.WriteLine("|---|");

            Console.ForegroundColor = ConsoleColor.Blue;

            Console.WriteLine("|---|");

            Console.WriteLine("|-2-|");

            Console.WriteLine("|---|");

            Console.ForegroundColor = ConsoleColor.Green;

            Console.WriteLine("|---|");

            Console.WriteLine("|-3-|");

            Console.WriteLine("|---|");

            Console.ForegroundColor = ConsoleColor.Cyan;

            Console.WriteLine("|---|");

            Console.WriteLine("|-4-|");

            Console.WriteLine("|---|");

            Console.ForegroundColor = ConsoleColor.White;

            answer3 = int.Parse(Console.ReadLine());


            if (answer2 == answer3 //If the Users answer equals the correct answer then:)

            {

                Console.WriteLine("Congrats, you have defused the bomb!");

            }

            else //Otherwise this code segment is run:

            {


                Console.WriteLine("You could not defuse the bomb in time!");

             }

             Console.ReadLine();


}
}
 
Last edited by a moderator:
D

Deleted member 613

Guest
@Master Yoda The game randomly chooses one of the wires as the one which will defuse the bomb. I then printed the 'wires' and coloured them using the 'Console.ForegroundColor = ConsoleColor.White'command. I then asked the user to enter in which wire they think will defuse the bomb. The Console.ReadLine(); command stores that answer. Then in the if statement, it checks if the userInput = the actual answer. If it is, it says 'Well done!' If it's wrong, it says 'You couldn't defuse the bomb in time :(.'
 

Mathematical

Active Coder
GameZee said:
The game randomly chooses one of the wires as the one which will defuse the bomb
One issue that I would like to point out is this. You have actually forgotten the top part of your C# file, thus' leaving out some of your code.

If you could go and make a quick edit to your post to include the missing code, then that would be great.
 
D

Deleted member 613

Guest
One issue that I would like to point out is this. You have actually forgotten the top part of your C# file, thus' leaving out some of your code.

If you could go and make a quick edit to your post to include the missing code, then that would be great.
@Mathematical All the code is there, im pretty sure(at least what is needed to make the game work)
 
Last edited by a moderator:

Mathematical

Active Coder
@Mathematical All the code is there, im pretty sure(at least what is needed to make the game work)
I actually just looked over it again and now I'm absolutely confused.

First, you ask the user to select the correct wire, then you declare a Random and an integer(Which has been given the identifier of "answer3"). Then, you create a new integer called "answer2" which if I'm correct, is initialized to have a random value assigned to it. I know what the last part does and it's that it checks to see if you got the answer right.

But everything else in my opinion is just a mess in my opinion.

I'm going to give you an important tip. That is: make sure that you look over your code and format it properly before you submit it to anywhere(An open-source project repository, your interviewer, here on CF or StackOverflow, or even distributing a copy of your own software). If you don't, you won't be able to write good code. Good code involves rigorous testing and numerous checks to see if it can easily be maintained if it gets any more complex than what it is.

So please, before you post any code next time, do not leave parts out(You literally left out the class name and what libraries you used - Do not do that either as that can make people question what the libraries you're using are). Make sure it's properly formatted too, uses meaningful variable names, and can easily be read over by somebody else.
 
D

Deleted member 613

Guest
I actually just looked over it again and now I'm absolutely confused.

First, you ask the user to select the correct wire, then you declare a Random and an integer(Which has been given the identifier of "answer3"). Then, you create a new integer called "answer2" which if I'm correct, is initialized to have a random value assigned to it. I know what the last part does and it's that it checks to see if you got the answer right.

But everything else in my opinion is just a mess in my opinion.

I'm going to give you an important tip. That is: make sure that you look over your code and format it properly before you submit it to anywhere(An open-source project repository, your interviewer, here on CF or StackOverflow, or even distributing a copy of your own software). If you don't, you won't be able to write good code. Good code involves rigorous testing and numerous checks to see if it can easily be maintained if it gets any more complex than what it is.

So please, before you post any code next time, do not leave parts out(You literally left out the class name and what libraries you used - Do not do that either as that can make people question what the libraries you're using are). Make sure it's properly formatted too, uses meaningful variable names, and can easily be read over by somebody else.
Ok,from now on i'll post all the code, make sure it is understandable to all, and maybe putt '//' text to say what each part does.
 

Mathematical

Active Coder
Ok,from now on i'll post all the code, make sure it is understandable to all, and maybe putt '//' text to say what each part does.
That would actually be preferable. Regarding comments(//... and /*...*/), I don't understand why you didn't include them before. Yeah, to a C# expert, it wouldn't need comments to understand what was going on in such a simple script, but to the newer ones, you need to include comments. They're literally one of the things you cannot forget when writing code and if you do forget them, nobody will be able to understand what the code you wrote does. Even experts.

Now don't take offense to this, but I wouldn't say that your skills are good enough to actually help produce tutorials. I would actually wait until you get to a special point in C# - Preferably, when you start working with OOP parts, such as classes, inheritance, etc. Again, I'm not trying to offend you or anything. You can still share small snippets of code and that is completely fine, but if you are going to produce C# tutorials, again, I would wait off until you become more proficient in the language.
 

Mandiux

New Coder
I think it's great @GameZee . We have all walked through this path of learning and well I can't say I haven't made mistakes in the past.
My recommendation is to get rid of the array at the top, since there is no other reference to it, and it's not being used.
After that all you can do is name the answer variables with better names. answer 2 can be for example 'randomWire' and answer3 can be 'userInput'.
Other than that great job in my opinion. Keep it up!
 

Mathematical

Active Coder
Hi,
I am new here. Don't these platform have compilers that we can run the code on and see the results here?
Hey there.

First of all, welcome to CodeForum. If you have any questions, feel free to ask them and we'll be there to help. Also, don't be afraid to introduce yourself over in the "Hello World!" board. I'm sure we'd all love to say hi and get to know you better.

Regarding your question, do you mean a compiler for C# on the web or...? I don't really understand the question so it'd be great if you could clear it up.

Thanks and welcome again.
 

Mathematical

Active Coder
What I meant was that can't we run the code here without using external compiler.
If you mean running compiled code here on CF, we do have these two tools that you can find at the bottom of any page here on the site:

The IDE is probably what you're looking for. The only issue is, is that it will more than likely be compiling with an old version of C#. That's great because of stability reasons but not great if you want to use new features that can only be used in newer versions of the C# compiler.

If I were you, I would suggest downloading a copy of Visual Studio Community Edition or perhaps look into MonoDevelop if you want your own kind of set-up, rather than having Microsoft do it all for you. From there, you'll actually have a much more recent version of C#, plus, it won't take forever to compile or write code in. Trying to compile code for a compiled-language would be painfully slow. Sure, it might be a little bit faster, but depending on not only your hardware but also the speed of your Internet, it can vary. Hence why I suggested you just download Visual Studio or MonoDevelop.
 

Top