Skip to content

Instantly share code, notes, and snippets.

@deathmarine
Created November 22, 2019 15:00
Show Gist options
  • Save deathmarine/e47db7938d49836266cfb0ed5f67a014 to your computer and use it in GitHub Desktop.
Save deathmarine/e47db7938d49836266cfb0ed5f67a014 to your computer and use it in GitHub Desktop.
100 line Keno Game in C#
using System;
using System.Collections;
using System.Text.RegularExpressions;
namespace KenoProject {
class Program {
static void Main(string[] args) {
Random generator = new Random();
Console.WriteLine("======= Play Keno =======\n\n");
while (true) {
Console.Write("Pick up to 10 numbers between (1 - 80): ");
String input = Console.ReadLine();
ArrayList pick_storage = new ArrayList(); //Plan to store some numbers
//Regular Expressions are great for String parsing and intuitively collecting data
if (Regex.Match(input, "[\\s,;:]").Success) { // This expression means if any character in the string matches a space, comma, semicolon, or colon.
String[] unums = Regex.Split(input, "[\\s,;:]"); // Same Expression but splits the string into an array using the expression.
for (int i = 0; i < unums.Length; i++) { //Iterate the array
try { //Splitting a string can have valid empty strings,
int a1 = int.Parse(unums[i]); //trying to parse the string will catch letters, special characters and empty strings
if (a1 > 0 && a1 <= 80 && !pick_storage.Contains(a1)) //Numbers must be positive less then the selection range and not duplicated
pick_storage.Add(a1);
} catch {
//Gracefully Handle Exceptions, Do Nothing! We'll assume the entry is a typo and skip it.
}
}
} else {//If the expression doesn't match, I'm assuming the user is entering a single number
try {
int a1 = int.Parse(input);
if (a1 > 0 && a1 <= 80 && !pick_storage.Contains(a1))
pick_storage.Add(a1);
} catch { //At this point they might be mashing buttons on a keyboard, we'll give the user a chance to leave
Console.WriteLine($"{input} is not a number.\n\n"); //Show them the error
Console.Write("Continue? (Yes, no): ");//Ask if they want to quit
String a2 = Console.ReadLine();
if (a2.StartsWith("Y") || a2.StartsWith("y")) //explicit yes
continue; //Start the loop over
else
break; //Break the loop
}
}
if (pick_storage.Count < 1) { //Santity check, I don't know if we even have numbers stored yet.
Console.WriteLine("No Numbers Found!\n\n");
Console.Write("Play again? (Yes, no): ");
String a2 = Console.ReadLine();
if (a2.StartsWith("Y") || a2.StartsWith("y")) //explicit yes
continue;
else
break;
}
Console.Write("Money to wager: $");
String input2 = Console.ReadLine();
Decimal money;
try {
money = new Decimal(double.Parse(input2)); //We must wager some fake money.
} catch {
Console.WriteLine($"{input2} is not a accepted value!\n\n");
Console.Write("Play again? (Yes, no):");
String a2 = Console.ReadLine();
if (a2.StartsWith("Y") || a2.StartsWith("y")) //explicit yes
continue;
else
break;
}
Console.WriteLine("\n\n===============Drawings Numbers===============");
int hits = 0;
ArrayList draw_storage = new ArrayList(); //Store already drawn numbers to not duplicate any
for (int i = 0; i < 20; i++) { //Select 20 numbers
System.Threading.Thread.Sleep(1000); //Add some suspense by forcing the user to wait for a second (time is in milliseconds)
int draw = generator.Next(1, 80);
while (draw_storage.Contains(draw)) { //Keep drawing until we get a unique number
draw = generator.Next(1, 80);
}
draw_storage.Add(draw); //Store the number so we can stop looking
if (pick_storage.Contains(draw)) { //Determine if the number matches one of the picks
Console.Write($"*{draw}* Hit ");
hits++; //Use a hit counter for a win formula
} else {
Console.Write($"{draw} ");
}
}
Console.WriteLine("\n\n==== Results ====\n");
Decimal prize = Decimal.Multiply(((hits / pick_storage.Count) * pick_storage.Count) * 2, money); //Formula I made up for keno
Console.WriteLine($"You picked {pick_storage.Count} number(s). Had {hits} Hits. Bet ${money:C}!");
if (prize > 0)
Console.WriteLine($"You win ${prize:C}!"); // (10/10) * 10 * 2 = 20 The win is 20x possible with 10 numbers
else
Console.WriteLine("Better luck next time!");// (1/1) * 1 * 2 = 2 The win is 2x possible with 1 number
Console.Write("\n\nPlay again? (Yes, no): ");
String a3 = Console.ReadLine();
if (a3.StartsWith("Y") || a3.StartsWith("y")) //explicit yes
continue;
else
break;
}
Console.WriteLine("Press Any Key...");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment