IRC Bot - Roulette Command - c#

I'm trying to make a roulette command for my bot, and this is what I got so far.
if (!String.IsNullOrEmpty(e.Data.Message.Replace("!roulette", ""))) {
string _u = e.Data.Nick;
string _b = e.Data.Message.Replace("!roulette", "");
string[] _c = { "R", "B", "G", "Red", "Black", "Green",
"r", "b", "g", "redblack green" };
Random _r = new Random();
int rnum = _r.Next(0, 36); // 0-35
if (_b.Contains(rnum.ToString()) && _b.Contains(_c.ToString())) {
MessageHandler(conf.Nick, e.Data.Nick + " spins the wheel.. " + _b.ToString() + " " + (string)_c[rnum] + "! We have a winner!", 8);
} else {
MessageHandler(conf.Nick, e.Data.Nick + " spins the wheel.. " + rnum.ToString() + " " + (string)_c[rnum] + "! You lose!", 8);
}
}
I get the Index outside the bounds of the array. error, it's very strange for something not so complicated.
How would I fix this, would I ignore the array and go for a Dictionary or List<>?

You are referencing _c array's element using the index rnum: _c[rnum].
Thernum variable can have any integer value from the range 0 - 35.
But the _c array has only 10 elements.
To fix it, limit the rnum variable to the range 0 - 9:
int rnum = _r.Next(0, 10);

Related

Fix user input not continuing until the entered user input loops to match its index in the array

The problem I have is that when I enter an element from the array, the user input resets to the start of the loop, for example when i user input the third element from the array, the user input question two more times to continue to the next user input question, how can I fix this? everybody know what I can adjust here.
worked loop image with 1st element entered
looping error
while (true)
{
String[] bgtype = { "cheeseburger","tlc", "bbq" };
int[] bgtypeprice = { 15, 25, 10 };
int max = bgtype.Length;
String[] product = new string[max];
String[] type = new string[max];
int[] qty = new int[max];
int[] disc = new int[max];
Console.WriteLine("");
for (int i = 0; i < max; i++)
{
Console.Write("What PRODUCT would you like to buy ?: "); ;
product[i] = Console.ReadLine();
if (product[i].Equals("burger", StringComparison.CurrentCultureIgnoreCase))
{
{
Console.Write("What TYPE of product would you like to buy?: ");
type[i] = Console.ReadLine();
if (bgtype[i].Contains(type[i]))
{
Console.Write("Enter your discount % (5% for adults & 7% for minors): ");
disc[i] = Convert.ToInt32(Console.ReadLine());
Console.Write("How many will you buy? ");
qty[i] = Convert.ToInt32(Console.ReadLine());
float total = bgtypeprice[i]; total *= qty[i];
Console.WriteLine("Total cost of " + type[i] + " " + product[i] + " is: " + qty[i] + " pieces x P" + bgtypeprice[i] + "= P" + total);
float totaldisc = 0; totaldisc = (total * disc[i]) / 100;
Console.WriteLine("Total amount of discount: P " + totaldisc);
float totalamt = 0; totalamt = total - totaldisc;
Console.WriteLine("Total cost of order: P " + totalamt);
Console.WriteLine("-------------------ORDER CONFIRMATION-------------------");
}}}}}
if (bgtype[i].Contains(type[i])) should be changed to if (bgtype.Contains(type[i]))
Array.Contains, verifies if an item is contained within an Array, not an Array value. By asking if bgtype[i] contains type[i], you are asking if cheesburger contains tlc, which it doesn't, hence why it starts from the beginning. By verifying if bgtype contains type[i], you are asking if ["cheesburger", "tlc", "bbq"] contains tlc, which it does. I hope this was clear enough.

Difference between two Arrays C# (Random input + User input)

I've created an array which generate 10 random numbers, and I want it to be compared to user input of 10 numbers. If 6 numbers are equal to the random generated numbers (from 1 to 25) then it should show a 6.
Also, the order should not matter. If the user input the number 8 as his or her first selection, it should be able to be compared to the random generated number if it generated number 8 as its last selection. Which in the end should show the result of minimum 1.
Random r = new Random();
int MyRandomNr = r.Next(1, 26);
// Random Generator
var ArrayRandom = new int[] { MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr, MyRandomNr };
Console.Write("HELLO AND WELCOME!"
+ System.Environment.NewLine + "Type in 10 numbers."
+ System.Environment.NewLine + "A number between from 1 to 25, one number at the time " + System.Environment.NewLine);
string[] ArrayUser = new string[10];
for (int i = 0; i < 10; i++)
{
Console.Write((i + 1) + ". Next number is: ");
ArrayUser [i] = Console.ReadLine();
}
I've looked through the Stackoverflow and the following code doesn't really compare the two arrays in the way I want it to compare to each other. The following code works more like an exam, but I'm looking for something similar to this...
int[] answer = { 1, 3, 4};
int[] exam = { 4, 1, 3};
int correctAnswers = 0;
int wrongAnswers = 0;
for (int index = 0; index < answer.Length; index++)
{
if (answer[index] == exam[index])
{
correctAnswers += 1;
}
else
{
wrongAnswers += 1;
}
}
Console.Write("The matching numbers are " + correctAnswers +
"\n" + "The non matching numbers are " + wrongAnswers);

C# Substring OutOfRangeException With Correct String Length

I encountered out of range exception during a substring operation.
My string's length is 100, and the position of substrings are 58 and 94, which should not have given an out of range exception.
Below are the logs and code:
string parameters = item.GetFormattedValue("Parameters").ToString();
Console.WriteLine("parameters = " + parameters.ToString());
Console.WriteLine("parameters length: " + parameters.Length);
Console.ReadKey();
int p1 = parameters.IndexOf(#">");
Console.WriteLine("p1 = " + p1);
int p2 = parameters.IndexOf(#"<", parameters.IndexOf(#"<") + 1);
Console.WriteLine("p2 = " + p2);
Console.ReadKey();
string parametersSub = parameters.Substring(p1, p2);
Console.WriteLine("parametersSub: " + parametersSub);
Console.ReadKey();
The second argument in Substring is the length of the string to select, rather than the index to select up to.
Since your arguments are 58 and 94, you are trying to select from index 58 for 94 characters which goes outside the length of your string.
To select between the two indices, get the difference between the two and use that for the length to select:
int p1 = parameters.IndexOf(#">");
Console.WriteLine("p1 = " + p1);
int p2 = parameters.IndexOf(#"<", parameters.IndexOf(#"<") + 1);
Console.WriteLine("p2 = " + p2);
Console.ReadKey();
string parametersSub = parameters.Substring(p1, p2 - p1);
Console.WriteLine("parametersSub: " + parametersSub);
Of course, you should still check that both the start index, and length are within the bounds of the string.

I want change variable to value of hashtables in arraylist

I made [Hashtable hash] such as
hash(i, 1)
hash(j, 2)
Also I made an [arraylist sArray] which include "i" or "j" such as
sArray[0] : hello
sArray[1] : first number is i
sArray[2] : second number is j
sArray[3] : bye
Now, I want to change the "i" and "j" in the sArray to the values of the hash.
How can I do it?
If I understand properly, I think this is the code in c#
//Your example
int i = 1;
int j = 2;
var hash = new System.Collections.Hashtable();
hash[i] = -173.5;
hash[j] = 37;
var sArray = new System.Collections.ArrayList();
sArray.Add("hello");
sArray.Add("first number is " + hash[i].ToString());
sArray.Add("second number is " + hash[j].ToString());
sArray.Add("bye");
// more general, you could have different i and j position
i = 3;
j = 4;
hash[i] = 33.3;
hash[j] = -44.4;
sArray[1] = "number in " + i.ToString() + " position is " + hash[i].ToString();
sArray[2] = "number in " + j.ToString() + " position is " + hash[j].ToString();
// I think following option is more easy to read and fast if iterated
i = 5;
j = 6;
hash[i] = 55.5;
hash[j] = -66.6;
sArray[1] = String.Format("number in {0} position is {1}", i, hash[i]);
sArray[2] = String.Format("number in {0} position is {1}", j, hash[j]);

Increment an index that uses numbers and characters (aka Base36 numbers)

I have a string based code that can be either two or three characters in length and I am looking for some help in creating a function that will increment it.
Each 'digit' of the code has a value of 0 to 9 and A to Z.
some examples:
the first code in the sequence is 000
009 - next code is - 00A
00D - next code is - 00E
AAZ - next code is - AB0
the last code is ZZZ.
Hope this makes some sense.
Thanks for the advice guys.
This is what I independently came up with.
private static String Increment(String s)
{
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char lastChar = s[s.Length - 1];
string fragment = s.Substring(0, s.Length - 1);
if (chars.IndexOf(lastChar) < 35)
{
lastChar = chars[chars.IndexOf(lastChar) + 1];
return fragment + lastChar;
}
return Increment(fragment) + '0';
}
I don't know if it is better/worse but seems to work. If anyone can suggest improvements then that is great.
Maintain the counter as an int and increment this. Convert the int to your character representation by modding and dividing by 36 iterativly. Map the modded range (0-35) to 0-Z.
Example
Updated with functions to go in either direction:
internal class Program
{
const int Base = 36;
public static void Main()
{
Console.WriteLine(ToInt("0AA"));
Console.WriteLine(ToString(370));
}
private static string ToString(int counter)
{
List<char> chars = new List<char>();
do
{
int c = (counter % Base);
char ascii = (char)(c + (c < 10 ? 48 : 55));
chars.Add(ascii);
}
while ((counter /= Base) != 0);
chars.Reverse();
string charCounter = new string(chars.ToArray()).PadLeft(3, '0');
return charCounter;
}
private static int ToInt(string charCounter)
{
var chars = charCounter.ToCharArray();
int counter = 0;
for (int i = (chars.Length - 1), j = 0; i >= 0; i--, j++)
{
int chr = chars[i];
int value = (chr - (chr > 57 ? 55 : 48)) * (int)Math.Pow(Base, j);
counter += value;
}
return counter;
}
For more variants of conversion code see Quickest way to convert a base 10 number to any base in .NET?.
Does this do what you need?
public class LetterCounter
{
private static readonly string[] _charactersByIndex = new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
public string GetStr(int i)
{
if (i < _charactersByIndex.Length)
return _charactersByIndex[i];
int x = i / (_charactersByIndex.Length - 1) - 1;
string a = _charactersByIndex[x];
string b = GetStr(i - (_charactersByIndex.Length - 1));
return a + b;
}
}
}
Based on #Martin answer, I found some error when two ZZ comes, this makes exception in code
private static String Increment(String s,bool IsFromRecursion=false)
{
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//Added this condition
if (IsFromRecursion && string.IsNullOrEmpty(number))
{
return "1";
}
//Added this condition
char lastChar = s[s.Length - 1];
string fragment = s.Substring(0, s.Length - 1);
if (chars.IndexOf(lastChar) < 35)
{
lastChar = chars[chars.IndexOf(lastChar) + 1];
return fragment + lastChar;
}
return Increment(fragment,true) + '0';
}
When we call this method we pass first parameter only.

Categories

Resources