World Cup group stage

From Rosetta Code
Revision as of 17:33, 18 June 2014 by rosettacode>Mwn3d (New (timely) task with a Java example to start)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
World Cup group stage is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

It's World Cup season (or at least it was when this page was created)! The World Cup is an international football/soccer tournament that happens every 4 years. Countries put their international teams together in the years between tournaments and qualify for the tournament based on their performance in other international games. Once a team has qualified they are put into a group with 3 other teams. For the first part of the World Cup tournament the teams play in "group stage" games where each of the four teams in a group plays all three other teams once. The results of these games determine which teams will move on to the "knockout stage" which is a standard single-elimination tournament. The two teams from each group with the most standings points move on to the knockout stage. Each game can result in a win for one team and a loss for the other team or it can result in a draw/tie for each team. A win is worth three points in the standings. A draw/tie is worth one point. A loss is not worth any points.

Generate all possible outcome combinations for the six group stage games. With three possible outcomes for each game there should be 36 = 729 of them. Calculate the standings points for each team with each combination of outcomes. Show a histogram (graphical, ASCII art, or straight counts--whichever is easiest/most fun) of the standings points for the first and second place teams over all possible outcomes.

Hint: there should be no possible way to end up in second place with less than two points as well as no way to end up in first with less than three. Oddly enough, there is no way to get 8 points at all.

Java

Works with: Java version 1.5+

This example codes results as a 6-digit number in base 3. Each digit is a game. A 2 is a win for the team on the left, a 1 is a draw, and a 0 is a loss for the team on the left. <lang java5>import java.util.Arrays;

public class GroupStage{ //team left digit vs team right digit static String[] games = {"12", "13", "14", "23", "24", "34"}; static String results = "000000";//start with left teams all losing

private static boolean nextResult(){ if(results.equals("222222")) return false; int res = Integer.parseInt(results, 3) + 1; results = Integer.toString(res, 3); while(results.length() < 6) results = "0" + results; //left pad with 0s return true; }

public static void main(String[] args){ int[] secPoints = new int[10]; //playing 3 games, points range from 0 to 9 int[] firPoints = new int[10]; do{ int[] records = {0,0,0,0}; for(int i = 0; i < 6; i++){ switch(results.charAt(i)){ case '2': records[games[i].charAt(0) - '1'] += 3; break; //win for left team case '1': //draw records[games[i].charAt(0) - '1']++; records[games[i].charAt(1) - '1']++; break; case '0': records[games[i].charAt(1) - '1'] += 3; break; //win for right team } } Arrays.sort(records); //sort ascending, first place team on the right secPoints[records[2]]++; firPoints[records[3]]++; }while(nextResult()); System.out.println("Second place: " + Arrays.toString(secPoints)); System.out.println("First place: " + Arrays.toString(firPoints)); } }</lang>

Output:
Second place: [0, 0, 4, 33, 338, 172, 164, 18, 0, 0]
First place: [0, 0, 0, 1, 14, 148, 152, 306, 0, 108]