Last active
April 25, 2016 00:36
-
-
Save adam-phillipps/5cfc3ad467d7c4d120a2403be99cb5f7 to your computer and use it in GitHub Desktop.
This class reports a win or loss on each attempt
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; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
using System.Threading; | |
using System.IO; | |
namespace Lottery_program | |
{ | |
public partial class Form1 : Form | |
{ | |
// this is a magic number. i don't know where you want it. | |
// when you figure it out, it'd be good to pass it to the lottocard | |
// class so it can be instantiated with it and the number can change the | |
// int digitsInLottoNumber field. | |
private int numberOfNumbers = 6; | |
private int numberOfTries; | |
public Form1() | |
{ | |
//Thread t = new Thread(new ThreadStart(SplashStart)); | |
//t.Start(); | |
//Thread.Sleep(2500); | |
InitializeComponent(); | |
addPrizeTable(); | |
//t.Abort(); | |
} | |
private void addPrizeTable() | |
{ | |
string[] words = | |
{ | |
" 1 of 6 Numbers matched = 1XBet\n", | |
"2 of 6 Numbers matched = 10XBet\n", | |
"3 of 6 Numbers matched = 100XBet\n", | |
"4 of 6 Numbers matched = 1000XBet\n", | |
"5 of 6 Numbers matched = 10000XBet\n", | |
"6 of 6 Numbers matched = Jackpot**\n\n\n", | |
"**You have a 1 in 1192052400 chance of getting the JackPot via 6 of 6 numbers." | |
// ((=((FACT(100))/(FACT(100-6)*FACT(6))))) | |
// ***used to calculate combinations from picking 6 out of 100 numbers. | |
}; | |
prizeTable.AppendText(Environment.NewLine); | |
for (int i = 0; i < words.Length; i++) | |
{ | |
string word = words[i]; | |
{ | |
prizeTable.AppendText(word); | |
prizeTable.AppendText(" "); | |
prizeTable.SelectAll(); | |
prizeTable.SelectionIndent += 5; | |
prizeTable.SelectionRightIndent += 5; | |
prizeTable.SelectionLength = 0; | |
} | |
} | |
} | |
private void Drawnumbers_click(object sender, EventArgs e) | |
{ | |
// this should happen in its own class but how do i | |
// get more than one input from the user on the button push | |
// and keep track of previous tries? | |
int numberOfLottoNumbers; | |
int.TryParse(Numberofnumbers.Text, out numberOfLottoNumbers); | |
LottoCard actualNumbers = new LottoCard( | |
countOfNumbersInCard: numberOfLottoNumbers); | |
Func<LottoCard, bool> isWin = tempCard => | |
tempCard.IsSuccess(actualNumbers) && tempCard.TurnsRemaining > 0; | |
LottoCard userCard = GetUserGuess(); | |
if (isWin(userCard)) | |
{ | |
PopulateActualNumbers(actualNumbers); | |
ResultsandPrizeWon.Text = Reporter.In(userCard, actualNumbers).Out( | |
@"C:\Users\adam\2530\Lottery\Lottery program\Lottery program\LottoReports.csv"); | |
Thread.Sleep(1000); | |
} | |
else | |
{ | |
PopulateActualNumbers(actualNumbers); | |
ResultsandPrizeWon.Text = Reporter.In(userCard, actualNumbers).Out( | |
@"C:\Users\adam\2530\Lottery\Lottery program\Lottery program\LottoReports.csv"); | |
Thread.Sleep(1000); | |
ClearUserGuessFields(); | |
ClearActualNumbers(); | |
} | |
} | |
private void PopulateActualNumbers(LottoCard numbers) | |
{ | |
dnum1.Text = numbers.LottoNumber[0].ToString(); | |
dnum2.Text = numbers.LottoNumber[1].ToString(); | |
dnum3.Text = numbers.LottoNumber[2].ToString(); | |
dnum4.Text = numbers.LottoNumber[3].ToString(); | |
dnum5.Text = numbers.LottoNumber[4].ToString(); | |
dnum6.Text = numbers.LottoNumber[5].ToString(); | |
} | |
private LottoCard GetUserGuess() | |
{ | |
int[] tempUserCard = new int[numberOfNumbers]; | |
string[] nums = | |
{ num1.Text, num2.Text, num3.Text, num4.Text, num5.Text, num6.Text }; | |
for (int i = 0; i < this.numberOfNumbers; i++) | |
int.TryParse(nums[i], out tempUserCard[i]); | |
return new LottoCard( | |
numbers: tempUserCard, countOfNumbersInCard: numberOfNumbers, turns: AdjustedNumberOfTries()); | |
} | |
private int AdjustedNumberOfTries() | |
{ | |
return --numberOfTries; | |
} | |
private void Resetbutton_click(object sender, EventArgs e) | |
{ | |
JackPotbox.Text = "100000000"; | |
Fundsbox.Text = "1000"; | |
Totalwinningsbox.Text = ""; | |
trackBarnumberofdraws.Value = 1; | |
trackBarnumberofnumbers.Value = 1; | |
Numberofnumbers.Text = "1"; | |
numofdraws.Text = "1"; | |
Betamount.Text = "1"; | |
num1.Clear(); | |
num2.Clear(); | |
num3.Clear(); | |
num4.Clear(); | |
num5.Clear(); | |
num6.Clear(); | |
// BetRest(); | |
} | |
private void ClearUserGuessFields() | |
{ | |
num1.Clear(); | |
num2.Clear(); | |
num3.Clear(); | |
num4.Clear(); | |
num5.Clear(); | |
num6.Clear(); | |
} | |
private void ClearActualNumbers() | |
{ | |
// what are the TextBox'es called? | |
} | |
private void trackBarnumberofnumbers_Scroll(object sender, EventArgs e) | |
{ | |
// this needs to rework the GUI so the correct number | |
// of fields appears | |
// numberOfNumbers = trackBarnumberofnumbers.Value; | |
Numberofnumbers.Text = numberOfNumbers.ToString(); | |
} | |
private void trackBarnumberofdraws_Scroll(object sender, EventArgs e) | |
{ | |
numberOfTries = trackBarnumberofdraws.Value; | |
numofdraws.Text = numberOfTries.ToString(); | |
} | |
private void numofdraws_TextChanged(object sender, EventArgs e) | |
{ | |
} | |
//public void SplashStart() | |
//{ | |
// Application.Run(new loadingScreen()); | |
//} | |
} | |
} |
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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Lottery_program | |
{ | |
public class LottoCard | |
{ | |
private const int lottoDigitMaxValue = 99; | |
private int digitsInLottoNumber; | |
private int[] lottoNumber; | |
public int[] LottoNumber | |
{ | |
get { return lottoNumber; } | |
private set { lottoNumber = value; } | |
} | |
public bool IsWin { get; private set; } | |
private int tR; | |
public int TurnsRemaining | |
{ | |
get { return tR--; } | |
private set { tR = value; } | |
} | |
public LottoCard(int countOfNumbersInCard, int turns = 5) | |
{ | |
digitsInLottoNumber = countOfNumbersInCard; | |
tR = turns; | |
LottoNumber = GenerateRandomLottoNumber(); | |
} | |
public LottoCard(int[] numbers, int countOfNumbersInCard, int turns = 5) | |
{ | |
digitsInLottoNumber = countOfNumbersInCard; | |
tR = turns; | |
LottoNumber = numbers; | |
} | |
public bool IsSuccess(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; | |
IsWin = 50 <= percent(correctGuesses, digitsInLottoNumber); // better than 50% right | |
return IsWin; | |
} | |
public override string ToString() | |
{ | |
return $"{String.Join("-", LottoNumber)}"; | |
} | |
private int[] GenerateRandomLottoNumber() | |
{ | |
Random rnd = new Random(); | |
int[] workingRandomNumbers = new int[digitsInLottoNumber]; | |
for (int i = 0; i < digitsInLottoNumber; i++) | |
workingRandomNumbers[i] = rnd.Next(lottoDigitMaxValue); | |
return workingRandomNumbers; | |
} | |
} | |
} |
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
date | playerNumbers | actualNumbers | win |
---|
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; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Lottery_program | |
{ | |
public static class Reporter | |
{ | |
public static string In(LottoCard user, LottoCard computer) | |
{ | |
return $"{DateTime.Now},{user},{computer}"; | |
} | |
public static string ReadIn(string fileName) | |
{ | |
try | |
{ | |
StringBuilder sb = new StringBuilder(); | |
using (StreamReader reader = new StreamReader(File.OpenRead(fileName))) | |
{ | |
string line; | |
IList<string> tempValues = new List<string>(); | |
bool isFirst = true; | |
while ((line = reader.ReadLine()) != null) | |
{ | |
if (!isFirst) | |
sb.Append(line.Split(',')); | |
else | |
isFirst = false; | |
} | |
return sb.ToString(); | |
} | |
} | |
catch (Exception e) | |
{ | |
return $"{e.Message}"; | |
} | |
} | |
public static string Out(this string body, string fileName) | |
{ | |
try | |
{ | |
using (StreamWriter sw = File.AppendText(fileName)) | |
{ | |
sw.WriteLine(body); | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
return body; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment