Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

So im learning c#… Heres a magic 8ball!


Jumbofile
 Share

Recommended Posts

Ive decided to learn C# after talking to some very helpful member here at eclipse and with the video series im watching ive made a Magic 8Ball!

![](http://i.imgur.com/wqu0URh.png)

It has 4 different answers and was a good way to teach me how to do multiple things in C# and help me better understand the language.

Download:
https://www.dropbox.com/s/8bzif1lhmvzhrcm/Magic8ball%20Codegasm.exe?dl=0
(100% safe, some antivirus programs dont think so but I promise you it is)

Source:
```
using System;

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

namespace Magic8ball_Codegasm
{
   //abstract class Greg
   //{
   //    static string name = "Greg";
   //    public static string alias = "Jumbofile";
   //}

   /// /// Entry point for magic 8ball program
   ///

   class Program
   {
       static void Main(string[] args)
       {

           //Preserve console text color
           ConsoleColor oldColor = Console.ForegroundColor;

           //Get Question
           Tellpeoplewhatprogramthisis();

           //Create a random object
           Random randomObject = new Random();

           //Loop forever!
           while(true)
           {
               string questionString = GetQuestion();

               //Think about question
               int numberOfSecondsToSleep = randomObject.Next(5) + 1;
               Console.WriteLine("Thinking....");
               Thread.Sleep(numberOfSecondsToSleep * 1000);

               //Checking response
               if(questionString.Length == 0)
               {
                   Console.WriteLine("You have to write something to get an answer!");
                   continue;
               }

               //Check if user wants to quit
               if (questionString.ToLower() == "quit")
               {
                   break;
               }

               //Check if user wants to be a loser
               if (questionString.ToLower() == "you suck")
               {
                   Console.ForegroundColor = ConsoleColor.Red;
                   Console.WriteLine("No you suck loser! Bye!");
                   break;
               }

               //Get Random Number
               int randomNumber = randomObject.Next(4);

               //Answer color
               Console.ForegroundColor = ConsoleColor.Yellow;

               //Use random number to determain response
               switch(randomNumber)
               {
                   case 0:
                       {
                           Console.WriteLine("Yes!");
                           break;
                       }
                   case 1:
                       {
                           Console.WriteLine("No!");
                           break;
                       }
                   case 2:
                       {
                           Console.WriteLine("Hell no!");
                           break;
                       }
                   case 3:
                       {
                           Console.WriteLine("Why even ask? DUH!");
                           break;
                       }
               }

           }
           //End of loop

           //Clean up
           Console.ForegroundColor = oldColor;

       }
       /// /// Prints name of program and who created it
       ///

       static void Tellpeoplewhatprogramthisis()
       {
           //Change console text color
           Console.ForegroundColor = ConsoleColor.Green;
           Console.Write("Magic 8ball by ");
           Console.ForegroundColor = ConsoleColor.Cyan;
           Console.WriteLine("Jumbofile");
       }
       /// /// Returns Question from user
       ///

       static string GetQuestion()
       {
           //This block of code asks question and stores answer
           Console.ForegroundColor = ConsoleColor.White;
           Console.Write("Ask a question!: ");
           Console.ForegroundColor = ConsoleColor.Gray;
           string questionString = Console.ReadLine();

           return questionString;
       }

   }
}

```
Link to comment
Share on other sites

Nice man, a hell of a lot nicer then my first program. Nice use of functions. I do not think of any those functions need to be static though. But it really doesnt matter. (Also I could be wrong and C# may be different)
Link to comment
Share on other sites

Very nice use of string functions. Not sure what you were going for with that abstract class at the top, but it was commented out so I'll disregard it c:
Small tip with Switch statements though. Individual cases don't have to be encapsulated by curly braces. It can simply be written like this.

```
Switch (obj) {

   case 0:
       // Do something here
       break;
}
```
Other than that, very good work for a first application! ^__^

@Evan

Those methods do have to be static because the Main method is itself a static method. He could use non-static methods, but then he'd have to create an instance of the 8ball game. Not bad to practice, but not necessary for such a small application.
Link to comment
Share on other sites

I never liked Switches. They look rather awkward, especially for small tidbits like this. Personally I would have used a Dictionary for this:

```
var responses = new Dictionary() {
{0,()=>{ Console.WriteLine("Yes!"); }},
{1,()=>{ Console.WriteLine("No!"); }},
{2,()=>{ Console.WriteLine("Hell no!"); }},
{3,()=>{ Console.WriteLine("Why even ask? DUH!"); }}
};
responses[randomnumber]();

```
There's also no need to constantly declare what you're about to fill a variable with. It's just informational overload and when you're making a large amount of variables inside a class it makes it really messy to read something like this:

```
SomeClass var1 = new SomeClass();
ThisIsAnotherClass var2 = new ThisIsAnotherClass();
Int32 var3 = default(Int32);

```
Hard to read compared to the following:

```
var var1 = new SomeClass();
var var2 = new ThisIsAnotherClass();
var var3 = default(Int32);

```With everything being directly above/below eachother it looks and reads a lot cleaner.

All of this is just personal preference though, so take it with a grain of salt.
Link to comment
Share on other sites

  • 1 month later...
```
Int32 var3 = default(Int32);
```
**See:** 8.2.1 of ECMA-334 C# language specification. 

You should be using the predefined types rather than directly referencing the primitive data structures. 

For example, if you were to attempt to implement the following code:
```
public class WeirdInteger : Int32 { }
``` 

you would be unable to, as the language forbids the deriving of a type from a struct. There are arguments for the explicit use of, say, Int32, but they would not be relevant in this particular case.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...