Added poo1.
This commit is contained in:
parent
e1a7391ecc
commit
409dd31138
|
@ -0,0 +1,2 @@
|
|||
# tema1poo
|
||||
draft for tema1poo
|
|
@ -0,0 +1,23 @@
|
|||
package com.tema1.cards;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CardsAssigner {
|
||||
public List<List<Integer>> divideCards(List<Integer> initialList){
|
||||
final int chunkLength = 10;
|
||||
|
||||
/* Split the cards in chunks of 10 that
|
||||
well be assigned to each player
|
||||
*/
|
||||
List<List<Integer>> chunks = new ArrayList<List<Integer>>();
|
||||
final int N = initialList.size();
|
||||
for(int i = 0; i < N; i += chunkLength){
|
||||
chunks.add(
|
||||
new ArrayList<Integer>(initialList.subList(i, Math.min(N, i + chunkLength)))
|
||||
);
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.tema1.cards;
|
||||
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
import com.tema1.goods.GoodsType;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CardsChecker {
|
||||
public boolean checkForIllegalCards(List<Integer> cards){
|
||||
for(Integer card : cards){
|
||||
if(GoodsFactory.getInstance().getGoodsById(card).
|
||||
getType() == GoodsType.Illegal){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean checkForLegalCards(List<Integer> cards){
|
||||
for(Integer card : cards){
|
||||
if(GoodsFactory.getInstance().getGoodsById(card).
|
||||
getType() == GoodsType.Legal){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package com.tema1.cards;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class CardsRemover {
|
||||
public void removeTenCards(List<Integer> cards){
|
||||
final int toRemove = 10;
|
||||
cards.subList(0, toRemove).clear();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.tema1.cards;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CardsRestorer {
|
||||
public static List<List<Integer>> restrictedCards = new ArrayList<List<Integer>>();
|
||||
public static List<List<Integer>> originalCards = new ArrayList<List<Integer>>();
|
||||
|
||||
public void putRestrictedCardsBack(List<Integer> cards){
|
||||
for(int i = 0; i < restrictedCards.size(); i++){
|
||||
for(Integer card : restrictedCards.get(i)){
|
||||
if(originalCards.get(i).contains(card)){
|
||||
cards.add(card);
|
||||
originalCards.get(i).remove(card);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,163 @@
|
|||
package com.tema1.converter;
|
||||
|
||||
import com.tema1.engine.inspection.InspectionOutcome;
|
||||
import com.tema1.player.Player;
|
||||
import com.tema1.strategy.Bag;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
|
||||
public class Converter {
|
||||
public Bag convertStringToFunctionCreateBag(String className, List<Integer> cards,
|
||||
int money, final int round){
|
||||
String classNameCopy = className;
|
||||
|
||||
className = classNameCopy.substring(0, 1).toUpperCase() + className.substring(1);
|
||||
className = "com.tema1.strategy." + className;
|
||||
|
||||
final String methodName = "createBag";
|
||||
|
||||
Class<?> strategyClass = null;
|
||||
try {
|
||||
strategyClass = Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Object strategy = null;
|
||||
try {
|
||||
assert strategyClass != null;
|
||||
strategy = strategyClass.getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException |
|
||||
InvocationTargetException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if(classNameCopy.equals("greedy")){
|
||||
Class<?>[] paramTypes = {List.class, int.class, int.class};
|
||||
Method createBag = null;
|
||||
try {
|
||||
assert strategy != null;
|
||||
createBag = strategy.getClass().getMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
assert createBag != null;
|
||||
return (Bag) createBag.invoke(strategy, cards, money, round);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Class<?>[] paramTypes = {List.class, int.class};
|
||||
Method createBag = null;
|
||||
try {
|
||||
assert strategy != null;
|
||||
createBag = strategy.getClass().getMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
assert createBag != null;
|
||||
return (Bag) createBag.invoke(strategy, cards, money);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Integer convertStringToFunctionDeclareBag(String className, List<Integer> cards){
|
||||
String classNameCopy = className;
|
||||
|
||||
className = classNameCopy.substring(0, 1).toUpperCase() + className.substring(1);
|
||||
className = "com.tema1.strategy." + className;
|
||||
|
||||
final String methodName = "declareBag";
|
||||
|
||||
Class<?> strategyClass = null;
|
||||
try {
|
||||
strategyClass = Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Object strategy = null;
|
||||
try {
|
||||
assert strategyClass != null;
|
||||
strategy = strategyClass.getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException |
|
||||
InvocationTargetException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Class<?>[] paramTypes = {List.class};
|
||||
Method declareBag = null;
|
||||
try {
|
||||
assert strategy != null;
|
||||
declareBag = strategy.getClass().getMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
assert declareBag != null;
|
||||
return (Integer) declareBag.invoke(strategy, cards);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public InspectionOutcome convertStringToFunctionInspectBag(String className, Player sheriff,
|
||||
Player player){
|
||||
|
||||
String classNameCopy = className;
|
||||
|
||||
className = classNameCopy.substring(0, 1).toUpperCase() + className.substring(1);
|
||||
className = "com.tema1.strategy." + className;
|
||||
|
||||
final String methodName = "inspectBag";
|
||||
|
||||
Class<?> strategyClass = null;
|
||||
try {
|
||||
strategyClass = Class.forName(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Object strategy = null;
|
||||
try {
|
||||
assert strategyClass != null;
|
||||
strategy = strategyClass.getDeclaredConstructor().newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException |
|
||||
InvocationTargetException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Class<?>[] paramTypes = {Player.class, Player.class};
|
||||
Method inspectBag = null;
|
||||
try {
|
||||
assert strategy != null;
|
||||
inspectBag = strategy.getClass().getMethod(methodName, paramTypes);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
assert inspectBag != null;
|
||||
return (InspectionOutcome) inspectBag.invoke(strategy, sheriff, player);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
package com.tema1.engine;
|
||||
|
||||
import com.tema1.cards.CardsAssigner;
|
||||
import com.tema1.cards.CardsRestorer;
|
||||
import com.tema1.engine.utils.Utils;
|
||||
import com.tema1.player.Player;
|
||||
import com.tema1.player.PlayerRole;
|
||||
import com.tema1.strategy.Basic;
|
||||
import com.tema1.strategy.Bribed;
|
||||
import com.tema1.strategy.Greedy;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GameEngine {
|
||||
private List<Player> playerList = new ArrayList<Player>();
|
||||
private List<Integer> cards;
|
||||
|
||||
public GameEngine(List<String> playerStrategy, List<Integer> assetsId){
|
||||
this.cards = assetsId;
|
||||
|
||||
for(int i = 0; i < playerStrategy.size(); i++){
|
||||
this.playerList.add(new Player(playerStrategy.get(i), PlayerRole.Trader, null));
|
||||
}
|
||||
}
|
||||
|
||||
public List<Player> play(final int rounds){
|
||||
final int subrounds = playerList.size();
|
||||
Utils gameEngineUtils = new Utils();
|
||||
|
||||
CardsAssigner cardsAssigner = new CardsAssigner();
|
||||
List<List<Integer>> cardChunks;
|
||||
|
||||
for(int i = 0; i < rounds; i++){
|
||||
//System.out.println("ROUND NUMBER:" + (i+1));
|
||||
|
||||
for(int j = 0; j < subrounds; j++){
|
||||
// System.out.println("Subround Number: " + j);
|
||||
|
||||
cardChunks = cardsAssigner.divideCards(cards);
|
||||
|
||||
gameEngineUtils.setRolesToTrader(playerList);
|
||||
gameEngineUtils.setRoleToSheriff(playerList.get(j));
|
||||
gameEngineUtils.assignCards(playerList, cardChunks, cards);
|
||||
|
||||
this.playSubround(playerList, i);
|
||||
|
||||
}
|
||||
|
||||
/* At the end of the subround put the restricted cards back in place */
|
||||
CardsRestorer cardsRestorer = new CardsRestorer();
|
||||
cardsRestorer.putRestrictedCardsBack(cards);
|
||||
}
|
||||
|
||||
GameStages gameStages = new GameStages();
|
||||
gameStages.calculateFinalScore(playerList);
|
||||
|
||||
return this.playerList;
|
||||
}
|
||||
|
||||
public void playSubround(List<Player> playerList, final int round){
|
||||
Basic basic = new Basic();
|
||||
Greedy greedy = new Greedy();
|
||||
Bribed bribed = new Bribed();
|
||||
GameStages gameStages = new GameStages();
|
||||
|
||||
// for(int i = 0; i < playerList.size(); i++){
|
||||
// System.out.println(i + " " + playerList.get(i).getStrategy().toUpperCase() + " " + playerList.get(i).getMoney());
|
||||
// }
|
||||
// System.out.println();
|
||||
// System.out.println();
|
||||
|
||||
gameStages.createBag(playerList, round);
|
||||
// for(int i = 0; i < playerList.size(); i++){
|
||||
// System.out.println(i + " " + playerList.get(i).getStrategy().toUpperCase() + " sack: "
|
||||
// + playerList.get(i).getCards());
|
||||
// }
|
||||
// System.out.println();
|
||||
gameStages.declareBag(playerList);
|
||||
gameStages.inspectBag(playerList);
|
||||
gameStages.putOnStall(playerList);
|
||||
// for(int i = 0; i < playerList.size(); i++){
|
||||
// System.out.println(i + " " + playerList.get(i).getStrategy().toUpperCase() + " table: "
|
||||
// + playerList.get(i).getStall());
|
||||
// }
|
||||
// System.out.println();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,86 @@
|
|||
package com.tema1.engine;
|
||||
|
||||
import com.tema1.converter.Converter;
|
||||
import com.tema1.engine.inspection.Inspection;
|
||||
import com.tema1.engine.utils.Utils;
|
||||
import com.tema1.player.FinalScoreCalculator;
|
||||
import com.tema1.player.Player;
|
||||
import com.tema1.player.PlayerRole;
|
||||
import com.tema1.strategy.Bag;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GameStages {
|
||||
private Converter converter = new Converter();
|
||||
|
||||
public void createBag(List<Player> playerList, final int round){
|
||||
|
||||
for(Player player : playerList){
|
||||
if(player.getRole() == PlayerRole.Trader){
|
||||
|
||||
Bag createdBag = converter.convertStringToFunctionCreateBag(player.getStrategy(),
|
||||
player.getCards(), player.getMoney(), round);
|
||||
|
||||
player.setCards(createdBag.getBag());
|
||||
player.setOfferedBribe(createdBag.getOfferedBribe());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void declareBag(List<Player> playerList){
|
||||
|
||||
for(Player player : playerList){
|
||||
if(player.getRole() == PlayerRole.Trader){
|
||||
|
||||
Integer declaredCard = converter.convertStringToFunctionDeclareBag(player.getStrategy(),
|
||||
player.getCards());
|
||||
|
||||
player.setDeclaredGood(declaredCard);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void inspectBag(List<Player> playerList){
|
||||
Utils utils = new Utils();
|
||||
Inspection inspection = new Inspection();
|
||||
Player sheriff = utils.getSheriff(playerList);
|
||||
|
||||
switch (sheriff.getStrategy()) {
|
||||
case "basic":
|
||||
inspection.inspectAsBasic(playerList, sheriff);
|
||||
break;
|
||||
case "greedy":
|
||||
inspection.inspectAsGreedy(playerList, sheriff);
|
||||
break;
|
||||
case "bribed":
|
||||
inspection.inspectAsBribed(playerList, sheriff);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void putOnStall(List<Player> playerList){
|
||||
for(Player player : playerList){
|
||||
if(player.getCards() != null){
|
||||
player.addToStall(player.getCards());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void calculateFinalScore(List<Player> playerList){
|
||||
FinalScoreCalculator calculator = new FinalScoreCalculator();
|
||||
|
||||
for(Player player : playerList){
|
||||
calculator.convertBonusToGoods(player);
|
||||
|
||||
player.addToFinalScore(player.getMoney());
|
||||
player.addToFinalScore(calculator.getTotalLegalProfit(player));
|
||||
player.addToFinalScore(calculator.getTotalIllegalProfit(player));
|
||||
|
||||
}
|
||||
|
||||
calculator.getKingQueenBonus(playerList);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package com.tema1.engine.inspection;
|
||||
|
||||
import com.tema1.cards.CardsRestorer;
|
||||
import com.tema1.converter.Converter;
|
||||
import com.tema1.player.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Inspection {
|
||||
private Converter converter = new Converter();
|
||||
private Payment payment = new Payment();
|
||||
|
||||
public void inspectAsBasic(List<Player> playerList, Player sheriff){
|
||||
|
||||
for(Player player : playerList){
|
||||
if(player != sheriff){
|
||||
InspectionOutcome outcome = converter.convertStringToFunctionInspectBag(
|
||||
sheriff.getStrategy(), sheriff, player);
|
||||
payment.outcomePay(outcome, sheriff, player);
|
||||
CardsRestorer.restrictedCards.add(outcome.getRestrictedGoods());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void inspectAsGreedy(List<Player> playerList, Player sheriff){
|
||||
|
||||
for(Player player : playerList){
|
||||
|
||||
if(player != sheriff){
|
||||
InspectionOutcome outcome = converter.convertStringToFunctionInspectBag(
|
||||
sheriff.getStrategy(), sheriff, player);
|
||||
|
||||
payment.outcomePay(outcome, sheriff, player);
|
||||
CardsRestorer.restrictedCards.add(outcome.getRestrictedGoods());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void inspectAsBribed(List<Player> playerList, Player sheriff){
|
||||
ProximityPlayers proximityPlayers = new ProximityPlayers();
|
||||
Player leftPlayer = proximityPlayers.getLeftPlayer(playerList, sheriff);
|
||||
Player rightPlayer = proximityPlayers.getRightPlayer(playerList, sheriff);
|
||||
|
||||
assert leftPlayer != null : "leftPlayer could not be found";
|
||||
assert rightPlayer != null : "rightPlayer could not be found";
|
||||
|
||||
if(leftPlayer != rightPlayer){
|
||||
leftPlayer.setOfferedBribe(0);
|
||||
InspectionOutcome outcome = converter.convertStringToFunctionInspectBag(
|
||||
sheriff.getStrategy(), sheriff, leftPlayer);
|
||||
|
||||
payment.outcomePay(outcome, sheriff, leftPlayer);
|
||||
CardsRestorer.restrictedCards.add(outcome.getRestrictedGoods());
|
||||
|
||||
rightPlayer.setOfferedBribe(0);
|
||||
outcome = converter.convertStringToFunctionInspectBag(
|
||||
sheriff.getStrategy(), sheriff, rightPlayer);
|
||||
|
||||
payment.outcomePay(outcome, sheriff, rightPlayer);
|
||||
CardsRestorer.restrictedCards.add(outcome.getRestrictedGoods());
|
||||
}
|
||||
else{
|
||||
leftPlayer.setOfferedBribe(0);
|
||||
|
||||
InspectionOutcome outcome = converter.convertStringToFunctionInspectBag(
|
||||
sheriff.getStrategy(), sheriff, leftPlayer);
|
||||
|
||||
payment.outcomePay(outcome, sheriff, leftPlayer);
|
||||
CardsRestorer.restrictedCards.add(outcome.getRestrictedGoods());
|
||||
}
|
||||
|
||||
for(Player player : playerList){
|
||||
|
||||
if(player != sheriff && (player != leftPlayer && player != rightPlayer &&
|
||||
player.getOfferedBribe() != 0)){
|
||||
// InspectionOutcome outcome = converter.convertStringToFunctionInspectBag(
|
||||
// sheriff.getStrategy(), sheriff, player);
|
||||
|
||||
InspectionOutcome outcome = new InspectionOutcome();
|
||||
|
||||
outcome.setPassedGoods(player.getCards());
|
||||
outcome.setMoneyToPayToSheriff(player.getOfferedBribe());
|
||||
|
||||
payment.outcomePay(outcome, sheriff, player);
|
||||
CardsRestorer.restrictedCards.add(outcome.getRestrictedGoods());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
package com.tema1.engine.inspection;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class InspectionOutcome {
|
||||
|
||||
private List<Integer> passedGoods;
|
||||
private List<Integer> restrictedGoods;
|
||||
|
||||
private Integer moneyToPayToSheriff;
|
||||
private Integer moneyToGetFromSheriff;
|
||||
|
||||
public InspectionOutcome(){
|
||||
this.passedGoods = new ArrayList<Integer>();
|
||||
this.restrictedGoods = new ArrayList<Integer>();
|
||||
|
||||
this.moneyToPayToSheriff = 0;
|
||||
this.moneyToGetFromSheriff = 0;
|
||||
}
|
||||
|
||||
|
||||
public List<Integer> getPassedGoods() {
|
||||
return passedGoods;
|
||||
}
|
||||
|
||||
public void setPassedGoods(List<Integer> passedGoods) {
|
||||
this.passedGoods = passedGoods;
|
||||
}
|
||||
|
||||
public List<Integer> getRestrictedGoods() {
|
||||
return restrictedGoods;
|
||||
}
|
||||
|
||||
public void setRestrictedGoods(List<Integer> restrictedGoods) {
|
||||
this.restrictedGoods = restrictedGoods;
|
||||
}
|
||||
|
||||
public Integer getMoneyToPayToSheriff() {
|
||||
return moneyToPayToSheriff;
|
||||
}
|
||||
|
||||
public void setMoneyToPayToSheriff(Integer moneyToPayToSheriff) {
|
||||
this.moneyToPayToSheriff = moneyToPayToSheriff;
|
||||
}
|
||||
|
||||
public Integer getMoneyToGetFromSheriff() {
|
||||
return moneyToGetFromSheriff;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "InspectionOutcome{" +
|
||||
"passedGoods=" + passedGoods +
|
||||
", restrictedGoods=" + restrictedGoods +
|
||||
", moneyToPayToSheriff=" + moneyToPayToSheriff +
|
||||
", moneyToGetFromSheriff=" + moneyToGetFromSheriff +
|
||||
'}';
|
||||
}
|
||||
|
||||
public void setMoneyToGetFromSheriff(Integer moneyToGetFromSheriff) {
|
||||
this.moneyToGetFromSheriff = moneyToGetFromSheriff;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.tema1.engine.inspection;
|
||||
|
||||
import com.tema1.player.Player;
|
||||
|
||||
public class Payment {
|
||||
public void outcomePay(InspectionOutcome outcome, Player sheriff, Player player){
|
||||
sheriff.setMoney(sheriff.getMoney() - outcome.getMoneyToGetFromSheriff());
|
||||
sheriff.setMoney(sheriff.getMoney() + outcome.getMoneyToPayToSheriff());
|
||||
|
||||
player.setMoney(player.getMoney() - outcome.getMoneyToPayToSheriff());
|
||||
player.setMoney(player.getMoney() + outcome.getMoneyToGetFromSheriff());
|
||||
|
||||
player.setCards(outcome.getPassedGoods());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.tema1.engine.inspection;
|
||||
|
||||
import com.tema1.player.Player;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ProximityPlayers {
|
||||
public Player getLeftPlayer(List<Player> playerList, Player sheriff){
|
||||
int pos = sheriffPosition(playerList, sheriff);
|
||||
|
||||
if(pos != -1) {
|
||||
if (pos - 1 < 0) {
|
||||
return playerList.get(playerList.size() - 1);
|
||||
}
|
||||
else{
|
||||
return playerList.get(pos-1);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Player getRightPlayer(List<Player> playerList, Player sheriff){
|
||||
int pos = sheriffPosition(playerList, sheriff);
|
||||
|
||||
if(pos != -1){
|
||||
if(pos + 1 >= playerList.size()){
|
||||
return playerList.get(0);
|
||||
}
|
||||
else{
|
||||
return playerList.get(pos+1);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private int sheriffPosition(List<Player> playerList, Player sheriff){
|
||||
for(int i = 0; i < playerList.size(); i++){
|
||||
if(playerList.get(i) == sheriff){
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
package com.tema1.engine.utils;
|
||||
|
||||
import com.tema1.cards.CardsRemover;
|
||||
import com.tema1.cards.CardsRestorer;
|
||||
import com.tema1.player.Player;
|
||||
import com.tema1.player.PlayerRole;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Utils {
|
||||
public void setRolesToTrader(List<Player> playerList){
|
||||
for(Player player : playerList){
|
||||
player.setRole(PlayerRole.Trader);
|
||||
}
|
||||
}
|
||||
|
||||
public void setRoleToSheriff(Player sheriff){
|
||||
sheriff.setRole(PlayerRole.Sheriff);
|
||||
sheriff.setCards(null);
|
||||
sheriff.setOfferedBribe(0);
|
||||
sheriff.setDeclaredGood(0);
|
||||
}
|
||||
|
||||
public Player getSheriff(List<Player> playerList){
|
||||
for(Player player : playerList){
|
||||
if(player.getRole() == PlayerRole.Sheriff){
|
||||
return player;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public void assignCards(List<Player> playerList, List<List<Integer>> cardChunks, List<Integer> cards){
|
||||
final int firstChunk = 0;
|
||||
CardsRemover cardsRemover = new CardsRemover();
|
||||
|
||||
for(Player player : playerList){
|
||||
|
||||
if(player.getRole() == PlayerRole.Trader){
|
||||
player.setCards(cardChunks.get(firstChunk));
|
||||
CardsRestorer.originalCards.add(cardChunks.get(firstChunk));
|
||||
cardChunks.remove(firstChunk);
|
||||
cardsRemover.removeTenCards(cards);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
package com.tema1.filters;
|
||||
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
import com.tema1.main.GameOutputFormat;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class Filter {
|
||||
public void filterIllegalByProfit(List<Integer> illegalCards){
|
||||
Comparator<Integer> comparatorProfit = (Integer a, Integer b) -> {
|
||||
Integer aProfit = GoodsFactory.getInstance().getGoodsById(a).getProfit();
|
||||
Integer bProfit = GoodsFactory.getInstance().getGoodsById(b).getProfit();
|
||||
|
||||
return bProfit.compareTo(aProfit);
|
||||
};
|
||||
|
||||
illegalCards.sort(comparatorProfit);
|
||||
}
|
||||
|
||||
public void filterLegalByProfitAndId(List<Integer> legalCards){
|
||||
Comparator<Integer> comparatorProfit = (Integer a, Integer b) -> {
|
||||
Integer aProfit = GoodsFactory.getInstance().getGoodsById(a).getProfit();
|
||||
Integer bProfit = GoodsFactory.getInstance().getGoodsById(b).getProfit();
|
||||
|
||||
return bProfit.compareTo(aProfit);
|
||||
};
|
||||
|
||||
Comparator<Integer> comparatorId = (Integer a, Integer b) -> {
|
||||
Integer aId = GoodsFactory.getInstance().getGoodsById(a).getId();
|
||||
Integer bId = GoodsFactory.getInstance().getGoodsById(b).getId();
|
||||
|
||||
return bId.compareTo(aId);
|
||||
};
|
||||
|
||||
legalCards.sort(comparatorProfit.thenComparing(comparatorId));
|
||||
}
|
||||
|
||||
public void filterLegalByFreqProfitAndId(List<Integer> legalCards){
|
||||
Comparator<Integer> comparatorFreq = (Integer a, Integer b) -> {
|
||||
Integer aFreq = Collections.frequency(legalCards, a);
|
||||
Integer bFreq = Collections.frequency(legalCards, b);
|
||||
|
||||
return bFreq.compareTo(aFreq);
|
||||
};
|
||||
|
||||
Comparator<Integer> comparatorProfit = (Integer a, Integer b) -> {
|
||||
Integer aProfit = GoodsFactory.getInstance().getGoodsById(a).getProfit();
|
||||
Integer bProfit = GoodsFactory.getInstance().getGoodsById(b).getProfit();
|
||||
|
||||
return bProfit.compareTo(aProfit);
|
||||
};
|
||||
|
||||
Comparator<Integer> comparatorId = (Integer a, Integer b) -> {
|
||||
Integer aId = GoodsFactory.getInstance().getGoodsById(a).getId();
|
||||
Integer bId = GoodsFactory.getInstance().getGoodsById(b).getId();
|
||||
|
||||
return bId.compareTo(aId);
|
||||
};
|
||||
|
||||
legalCards.sort(comparatorFreq.thenComparing(comparatorProfit).thenComparing(comparatorId));
|
||||
}
|
||||
|
||||
public void filterPlayersByScore(List<GameOutputFormat> outputPlayerList){
|
||||
Comparator<GameOutputFormat> comparatorScore = (GameOutputFormat a, GameOutputFormat b) -> {
|
||||
Integer aScore = a.getScore();
|
||||
Integer bScore = b.getScore();
|
||||
|
||||
return bScore.compareTo(aScore);
|
||||
};
|
||||
|
||||
outputPlayerList.sort(comparatorScore);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.tema1.filters;
|
||||
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
import com.tema1.goods.GoodsType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GetCards {
|
||||
public List<Integer> getIllegalCards(List<Integer> cards){
|
||||
List<Integer> illegalCards = new ArrayList<Integer>(cards);
|
||||
illegalCards.removeIf(filter -> GoodsFactory.getInstance().getGoodsById(filter).
|
||||
getType() == GoodsType.Legal);
|
||||
|
||||
return illegalCards;
|
||||
}
|
||||
|
||||
public List<Integer> getLegalCards(List<Integer> cards){
|
||||
List<Integer> legalCards = new ArrayList<Integer>(cards);
|
||||
legalCards.removeIf(filter -> GoodsFactory.getInstance().getGoodsById(filter).
|
||||
getType() == GoodsType.Illegal);
|
||||
|
||||
return legalCards;
|
||||
}
|
||||
|
||||
public void getBestCardForBasic(List<Integer> legalCards){
|
||||
Integer bestCard = legalCards.get(0);
|
||||
legalCards.removeIf(filter -> filter != bestCard);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.tema1.goods;
|
||||
|
||||
public abstract class Goods {
|
||||
private final int id;
|
||||
private final GoodsType type;
|
||||
private final int profit;
|
||||
private final int penalty;
|
||||
|
||||
public Goods(final int id, final GoodsType type, final int profit, final int penalty) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.profit = profit;
|
||||
this.penalty = penalty;
|
||||
}
|
||||
|
||||
public final int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public final GoodsType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public final int getProfit() {
|
||||
return profit;
|
||||
}
|
||||
|
||||
public final int getPenalty() {
|
||||
return penalty;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,125 @@
|
|||
package com.tema1.goods;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GoodsFactory {
|
||||
private static GoodsFactory instance = null;
|
||||
|
||||
private final Map<Integer, Goods> goodsById;
|
||||
|
||||
private final Map<Integer, IllegalGoods> illegalGoodsById;
|
||||
|
||||
private final Map<Integer, LegalGoods> legalGoodsById;
|
||||
|
||||
private GoodsFactory() {
|
||||
goodsById = new HashMap<Integer, Goods>();
|
||||
illegalGoodsById = new HashMap<Integer, IllegalGoods>();
|
||||
legalGoodsById = new HashMap<Integer, LegalGoods>();
|
||||
|
||||
initLegalGoods();
|
||||
initIllegalGoods();
|
||||
}
|
||||
|
||||
private final void initLegalGoods() {
|
||||
// create the types of legal goods
|
||||
Goods good0 = new LegalGoods(0, 2, 2, 20, 10);
|
||||
Goods good1 = new LegalGoods(1, 3, 2, 19, 9);
|
||||
Goods good2 = new LegalGoods(2, 4, 2, 18, 9);
|
||||
Goods good3 = new LegalGoods(3, 4, 2, 17, 8);
|
||||
Goods good4 = new LegalGoods(4, 3, 2, 16, 7);
|
||||
Goods good5 = new LegalGoods(5, 2, 2, 15, 6);
|
||||
Goods good6 = new LegalGoods(6, 3, 2, 14, 5);
|
||||
Goods good7 = new LegalGoods(7, 5, 2, 13, 4);
|
||||
Goods good8 = new LegalGoods(8, 2, 2, 12, 3);
|
||||
Goods good9 = new LegalGoods(9, 3, 2, 11, 2);
|
||||
|
||||
// insert legal goods into a hashMap
|
||||
goodsById.put(0, good0);
|
||||
goodsById.put(1, good1);
|
||||
goodsById.put(2, good2);
|
||||
goodsById.put(3, good3);
|
||||
goodsById.put(4, good4);
|
||||
goodsById.put(5, good5);
|
||||
goodsById.put(6, good6);
|
||||
goodsById.put(7, good7);
|
||||
goodsById.put(8, good8);
|
||||
goodsById.put(9, good9);
|
||||
|
||||
legalGoodsById.put(0, (LegalGoods) good0);
|
||||
legalGoodsById.put(1, (LegalGoods) good1);
|
||||
legalGoodsById.put(2, (LegalGoods) good2);
|
||||
legalGoodsById.put(3, (LegalGoods) good3);
|
||||
legalGoodsById.put(4, (LegalGoods) good4);
|
||||
legalGoodsById.put(5, (LegalGoods) good5);
|
||||
legalGoodsById.put(6, (LegalGoods) good6);
|
||||
legalGoodsById.put(7, (LegalGoods) good7);
|
||||
legalGoodsById.put(8, (LegalGoods) good8);
|
||||
legalGoodsById.put(9, (LegalGoods) good9);
|
||||
}
|
||||
|
||||
private final void initIllegalGoods() {
|
||||
// create LegalGoods - quantity hashMaps
|
||||
Map<Goods, Integer> bonus0 = new HashMap<Goods, Integer>();
|
||||
Map<Goods, Integer> bonus1 = new HashMap<Goods, Integer>();
|
||||
Map<Goods, Integer> bonus2 = new HashMap<Goods, Integer>();
|
||||
Map<Goods, Integer> bonus3 = new HashMap<Goods, Integer>();
|
||||
Map<Goods, Integer> bonus4 = new HashMap<Goods, Integer>();
|
||||
|
||||
bonus0.put(goodsById.get(1), 3);
|
||||
bonus1.put(goodsById.get(3), 2);
|
||||
bonus2.put(goodsById.get(2), 2);
|
||||
bonus3.put(goodsById.get(7), 4);
|
||||
bonus4.put(goodsById.get(3), 1);
|
||||
bonus4.put(goodsById.get(4), 2);
|
||||
bonus4.put(goodsById.get(6), 3);
|
||||
|
||||
// create the types of illegal goods
|
||||
IllegalGoods good0 = new IllegalGoods(20, 9, 4, bonus0);
|
||||
IllegalGoods good1 = new IllegalGoods(21, 8, 4, bonus1);
|
||||
IllegalGoods good2 = new IllegalGoods(22, 7, 4, bonus2);
|
||||
IllegalGoods good3 = new IllegalGoods(23, 6, 4, bonus3);
|
||||
IllegalGoods good4 = new IllegalGoods(24, 12, 4, bonus4);
|
||||
|
||||
// insert legal goods into a hashMap
|
||||
goodsById.put(20, good0);
|
||||
goodsById.put(21, good1);
|
||||
goodsById.put(22, good2);
|
||||
goodsById.put(23, good3);
|
||||
goodsById.put(24, good4);
|
||||
|
||||
illegalGoodsById.put(20, good0);
|
||||
illegalGoodsById.put(21, good1);
|
||||
illegalGoodsById.put(22, good2);
|
||||
illegalGoodsById.put(23, good3);
|
||||
illegalGoodsById.put(24, good4);
|
||||
}
|
||||
|
||||
public static GoodsFactory getInstance() {
|
||||
if(instance == null) {
|
||||
instance = new GoodsFactory();
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
public final Goods getGoodsById(final int id) {
|
||||
return goodsById.get(id);
|
||||
}
|
||||
|
||||
public final IllegalGoods getIllegalGoodsById(final int id) {
|
||||
return illegalGoodsById.get(id);
|
||||
}
|
||||
|
||||
public final LegalGoods getLegalGoodsById(final int id) {
|
||||
return legalGoodsById.get(id);
|
||||
}
|
||||
|
||||
public final Map<Integer, Goods> getAllGoods() {
|
||||
return goodsById;
|
||||
}
|
||||
|
||||
public final Map<Integer, IllegalGoods> getAllIllegalGoods(){
|
||||
return illegalGoodsById;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.tema1.goods;
|
||||
|
||||
public enum GoodsType {
|
||||
Legal, Illegal;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.tema1.goods;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class IllegalGoods extends Goods {
|
||||
private final Map<Goods, Integer> illegalBonus;
|
||||
|
||||
public IllegalGoods(final int id, final int profit, final int penalty, final Map<Goods, Integer> illegalBonus) {
|
||||
super(id, GoodsType.Illegal, profit, penalty);
|
||||
|
||||
this.illegalBonus = illegalBonus;
|
||||
}
|
||||
|
||||
public Map<Goods, Integer> getIllegalBonus() {
|
||||
return illegalBonus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "IllegalGoods{" +
|
||||
"illegalBonus=" + illegalBonus +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.tema1.goods;
|
||||
|
||||
public class LegalGoods extends Goods {
|
||||
private final int kingBonus;
|
||||
private final int queenBonus;
|
||||
|
||||
public LegalGoods(final int id, final int profit, final int penalty, final int kingBonus, final int queenBonus) {
|
||||
super(id, GoodsType.Legal, profit, penalty);
|
||||
|
||||
this.kingBonus = kingBonus;
|
||||
this.queenBonus = queenBonus;
|
||||
}
|
||||
|
||||
public int getKingBonus() {
|
||||
return kingBonus;
|
||||
}
|
||||
|
||||
public int getQueenBonus() {
|
||||
return queenBonus;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.tema1.main;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class GameInput {
|
||||
// DO NOT MODIFY
|
||||
private final List<Integer> mAssetOrder;
|
||||
private final List<String> mPlayersOrder;
|
||||
private int mRounds;
|
||||
|
||||
public GameInput() {
|
||||
mAssetOrder = null;
|
||||
mPlayersOrder = null;
|
||||
mRounds = -1;
|
||||
}
|
||||
|
||||
public GameInput(final int rounds, final List<Integer> assets, final List<String> players) {
|
||||
mAssetOrder = assets;
|
||||
mPlayersOrder = players;
|
||||
mRounds = rounds;
|
||||
}
|
||||
|
||||
public final List<Integer> getAssetIds() {
|
||||
return mAssetOrder;
|
||||
}
|
||||
|
||||
public final List<String> getPlayerNames() {
|
||||
return mPlayersOrder;
|
||||
}
|
||||
|
||||
public final int getRounds() {
|
||||
return mRounds;
|
||||
}
|
||||
|
||||
public final boolean isValidInput() {
|
||||
boolean membersInstantiated = mAssetOrder != null && mPlayersOrder != null;
|
||||
boolean membersNotEmpty = mAssetOrder.size() > 0 && mPlayersOrder.size() > 0 && mRounds > 0;
|
||||
|
||||
return membersInstantiated && membersNotEmpty;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.tema1.main;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fileio.FileSystem;
|
||||
|
||||
public final class GameInputLoader {
|
||||
private final String mInputPath;
|
||||
private final String mOutputPath;
|
||||
|
||||
GameInputLoader(final String inputPath, final String outputPath) {
|
||||
mInputPath = inputPath;
|
||||
mOutputPath = outputPath;
|
||||
}
|
||||
|
||||
public GameInput load() {
|
||||
List<Integer> assetsIds = new ArrayList<>();
|
||||
List<String> playerOrder = new ArrayList<>();
|
||||
int rounds = 0;
|
||||
int noPlayers = 0;
|
||||
int noGoods = 0;
|
||||
|
||||
try {
|
||||
FileSystem fs = new FileSystem(mInputPath, mOutputPath);
|
||||
|
||||
rounds = fs.nextInt();
|
||||
noPlayers = fs.nextInt();
|
||||
|
||||
for (int i = 0; i < noPlayers; ++i) {
|
||||
playerOrder.add(fs.nextWord());
|
||||
}
|
||||
|
||||
noGoods = fs.nextInt();
|
||||
|
||||
for (int i = 0; i < noGoods; ++i) {
|
||||
assetsIds.add(fs.nextInt());
|
||||
}
|
||||
|
||||
fs.close();
|
||||
|
||||
} catch (Exception e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
return new GameInput(rounds, assetsIds, playerOrder);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
package com.tema1.main;
|
||||
|
||||
import com.tema1.filters.Filter;
|
||||
import com.tema1.player.Player;
|
||||
|
||||
import fileio.implementations.FileWriter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GameOutput {
|
||||
public void writeToFile(List<Player> playerList, String outputFile) throws IOException {
|
||||
List<GameOutputFormat> outputPlayerList = new ArrayList<GameOutputFormat>();
|
||||
|
||||
for(int i = 0; i < playerList.size(); i++) {
|
||||
outputPlayerList.add(new GameOutputFormat(i, playerList.get(i).getStrategy().
|
||||
toUpperCase(), playerList.get(i).getFinalScore()));
|
||||
}
|
||||
|
||||
Filter filter = new Filter();
|
||||
filter.filterPlayersByScore(outputPlayerList);
|
||||
|
||||
for(GameOutputFormat player : outputPlayerList){
|
||||
System.out.printf("%d %s %d\n", player.getNumber(), player.getStrategy(),
|
||||
player.getScore());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.tema1.main;
|
||||
|
||||
public class GameOutputFormat {
|
||||
private int number;
|
||||
private String strategy;
|
||||
private int score;
|
||||
|
||||
public GameOutputFormat(int number, String strategy, int score) {
|
||||
this.number = number;
|
||||
this.strategy = strategy;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
public int getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
public void setNumber(int number) {
|
||||
this.number = number;
|
||||
}
|
||||
|
||||
public String getStrategy() {
|
||||
return strategy;
|
||||
}
|
||||
|
||||
public void setStrategy(String strategy) {
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public void setScore(int score) {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "GameOutputFormat{" +
|
||||
"number=" + number +
|
||||
", strategy='" + strategy + '\'' +
|
||||
", score=" + score +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.tema1.main;
|
||||
|
||||
|
||||
import com.tema1.engine.GameEngine;
|
||||
import com.tema1.player.Player;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(final String[] args) throws ClassNotFoundException, NoSuchMethodException,
|
||||
InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
|
||||
GameInputLoader gameInputLoader = new GameInputLoader(args[0], args[1]);
|
||||
GameInput gameInput = gameInputLoader.load();
|
||||
//TODO implement homework logic
|
||||
|
||||
List<Player> playerList;
|
||||
|
||||
GameEngine gameEngine = new GameEngine(gameInput.getPlayerNames(), gameInput.getAssetIds());
|
||||
playerList = gameEngine.play(gameInput.getRounds());
|
||||
|
||||
GameOutput gameOutput = new GameOutput();
|
||||
gameOutput.writeToFile(playerList, args[1]);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,114 @@
|
|||
package com.tema1.player;
|
||||
|
||||
import com.tema1.goods.Goods;
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
import com.tema1.goods.GoodsType;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class FinalScoreCalculator {
|
||||
|
||||
public void convertBonusToGoods(Player player){
|
||||
List<Integer> bonusList = new ArrayList<Integer>();
|
||||
|
||||
for (Integer good : player.getStall()) {
|
||||
if(GoodsFactory.getInstance().getGoodsById(good).getType() == GoodsType.Illegal) {
|
||||
|
||||
for(Map.Entry<Goods, Integer> bonus : GoodsFactory.getInstance().getIllegalGoodsById(good).
|
||||
getIllegalBonus().entrySet()){
|
||||
|
||||
int times = bonus.getValue();
|
||||
|
||||
for(int i = 0; i < times; i++){
|
||||
bonusList.add(bonus.getKey().getId());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
player.addToStall(bonusList);
|
||||
}
|
||||
|
||||
public int getTotalLegalProfit(Player player) {
|
||||
int totalProfit = 0;
|
||||
|
||||
for (Integer good : player.getStall()) {
|
||||
if(GoodsFactory.getInstance().getGoodsById(good).getType() == GoodsType.Legal) {
|
||||
totalProfit += GoodsFactory.getInstance().getGoodsById(good).getProfit();
|
||||
}
|
||||
}
|
||||
|
||||
return totalProfit;
|
||||
}
|
||||
|
||||
public int getTotalIllegalProfit(Player player){
|
||||
int totalProfit = 0;
|
||||
|
||||
for (Integer good : player.getStall()) {
|
||||
if(GoodsFactory.getInstance().getGoodsById(good).getType() == GoodsType.Illegal) {
|
||||
totalProfit += GoodsFactory.getInstance().getGoodsById(good).getProfit();
|
||||
}
|
||||
}
|
||||
|
||||
return totalProfit;
|
||||
}
|
||||
|
||||
public void getKingQueenBonus(List<Player> playerList){
|
||||
Map<Integer, Goods> allGoods = GoodsFactory.getInstance().getAllGoods();
|
||||
|
||||
for(Map.Entry<Integer, Goods> entry : allGoods.entrySet()){
|
||||
SpecialPlayer kingPlayer = new SpecialPlayer();
|
||||
SpecialPlayer queenPlayer = new SpecialPlayer();
|
||||
|
||||
if(entry.getValue().getType() == GoodsType.Legal){
|
||||
|
||||
for(Player player : playerList){
|
||||
if(calculateHowManyOfOneGood(player, entry.getKey()) > kingPlayer.getHowMany()){
|
||||
|
||||
queenPlayer.setHowMany(kingPlayer.getHowMany());
|
||||
queenPlayer.setPlayer(kingPlayer.getPlayer());
|
||||
|
||||
kingPlayer.setPlayer(player);
|
||||
kingPlayer.setHowMany(calculateHowManyOfOneGood(player, entry.getKey()));
|
||||
|
||||
}
|
||||
else if(calculateHowManyOfOneGood(player, entry.getKey()) > queenPlayer.getHowMany()
|
||||
&& calculateHowManyOfOneGood(player, entry.getKey()) <= kingPlayer.getHowMany()){
|
||||
|
||||
queenPlayer.setHowMany(calculateHowManyOfOneGood(player, entry.getKey()));
|
||||
queenPlayer.setPlayer(player);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(kingPlayer.getPlayer() != null) {
|
||||
kingPlayer.getPlayer().addToFinalScore(GoodsFactory.getInstance().getLegalGoodsById(entry.getKey()).
|
||||
getKingBonus());
|
||||
}
|
||||
|
||||
if(queenPlayer.getPlayer() != null){
|
||||
queenPlayer.getPlayer().addToFinalScore(GoodsFactory.getInstance().getLegalGoodsById(entry.getKey()).
|
||||
getQueenBonus());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int calculateHowManyOfOneGood(Player player, int good){
|
||||
int counter = 0;
|
||||
|
||||
for(Integer stallGood : player.getStall()){
|
||||
if(stallGood.equals(good)){
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,107 @@
|
|||
package com.tema1.player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Player {
|
||||
private String strategy;
|
||||
private PlayerRole role;
|
||||
private List<Integer> cards;
|
||||
private Integer offeredBribe;
|
||||
private Integer declaredGood;
|
||||
private int money;
|
||||
private List<Integer> stall;
|
||||
private int finalScore;
|
||||
|
||||
public Player(final String strategy, PlayerRole role, final List<Integer> cards){
|
||||
this.strategy = strategy;
|
||||
this.role = role;
|
||||
this.cards = cards;
|
||||
|
||||
this.offeredBribe = 0;
|
||||
this.declaredGood = 0;
|
||||
this.money = 80;
|
||||
|
||||
this.stall = new ArrayList<Integer>();
|
||||
this.finalScore = 0;
|
||||
}
|
||||
|
||||
public String getStrategy() {
|
||||
return strategy;
|
||||
}
|
||||
|
||||
public void setStrategy(String strategy) {
|
||||
this.strategy = strategy;
|
||||
}
|
||||
|
||||
public PlayerRole getRole() {
|
||||
return role;
|
||||
}
|
||||
|
||||
public void setRole(PlayerRole role) {
|
||||
this.role = role;
|
||||
}
|
||||
|
||||
public List<Integer> getCards() {
|
||||
return cards;
|
||||
}
|
||||
|
||||
public void setCards(List<Integer> cards) {
|
||||
this.cards = cards;
|
||||
}
|
||||
|
||||
public Integer getOfferedBribe() {
|
||||
return offeredBribe;
|
||||
}
|
||||
|
||||
public void setOfferedBribe(Integer offeredBribe) {
|
||||
this.offeredBribe = offeredBribe;
|
||||
}
|
||||
|
||||
public Integer getDeclaredGood() {
|
||||
return declaredGood;
|
||||
}
|
||||
|
||||
public void setDeclaredGood(Integer declaredGood) {
|
||||
this.declaredGood = declaredGood;
|
||||
}
|
||||
|
||||
public int getMoney() {
|
||||
return money;
|
||||
}
|
||||
|
||||
public List<Integer> getStall() {
|
||||
return stall;
|
||||
}
|
||||
|
||||
public void addToStall(List<Integer> stall) {
|
||||
this.stall.addAll(stall);
|
||||
}
|
||||
|
||||
public void setMoney(int money) {
|
||||
this.money = money;
|
||||
}
|
||||
|
||||
public int getFinalScore() {
|
||||
return finalScore;
|
||||
}
|
||||
|
||||
public void addToFinalScore(int finalScore) {
|
||||
this.finalScore += finalScore;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Player{" +
|
||||
"strategy='" + strategy + '\'' +
|
||||
", role=" + role +
|
||||
", cards=" + cards +
|
||||
", offeredBribe=" + offeredBribe +
|
||||
", declaredGood=" + declaredGood +
|
||||
", money=" + money +
|
||||
", stall=" + stall +
|
||||
", finalScore=" + finalScore +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.tema1.player;
|
||||
|
||||
public enum PlayerRole {
|
||||
Trader, Sheriff
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.tema1.player;
|
||||
|
||||
public class SpecialPlayer {
|
||||
private Player player;
|
||||
private int howMany;
|
||||
|
||||
public SpecialPlayer() {
|
||||
this.player = null;
|
||||
this.howMany = 0;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return player;
|
||||
}
|
||||
|
||||
public void setPlayer(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
public int getHowMany() {
|
||||
return howMany;
|
||||
}
|
||||
|
||||
public void setHowMany(int howMany) {
|
||||
this.howMany = howMany;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.tema1.strategy;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Bag {
|
||||
private List<Integer> bag;
|
||||
private int offeredBribe;
|
||||
|
||||
public Bag(List<Integer> bag, int offeredBribe) {
|
||||
this.bag = bag;
|
||||
this.offeredBribe = offeredBribe;
|
||||
}
|
||||
|
||||
public List<Integer> getBag() {
|
||||
return bag;
|
||||
}
|
||||
|
||||
public void setBag(List<Integer> bag) {
|
||||
this.bag = bag;
|
||||
}
|
||||
|
||||
public int getOfferedBribe() {
|
||||
return offeredBribe;
|
||||
}
|
||||
|
||||
public void setOfferedBribe(int offeredBribe) {
|
||||
this.offeredBribe = offeredBribe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bag{" +
|
||||
"bag=" + bag +
|
||||
", offeredBribe=" + offeredBribe +
|
||||
'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package com.tema1.strategy;
|
||||
|
||||
import com.tema1.cards.CardsChecker;
|
||||
import com.tema1.engine.inspection.InspectionOutcome;
|
||||
import com.tema1.filters.Filter;
|
||||
import com.tema1.filters.GetCards;
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
import com.tema1.player.Player;
|
||||
import com.tema1.strategy.basicutils.BasicUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Basic {
|
||||
public Bag createBag(List<Integer> cards, int money){
|
||||
|
||||
BasicUtils basicUtils = new BasicUtils();
|
||||
CardsChecker cardsChecker = new CardsChecker();
|
||||
List<Integer> cardsInBag = new ArrayList<Integer>();
|
||||
|
||||
if(!cardsChecker.checkForLegalCards(cards)){
|
||||
cardsInBag.add(basicUtils.getMostProfitableIllegalCard(cards));
|
||||
|
||||
if(money - GoodsFactory.getInstance().getGoodsById(cardsInBag.get(0)).getPenalty() < 0){
|
||||
cardsInBag.clear();
|
||||
}
|
||||
|
||||
return new Bag(cardsInBag, 0);
|
||||
}
|
||||
|
||||
GetCards getCards = new GetCards();
|
||||
List<Integer> legalCards = getCards.getLegalCards(cards);
|
||||
|
||||
Filter filter = new Filter();
|
||||
filter.filterLegalByFreqProfitAndId(legalCards);
|
||||
getCards.getBestCardForBasic(legalCards);
|
||||
|
||||
if(cardsInBag.size() > 8) {
|
||||
basicUtils.truncate(legalCards);
|
||||
}
|
||||
|
||||
return new Bag(legalCards, 0);
|
||||
}
|
||||
|
||||
public Integer declareBag(List<Integer> cards){
|
||||
CardsChecker cardsChecker = new CardsChecker();
|
||||
|
||||
/* If there is no legal card it means that one illegal card
|
||||
was choosen and apples will be declared
|
||||
*/
|
||||
if(!cardsChecker.checkForLegalCards(cards)){
|
||||
return 0;
|
||||
}
|
||||
else{
|
||||
return cards.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
public InspectionOutcome inspectBag(Player sheriff, Player player){
|
||||
InspectionOutcome outcome = new InspectionOutcome();
|
||||
|
||||
if(sheriff.getMoney() < 16){
|
||||
return outcome;
|
||||
}
|
||||
|
||||
|
||||
Integer declaredCard = player.getDeclaredGood();
|
||||
|
||||
for(Integer card : player.getCards()){
|
||||
|
||||
if(card.equals(declaredCard)){
|
||||
outcome.setMoneyToGetFromSheriff(outcome.getMoneyToGetFromSheriff() +
|
||||
GoodsFactory.getInstance().getGoodsById(card).getPenalty());
|
||||
outcome.getPassedGoods().add(card);
|
||||
}
|
||||
else{
|
||||
outcome.setMoneyToPayToSheriff(outcome.getMoneyToPayToSheriff() +
|
||||
GoodsFactory.getInstance().getGoodsById(card).getPenalty());
|
||||
outcome.getRestrictedGoods().add(card);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Check if the trader was honest or not */
|
||||
if(!outcome.getRestrictedGoods().isEmpty() && outcome.getMoneyToPayToSheriff() != 0){
|
||||
outcome.setMoneyToGetFromSheriff(0);
|
||||
}
|
||||
|
||||
return outcome;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,155 @@
|
|||
package com.tema1.strategy;
|
||||
|
||||
import com.tema1.cards.CardsChecker;
|
||||
import com.tema1.engine.inspection.InspectionOutcome;
|
||||
import com.tema1.filters.Filter;
|
||||
import com.tema1.filters.GetCards;
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
import com.tema1.player.Player;
|
||||
import com.tema1.strategy.bribedutils.BribedUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Bribed extends Basic {
|
||||
@Override
|
||||
public Bag createBag(List<Integer> cards, int money){
|
||||
|
||||
CardsChecker cardsChecker = new CardsChecker();
|
||||
|
||||
/* If there are no illegal cards play as basic */
|
||||
if(!cardsChecker.checkForIllegalCards(cards) || money < 5){
|
||||
Basic basicPlayer = new Basic();
|
||||
return basicPlayer.createBag(cards, money);
|
||||
}
|
||||
|
||||
GetCards getCards = new GetCards();
|
||||
List<Integer> illegalCards = getCards.getIllegalCards(cards);
|
||||
|
||||
Filter filter = new Filter();
|
||||
filter.filterIllegalByProfit(illegalCards);
|
||||
|
||||
|
||||
/* Put illegal cards in the bag */
|
||||
List<Integer> cardsInBag = new ArrayList<Integer>();
|
||||
int totalPenalty = 0;
|
||||
|
||||
BribedUtils bribedUtils = new BribedUtils();
|
||||
|
||||
for(Integer card : illegalCards){
|
||||
|
||||
if(cardsInBag.size() < 8){
|
||||
cardsInBag.add(card);
|
||||
int bribe = bribedUtils.calculateBribe(cardsInBag.size());
|
||||
|
||||
int penalty = GoodsFactory.getInstance().getGoodsById(card).getPenalty();
|
||||
totalPenalty += penalty;
|
||||
|
||||
if(money - bribe <= 0 || money - totalPenalty <= 0){
|
||||
cardsInBag.remove(card);
|
||||
totalPenalty -= penalty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If the cardsInBag is empty it means that the bribe player
|
||||
could not afford any illegal good so he will try to play base
|
||||
strategy
|
||||
*/
|
||||
if(cardsInBag.isEmpty()){
|
||||
Basic basicPlayer = new Basic();
|
||||
return basicPlayer.createBag(cards, money);
|
||||
}
|
||||
|
||||
/* If there is remaining space try to add some legal cards too */
|
||||
int finalBribe = bribedUtils.calculateBribe(cardsInBag.size());
|
||||
|
||||
/* Add legal cards until limit 8 is reached or the player has no more money
|
||||
Add them by profit and id
|
||||
*/
|
||||
|
||||
if(cardsInBag.size() < 8){
|
||||
List<Integer> legalCards = getCards.getLegalCards(cards);
|
||||
filter.filterLegalByProfitAndId(legalCards);
|
||||
for(Integer card : legalCards){
|
||||
|
||||
if(cardsInBag.size() < 8){
|
||||
cardsInBag.add(card);
|
||||
|
||||
int penalty = GoodsFactory.getInstance().getGoodsById(card).getPenalty();
|
||||
totalPenalty += penalty;
|
||||
|
||||
/* If the card is apple it means that the bribed player has to pay no
|
||||
penalty because he will declare apples
|
||||
*/
|
||||
// if(GoodsFactory.getInstance().getGoodsById(card).getId() == 0){
|
||||
// totalPenalty -= penalty;
|
||||
// penalty = 0;
|
||||
// }
|
||||
|
||||
if(money - finalBribe < 0 || money - totalPenalty <= 0){
|
||||
cardsInBag.remove(card);
|
||||
totalPenalty -= penalty;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return new Bag(cardsInBag, finalBribe);
|
||||
}
|
||||
|
||||
public Integer declareBag(List<Integer> cards){
|
||||
/* If there is no legal good it means that the bribe strategy
|
||||
was not applied so we return the declaration for basic strategy
|
||||
*/
|
||||
CardsChecker cardsChecker = new CardsChecker();
|
||||
if(!cardsChecker.checkForIllegalCards(cards)){
|
||||
return super.declareBag(cards);
|
||||
}
|
||||
|
||||
/* Otherwise return apples */
|
||||
return 0;
|
||||
}
|
||||
|
||||
public InspectionOutcome inspectBag(Player sheriff, Player player) {
|
||||
InspectionOutcome outcome = new InspectionOutcome();
|
||||
|
||||
if(sheriff.getMoney() < 16 ){
|
||||
outcome.setPassedGoods(new ArrayList<Integer>(player.getCards()));
|
||||
return outcome;
|
||||
}
|
||||
|
||||
int bribe = player.getOfferedBribe();
|
||||
|
||||
if (bribe != 0) {
|
||||
outcome.setMoneyToPayToSheriff(bribe);
|
||||
outcome.setPassedGoods(new ArrayList<Integer>(player.getCards()));
|
||||
|
||||
return outcome;
|
||||
} else {
|
||||
Integer declaredCard = player.getDeclaredGood();
|
||||
|
||||
for (Integer card : player.getCards()) {
|
||||
|
||||
if (card.equals(declaredCard)) {
|
||||
outcome.setMoneyToGetFromSheriff(outcome.getMoneyToGetFromSheriff() +
|
||||
GoodsFactory.getInstance().getGoodsById(card).getPenalty());
|
||||
outcome.getPassedGoods().add(card);
|
||||
} else {
|
||||
outcome.setMoneyToPayToSheriff(outcome.getMoneyToPayToSheriff() +
|
||||
GoodsFactory.getInstance().getGoodsById(card).getPenalty());
|
||||
outcome.getRestrictedGoods().add(card);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Check if the trader was honest or not */
|
||||
if(!outcome.getRestrictedGoods().isEmpty() && outcome.getMoneyToPayToSheriff() != 0){
|
||||
outcome.setMoneyToGetFromSheriff(0);
|
||||
}
|
||||
|
||||
return outcome;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,103 @@
|
|||
package com.tema1.strategy;
|
||||
|
||||
import com.tema1.cards.CardsChecker;
|
||||
import com.tema1.engine.inspection.InspectionOutcome;
|
||||
import com.tema1.filters.Filter;
|
||||
import com.tema1.filters.GetCards;
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
import com.tema1.player.Player;
|
||||
import com.tema1.strategy.basicutils.BasicUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Greedy extends Basic{
|
||||
public Bag createBag(List<Integer> cards, int money, int round){
|
||||
Bag createdBag = super.createBag(cards, money);
|
||||
|
||||
for(int i = 0; i < createdBag.getBag().size(); i++){
|
||||
cards.remove(createdBag.getBag().get(i));
|
||||
}
|
||||
|
||||
if(((round % 2) == 1) && (createdBag.getBag().size() < 8)){
|
||||
BasicUtils basicUtils = new BasicUtils();
|
||||
GetCards getCards = new GetCards();
|
||||
Filter filter = new Filter();
|
||||
|
||||
List<Integer> illegalCards = getCards.getIllegalCards(cards);
|
||||
|
||||
if(illegalCards.size() == 0){
|
||||
return createdBag;
|
||||
}
|
||||
|
||||
Integer additional = basicUtils.
|
||||
getMostProfitableIllegalCard(illegalCards);
|
||||
|
||||
if(money - GoodsFactory.getInstance().getGoodsById(additional).getPenalty() <= 0){
|
||||
return createdBag;
|
||||
}
|
||||
|
||||
createdBag.getBag().add(additional);
|
||||
|
||||
return createdBag;
|
||||
|
||||
|
||||
}
|
||||
|
||||
return createdBag;
|
||||
}
|
||||
|
||||
public Integer declareBag(List<Integer> cards){
|
||||
CardsChecker cardsChecker = new CardsChecker();
|
||||
|
||||
/* If the player has illegal cand legal cards at the same time
|
||||
it means that he played greedy strategy and we return tha
|
||||
first element which is the best legal card
|
||||
*/
|
||||
if(cardsChecker.checkForLegalCards(cards) && cardsChecker.
|
||||
checkForIllegalCards(cards)){
|
||||
return cards.get(0);
|
||||
}
|
||||
else{
|
||||
return super.declareBag(cards);
|
||||
}
|
||||
}
|
||||
|
||||
public InspectionOutcome inspectBag(Player sheriff, Player player){
|
||||
InspectionOutcome outcome = new InspectionOutcome();
|
||||
|
||||
int bribe = player.getOfferedBribe();
|
||||
|
||||
if(bribe != 0){
|
||||
outcome.setMoneyToPayToSheriff(bribe);
|
||||
outcome.setPassedGoods(new ArrayList<Integer>(player.getCards()));
|
||||
|
||||
return outcome;
|
||||
}
|
||||
else{
|
||||
Integer declaredCard = player.getDeclaredGood();
|
||||
|
||||
for(Integer card : player.getCards()){
|
||||
|
||||
if(card.equals(declaredCard)){
|
||||
outcome.setMoneyToGetFromSheriff(outcome.getMoneyToGetFromSheriff() +
|
||||
GoodsFactory.getInstance().getGoodsById(card).getPenalty());
|
||||
outcome.getPassedGoods().add(card);
|
||||
}
|
||||
else{
|
||||
outcome.setMoneyToPayToSheriff(outcome.getMoneyToPayToSheriff() +
|
||||
GoodsFactory.getInstance().getGoodsById(card).getPenalty());
|
||||
outcome.getRestrictedGoods().add(card);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Check if the trader was honest or not */
|
||||
if(!outcome.getRestrictedGoods().isEmpty() && outcome.getMoneyToPayToSheriff() != 0){
|
||||
outcome.setMoneyToGetFromSheriff(0);
|
||||
}
|
||||
|
||||
return outcome;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.tema1.strategy.basicutils;
|
||||
|
||||
import com.tema1.filters.GetCards;
|
||||
import com.tema1.goods.GoodsFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class BasicUtils {
|
||||
public Integer getMostProfitableIllegalCard(List<Integer> cards){
|
||||
if(cards.isEmpty()){
|
||||
return -1;
|
||||
}
|
||||
|
||||
GetCards getCards = new GetCards();
|
||||
List<Integer> illegalCards = getCards.getIllegalCards(cards);
|
||||
|
||||
Comparator<Integer> comparatorProfit = (Integer a, Integer b) ->{
|
||||
Integer aProfit = GoodsFactory.getInstance().getGoodsById(a).getProfit();
|
||||
Integer bProfit = GoodsFactory.getInstance().getGoodsById(b).getProfit();
|
||||
|
||||
return aProfit.compareTo(bProfit);
|
||||
};
|
||||
|
||||
return Collections.max(illegalCards, comparatorProfit);
|
||||
}
|
||||
|
||||
public void truncate(List<Integer> legalCards){
|
||||
legalCards = legalCards.subList(0, 8);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.tema1.strategy.bribedutils;
|
||||
|
||||
public class BribedUtils {
|
||||
public int calculateBribe(final int illegalCards){
|
||||
|
||||
if(illegalCards == 0){
|
||||
return 0;
|
||||
}
|
||||
else if(illegalCards <= 2){
|
||||
return 5;
|
||||
}
|
||||
else if(illegalCards > 2){
|
||||
return 10;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue