Last active
September 13, 2020 15:22
-
-
Save Redouane64/049d170471f2c8cf4a6ada0f1865bf2e to your computer and use it in GitHub Desktop.
Football Tournament Simulation
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; | |
| namespace FootballTournamentSimulation | |
| { | |
| class Program | |
| { | |
| // A team model. | |
| class Team | |
| { | |
| public string Name { get; } | |
| public float Lambda { get; } | |
| readonly Random generator = new Random(); | |
| public Team(string name) | |
| { | |
| this.Name = name; | |
| this.Lambda = (float)generator.NextDouble(); | |
| } | |
| public override string ToString() | |
| { | |
| return this.Name; | |
| } | |
| } | |
| // Some teams. | |
| const int NumberOfTeams = 8; | |
| readonly static Team[] Teams = new Team[NumberOfTeams] | |
| { | |
| new Team("ARS"), new Team("MAN"), new Team("MCI"), new Team("BOU"), | |
| new Team("WOL"), new Team("FUL"), new Team("LIV"), new Team("CHE") | |
| }; | |
| static void Main(string[] args) | |
| { | |
| var tournament = new Queue<Team>(NumberOfTeams); | |
| // Add teams to tournment queue. | |
| Array.ForEach(Teams, tournament.Enqueue); | |
| while (true) | |
| { | |
| // Enqueue the first two from `tournament` queue | |
| // Determine the winner. until only one team remains in | |
| // queue. this remaining team is the tournament winner. | |
| Team teamA = tournament.Dequeue(); | |
| Team teamB = null; | |
| if(!tournament.TryDequeue(out teamB)) | |
| { | |
| Console.WriteLine("Tournament Winner is {0} \n", teamA.Name); | |
| break; | |
| } | |
| Team winner = DetermineWinner(teamA, teamB); | |
| tournament.Enqueue(winner); | |
| Console.WriteLine("{0} x {1} - {2} Wins.", teamA, teamB, winner); | |
| } | |
| } | |
| static float PoissonFunction(float lambda) | |
| { | |
| var t = 1; | |
| var m = 1; | |
| return (float)((lambda * t / m) * Math.Exp(-lambda * t)); | |
| } | |
| static Team DetermineWinner(Team a, Team b) | |
| { | |
| return PoissonFunction(a.Lambda) > PoissonFunction(b.Lambda) ? a : b; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment