Commit 30fed0bd authored by Rémi BERTHO's avatar Rémi BERTHO

Add getter to Game

parent c1757bd6
/*
* NotFound.java
*
* Copyright 2014-2016
* Remi BERTHO <remi.bertho@openmailbox.org>
*
* This file is part of Csuper.
*
* Csuper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Csuper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
package org.dalan.csuper.Core.Exception;
/**
* This class represent a error when there is an out of range.
*/
public class NotFound extends RuntimeException {
/**
* Constructor with the string parameter
* @param s the string of the error
*/
public NotFound(String s){
super(s);
}
}
/*
* WrongUse.java
*
* Copyright 2014-2016
* Remi BERTHO <remi.bertho@openmailbox.org>
*
* This file is part of Csuper.
*
* Csuper is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Csuper is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
package org.dalan.csuper.Core.Exception;
/**
* This class represent a error when there is an out of range.
*/
public class WrongUse extends RuntimeException {
/**
* Constructor with the string parameter
* @param s the string of the error
*/
public WrongUse(String s){
super(s);
}
}
......@@ -27,6 +27,9 @@
package org.dalan.csuper.Core;
import org.dalan.csuper.Core.Exception.FileError;
import org.dalan.csuper.Core.Exception.NotFound;
import org.dalan.csuper.Core.Exception.OutOfRange;
import org.dalan.csuper.Core.Exception.WrongUse;
import org.dalan.csuper.Core.Exception.XmlError;
import org.dalan.csuper.Core.Listener.GameConfigurationListener;
import org.dalan.csuper.Core.Listener.GameListener;
......@@ -34,7 +37,6 @@ import org.dalan.csuper.Core.Utility.Share;
import org.jdom2.Content;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
......@@ -44,6 +46,8 @@ import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
......@@ -143,7 +147,7 @@ public class Game implements Cloneable, GameConfigurationListener, GameListener
SAXBuilder b = new SAXBuilder();
Document doc;
try {
doc = (Document)b.build(file);
doc = b.build(file);
} catch (Exception e) {
XmlError error = new XmlError("Cannot open the file " + file.getAbsolutePath());
error.initCause(e);
......@@ -248,7 +252,7 @@ public class Game implements Cloneable, GameConfigurationListener, GameListener
*/
@Override
public Game clone(){
Game game = null;
Game game;
try {
game = (Game)super.clone();
......@@ -434,4 +438,307 @@ public class Game implements Cloneable, GameConfigurationListener, GameListener
public void onGameExceedMaxNumber() {
}
///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////// Getter /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
/**
* Get the index of a player from his name
* @param player_name the player name
* @return the index
* @throws NotFound if the player name doesn't exist
*/
private int getPlayerIndex(String player_name) throws NotFound{
for (int i=0 ; i<nb_player_ ; i++)
{
if (player_name.equals(players_.get(i).getName()))
return i;
}
throw new NotFound("Player " + player_name + " is not found in the game");
}
/**
* Return the player
* @param index the index of the player
* @return the player
* @throws OutOfRange if index is greater the the number of player
*/
protected Player getPlayer(int index) throws OutOfRange{
if (index >= nb_player_)
throw new OutOfRange("Cannot access to the " + index + "th player, there is only " + nb_player_ + " player");
return players_.get(index);
}
/**
* Return the player
* @param player_name the player name
* @return the player
* @throws OutOfRange if index is greater the the number of player
*/
protected Player getPlayer(String player_name) throws OutOfRange{
return getPlayer(getPlayerIndex(player_name));
}
/**
* Return the version
* @return the version
*/
public double getVersion(){
return version_;
}
/**
* Return the size max of a name
* @return the size_max_name_
*/
public int getSizeMaxName(){
return size_max_name_;
}
/**
* Return the date
* @return the date
*/
public Calendar getDate(){
return date_;
}
/**
* Return the nb player
* @return the nb player
*/
public int getNbPlayer(){
return nb_player_;
}
/**
* Return the game configuration
* @return the game configuration
*/
public GameConfiguration getConfig(){
return config_;
}
/**
* Return the distributor
* @return the distributor
*/
public int getDistributor(){
return distributor_;
}
/**
* Return nb turn distributor
* @return the nb turn distributor
*/
public int getNbTurnDistributor(){
return nb_turn_distributor_;
}
/**
* Return the player name
* @param player_index the player index
* @return the player name
* @throws OutOfRange if index is greater the the number of player
*/
public String getPlayerName(int player_index) throws OutOfRange{
return getPlayer(player_index).getName();
}
/**
* Return the distributor name
* @return the distributor name
*/
public String getDistributorName(){
return players_.get(distributor_).getName();
}
/**
* Return the number of turn of a player
* @param player_index the player index
* @return the number of turn
* @throws OutOfRange if index is greater than the number of player
*/
public int getNbTurn(int player_index) throws OutOfRange{
return getPlayer(player_index).getNbTurn();
}
/**
* Return the number of turn of a player
* @param player_name the player name
* @return the number of turn
* @throws NotFound if the player name doesn't exist
*/
public int getNbTurn(String player_name) throws NotFound{
return getPlayer(player_name).getNbTurn();
}
/**
* Return the points of a player in a given turn
* @param player_index the player index
* @param turn the turn,
* @return the points
* @throws OutOfRange if index is greater than the number of player
* @throws OutOfRange if turn is greater than the number of turn
*/
public double getPoints(int player_index, int turn) throws OutOfRange{
return getPlayer(player_index).getPoints(turn);
}
/**
* Return the points of a player in a given turn
* @param player_name the player name
* @param turn the turn
* @return the points
* @throws OutOfRange if turn is greater than the number of turn
* @throws NotFound if the player name doesn't exist
*/
public double getPoints(String player_name, int turn) throws OutOfRange, NotFound{
return getPlayer(player_name).getPoints(turn);
}
/**
* Return the total points of a player in a given turn
* @param player_index the player index
* @param turn the turn,
* @return the total points
* @throws OutOfRange if index is greater than the number of player
* @throws OutOfRange if turn is greater than the number of turn
*/
public double getTotalPoints(int player_index, int turn) throws OutOfRange{
return getPlayer(player_index).getTotalPoints(turn);
}
/**
* Return the total points of a player in the last turn
* @param player_index the player index
* @return the total points
* @throws OutOfRange if index is greater than the number of player
*/
public double getTotalPoints(int player_index) throws OutOfRange{
return getPlayer(player_index).getTotalPoints();
}
/**
* Return the total points of a player in a given turn
* @param player_name the player name
* @param turn the turn
* @return the total points
* @throws OutOfRange if turn is greater than the number of turn
* @throws NotFound if the player name doesn't exist
*/
public double getTotalPoints(String player_name, int turn) throws OutOfRange, NotFound{
return getPlayer(player_name).getTotalPoints(turn);
}
/**
* Return the total points of a player in the last turn
* @param player_name the player name
* @return the total points
* @throws NotFound if the player name doesn't exist
*/
public double getTotalPoints(String player_name) throws NotFound{
return getPlayer(player_name).getTotalPoints();
}
/**
* Return the ranking of a player
* @param player_index the player index
* @return the ranking
* @throws OutOfRange if index is greater than the number of player
*/
public int getRanking(int player_index) throws OutOfRange{
return getPlayer(player_index).getRanking();
}
/**
* Return the ranking of a player
* @param player_index the player index
* @param turn the turn
* @return the ranking
* @throws OutOfRange if turn is greater than the number of turn
* @throws OutOfRange if index is greater than the number of player
* @throws WrongUse if this function is used in a non turn game with a specific turn
*/
public int getRanking(int player_index, int turn) throws OutOfRange,WrongUse{
if (!(getConfig().getTurnBased())){
throw new WrongUse("The ranking function should only be used in a turn based game when" +
" a specific turn is specify");
}
if (!(getPlayer(player_index).hasTurn(turn))){
throw new OutOfRange("Cannot access to the " + turn + "th turn, there is only " +
getPlayer(player_index).getNbTurn() + " turn");
}
List<Double> sort_points = new ArrayList<>();
for (Player player : players_)
sort_points.add(player.getTotalPoints(turn));
// Sort the points base on the first way
if (getConfig().getMaxWinner())
Collections.sort(sort_points,Collections.reverseOrder());
else
Collections.sort(sort_points);
double points_player = getTotalPoints(player_index,turn);
for (int i=0 ; i<nb_player_ ; i++){
if (sort_points.get(i).equals(points_player))
return i+1;
}
return 0;
}
/**
* Return the ranking of a player
* @param player_name the player name
* @return the ranking
* @throws NotFound if the player name doesn't exist
*/
public int getRanking(String player_name) throws NotFound{
return getPlayer(player_name).getRanking();
}
/**
* Return the ranking of a player
* @param player_name the player name
* @param turn the turn
* @return the ranking
* @throws NotFound if the player name doesn't exist
* @throws OutOfRange if turn is greater than the number of turn
* @throws WrongUse if this function is used in a non turn game with a specific turn
*/
public int getRanking(String player_name, int turn) throws NotFound, OutOfRange, WrongUse{
return getRanking(getPlayerIndex(player_name),turn);
}
/**
* Return true if the player has points on the turn, false otherwise
* @param player_index the player index
* @param turn the turn
* @return true if the player has points on the turn, false otherwise
* @throws OutOfRange if index is greater than the number of player
*/
boolean hasTurn(int player_index, int turn) throws OutOfRange{
return getPlayer(player_index).hasTurn(turn);
}
/**
* Return true if the player has points on the turn, false otherwise
* @param player_name the player name
* @param turn turn the turn
* @return true if the player has points on the turn, false otherwise
* @throws NotFound if the player name doesn't exist
*/
boolean hasTurn(String player_name, int turn) throws NotFound{
return getPlayer(player_name).hasTurn(turn);
}
}
......@@ -347,7 +347,7 @@ public class GameConfiguration implements Cloneable, Parcelable {
sendSignalChange();
}
public boolean useMaximum() {
public boolean getUseMaximum() {
return use_maximum_;
}
......@@ -356,7 +356,7 @@ public class GameConfiguration implements Cloneable, Parcelable {
sendSignalChange();
}
public boolean turnBased() {
public boolean getTurnBased() {
return turn_based_;
}
......@@ -365,7 +365,7 @@ public class GameConfiguration implements Cloneable, Parcelable {
sendSignalChange();
}
public boolean useDistributor() {
public boolean getUseDistributor() {
return use_distributor_;
}
......@@ -383,7 +383,7 @@ public class GameConfiguration implements Cloneable, Parcelable {
sendSignalChange();
}
public boolean maxWinner() {
public boolean getMaxWinner() {
return max_winner_;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment