private void buttonComputeScore_Click(object sender, EventArgs e) { //sets the delegate based on radio button selection if (radioButtonAdult.Checked == true) { scoreComputer = new ScoreDelegate(Scorer.AdultScore); } if (radioButtonChild.Checked == true) { scoreComputer = new ScoreDelegate(Scorer.ChildScore); } //creates variables for inputs and checks if they are numeric int correct; int incorrect; bool isCorrectNumeric = int.TryParse(textBoxCorrect.Text, out correct); bool isIncorrectNumeric = int.TryParse(textBoxIncorrect.Text, out incorrect); //if inputs are valid set the score label else display error msg if (isCorrectNumeric == true && isIncorrectNumeric == true) { labelScore.Text = scoreComputer(correct, incorrect).ToString(); } else { MessageBox.Show("Please make sure only numeric values are entered into the fields."); } }
// Instantiate the delegate and pass the Scorer methods. // Set the score to the score label private void DelegateScore() { scoreComputer = null; if (rdoAdult.Checked) { scoreComputer = new ScoreDelegate(Scorer.AdultScore); } else if (rdoChild.Checked) { scoreComputer = new ScoreDelegate(Scorer.ChildScore); } else { MessageBox.Show("Please pick a team!"); } // Parse text to int int correctAns = Int32.Parse(txtCorrect.Text.ToString()); int incorrectAns = Int32.Parse(txtIncorrect.Text.ToString()); // Pass correct and incorrect answer as arguement to the delegate and // save it as result value int result = scoreComputer(correctAns, incorrectAns); // Set the result to the score label lblScore.Text = "Score: " + result; }
private void btnCalculate_Click(object sender, EventArgs e) { ScoreDelegate scorer; if (rdoChildren.Checked) scorer = new ScoreDelegate(Scorer.ChildrenScore); else scorer = new ScoreDelegate(Scorer.AdultsScore); try { int numberCorrect = Convert.ToInt32(txtCorrect.Text); int numberIncorrect = Convert.ToInt32(txtIncorrect.Text); if (numberCorrect < 0 || numberIncorrect < 0) throw new FormatException(); int score = scorer(numberCorrect, numberIncorrect); lblScore.Text = score.ToString(); } catch (FormatException exception) { MessageBox.Show("Number correct and number incorrect must be a numbers 0 or greater"); } }
private void CleanDelegate() { Delegate[] functions = Score.GetInvocationList(); for (int i = 0; i < functions.Length; i++) { Score -= (ScoreDelegate)functions[i]; } }
public EventScore() { calls_ = new ScoreDelegate[] { ScoreWest, ScoreTan, ScoreGray, ScorePink, ScoreHouse, ScoreEvacuation, ScoreTree, ScoreWaves, ScoreTruck, ScorePlane, ScoreAmbulance, ScoreRunway, ScorePointer, ScorePeople, ScoreRobot, ScoreDebris, ScoreJunk }; }
private int GetScore(ScoreDelegate scoreCalculator) { int score = 0; for (int i = 0; i < m_ingredients.Count; i++) { score += scoreCalculator(m_ingredients[i]); } return(score); }
private void displayScoresBtn_Click(object sender, EventArgs e) { if (adultsRbtn.Checked) scoreComputer = new ScoreDelegate(Scorers.Adults); if (kidsRbtn.Checked) scoreComputer = new ScoreDelegate(Scorers.Children); string score = scoreComputer(Convert.ToInt32(correctTxtBox.Text.ToString()), Convert.ToInt32(incorrectTxtBox.Text.ToString())).ToString(); scoreLbl.Text = "Score = " + score; }
void OnGameOver(PlayerStates[] playerStates) { //I would like to know who killed the most //I would like to know who captured the flags the most ScoreDelegate killDelegate = ScoreByKillCount; string playerMostKilled = GetPlayerNameTopScore(playerStates, killDelegate); ScoreDelegate flagCapturedDelegate = ScoreByFlagCaptured; string playerMostCaptured = GetPlayerNameTopScore(playerStates, flagCapturedDelegate); Console.WriteLine(playerMostKilled); }
public void selectScoreComputer(int teamType) { switch (teamType) { case 0: scoreComputer = new ScoreDelegate(Scorer.AdultScore); break; case 1: scoreComputer = new ScoreDelegate(Scorer.ChildScore); break; } }
private void btScore_Click(object sender, EventArgs e) { if(rbadult.Checked) { ScoreComputer = new ScoreDelegate(Scorer.AdultScore); } else { ScoreComputer = new ScoreDelegate(Scorer.ChildScore); } lbScore.Text = ScoreComputer(Convert.ToInt32(tbcorrect.Text.Trim()), Convert.ToInt32(tbincorrect.Text.Trim())).ToString(); }
private void bCalculate_Click(object sender, EventArgs e) { // Get scores int correct = (int)nUpDownCorrect.Value; int incorrect = (int)nUpDownIncorrect.Value; // Determine team if (rbAdultTeam.Checked) scoreCalculator = new ScoreDelegate(Scorer.AdultScore); if (rbChildTeam.Checked) scoreCalculator = new ScoreDelegate(Scorer.ChildScore); // Set value of team score nUpDownTeamScore.Value = scoreCalculator(correct, incorrect); }
private void displayScoresBtn_Click(object sender, EventArgs e) { if (adultsRbtn.Checked) { scoreComputer = new ScoreDelegate(Scorers.Adults); } if (kidsRbtn.Checked) { scoreComputer = new ScoreDelegate(Scorers.Children); } string score = scoreComputer(Convert.ToInt32(correctTxtBox.Text.ToString()), Convert.ToInt32(incorrectTxtBox.Text.ToString())).ToString(); scoreLbl.Text = "Score = " + score; }
private void btn_ComputeScore_Click(object sender, EventArgs e) { if (rb_Adult.Checked == true) { scoreComputer = new ScoreDelegate(Scorer.AdultScore); } else scoreComputer = new ScoreDelegate(Scorer.ChildScore); int correct = Convert.ToInt32(tb_CorrectAnswers.Text); int incorrect = Convert.ToInt32(tb_IncorrectAnswers.Text); int score = scoreComputer(correct, incorrect); lbl_Score.Text = Convert.ToString(score); }
string GetPlayerNameMost(PlayerStats[] allPlayerStats, ScoreDelegate scoreCalculator) { string name = ""; int bestScore = 0; foreach (PlayerStats stats in allPlayerStats) { int score = scoreCalculator(stats); if (score > bestScore) { bestScore = score; name = stats.name; } } return(name); }
private string GetMVP(PlayerController[] allPlayers, ScoreDelegate scoreCalculator) { string name = ""; int bestScore = 0; foreach (PlayerController player in allPlayers) { int _score = scoreCalculator(player); if (bestScore > _score) { bestScore = _score; name = player.name; } } return(name); }
private void btn_ComputeScore_Click(object sender, EventArgs e) { if (rb_Adult.Checked == true) { scoreComputer = new ScoreDelegate(Scorer.AdultScore); } else { scoreComputer = new ScoreDelegate(Scorer.ChildScore); } int correct = Convert.ToInt32(tb_CorrectAnswers.Text); int incorrect = Convert.ToInt32(tb_IncorrectAnswers.Text); int score = scoreComputer(correct, incorrect); lbl_Score.Text = Convert.ToString(score); }
//private string GetPlayerNameMostKilled(PlayerStates[] playerStates) //{ // string name = ""; // int bestScore = 0; // foreach(PlayerStates state in playerStates) // { // int score = state.kills; // if(score > bestScore) // { // bestScore = score; // name = state.name; // } // } // return name; //} //private string GetPlayerNameMostCapturedFlag(PlayerStates[] playerStates) //{ // string name = ""; // int bestScore = 0; // foreach (PlayerStates state in playerStates) // { // int score = state.flagCaptured; // if (score > bestScore) // { // bestScore = score; // name = state.name; // } // } // return name; //} string GetPlayerNameTopScore(PlayerStates[] playerStates, ScoreDelegate scoreDelegate) { string name = ""; int bestScore = 0; foreach (PlayerStates state in playerStates) { int score = scoreDelegate(state); if (score > bestScore) { bestScore = score; name = state.name; } } return(name); }
//int ScoreByKillsCount(PlayerStats stats) //{ // return stats.kills; //} //int ScoreByFlagsCount(PlayerStats stats) //{ // return stats.flagsCaptured; //} private string GetPlayersMostTopScore(PlayerStats[] allPlayerStates, ScoreDelegate scoreCalculator) { string name = ""; int bestScore = 0; foreach (PlayerStats p in allPlayerStates) { int score = scoreCalculator(p); if (score > bestScore) { name = p.name; bestScore = score; } } return(name); }
string GetPlayerNameTopScore(PlayerStats[] allPlayerStats, ScoreDelegate scoreCalculator) { int bestScore = 0; string playerName = string.Empty; foreach (var playerStat in allPlayerStats) { int score = scoreCalculator(playerStat); if (score > bestScore) { bestScore = score; playerName = playerStat.Name; } } return(playerName); }
private PlayerStats GetPlayerStatsTopScore(PlayerStats[] allPlayerStats, ScoreDelegate scoreCalculator) { int topScoreIndex = 0; int i = 0; int bestScore = 0; foreach (PlayerStats stats in allPlayerStats) { int score = scoreCalculator(stats); if (score > bestScore) { bestScore = score; topScoreIndex = i; } i++; } return(allPlayerStats[topScoreIndex]); }
private void bCalculate_Click(object sender, EventArgs e) { // Get scores int correct = (int)nUpDownCorrect.Value; int incorrect = (int)nUpDownIncorrect.Value; // Determine team if (rbAdultTeam.Checked) { scoreCalculator = new ScoreDelegate(Scorer.AdultScore); } if (rbChildTeam.Checked) { scoreCalculator = new ScoreDelegate(Scorer.ChildScore); } // Set value of team score nUpDownTeamScore.Value = scoreCalculator(correct, incorrect); }
private void btnCompute_Click(object sender, EventArgs e) { try { correct = Int32.Parse(txtCorrect.Text); incorrect = Int32.Parse(txtIncorrect.Text); } catch (FormatException) { MessageBox.Show("Please enter valid numerical values in all fields"); } if (rbChildren.Checked) // We will use ChildrenScore scoreComputer = new ScoreDelegate(Scorer.ChildrenScore); else // We will use AdultScore scoreComputer = new ScoreDelegate(Scorer.AdultScore); // Our delegate will return int -- convert to string for displaying lblScore.Text = scoreComputer(correct, incorrect).ToString(); }
private void button1_Click(object sender, EventArgs e) { //determine which rdo clicked if (rdoAdult.Checked) { //bind delegate to adult score method scoreComputer = new ScoreDelegate(Scorer.adultScore); } else { scoreComputer = new ScoreDelegate(Scorer.childrenScore); } //parse text to ints int parseCorrect = Int32.Parse(txtCorrect.Text); int parseIncorrect = Int32.Parse(txtIncorrect.Text); int resultScore = scoreComputer(parseCorrect, parseIncorrect); //Run delegate method and output score lbResult.Text = "" + resultScore + " points."; }
public void OnGameOver(PlayerStats[] allPlayerStats) { var instance1 = new ScoreDelegate(ScoreByFlagsCapped); Console.WriteLine($"==== Top Players ===="); string playNameMostKills = GetPlayerNameMost(allPlayerStats, stats => stats.kills); string playNameMostFlags = GetPlayerNameMost(allPlayerStats, stats => stats.flagsCaptured); string playNameMostFlagsV2 = GetPlayerNameMost(allPlayerStats, ScoreByFlagsCapped); string playNameMostFlagsV3 = GetPlayerNameMost(allPlayerStats, instance1); Console.WriteLine($"Most kills: {playNameMostKills}"); Console.WriteLine($"Most flags: {playNameMostFlags}"); Console.WriteLine($"Most flagsV2: {playNameMostFlagsV2}"); Console.WriteLine($"Most flagsV3: {playNameMostFlagsV3}"); Console.WriteLine($"\n==== Player Stats ===="); foreach (var player in allPlayerStats) { Console.WriteLine(player.ToString()); } }
/// Finds the item in [collection] whose score is lowest. /// /// The score for an item is determined by calling [callback] on it. Returns /// `null` if the [collection] is `null` or empty. public T FindLowest <T>(List <T> collection, ScoreDelegate <T> callback) where T : class { if (collection == null) { return(null); } T bestItem = null; var bestScore = int.MaxValue; foreach (T item in collection) { var score = callback(item); if (score < bestScore) { bestItem = item; bestScore = score; } } return(bestItem); }
/// Finds the item in [collection] whose score is highest. /// /// The score for an item is determined by calling [callback] on it. Returns /// `null` if the [collection] is `null` or empty. public T FindHighest <T>(List <T> collection, ScoreDelegate <T> callback) where T : class { if (collection == null) { return(null); } T bestItem = null; int bestScore = 0; foreach (T item in collection) { var score = callback(item); if (score > bestScore) { bestItem = item; bestScore = score; } } return(bestItem); }
private void btnComputeScore_Click(object sender, EventArgs e) { ScoreDelegate scoreComputer; if (rbAdult.Checked) { scoreComputer = new ScoreDelegate(Scorer.AdultScore); } else { //Must be Child selected scoreComputer = new ScoreDelegate(Scorer.ChildScore); } //Get the scores from the form //TODO Catch exceptions etc int correct = Int32.Parse(tbCorrect.Text); int incorrect = Int32.Parse(tbIncorrect.Text); //get score from delegate int score = scoreComputer(correct, incorrect); //set score on the form lblScore.Text = score.ToString(); }
private void btnCompute_Click(object sender, EventArgs e) { try { correct = Int32.Parse(txtCorrect.Text); incorrect = Int32.Parse(txtIncorrect.Text); } catch (FormatException) { MessageBox.Show("Please enter valid numerical values in all fields"); } if (rbChildren.Checked) // We will use ChildrenScore { scoreComputer = new ScoreDelegate(Scorer.ChildrenScore); } else // We will use AdultScore { scoreComputer = new ScoreDelegate(Scorer.AdultScore); } // Our delegate will return int -- convert to string for displaying lblScore.Text = scoreComputer(correct, incorrect).ToString(); }
private void Form1_Load(object sender, EventArgs e) { scoreCalculator = null; }