Jump to content

Magic 8 Ball App

Dobbsjr

This just a random program that acts like Magic 8 ball with the standard 20 answers. This is the basis of a text adventure game that I'm currently working on. Any suggestion on how to make it 'smarter'?(Maybe using regular expression will help, but I have no idea how they work :( ). [Written in Visual C#, Compiled in Visual Studio 2013].

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Threading;namespace Magic8Ball_{    class Program    {        static void Main(string[] args)        {            //Sets font color to original            ConsoleColor oldColor = Console.ForegroundColor;                        ProgramName();            //Create randomizer object            Random randomObject = new Random();                        //Loop questions            while(true)            {                string questionString = GetQuestion();                int toSleep = randomObject.Next(2) + 1;                Console.WriteLine("Thinking about your answer, please stand by...");                Thread.Sleep(toSleep * 1000);                                //Checks length                if (questionString.Length == 0)                {                    Console.WriteLine("Does not compute!");                    continue;                }                                //Quit the program                if (questionString.ToLower() == "quit")                {                    break;                }                                //Generate new int                int ranInt = randomObject.Next(20);                Console.ForegroundColor = (ConsoleColor)randomObject.Next(15);                switch(ranInt)                {                    case 0:                        {                            Console.WriteLine("Signs point to yes");                            break;                        }                    case 1:                        {                            Console.WriteLine("Yes");                            break;                        }                    case 2:                        {                            Console.WriteLine("Reply hazy, try again");                            break;                        }                    case 3:                        {                            Console.WriteLine("Without a doubt");                            break;                        }                    case 4:                        {                            Console.WriteLine("My sources say no");                            break;                        }                    case 5:                        {                            Console.WriteLine("As I see it, yes");                            break;                        }                    case 6:                        {                            Console.WriteLine("You may rely on it");                            break;                        }                    case 7:                        {                            Console.WriteLine("Concentrate and ask again");                            break;                        }                    case 8:                        {                            Console.WriteLine("Outlook not so good");                            break;                        }                    case 9:                        {                            Console.WriteLine("It is decidedly so");                            break;                        }                    case 10:                        {                            Console.WriteLine("Better not tell you now");                            break;                        }                    case 11:                        {                            Console.WriteLine("Very doubtful");                            break;                        }                    case 12:                        {                            Console.WriteLine("Yes - definitely");                            break;                        }                    case 13:                        {                            Console.WriteLine("It is certain");                            break;                        }                    case 14:                        {                            Console.WriteLine("Cannot predict now");                            break;                        }                    case 15:                        {                            Console.WriteLine("Most likely");                            break;                        }                    case 16:                        {                            Console.WriteLine("Ask again later");                            break;                        }                    case 17:                        {                            Console.WriteLine("My reply is no");                            break;                        }                    case 18:                        {                            Console.WriteLine("Outlook good");                            break;                        }                    case 19:                        {                            Console.WriteLine("Don't count on it");                            break;                        }                }            }                        //Cleaning up font color            Console.ForegroundColor = oldColor;        }                static void ProgramName()        {            //Displays name of application            Console.ForegroundColor = ConsoleColor.DarkCyan;            Console.WriteLine("Magic 8 Ball Application (by: Dobbsjr)");            Console.WriteLine("");        }                static string GetQuestion()        {            //Asks a question            Console.ForegroundColor = ConsoleColor.Cyan;            Console.Write("Ask a no/yes question: ");            Console.ForegroundColor = ConsoleColor.White;            string questionString = Console.ReadLine();            return questionString;        }    }}

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
Share on other sites

@Dobbsjr

Isn't there a shuffle method on a list in C# ?
You could put all those reply's in an array, shuffle it and then pick one on location X:
 
The following is Java code, but should do what you want, and get's rid of that very long switch

 

public static void main(String[] args) {        // List of all the answers	    String[] answers = {"Your future", "What are you doing", "Am I doing this right"};        // A random number generator        Random r = new Random();        // Print the answer at location X. X determined by the random number gen.        System.out.println(answers[r.nextInt(answers.length)]);} 

 
PS: What do you want to do with the Regular Expressions you are talking about in your original post?
If you would like to do something in the region of replacing keywords within a sentence with random answers, you don't even need Reg Ex. (Once again Java code, but should express the "algorithm")
 

        String a = "Apes";        String b = "Bananans";        System.out.println(String.format("This sentence has lots of %s in a sentence. They allow for instant replacing of %s", a,b));
This sentence has lots of Apes in a sentence. They allow for instant replacing of Bananans

 
Any language should have a format option that allows you to input the content of variables in a string.

If you need any further help, feel free to quote me or even Pm me.

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to comment
Share on other sites

Link to post
Share on other sites

@Dobbsjr

Isn't there a shuffle method on a list in C# ?

You could put all those reply's in an array, shuffle it and then pick one on location X:

 

The following is Java code, but should do what you want, and get's rid of that very long switch

 

public static void main(String[] args) {        // List of all the answers	    String[] answers = {"Your future", "What are you doing", "Am I doing this right"};        // A random number generator        Random r = new Random();        // Print the answer at location X. X determined by the random number gen.        System.out.println(answers[r.nextInt(answers.length)]);} 

 

PS: What do you want to do with the Regular Expressions you are talking about in your original post?

If you would like to do something in the region of replacing keywords within a sentence with random answers, you don't even need Reg Ex. (Once again Java code, but should express the "algorithm")

 

        String a = "Apes";        String b = "Bananans";        System.out.println(String.format("This sentence has lots of %s in a sentence. They allow for instant replacing of %s", a,b));
This sentence has lots of Apes in a sentence. They allow for instant replacing of Bananans

 

Any language should have a format option that allows you to input the content of variables in a string.

If you need any further help, feel free to quote me or even Pm me.

Thanks for the array, I didn't think about that! :)

What I meant about the regular expressions was using it somehow to check if the user actually asked a legit question instead of spamming.

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the array, I didn't think about that! :)

What I meant about the regular expressions was using it somehow to check if the user actually asked a legit question instead of spamming.

 

Hum, checking if input is legit, is quite hard. The only way I can think of at this moment is for you to check if there any English words in the sentence. And for that you would need to have :

A. Acces to a full up to date library of English words

B. A lot of patience since it would require lots of calculations to verify that.

 

So my advice is to simply not bother about that. Later in your game, you can simply filter out any words that aren't pre specified commands.

 

I think of like: 

Game: I see 2 doors, one on the left and one on the right. Which shall I pick?

Input by user: Left / Right. ==> Ok, proceed

Input by user: Bananas are cool. Invalid input (does not contain a variation of left / right) ==> Ignore or point the user to his error

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to comment
Share on other sites

Link to post
Share on other sites

+ I don't think your quit functionality will work. As to my understanding a "break" simply jumps out of that iteration of the loop. To fully stop I think it is either quit or exit.

 

(Not a C# Dev , so could be totally wrong).

That time I saved Linus' WiFi pass from appearing on YouTube: 

A sudden Linus re-appears : http://linustechtips.com/main/topic/390793-important-dailymotion-account-still-active/

Link to comment
Share on other sites

Link to post
Share on other sites

Hum, checking if input is legit, is quite hard. The only way I can think of at this moment is for you to check if there any English words in the sentence. And for that you would need to have :

A. Acces to a full up to date library of English words

B. A lot of patience since it would require lots of calculations to verify that.

 

So my advice is to simply not bother about that. Later in your game, you can simply filter out any words that aren't pre specified commands.

 

I think of like: 

Game: I see 2 doors, one on the left and one on the right. Which shall I pick?

Input by user: Left / Right. ==> Ok, proceed

Input by user: Bananas are cool. Invalid input (does not contain a variation of left / right) ==> Ignore or point the user to his error

Okay thanks!

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
Share on other sites

+ I don't think your quit functionality will work. As to my understanding a "break" simply jumps out of that iteration of the loop. To fully stop I think it is either quit or exit.

 

(Not a C# Dev , so could be totally wrong).

it doesn't actually close the window. This is a simple console application so breaking out of the while loop "quits" the program and then you press any key to close the console.

Did my post help you? Then make sure to rate it!

Check out my post on symbolic links! || PSU ranking and tiers || Pokemon Thread

 

Link to comment
Share on other sites

Link to post
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

×