/// <summary> /// Engine of penalty kick. /// </summary> /// <param name="footballMatch">The football match.</param> /// <param name="isTurnA">If set to <c>true</c> then it is turn of <see cref="Team"/> A.</param> private void PenaltyEngine(ref FootballMatch footballMatch, bool isTurnA) { if (isTurnA) { Debug.Write($"{footballMatch.ReturnTeamA().name} is executing this penalty... "); if (IfTeamScored(5)) { Debug.Write($"and they scored a beautiful GOAL!"); footballMatch.AddScoreToTeamA(); } else { Debug.Write($"and they missed."); } } else { Debug.Write($"{footballMatch.ReturnTeamB().name} is executing this penalty... "); if (IfTeamScored(5)) { Debug.Write($"and they scored a beautiful GOAL!"); footballMatch.AddScoreToTeamB(); } else { Debug.Write($"and they missed."); } } }
/// <summary> /// <see cref="Engine(ref FootballMatch, int, int, int)" /> is the specified football match engine, which is responsible for executing play simulation. /// </summary> /// <param name="footballMatch">The football match.</param> /// <param name="isDeathmatch">If set to <c>true</c> than this is deathmatch.</param> private void Engine(ref FootballMatch footballMatch, bool isDeathmatch) { min = 600000; max = 900000; int matchTime = playTime / 1000 / 60; int currentTime; bool breaktime = false; Debug.WriteLine($"00': Match started."); for (int i = random.Next(min, max); i <= this.playTime; i += random.Next(min, max)) { currentTime = i / 1000 / 60; Debug.Write($"{currentTime}': The ball is in possession of "); if (Chance() % 2 == 0) { // Team A has the ball Debug.Write($"{footballMatch.ReturnTeamA().name}. "); inPossession = footballMatch.ReturnTeamA(); if (IfTeamScored(3)) { Debug.WriteLine($"\tThey scored a beautiful GOAL!"); footballMatch.AddScoreToTeamA(); } else { Debug.WriteLine($" \tThey shot... and missed."); } } else { // Team B has the ball Debug.Write($"{footballMatch.ReturnTeamB().name}. "); inPossession = footballMatch.ReturnTeamB(); if (IfTeamScored(3)) { Debug.WriteLine($"\tThey scored a beautiful GOAL!"); footballMatch.AddScoreToTeamB(); } else { Debug.WriteLine($" \tThey shot... and missed."); } } //Thread.Sleep(i / 1000); //Uncomment for real time. if (breaktime == false) { if ((i + max) > this.playTime / 2 && breaktime == false) { Debug.WriteLine($"{this.playTime / 2 / 1000 / 60}': First half ended. Braktime.\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA()} : {footballMatch.GetScoreOfTeamB()} {footballMatch.ReturnTeamB().name}."); breaktime = true; i += max - min; Debug.WriteLine($"{this.playTime / 2 / 1000 / 60}': Second half on the go."); } } else if ((i + min) > this.playTime) { i -= min / 10; } } Debug.WriteLine($"{matchTime}': Regular time's up."); if (isDeathmatch == true && footballMatch.GetScoreOfTeamA() == footballMatch.GetScoreOfTeamB()) { Debug.WriteLine($"90': Regular time wasn't enough.\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA()} : {footballMatch.GetScoreOfTeamB()} {footballMatch.ReturnTeamB().name}."); Debug.WriteLine($"90': Play-off time! It's a lottery now."); matchTime += playoffTime / 1000 / 60; breaktime = false; currentTime = playTime / 1000 / 60; min = 300000; max = 600000; for (int i = random.Next(min, max); i <= playoffTime; i += random.Next(min, max)) { currentTime = (playTime + i) / 1000 / 60; Debug.Write($"{currentTime}': The ball is in possession of "); if (Chance() % 2 == 0) { // Team A has the ball Debug.Write($"{footballMatch.ReturnTeamA().name}. "); inPossession = footballMatch.ReturnTeamA(); if (IfTeamScored(3)) { Debug.WriteLine($"\tThey scored a beautiful GOAL!"); footballMatch.AddScoreToTeamA(); } else { Debug.WriteLine($" \tThey shot... and missed."); } } else { // Team B has the ball Debug.Write($"{footballMatch.ReturnTeamB().name}. "); inPossession = footballMatch.ReturnTeamB(); if (IfTeamScored(3)) { Debug.WriteLine($"\tThey scored a beautiful GOAL!"); footballMatch.AddScoreToTeamB(); } else { Debug.WriteLine($" \tThey shot... and missed."); } } //Thread.Sleep(i / 1000); if (breaktime == false) { if ((i + max) > this.playoffTime / 2 && breaktime == false) { Debug.WriteLine($"{playoffTime / 2 / 1000 / 60 + playTime / 1000 / 60}': First half ended. Braktime.\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA()} : {footballMatch.GetScoreOfTeamB()} {footballMatch.ReturnTeamB().name}."); breaktime = true; i += max - min; } } else if ((i + min) > this.playoffTime) { i -= min / 10; } } Debug.WriteLine($"{(playTime + playoffTime) / 1000 / 60}': Play-off time's up."); if (footballMatch.GetScoreOfTeamA() == footballMatch.GetScoreOfTeamB()) { Debug.WriteLine($"{(playTime + playoffTime) / 1000 / 60}': It is time for penalty kicks."); PenaltyKicksEngine(ref footballMatch); Debug.WriteLine($"Penalty kicks ended. The winner is: {(footballMatch.GetScoreOfTeamA() > footballMatch.GetScoreOfTeamB() ? footballMatch.ReturnTeamA().name : footballMatch.ReturnTeamB().name)}."); } } Debug.WriteLine($"{matchTime}': Match ends with result:\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA()} : {footballMatch.GetScoreOfTeamB()} {footballMatch.ReturnTeamB().name}."); if (footballMatch.GetScoreOfTeamA() > footballMatch.GetScoreOfTeamB()) { footballMatch.ReturnTeamA().AddWin(); footballMatch.ReturnTeamB().AddLoss(); } else if (footballMatch.GetScoreOfTeamB() > footballMatch.GetScoreOfTeamA()) { footballMatch.ReturnTeamB().AddWin(); footballMatch.ReturnTeamA().AddLoss(); } else { footballMatch.ReturnTeamA().AddDraw(); footballMatch.ReturnTeamB().AddDraw(); } footballMatch.ReturnTeamA().AddMatchPlayed(); footballMatch.ReturnTeamB().AddMatchPlayed(); System.Threading.Thread.Sleep(100); }
public async void SimulateFootballMatchAsync(FootballMatch footballMatch, bool isDeathmatch) { Debug.WriteLine($"----------------------------------------------------------"); Engine(ref footballMatch, isDeathmatch); Debug.WriteLine($"----------------------------------------------------------"); }
/// <summary> /// Engine of the penalty kicks competition. /// </summary> /// <param name="footballMatch">The football match.</param> private void PenaltyKicksEngine(ref FootballMatch footballMatch) { bool isTurnA; int regularGoalsCount = footballMatch.GetScoreOfTeamA(); if (Chance() % 2 == 0) { // Starts Team A. isTurnA = true; PenaltyEngine(ref footballMatch, isTurnA); } else { // Starts Team B. isTurnA = false; PenaltyEngine(ref footballMatch, isTurnA); } Debug.WriteLine($"\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA() - regularGoalsCount} : {footballMatch.GetScoreOfTeamB() - regularGoalsCount} {footballMatch.ReturnTeamB().name}"); int tenShots = 1; while (tenShots < 10) { if (!isTurnA) { // It's Team A turn. isTurnA = true; PenaltyEngine(ref footballMatch, isTurnA); tenShots++; } else { // It's Team B turn. isTurnA = false; PenaltyEngine(ref footballMatch, isTurnA); tenShots++; } Debug.WriteLine($"\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA() - regularGoalsCount} : {footballMatch.GetScoreOfTeamB() - regularGoalsCount} {footballMatch.ReturnTeamB().name}"); if (footballMatch.GetScoreOfTeamA() == footballMatch.GetScoreOfTeamB() + 3 | footballMatch.GetScoreOfTeamB() == footballMatch.GetScoreOfTeamA() + 3) { // If it's 3:0 / 4:1 / 5:2 in penalties. Debug.WriteLine("That's over now."); break; } //TODO: Add elese if here to check if the last penalty execution is necessary. //if(footballMatch.GetScoreOfTeamA()) } Debug.WriteLine("MORE"); while (footballMatch.GetScoreOfTeamA() == footballMatch.GetScoreOfTeamB() + 1 | footballMatch.GetScoreOfTeamA() + 1 == footballMatch.GetScoreOfTeamB()) { if (footballMatch.GetScoreOfTeamA() == footballMatch.GetScoreOfTeamB() + 1) { if (!isTurnA) { isTurnA = true; int score = footballMatch.GetScoreOfTeamA(); PenaltyEngine(ref footballMatch, isTurnA); if (footballMatch.GetScoreOfTeamA() != score) { break; } } else { isTurnA = false; int score = footballMatch.GetScoreOfTeamB(); PenaltyEngine(ref footballMatch, isTurnA); if (footballMatch.GetScoreOfTeamB() == score) { break; } } } else if (footballMatch.GetScoreOfTeamA() + 1 == footballMatch.GetScoreOfTeamB()) { if (!isTurnA) { isTurnA = true; int score = footballMatch.GetScoreOfTeamA(); PenaltyEngine(ref footballMatch, isTurnA); if (footballMatch.GetScoreOfTeamA() == score) { break; } } else { isTurnA = false; int score = footballMatch.GetScoreOfTeamB(); PenaltyEngine(ref footballMatch, isTurnA); if (footballMatch.GetScoreOfTeamB() != score) { break; } } } Debug.WriteLine($"\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA() - regularGoalsCount} : {footballMatch.GetScoreOfTeamB() - regularGoalsCount} {footballMatch.ReturnTeamB().name}"); } Debug.WriteLine("DRAW"); while (footballMatch.GetScoreOfTeamA() == footballMatch.GetScoreOfTeamB()) { if (!isTurnA) { isTurnA = true; PenaltyEngine(ref footballMatch, isTurnA); } else { isTurnA = false; PenaltyEngine(ref footballMatch, isTurnA); } Debug.WriteLine($"\t{footballMatch.ReturnTeamA().name} {footballMatch.GetScoreOfTeamA() - regularGoalsCount} : {footballMatch.GetScoreOfTeamB() - regularGoalsCount} {footballMatch.ReturnTeamB().name}"); } }
/// <summary> /// Starts the football <see cref="Cup" />. Execution intended in console mode. /// </summary> /// <returns> /// The winner <see cref="Team" />. /// </returns> /// <exception cref="Exception">Not enough teams or referees in a cup. /// or /// Not even number of teams in a cup.</exception> public Team StartFootballCup() { if (isFinished) { throw new Exception("Cup already played out."); } int teamsInCup = listOfTeamsInCup.Count(); int refereesInCup = listOfRefereesInCup.Count(); if (teamsInCup < 8 && refereesInCup < 3) { throw new Exception("Not enough teams or referees in a cup."); } if (teamsInCup % 2 != 0) { throw new Exception("Not even number of teams in a cup."); } if (teamsInCup != 8 & teamsInCup != 16 & teamsInCup != 24) { throw new Exception("Need to be 8, 16 or 24 teams for a cup."); } int xx = 1; int yy = listOfTeamsInCup.Count(); while ((yy / 2) > 1) { xx++; yy /= 2; } for (int h = 0; h < xx; h++) { List <Team> tempList = new List <Team>(listOfTeamsInCup); for (int i = 0; i < teamsInCup / 2; i++) { TwoTeams teams = RandomTeamsForMatch(tempList); Team teamA = teams.GetHostTeam; Team teamB = teams.GetGuestTeam; ThreeReferees referees = RandomRefereesForMatch(); Referee mainReferee = referees.GetMainReferee; Referee suppRef1 = referees.GetSupportingReferee1; Referee suppRef2 = referees.GetSupportingReferee2; FootballMatch footballMatch = new FootballMatch(mainReferee, teamA, teamB, suppRef1, suppRef2); matchEngine.SimulateFootballMatch(ref footballMatch, true); if (footballMatch.GetScoreOfTeamA() > footballMatch.GetScoreOfTeamB()) { listOfTeamsInCup.Remove(teamB); } else if (footballMatch.GetScoreOfTeamB() > footballMatch.GetScoreOfTeamA()) { listOfTeamsInCup.Remove(teamA); } tempList.Remove(teamA); tempList.Remove(teamB); } teamsInCup /= 2; } listOfTeamsInCup.ElementAt(0).AddTournamentWon(); isFinished = true; return(listOfTeamsInCup.ElementAt(0)); }