Code Search for Developers
 
 
  

Tournament.java from The Geronimo Project at Krugle


Show Tournament.java syntax highlighted

package geronimo.hoshigo.model.tournament;

import geronimo.hoshigo.model.GoProgramConfig;
import geronimo.hoshigo.model.external.ComputerPlayer;
import geronimo.hoshigo.model.external.ComputerReferee;
import geronimo.hoshigo.model.external.GoProgram;
import geronimo.hoshigo.model.external.GtpProgram;
import geronimo.hoshigo.model.game.Game;
import geronimo.hoshigo.model.game.InvalidHandicapException;
import geronimo.hoshigo.model.game.Player;
import geronimo.hoshigo.model.game.Referee;
import geronimo.hoshigo.model.game.Score;
import geronimo.hoshigo.model.goban.GoColor;
import geronimo.hoshigo.model.goban.GoColorTools;
import geronimo.hoshigo.model.goban.IllegalGobanSize;
import geronimo.hoshigo.view.newtournament.OptionSet;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Tournament
{
	private final String name;
	private final Set<TournamentGame> games;

	public Tournament(int gobanSize, int handicap, float komi,
			boolean freeHandicap, GoProgramConfig programConfig,
			OptionSet optionSet, GoProgramConfig refereeConfig)
	{
		this.name = programConfig.name;
		
		// Création des parties
		this.games = new HashSet<TournamentGame>();
		List<List<String>> options = optionSet.getCartesianProduct();
		for(List<String> blackPlayeroptionList : options)
		{
			for(List<String> whitePlayeroptionList : options)
			{
				if( blackPlayeroptionList != whitePlayeroptionList )
				{
					// Création du joueur noir
					GoProgram blackProgram = new GtpProgram(programConfig, blackPlayeroptionList);
					Player black = new ComputerPlayer(blackProgram, GoColor.BLACK);
					
					// Création du joueur blanc
					GoProgram whiteProgram = new GtpProgram(programConfig, whitePlayeroptionList);
					Player white = new ComputerPlayer(whiteProgram, GoColor.WHITE);
					
					// Création de l'arbitre
					GoProgram refereeProgram = new GtpProgram(refereeConfig, new LinkedList<String>());
					Referee referee = new ComputerReferee(refereeProgram);
					
					try
					{
						TournamentGame game = new TournamentGame
						(
							this,
							gobanSize,
							handicap,
							komi,
							freeHandicap,
							black,
							white,
							referee
						);

						// Ajout de la partie
						this.games.add(game);
					}
					catch (InvalidHandicapException e)
					{
						e.printStackTrace();
					}
					catch (IllegalGobanSize e)
					{
						e.printStackTrace();
					}
				}
			}
		}
	}

	public Set<TournamentGame> getGames()
	{
		return this.games;
	}

	public void start()
	{
		for(TournamentGame game : this.games)
		{
			game.start();
		}
	}

	public void destroy()
	{
		for(TournamentGame game : this.games)
		{
			game.destroy();
		}
	}
	
	public boolean isEnded()
	{
		boolean theEnd = true;
		
		for (Game g : this.games)
		{
			theEnd = theEnd && g.isEnded();
		}
		
		return theEnd;
	}
	
	public String getWinner()
	{
		if (this.isEnded())
		{
			Map<String,Integer> nbVictoires = new HashMap<String,Integer>();
			Map<String,Float> points = new HashMap<String,Float>();
			
			for (Game g : this.games)
			{
				Score score = g.getScore();
				Player winner = g.getPlayer(score.winner);
				Player looser = g.getPlayer(GoColorTools.opposite(score.winner));
				
				// ajout des infos pour le gagnant
				if (! nbVictoires.containsKey(winner.getName()))
				{
					nbVictoires.put(winner.getName(),0);
				}
				if (! points.containsKey(winner.getName()))
				{
					points.put(winner.getName(),(float)0);
				}
				
				nbVictoires.put(winner.getName(),nbVictoires.get(winner.getName()) + 1);
				points.put(winner.getName(),points.get(winner.getName()) + score.score);
				
				// ajout des infos pour le perdant
				if (! points.containsKey(looser.getName()))
				{
					points.put(looser.getName(),(float)0);
				}
				if (! points.containsKey(looser.getName()))
				{
					points.put(looser.getName(),(float)0);
				}				
			}
			
			int nbVictoiresMax = Collections.max(nbVictoires.values());
			float scoreMax = Collections.max(points.values());
			
			String winner = null;
			for (String player : nbVictoires.keySet())
			{
				if (nbVictoires.get(player) == nbVictoiresMax)
				{
					if (winner == null)
					{
						winner = player;
					}
					else
					{
						if (points.get(player) == scoreMax)
						{
							winner = player;
						}
					}
				}
			}
			
			return winner;
			
		}
		else
		{
			return null;
		}
	}
	
	public String toString()
	{
		return this.name;
	}

}




See more files for this project here

The Geronimo Project

The Geronimo project concists of two software :\n- Geronimo Hoshigo : a playable graphical user interface to play Go\n- Geronimo Margo : a artificial intelligence program which plays Go

Project homepage: http://sourceforge.net/projects/geronimo
Programming language(s): Java,Pascal,Perl,PHP
License: gpl2

  IllegalPlayerChangeException.java
  NotParticipatingPlayerException.java
  Tournament.java
  TournamentGame.java