Skip to content

Instantly share code, notes, and snippets.

@janellbaxter
Created February 20, 2017 00:08
Show Gist options
  • Save janellbaxter/e6054bfcc13b92a5749dd1d0b085bec0 to your computer and use it in GitHub Desktop.
Save janellbaxter/e6054bfcc13b92a5749dd1d0b085bec0 to your computer and use it in GitHub Desktop.
7.2 Logical Operators; Programming is Fun: C# Adventure Game (Learn C#)
/*
* 7.2 Logical Operators
* Example code from Programming is Fun: C# Adventure Game
* Learn C# (for beginners)
* http://programmingisfun.com/learn/c-sharp-adventure-game
*/
using System;
namespace LogicalOperators
{
class Program
{
static Random random;
static void Main(string[] args)
{
Verify();
}
static void Verify()
{
bool Key = false;
bool Coin = false;
Console.WriteLine("Using a random coin toss to determine key and/or coin:");
Key = CoinToss(random);
Console.WriteLine("Key: " + Key);
Coin = CoinToss(random);
Console.WriteLine("Coin: " + Coin);
if (Key || Coin)
{
Console.WriteLine("OR: Player has found a coin and/or a key.");
}
else
{
Console.WriteLine("Player has found neither coin or key.");
}
if (Key && Coin)
{
Console.WriteLine("AND: Player has found both coin and key.");
}
else
{
Console.WriteLine("Player has not found both.");
}
Console.WriteLine("Press enter to try again, X to quit.");
string input = Console.ReadLine();
if (input.ToLower() != "x")
{
//recursive call to try again:
Verify();
}
}
static bool CoinToss(Random random)
{
random = new Random();
if (random.Next(2) == 0)
{
return true;
}
else
{
return false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment