Created
April 3, 2016 05:40
-
-
Save adam-phillipps/f735e93b08678180b780e5c822e5ac87 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Casino | |
{ | |
class CasinoConsole | |
{ | |
private static LottoCard lottoCard = new LottoCard(); | |
public static int turnsRemaining = 3; | |
static void Main(string[] args) | |
{ | |
LottoCard playerGuess; | |
do | |
{ | |
playerGuess = PlayerInteraction(); // this is where the gui can be put in | |
} while (!playerGuess.GetSuccess(lottoCard) && 0 > turnsRemaining--); | |
Console.WriteLine(playerGuess.GetSuccess(lottoCard)); | |
Console.ReadLine(); | |
} | |
private static LottoCard PlayerInteraction(string additionalGreeting = "") | |
{ | |
const int digitsInLottoNumber = 5; | |
Console.WriteLine($"{additionalGreeting} i need {digitsInLottoNumber} digits (1 - 99)."); | |
int[] tempGuess = new int[digitsInLottoNumber]; | |
for (int i = 0; i < 5; i++) | |
int.TryParse(Console.ReadLine(), out tempGuess[i]); | |
return new LottoCard(tempGuess); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace Casino | |
{ | |
class LottoCard | |
{ | |
private const int digitsInLottoNumber = 5; | |
private const int lottoDigitMaxValue = 99; | |
private int[] lottoNumber; | |
public LottoCard() | |
{ | |
lottoNumber = GenerateRandomLottoNumber(); | |
} | |
public LottoCard(int[] nums) | |
{ | |
lottoNumber = nums; | |
} | |
public bool GetSuccess(LottoCard otherCard) | |
{ | |
int correctGuesses = 0; | |
Func<int, int, int> percent = | |
(int num, int denom) => Convert.ToInt16(100.0 * ((1.0 * num) / (1.0 * denom))); | |
for (int i = 0; i < digitsInLottoNumber; i++) | |
correctGuesses += lottoNumber[i] == otherCard.lottoNumber[i] ? 1 : 0; | |
return 50 <= percent(correctGuesses, digitsInLottoNumber); // better than 50% right | |
} | |
private int[] GenerateRandomLottoNumber() | |
{ | |
Random rnd = new Random(); | |
int[] tempNum = new int[digitsInLottoNumber]; | |
for (int i = 0; i < digitsInLottoNumber; i++) | |
tempNum[i] = rnd.Next(lottoDigitMaxValue); | |
return tempNum; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment