Make a custom 8-ball.

Make a custom 8-ball.

Postby Leeizazombie » 20 Feb 2014, 13:34

Hey I just decided to make a custom 8-ball for the sake of code practise, and you guys can see what can be done (I'm sure there are other ways)

Just follow the comments within the code:
Code: Select all
using System;
using System.Xml;

namespace MCDzienny
{
    public class CmdTestMe : Command
    {
        public override string name { get { return "eightball"; } }
        public override string shortcut { get { return "8-ball"; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return true; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }

        public override void Use(Player p, string message)
        {
            if (message != null && message.Length >= 3) //Question must contain text and is greater or equal to 3 in lenght.
            {
                string[] answer = new string[3];    //Using the variable "answer" to create "3" diffrent outputs.
                answer[0] = "Yes.";     //Random answer #1
                answer[1] = "Maybe.";   //Random answer #2
                answer[2] = "No.";      //Random answer #3
                Random rnd = new Random();
                Player.SendMessage(p, "%7Your question is:%b " + message + ", %7The 8 ball says: %b" + answer[rnd.Next(0, 2)]);
            }
            else
                Player.SendMessage(p, "Please ask a question.");
        }


        public override void Help(Player p)
        {
            Player.SendMessage(p, "/8-ball [question] - Ask the 8-ball a question.");
        }
    }
}


So if you want to add more answers just do the following sequence:
Code: Select all
string[] answer = new string[4];    //Change new string[3] to new string[4]
                answer[0] = "Yes.";   
                answer[1] = "Maybe."; 
                answer[2] = "No.";   
                answer[3] = "Sometimes.";  //Addded my new answer with [3]


I hope you enjoy it! <ok>

Uses:
/eightball [question]
/8-ball [question]

Note
There is already an 8ball command, just be sure on how you use the command, the other 8 ball is used by /8ball, this one is /8-ball, also this one doesn't show everyone (but you can change that).

Feel free to ask questions if you have any problems!

Picture of command used:
Spoiler:
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Make a custom 8-ball.

Postby Leeizazombie » 20 Feb 2014, 13:41

Um, I need a moderator to remove the unnecessary "using System.Xml;" and I named the class TestMe, please change it to 8-ball. Thanks.
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Make a custom 8-ball.

Postby joppiesaus » 20 Feb 2014, 17:33

It's easier to do this:
Code: Select all
string[] asnwer = new string[] {
   "Yes.", "NO!", "ABSOLUTELY NOT!", "What the heck are you asking me?!"
   "What do you think?", "Maybe." };

Rather than this:
Code: Select all
string[] answer = new string[4];    //Change new string[3] to new string[4]
                answer[0] = "Yes.";   
                answer[1] = "Maybe."; 
                answer[2] = "No.";   
                answer[3] = "Sometimes.";  //Addded my new answer with [3]


Also, if you put that outside the "Use" void, it'll doesn't have to re-create it self over time.
And about the random part: You need to change that too.

Let me explain:
random.Next(minValue, maxValue);
It'll pick the minValue as a minimum, for example 0. If it's zero, you can leave it if you want.
the maxValue is [u]not the max value[/u.] it's the maximum value + 1.
For example: random.Next(0, 2) will either return 0 or 1, but never 2. I don't know why, but it's handy with arrays.

Since I changed the array, it's length is automatically calculated by the computer, so you don't have to change that.
so, instead of answer[rnd.Next(0, 2)] we pick answer[rnd.Next(answer.Length)]. It does the same job as answer[rnd.Next(0, answer.Length)]

So you have to do nothing else than change words in the array, it'll automaticly do the magic.

What you also could do for the 8 ball, is to let him answer always the same on the same question, but still with the magic.
To do this, we make a seed generator for the random thinggy!

This code will crash if you have for example a question with 1290384129 characters. Beware!
Code: Select all
char[] chars = message.ToCharArray(); // Cut the question in seperate characters
int seed = chars.Length; // This one is just for fun!

for (int i = 0; i < chars.Length; i++)
{
   seed += Convert.ToInt32(chars[i]); // Converts the character into the ASCII value of the character.
}

// make the random!
Random rnd = new Random(seed);
joppiesaus
 
Posts: 379
Joined: 20 Aug 2012, 07:28
Location: in a obsedian house, with glass in it so i can see the lava!

Re: Make a custom 8-ball.

Postby Leeizazombie » 20 Feb 2014, 18:25

Thanks for the feedback, also that seed generator looks very interesting, thanks for the tips!
Owner of:
LeeIzaZombie Freebuild and Lava Survival V2 (Shut Down and updated)
LeeIzaZombie Survival (Comming back soon)

Contact:
Skype: leeizazombie
IRC: irc.geekshed.net, #leeizazombie, #mcclassichosting
User avatar
Leeizazombie
 
Posts: 536
Joined: 10 Jun 2013, 17:45
Location: Ireland.

Re: Make a custom 8-ball.

Postby Conor » 20 Feb 2014, 23:24

Very nice.

You could alternatively use a List to store the string variables, outside of the Use method. This could allow the user to add to the list from their client (for example '/8ball add [newAnswer]') and you can just use List.Add(string) to add the new response immediately!
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor


Return to Custom Commands

Who is online

Users browsing this forum: No registered users and 1 guest

cron