World Cup Simulation

Created in April 2019 as part 2 of a sport simulation project. GitHub Link


Methodology

For this simulation, I decided to simulate the world cup itself and a qualification round (not exactly the same as how qualifiers are run though). To rank the teams, I used the official FIFA/Coca-Cola rankings.

Implementation of Compete methods

The following code is found in the Team.java file.

  public void compete(Team rival) {
    // temp just for testing 
    double constant;
    double coeff = .6;
    double modifier = 400;
    if (this.ranking > rival.ranking) {
      constant = (coeff)*((rival.ranking - modifier)/(this.ranking - modifier));
    }
    else {
      constant = (coeff)*((this.ranking - modifier)/(rival.ranking - modifier));
    }
    double random = Math.random();
    if (random < constant) {
      this.ties++;
      rival.ties++;
      this.points++;
      rival.points++;
    }
    else {
      int constant2 = 850;
      //modify constant as you see fit
      //must be greater than the lowest team's ranking (858)
      int rank_a = this.ranking - constant2;
      int rank_b = rival.ranking - constant2;
      int rank_add = rank_a + rank_b;
      int random2 = (int)(Math.random()*rank_add);
      if (random2 < rank_a) {
	this.wins++;
	rival.losses++;
        this.points += 3;
      }
      else {
        this.losses++;
	rival.wins++;
        rival.points += 3;
      }
    }					
  }
	
  public boolean postCompete(Team rival) {
    int constant = 1300;
    //modify constant as you see fit
    //must be greater than the lowest team's ranking (858)
    int rank_a = this.ranking - constant;
    int rank_b = rival.ranking - constant;
    int rank_add = rank_a + rank_b;
    int random = (int)(Math.random()*rank_add);
    if (random < rank_a) {
      return true;
    }
    else {
      return false;
    }
  }

Here, .compete(Team rival) is used for qualifying and group matches (where ties are allowed), and .postCompete(Team rival) is used for all elimination matches.

Compete method

First, I had to account for the possibility of draws happening. The variable coeff here is simply the probability of two teams of equal ranking drawing. The variable modifier was changed until desirable distributions were reached. If a draw does not happen, then the winning team is calculated by a similar process to how wins were calculated in my baseball program. Given Team A and Team B with ranks A' and B', the probability of Team A winning is A'/(A' + B').

postCompete method

Here, the constant value is something that is easily modified. I adjusted the team's ranks by subtracting the constant value, and then the winning team is found by the same process as the regular compete method.

Implementation of WorldCup Class

I chose to implement the 48 team version of the world cup (16 groups of 3) instead of the 32 team version because it will be used starting in the 2026 world cup and then continue to be used. Also, in the 48 team version, each region gets a concrete number of spots and there are no more playoffs, while in the 32 team version, some regions like Oceania get .5 spots and some teams must compete in inter-regional playoff games. First, I sorted all the teams in arrays based on region and then each team played each other team in the region. Then, if some region was given X spots for the tournament, the top X teams in that region were added to a qualifiers ArrayList. The qualifiers were then randomly sorted in 16 groups, where each team in the group plays both others, and the top 2 qualify for the knockout stages.

Sample Output

Group A Group B Group C Group D
Morocco: 4 England: 4 El Salvador: 4 Senegal: 6
Curaçao: 2 Italy: 3 Romania: 2 Saudi Arabia: 3
Serbia: 1 Egypt: 1 Iran: 1 Switzerland: 0
Group E Group F Group G Group H
Australia: 4 Wales: 2 Colombia: 4 Poland: 4
Korea Republic: 3 Nigeria: 2 Ghana: 2 Cameroon: 2
Jamaica: 1 Honduras: 2 Peru: 1 Qatar: 1
Group I Group J Group K Group L
Sweden: 4 Costa Rica: 4 Brazil: 2 Belgium: 4
Japan: 3 France: 4 United Arab Emirates: 2   Paraguay: 3
Congo DR: 1 New Zealand: 0   Portugal: 2 China: 1
Group M Group N Group O Group P
Tunisia: 4 Germany: 4 Netherlands: 4 Denmark: 4
Croatia: 4 Venezuela: 4 Spain: 2 Algeria: 3
Solomon Islands: 0   Mexico: 0 Uruguay: 1 United States: 1  

Round of 32

Italy eliminates Morocco
El Salvador eliminates Saudi Arabia
Australia eliminates Nigeria
Colombia eliminates Cameroon
Sweden eliminates France
Brazil eliminates Paraguay
Venezuela eliminates Tunisia
Netherlands eliminates Algeria
England eliminates Curaçao
Senegal eliminates Romania
Korea Republic eliminates Wales
Poland eliminates Ghana
Costa Rica eliminates Japan
United Arab Emirates eliminates Belgium
Germany eliminates Croatia
Spain eliminates Denmark

Round of 16

Italy eliminates El Salvador
Colombia eliminates Australia
Sweden eliminates Brazil
Venezuela eliminates Netherlands
England eliminates Senegal
Poland eliminates Korea Republic
Costa Rica eliminates United Arab Emirates
Spain eliminates Germany

Quarter-Finals

Colombia eliminates Italy
Sweden eliminates Venezuela
England eliminates Poland
Spain eliminates Costa Rica

Semi-Finals

Sweden eliminates Colombia
Spain eliminates England

World Cup Final

Spain wins the World Cup!