Ad

This Kata simulates the scoring style of a football league.
The league is complete when 10 games have been played to obtain the team's final score (no more or less).

Input:
An array of strings representing the results of each match played.
e.g. String [] {"0-1","1-1","1-0"...}. And a boolean indicating whether the first match is at home true/false.

Output:
An int that indicates the team's final score.

- Scores

  • If it's "home match", our goals scored are reflected on the left side of the hyphen(-).
  • If it's "away match", our goals scored are reflected on the right side of the hyphen(-).

The goals of the opposing team are reflected on the opposite side. (e.g.)

"3-0" Home match -> VICTORY (3 goals scored)
"0-3" Away match -> VICTORY (3 goals scored)
"1-2" Home match -> DEFEAT (1 goal scored but 2 goals scored by the opposing team )

- Matchs

The matches are decided whether they are away or at home by alternation.

One away and one at home, it will depend on the boolean parameter passed to the function. If it's true, the first game is a "home match". If false, it's "away match". (e.g.)

isHomeMatch=true;         isHomeMatch=false; 
"3-0" Home match          "0-3" Away match
"0-3" Away match          "3-2" Home match
"1-2" Home match          "3-3" Away match
"0-3" Away match          "1-0" Home match
"1-2" Home match          "0-0" Away match
[...]                     [...]

- Scoring

  • Victory = +3 points
  • Draw = +1 points
  • Defeat = +0 points
Note: the array of matches may be incomplete (less than 10 matches played) or empty/null. In this case return -1

Good luck! ;)

Code
Diff
  • import java.util.regex.Pattern;
    
    public class Ranking{
      private static final int REQUIRED_MATCHES=10;
      public static int getPoints(String[] matches, boolean isHomeMatch) {
        if(matches == null) return -1;
        
    		int points = 0;
    		
        if(matches.length!=REQUIRED_MATCHES) return -1;
    		
    		for (int i = 0; i < matches.length; i++) {
          if(matches[i]==null) return -1;
    			
    			if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i]))	return -1;
    
    			String[] actualMatch = matches[i].split("-");
    			if (!isHomeMatch) {
    				String aux = actualMatch[0];
    				actualMatch[0] = actualMatch[1];
    				actualMatch[1] = aux;
    			}
    
    			if (Integer.valueOf(actualMatch[0]) > Integer.valueOf(actualMatch[1])) {
    				points += 3;
    			} else if (Integer.valueOf(actualMatch[0]) == Integer.valueOf(actualMatch[1])) {
    				points += 1;
    			}
    			
          isHomeMatch = !isHomeMatch;
    		}
    		return points;
    	}
    }
    • import java.util.regex.Pattern;
    • public class Ranking{
    • private static final int REQUIRED_MATCHES=10;
    • public static int getPoints(String[] matches, boolean isHomeMatch) {
    • if(matches == null) return -1;
    • int points = 0;
    • if(matches.length!=10) {
    • return -1;
    • }
    • if(matches.length!=REQUIRED_MATCHES) return -1;
    • for (int i = 0; i < matches.length; i++) {
    • if(matches[i]==null) {
    • return -1;
    • }
    • if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) {
    • return -1;
    • }
    • if(matches[i]==null) return -1;
    • if (!Pattern.matches("^[0-9]+[-]{1}[0-9]+$", matches[i])) return -1;
    • String[] actualMatch = matches[i].split("-");
    • if (!isHomeMatch) {
    • String aux = actualMatch[0];
    • actualMatch[0] = actualMatch[1];
    • actualMatch[1] = aux;
    • }
    • if (Integer.parseInt(actualMatch[0]) > Integer.parseInt(actualMatch[1])) {
    • if (Integer.valueOf(actualMatch[0]) > Integer.valueOf(actualMatch[1])) {
    • points += 3;
    • } else if (Integer.parseInt(actualMatch[0]) == Integer.parseInt(actualMatch[1])) {
    • } else if (Integer.valueOf(actualMatch[0]) == Integer.valueOf(actualMatch[1])) {
    • points += 1;
    • }
    • isHomeMatch = !isHomeMatch;
    • isHomeMatch = !isHomeMatch;
    • }
    • return points;
    • }
    • }

This Kata simulates the scoring style of a football league.
The league is complete when 10 games have been played to obtain the team's final score (no more or less).

- Scores

  • If it's "home match", our goals scored are reflected on the left side of the hyphen(-).
  • If it's "away match", our goals scored are reflected on the right side of the hyphen(-).

The goals of the opposing team are reflected on the opposite side. (e.g.)

"3-0" Home match -> VICTORY (3 goals scored)
"0-3" Away match -> VICTORY (3 goals scored)
"1-2" Home match -> DEFEAT (1 goal scored but 2 goals scored by the opposing team )

- Matchs

The matches are decided whether they are away or at home by alternation.

One away and one at home, it will depend on the boolean parameter passed to the function. If it's true, the first game is a "home match". If false, it's "away match". (e.g.)

isHomeMatch=true;         isHomeMatch=false; 
"3-0" Home match          "0-3" Away match
"0-3" Away match          "3-2" Home match
"1-2" Home match          "3-3" Away match
"0-3" Away match          "1-0" Home match
"1-2" Home match          "0-0" Away match
[...]                     [...]

- Scoring

  • Victory = +3 points
  • Draw = +1 points
  • Defeat = +0 points
Note: the array of matches may be incomplete (less than 10 matches played) or empty/null. In this case return -1

Good luck! ;)

Code
Diff
  • public class Ranking{
      
    }
    • public class Ranking{}
    • public class Ranking{
    • }