0

Just trying to make a coin flip game, but also trying to make the coin which equals a random number 0 or 1 to convert to a string type "heads" or "tails". New to programming so please don't judge if it seems amateurish. Thank you.

  namespace TestCoin2
 {
        class Program
   {  

    static void Main(string[] args)
    {
        int coin;// this will hold my random int numbers.

        string userInput;// This will hold all my user input/ answers.




        Console.WriteLine("Hello, Pick Heads or Tails:");
        userInput =Console.ReadLine();

        Random rng = new Random();

        coin = rng.Next(0, 2);


        string myString2;
        string myString;
        if (coin == 0)
        {
            myString = coin.ToString("heads");
        }

        else if (coin == 1)
        {
             myString2 = coin.ToString("tails"); <error under myString2
        }

       if (myString && userInput == "heads")
        {

            Console.WriteLine("You picked Right! Heads! YOU WIN!");
        }

        else if (coin == 1 && userInput == "Tails")
        {

            Console.WriteLine("You picked Right! Tails! YOU WIN!");
        }

        else
        {
        Console.WriteLine("You picked Wrong! it was..." + myString); <error

        }

        Console.ReadLine();
    }



}

}

Rom
  • 53
  • 1
  • 3
  • You don't need to declare a second `mystring2`, just use the same `mystring` variable. Also, look up [string comparison](http://www.google.com.au/search?q=c%23+string+comparison). – Ian Jan 17 '16 at 23:02
  • Since Heads and Tails are 2 sides of the same thing, the result is one or the other, so one var can store the result. That way you can get rid of the `if (coin)` block – Ňɏssa Pøngjǣrdenlarp Jan 17 '16 at 23:04

3 Answers3

1

Since you are converting a fixed number of ints to something else. Your best bet would be to define an enum.

public enum CoinSide
{
    Heads = 0,
    Tails = 1
}

Then you can just cast the int to the enum and it will output the side.

var rng = new Random();
var coin = rng.Next(0,2);
Console.WriteLine((CoinSide)coin);

If you need to compare them you can use this.

if (((CoinSide)coin).ToString() == "Heads")
    Console.WriteLine("Winner");

Full Solution

//Accept user input
Console.WriteLine("Hello, Pick Heads or Tails:");
var userInput = Console.ReadLine();

//Create and flip the coin
var rng = new Random();
var coin = (CoinSide)rng.Next(0, 2);

//Compare input to coin
if (coin.ToString() == userInput)
    Console.WriteLine("You picked Right! {0}! YOU WIN!", coin);
else
    Console.WriteLine("You picked Wrong! it was... {0}", coin);
Matt Rowland
  • 4,497
  • 3
  • 23
  • 32
0

You can simply use the ?: operator like this:

myString = coin == 0 ? "heads" : "tails";
Yacoub Massad
  • 26,006
  • 2
  • 31
  • 56
0

To make it more readable you could use enum like below. Instead of 0 and 1, you can use any number that you prefer. Define your enum in the Class and use it in the method.

public enum CoinSides{ Heads = 0, Tails = 1}

You could rewrite your complete code as below,

 namespace TestCoin2
{

class Program
{
    public enum CoinSides { Heads = 0, Tails = 1 }

    static void Main(string[] args)
    {
        string userInput;// This will hold all my user input/ answers.
        Console.WriteLine("Hello, Pick Heads or Tails:");
        userInput = Console.ReadLine();

        int coin;// this will hold my random int numbers.
        Random rng = new Random();
        coin = rng.Next(0, 2);

        if (coin == Convert.ToInt32(CoinSides.Heads) && userInput == CoinSides.Heads.ToString("D"))
        {
            Console.WriteLine("You picked Right! Heads! YOU WIN!");
        }
        else if (coin == Convert.ToInt32(CoinSides.Tails) && userInput == CoinSides.Tails.ToString("D"))
        {
            Console.WriteLine("You picked Right! Tails! YOU WIN!");
        }
        else
        {
            Console.WriteLine("You picked Wrong! it was..." + myString);
        }

        Console.ReadLine();
    }
}
}
Martin
  • 594
  • 4
  • 10
  • I'm not to familiar with enums. Can you show me how the full program looks with the enum implemented? Thank you – Rom Jan 17 '16 at 23:58
  • I have updated the answer above. Please check if this works for you. – Martin Jan 18 '16 at 00:17