Buscar

FootLeague

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

FootLeague/.classpath
 
	 
	 
	 
FootLeague/.project
 
	 FootLeague
	 
	 
	
	 
		 
			 org.eclipse.jdt.core.javabuilder
			 
			
		
	
	 
		 org.eclipse.jdt.core.javanature
	
FootLeague/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
FootLeague/bin/championship/ScoreBoard.class
package championship;
public synchronized class ScoreBoard {
 private Team[] teams;
 private void addTeams(String[]);
 private Team getTeam(String);
 private void sort();
 private void showHeader();
 public void ScoreBoard(String[]);
 public void gameScore(String, String, int, int);
 public void show();
}
FootLeague/bin/championship/Team.class
package championship;
synchronized class Team {
 private String name;
 private int winnings;
 private int draws;
 private int defeats;
 private int forward;
 private int against;
 public void Team(String);
 public void newGame(int, int);
 public int getPoints();
 public String getName();
 public String toString();
 public void show();
}
FootLeague/bin/Game.class
public synchronized class Game {
 public final int t1;
 public final int t2;
 private boolean used;
 public void Game(int, int);
 public boolean intersects(Game);
 public void setUsed();
 public void resetUsed();
 public boolean isUsed();
 public void show();
}
FootLeague/bin/Rounds.class
public abstract synchronized class Rounds {
 public final int gamesInRound;
 public final int nRounds;
 protected int nTeams;
 public void Rounds(int);
 public int getNGames();
 public void show();
 public abstract void restart();
 public abstract Game[] next();
}
FootLeague/bin/RoundsComb.class
public synchronized class RoundsComb extends Rounds {
 private Game[] games;
 private Game[][] rounds;
 private int currentRound;
 public void RoundsComb(int);
 private int removeNotInRound(Game[], int, int);
 private void genRounds();
 private boolean genRound(Game[], int);
 private void genAllGames();
 public void restart();
 public Game[] next();
}
FootLeague/bin/RoundsRobin.class
public synchronized class RoundsRobin extends Rounds {
 private Game[] round;
 private int[] teams;
 private int currentRound;
 private int getPos;
 public void RoundsRobin(int);
 private void incPos();
 public void restart();
 public Game[] next();
}
FootLeague/bin/Simulator.class
public synchronized class Simulator {
 public static String[] teamNames;
 static void <clinit>();
 public void Simulator();
 private static int getGoals();
 private static void showResult(Game, int, int);
 public static void main(String[]);
}
FootLeague/src/championship/ScoreBoard.class
package championship;
public synchronized class ScoreBoard {
 private Team[] teams;
 private void addTeams(String[]);
 private Team getTeam(String);
 private void sort();
 private void showHeader();
 public void ScoreBoard(String[]);
 public void gameScore(String, String, int, int);
 public void show();
}
FootLeague/src/championship/ScoreBoard.java
package championship;
public class ScoreBoard {
	private Team[] teams;
 	
	/* auxiliary methods */
	
	/**
	 * Used to populate teams array from names
	 * @param names
	 */
	private void addTeams(String[] names) { 	
		for(int i=0; i < names.length; ++i) 
			teams[i] = new Team(names[i]); 
	}
	
	/**
	 * retrieve a team from it´s name 
	 * @param name
	 * @return
	 */
	private Team getTeam(String name) {
		for(int i=0; i < teams.length; ++i) {
			if (teams[i].getName().equals(name) ) 
				return teams[i];
		}
		return null;
	}
	
	/**
	 * Sort the teams array from team from descending team pontuation order
	 */
	private void sort() {
		for (int i=0; i < teams.length ; ++i) {
			Team stronger = teams[i];
			int strongerPos = i;
			for(int j= i+1; j < teams.length; j++) {
				if (teams[j].getPoints() > stronger.getPoints()) {
					stronger = teams[j];
					strongerPos = j;
				}
			}
			teams[strongerPos] = teams[i];
			teams[i] = stronger;
		}
	}
	
	private void showHeader() { 
		System.out.println("Nome\t\t Vitórias Empates Derrotas Marcados Sofridos Pontos");
	}
	
	public ScoreBoard(String[] names) {
		teams = new Team[names.length];
		addTeams(names);
	}
	
