2
\$\begingroup\$

I made a C# console roulette game. Check it out and give opinions on how to improve or ideas for some other features? It's not finished yet so it has lots of room for improvement. I am very new so take it easy on me.

GitHub link

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

namespace somekinda
{
class Program
{
static void Main(string[] args)
{          
    Console.ForegroundColor = ConsoleColor.Yellow;                
    Random ran = new Random();
    var r = new Random();
    string[] color = { "Red", "Black" };                 
    string guess;
    int attempts = 0;
    int bet;
    int money = 500;
    while (money != 0)
    {
        Console.WriteLine("Roulette Roller by Alifyandra\n");
        Console.WriteLine("Money:$" + money+"                  Attempts: "+attempts);
        Console.WriteLine("Type in any off the following letters below:");
        Console.WriteLine("a.Even    b.Odd    c.1 to 18    d.19 to 36");
        Console.WriteLine("e.Red     f.Black  g.1st 12     h.2nd 12");
        Console.WriteLine("i.3rd 12");
        guess = (Console.ReadLine());
        //guess verifier
        guess.ToLower();
        bool check = guess == "a" || guess == "b" || guess == "c" || guess == "d" || guess == "e" || guess == "f" || guess == "g" || guess == "h" || guess == "i";                                                     
        if (check == false)
        {
            Console.WriteLine("You did not enter the correct input value(even/odd)");
            Console.ReadKey();
            Console.Clear();
            continue;
        }
        else
        {
        bet:
            Console.WriteLine("Enter an amount to bet");
            bet=Convert.ToInt32(Console.ReadLine());
            //bet verifier
            if (bet > money)
            {
                Console.WriteLine("You dont have enough money!");
                Console.WriteLine("Press enter to try again.");
                Console.ReadKey();                          
                goto bet;
            }
            else {
                money -= bet;
                int roll = ran.Next(0, 37);
                string ranColor = color[r.Next(color.Length)];
                bool even = roll % 2 == 0;
                if ((((guess == "a") && (even == true))) || (((guess == "b") && (even == false)))||((guess == "e") && (ranColor == "Red")||(guess == "f") && (ranColor == "Black")))
                {
                    Console.WriteLine("The roulette rolled: " + ranColor + " " + roll);
                    Console.WriteLine("You won! +$" + bet * 2 + "!");
                    Console.WriteLine("<Press enter to continue>");
                    money += bet * 2;
                    attempts += 1;
                    Console.ReadKey();
                }
                else if ((guess == "c") && ((roll > 0) && (roll < 19)))
                {
                    Console.WriteLine("The roulette rolled: " + ranColor + " " + roll);
                    Console.WriteLine("You won! +$" + bet * 2 + "!");
                    Console.WriteLine("<Press enter to continue>");
                    money += bet * 2;
                    attempts += 1;
                    Console.ReadKey();
                }
                else if ((guess == "d") && ((roll > 18) && (roll < 37)))
                {
                    Console.WriteLine("The roulette rolled: " + ranColor + " " + roll);
                    Console.WriteLine("You won! +$" + bet * 2 + "!");
                    Console.WriteLine("<Press enter to continue>");
                    money += bet * 2;
                    attempts += 1;
                    Console.ReadKey();
                }       
                else if ((guess == "g") && (roll > 0 && roll < 13) || (guess == "h") && (roll > 12 && roll < 25) || (guess == "i") && (roll > 24 && roll < 37))
                {
                    Console.WriteLine("The roulette rolled: " + ranColor + " " + roll);
                    Console.WriteLine("You won! +$" + bet * 2 + "!");
                    Console.WriteLine("<Press enter to continue>");
                    money += bet * 3;
                    attempts += 1;
                    Console.ReadKey();
                }
                else
                {
                    Console.WriteLine("The roulette rolled: " + ranColor + " " + roll);
                    Console.WriteLine("You lost! -$" + bet + "!");
                    Console.WriteLine("<Press enter to continue>");
                    attempts += 1;
                    Console.ReadKey();
                    if (money == 0)
                    {
                        Console.WriteLine("You are out of money.");
                        Console.WriteLine("<Press enter to continue>");
                        Console.ReadKey();
                    }
                }
            }
        }
        Console.Clear();                
    }
}
}
}
\$\endgroup\$
1
\$\begingroup\$

Avoid using goto statements instead put the code that you dont want to be displayed the second time into an if statement that will check if a boolean variable named firstTime for example has value of true if it's true than print the information else just skip it and go ahead. Your code will also throw a lot of exceptions like Formatexception etc since you are not verifying that the input is an int also if the player has more money than 2^31 your program will bug too so use long instead.

Your check is a little bit redundant like this bool check = guess == "a" || guess == "b" || guess == "c" || guess == "d" || guess == "e" || guess == "f" || guess == "g" || guess == "h" || guess == "i";

Well you already wrote all the possible guesses so you might want to do something like this :

bool[] conditions = {guess == "a", guess == "b", guess == "c"....}

this allows you to use Array.IndexOf this will make your check looks like this :

int check = Array.IndexOf(conditions, true);

This will return -1 if no condition was met so use into your advantage before you where checking if(check==false) now you will do if(check==-1) and you will also have the bonus that you already know which specific condition was met because Array.IndexOf obviously returns the index of a specific value.

This is also not a good approach

        Console.WriteLine("Enter an amount to bet");
        bet=Convert.ToInt32(Console.ReadLine());
        //bet verifier
        if (bet > money)
        {
            Console.WriteLine("You dont have enough money!");
            Console.WriteLine("Press enter to try again.");
            Console.ReadKey();                          
            goto bet;
        }

instead put this into a while loop that will break only when the user enter's correct values.

    Console.WriteLine("Enter an amount to bet");
    bet = Convert.ToInt32(Console.ReadLine());
    while (bet > money)
    {
        Console.WriteLine("You dont have enough money!");
        Console.WriteLine("Press enter to try again.");
        Console.ReadKey();

        Console.WriteLine("Enter an amount to bet");
        bet = Convert.ToInt32(Console.ReadLine());
    }

Now one small problem here is that you say "Press Enter to try again" however you are not looking for the specific "Enter" key you accept any keys. If you really want enter to be used do it like this :

            Console.WriteLine("Press enter to try again.");
            while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter))
            {

            }

One major problem are the long if statements that include magic numbers as well. Simple fix would be to refactor them into functions with respective names they are really long really hard to read and follow what's going on. Declare the magic number as constant's that's really important. You are also repeating the same text over and over again.

                Console.WriteLine("The roulette rolled: " + ranColor + " " + roll);
                Console.WriteLine("You lost! -$" + bet + "!");
                Console.WriteLine("<Press enter to continue>");

Instead first refactor the if statements into boolean functions than refactor the entire if into 1 method that way your code will become really clearer and much better. You wont even need to check if(guess == "some string") instead make the check a private static int declared outside of the static void Main() this way you can access it in your method without passing it as parameter. Even a better approach would be to create a static class with an enum that will work together. Also make the enter check into a method and like this :

    private static bool IsEnterPressed()
    {
        while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter))
        {
        }
        return true;
    }

and use it everywhere you ask for the Enter key to be pressed

            if (IsEnterPressed())
            {
                Console.WriteLine("Enter an amount to bet");
                bet = Convert.ToInt32(Console.ReadLine());
            }

How to verify the input is not a string :

        long bet;
        string tempInput = Console.ReadLine();
        while (!long.TryParse(tempInput, out bet))
        {
            Console.WriteLine("You didn't enter correct input !");
            Console.Write("Enter amount to bet : ");
            tempInput = Console.ReadLine();
        }

One last thing you have 2 Random variables. You need just one you do int a = rand.Next(x,y) anyway you can do it with just 1 variable.

\$\endgroup\$
  • \$\begingroup\$ wow you put that much effort on answering my question, thanks it really helped:D \$\endgroup\$ – Ahmad Izzudin Alifyandra Apr 17 '16 at 10:38
  • \$\begingroup\$ can you help me how to make an efficient number checker im trying to add so that you can bet on individual numbers from 1 to 36, i cant seem to do it. im trying to do it by first generating an enumarable array from 1 to 36 but i cant make it check if the guess contains the number from the array. \$\endgroup\$ – Ahmad Izzudin Alifyandra Apr 17 '16 at 16:22
  • \$\begingroup\$ I'm mostly a code review user instead of Stackoverflow one. Your question seems to be more relevant for that site. Once you have a working code feel free to tag me under your question comment's so I can take a look at it.However checking if array contains a specific value is trivial just use array.Contains(your value). By the way why would you need an enumerable array anyway , why would you even need an array for that ? Just use a for loop 1-36 and check if the current guess has been matched during the iteration of those numbers. \$\endgroup\$ – Denis Apr 17 '16 at 16:28
  • \$\begingroup\$ alright thanks, can you explain me the for loop method? \$\endgroup\$ – Ahmad Izzudin Alifyandra Apr 18 '16 at 10:08
  • \$\begingroup\$ As I said implement a working code than I can help you improve it. \$\endgroup\$ – Denis Apr 18 '16 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.