	/**
	 * Adjust the teams statistics from the game result 
	 * @param home
	 * @param away
	 * @param scored
	 * @param concedeed
	 */
	public void gameScore(String home, String away, int scored, int concedeed ) {
	 	
		Team homeTeam = getTeam(home);
		Team awayTeam = getTeam(away);
		if (awayTeam == homeTeam) return;
		
		homeTeam.newGame(scored, concedeed);
		awayTeam.newGame(concedeed, scored);
	}
	
	
	/**
	 * Show the scoreboard representation on the standard output
	 */
	public void show() {
		sort();
		showHeader();
		for(int i=0; i < teams.length; ++i) {
			 //System.out.println(teams[i].toString());
			 teams[i].show();
		}
	}
}
FootLeague/src/championship/Team.class
package championship;
synchronized class Team {
 private String name;
 private int winnings;
 private int draws;
 private int defeats;
 private int forward;
 private int against;
 public void Team(String);
 public void newGame(int, int);
 public int getPoints();
 public String getName();
 public String toString();
 public void show();
}
FootLeague/src/championship/Team.java
package championship;
class Team {
	private String name;
	
	private int winnings, draws, defeats;	
	private int forward, against;
	
	public Team(String name) {
		this.name = name;
	}
	
	public void newGame(int forward, int against) {
		if (forward > against) 	// victory
			winnings++;
		else if (forward == against)
			draws++;
		else
			defeats++;
		this.forward += forward;
		this.against += against;
		
	}
	
	public int getPoints() {
		return winnings*3 + draws;
	}
	
	public String getName() {
		return name;
	}
	
	public String toString() {
	 	
		return name + " \t\t" + winnings + '\t' + draws + '\t' + defeats + '\t'+ forward + '\t' + against + 
				'\t' + getPoints();
	}
	
	public void show() {
	 	
		System.out.format("%-10s %8d %8d %8d %8d %8d %8d\n",
		 name, winnings, draws, defeats, forward, against, getPoints());
	}
	
}
FootLeague/src/Game.java
public class Game {
	public final int t1, t2;
	private boolean used;
	
	public Game( int t1, int t2) {
		this.t1 = t1; this.t2=t2;
		used = false;
	}
	
	public boolean intersects(Game m) {
		return t1 == m.t1 || t1 == m.t2 || t2 == m.t1 || t2 == m.t2;
	}
	
	public void setUsed() { used = true; }
	
	public void resetUsed() { used = false; }
	
	public boolean isUsed() { return used; }
	
	public void show() {
		System.out.println("(t1=" + t1 + ", t2=" + t2 + ")");
	}
}
FootLeague/src/Rounds.java
public abstract class Rounds {
	public final int gamesInRound;
	public final int nRounds;
	protected int nTeams;
	
	public Rounds(int nTeams) {
		if (nTeams %2 != 0) nTeams++;
		this.nTeams = nTeams;
		
		int nGames = (nTeams*(nTeams-1))/2;
		gamesInRound= nTeams/2;
		nRounds = nGames/gamesInRound;
}
	
	public int getNGames() {
		return gamesInRound*nRounds;
	}
	
	public void show() {
		Game[] round;
		int currRound=1;
		
		restart();
		while ((round = next()) != null) {
			 System.out.println("round[" + currRound++ + "]");
			 for (int j=0; j < gamesInRound; j++) 
				 if (round[j] != null)
					 round[j].show();
		}
			
	}
	
	public abstract void restart();
	public abstract Game[] next();
	
}
FootLeague/src/RoundsComb.java
public class RoundsComb extends Rounds {
	private Game[] games;
	private Game[][] rounds;
	private int currentRound;
	
 
	public RoundsComb(int nTeams) {
		super(nTeams);
	}
	
	private int removeNotInRound(Game[] round, int n, int start) {
		boolean intersects;
		for (int i= start; i < getNGames(); ++i) {
			if (!games[i].isUsed()) {
				intersects=false;
				for (int j=0; j < n; ++j)			
					if (games[i].intersects(round[j])) {
						intersects=true;
						break;
					}
				if (!intersects) {
					games[i].setUsed();
					return i;
				}
			
			}
		}
		return -1;
	}
	
	private void genRounds() {
		rounds = new Game[nRounds][gamesInRound];
		
		// first round
		for (int i=0; i < gamesInRound; ++i)
			rounds[0][i] = games[removeNotInRound(rounds[0], i, 0)];
		// first match in next rounds
		for (int i=1; i < nRounds; ++i)
			rounds[i][0] = games[removeNotInRound(rounds[i], 0, 0)];
		
		// complete rounds
		for (int i=1; i < nRounds; ++i) {
			genRound(rounds[i], 1);
		}
	}
	
	private boolean genRound(Game[] round, int gr) {
		if (gr == gamesInRound) return true;
		int pos =-1;
		do {
			pos = removeNotInRound(round, gr, pos+1);
			if (pos == -1) return false;
			Game aux = games[pos];
			
			round[gr] = aux;
			if (genRound(round, gr+1))
				return true;
			aux.resetUsed();
		}
		while (true);
	}
	
	private void genAllGames() {
		int currGame=0;
		
		// generate combinations
		games = new Game[getNGames()];
	 	
		for (int i=0; i < nTeams; ++i)
			for (int j=i+1; j < nTeams; ++j)
				games[currGame++] = new Game(i, j);
	}
	
	/* implementação dos métodos abstractosde Round */
	
	public void restart() {
		currentRound = 0;
		if (rounds == null) {
			genAllGames();
			genRounds();
		}
	}
	
	public Game[] next() {
		if (rounds == null || currentRound >= nRounds)
			return null;
		return rounds[currentRound++];
	}
	
}
FootLeague/src/RoundsRobin.java
/**
 * this class is a round generator based on 
 * round robin algorithm
 * 
 * @author jmartins
 *
 */
public class RoundsRobin extends Rounds {
	private Game[] round;
	private int[] teams;
	private int currentRound;
	private int getPos;
		
	public RoundsRobin(int nTeams) {
		super(nTeams);
		
		teams = new int[nTeams-1];
	 	
		int middle = nTeams/2;
		// round-robin teams
		for (int i=1; i < middle; ++i)
			teams[i-1] = i;
		for (int i=middle; i < nTeams; ++i)
			teams[i-1] = nTeams -1 + middle - i;
	}
	
	private void incPos() {
		if (++getPos >= nTeams-1)
			getPos = 0;
	}
	
	/* implementação dos métodos abstractos de Round */
	
	 
	public void restart() {
		currentRound = 1;
		getPos = nTeams-2;
		
		// first round
		round = new Game[gamesInRound];
		for (int i=0; i < gamesInRound; ++i) 
			round[i] = new Game(i, gamesInRound+i);
	}
	
	public Game[] next() {
		if (currentRound++ > gamesInRound) return null;
	 
		Game[] currRound = round;
		
		// generate next round
		int[][] raux = new int[gamesInRound][2];
		raux[0][0] = 0;
		
		for (int i = 1; i < gamesInRound; ++i) {
			raux[i][0] = teams[getPos];
			incPos();
		}
		for (int i = 0; i < gamesInRound; ++i) {
			raux[i][1] = teams[getPos];
			incPos();
		}
		
		getPos = nTeams - currentRound - 1;
		for (int i= 0; i < gamesInRound; ++i) {
			round[i] = new Game(raux[i][0], raux[i][1]);
		}
		 
		return currRound;
	}
	
}
FootLeague/src/Simulator.java
import championship.*;
import java.util.*;
public class Simulator {
	
	public static String[] teamNames = {
		"Benfica",
		"Porto",
		"Sporting",
		"Vit. Guim.",
		"Estoril",
		"Braga",
		"Paç. de F.",
		"Belenenses",
		"Vit. Set.",
		"Olhanense",
		"Arouca",
		"Nacional",
		"Maritimo",
		"Gil Vic.",
		"Rio Ave",
		"Academica"
	};
	
	private static int getGoals() {
		return (int) (Math.random() * 4);
	}
	
	/**
	 * É utilizado o método format de system.out que permite
	 * a apresentação com o formato usual de lista de resultados
	 * @param g
	 * @param homeGoals
	 * @param awayGoals
	 */
	private static void showResult(Game g, int homeGoals, int awayGoals) {
		System.out.format("%10s %-2d-%2d %-10s\n",
				teamNames[g.t1], homeGoals, awayGoals, teamNames[g.t2]);
	}
	
	public static void main(String[] args) {
		ScoreBoard table = new ScoreBoard(teamNames);
	
		// o geradore de jornadas tem duas implementações 
		// RoundsRobin e RoundsComb
		Rounds r = new RoundsRobin(teamNames.length);
		Scanner s = new Scanner(System.in);
		
		r.restart();
		int roundNum = 1;
		Game[] round;
		
		
		while ((round = r.next()) != null) {
			// para cada jornada
			System.out.println("Jornada " + roundNum++ + ":");
		 
			for(int i=0; i < r.gamesInRound; ++i) {
				Game game = round[i];
				int homeGoals = getGoals();
				int awayGoals = getGoals();
				
				showResult(game, homeGoals, awayGoals);		
				table.gameScore(teamNames[game.t1], teamNames[game.t2], 
						homeGoals, awayGoals);
			}	
			
			System.out.println();
			table.show();
			System.out.println();
			if (roundNum < r.nRounds) {
				System.out.println("Prima c<enter> para próxima jornada");
				s.next();
			}
		}
	}
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais