Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Pandemic.player;
- import Pandemic.Gameboard.GameBoard;
- import Pandemic.Gameboard.SimulatePandemic;
- import Pandemic.cities.City;
- import Pandemic.cities.CityList;
- import Pandemic.variables.Disease;
- import Pandemic.variables.Piece;
- import Pandemic.variables.Variables;
- import Pandemic.actions.*;
- import java.io.*;
- import java.util.*;
- public class Player{
- String playerName;
- int tactic;
- String playerRole;
- String tmpRole;
- GameBoard pandemicBoard;
- Piece playerPiece;
- int playerAction; //number of action per turn for each player
- boolean activePlayer;
- public ArrayList<City> hand ; //hand_cards maybe from action
- ArrayList<City> tmpFriendHand;
- String[] possibleColour = {"Red","Blue","Yellow","Black"};
- ArrayList<Action> suggestions = new ArrayList<Action>();
- City tmpLocation ;
- final int MAX_DEPTH = 4; //Depth of the heuristic_function
- ArrayList<Action> max_score_sequence = new ArrayList<Action>();
- float max_score= 0;
- int numOfRounds = 0;
- boolean ExecuteFlag = false;
- boolean friendPlaying = false;
- //------------for storing
- private ArrayList<City> freezeCities = new ArrayList<City>();
- private int f_redCube,f_blueCubes,f_yellowCubes,f_blackCubes;
- //private ArrayList<Disease> f_diseases = new ArrayList<Disease>();
- private ArrayList<City> f_ResearchStation = new ArrayList<City>();
- private City f1_location,f2_location,f3_location,f4_location;
- private boolean diseasesCured[] = new boolean[4];
- private ArrayList<City> freezeHand = new ArrayList<City>();
- //--------------to return
- /*
- * Constructor for objects of class Player
- */
- /* History of Actions
- */
- public Player(String pName,String pRole)
- {
- playerName = pName;
- hand = new ArrayList<City>();
- playerAction = 4;
- tactic = 50;
- playerRole = pRole;
- numOfRounds = 0;
- }
- public void setGameBoard(GameBoard currentBoard) { pandemicBoard = currentBoard; }
- public void setPlayerPiece(Piece newPiece) { playerPiece = newPiece; }
- public ArrayList<Action> getSuggestions() { return suggestions; }
- public void setSuggestions(ArrayList<Action> suggestions) {this.suggestions = suggestions; }
- /*
- *draw @param numberOfCards from playerPile and check if it is epidemic
- *and call @resolveEpidemic() or draw to @hand()
- */
- public void drawCard(int numberOfCards)
- {
- // draws a card for the player from the board
- for (int i = 0; i<numberOfCards; i++)
- {
- if (pandemicBoard.playerPile.size()!= 0)
- {
- //first element from array list PlayerPile
- if (pandemicBoard.playerPile.get(0).equals(Variables.isEpidemic)) {
- System.out.println("-----EPIDEMIC DRAWN!-----");
- pandemicBoard.resolveEpidemic();//follow the steps for epidemic event
- }
- else
- {
- //adds a new card to the players hand.
- hand.add((City) pandemicBoard.playerPile.get(0));
- }
- pandemicBoard.playerPile.remove(0);//remove the card from PlayerDeck
- System.out.println(this.getPlayerName() + " draws a card");
- }
- else
- {
- System.out.println("no more cards left");
- }
- }
- }
- /*
- * Count how many cards with a specific colour
- * player has in his hands
- * @param colour
- */
- public int getCountXCards(String colour)
- {
- int toReturn = 0;
- for (int i = 0 ; i < hand.size(); i++)
- {
- if (hand.get(i).getColour().equals(colour))
- {
- toReturn ++;
- }
- }
- return toReturn;
- }
- /*
- * remove a card from hand ,
- * then calls methods to put it in the discard pile()
- * and remove the card from the hand.
- */
- public void discardCard(String cardName)
- {
- int toDiscard =0;
- for (int i = 0; i < getHand().size(); i++)
- {
- if (cardName.equals(getHand().get(i).getName())){
- //System.out.println("found matching card in hand");
- toDiscard=i;
- }
- }
- pandemicBoard.addPlayerCardDiscard(hand.get(toDiscard));//remove from playerDeck to put in PlayerDiscardDeck
- hand.remove(toDiscard);
- }
- //discard all the cards needed for cure
- public void discardCureCards(String colour,int numberToDiscard)
- {
- for (int i = 0 ; i < numberToDiscard ; i ++)
- {
- for (int x = 0 ; x < hand.size(); x++ )
- {
- if (hand.get(x).getColour().equals(colour))
- {
- discardCard(hand.get(x).getName());
- break;
- }
- }
- }
- }
- //get PlayerAction
- public int getPlayerAction(){ return playerAction; }
- //decrease the playerAction (Max=4)
- public void decreasePlayerAction(){ playerAction--;}
- // sets a players action back to 4
- public void resetPlayerAction() {
- System.out.println("?????????");
- playerAction=4; }
- //return the name of player
- public String getPlayerName() { return playerName; }
- // Returns an array with the players cards in hand
- public ArrayList<City> getHand() { return hand; }
- public String getPlayerRole() {return playerRole; }
- public void setPlayerRole(String playerRole) {this.playerRole = playerRole; }
- public Piece getPlayerPiece() { return playerPiece; }
- /**********************************************************************************
- *******These are the main (7+4) methods used to control the players actions********
- **********************************************************************************/
- //Build research station as OPERATIONS_EXPERT
- //Build research station
- public boolean buildResearchStation ()
- {
- buildResearchStation tmp = new buildResearchStation(playerPiece.getLocation(),getHand());
- if (playerRole.equals("OPERATIONS_EXPERT") && !Variables.CITY_WITH_RESEARCH_STATION.contains(playerPiece.getLocation()))
- {
- Variables.ADD_CITY_WITH_RESEARCH_STATION(playerPiece.getLocation());
- if(ExecuteFlag) {
- System.out.println("building a research station in " + playerPiece.getLocation().getName());
- suggestions.add(tmp);
- decreasePlayerAction();
- }
- return true;
- }
- else
- {
- if (tmp.isaLegalMove())
- {
- discardCard(playerPiece.getLocation().getName());
- Variables.ADD_CITY_WITH_RESEARCH_STATION(playerPiece.getLocation());
- if(ExecuteFlag) {
- System.out.println("building a research station in " + playerPiece.getLocation().getName());
- suggestions.add(tmp);
- decreasePlayerAction();
- }
- return true;
- }
- }
- return false;
- }
- //Treat disease action
- public boolean treatDisease (City location, String colour) {
- treatDisease tmp = new treatDisease(location,colour);
- if (playerRole.equals("MEDIC")) {
- if(tmp.isaLegalMove()==true && location == playerPiece.getLocation())
- {
- if(ExecuteFlag)
- System.out.println("Removing all " + colour + " cube from " + location.getName());
- int abc = location.getCubeColour(colour);
- for (int i=0;i<abc;i++) {
- //System.out.println(i);
- location.removeCube(colour);
- if(ExecuteFlag) {
- pandemicBoard.addCube(colour);//add in pool of cube
- decreasePlayerAction();
- }
- }
- if(ExecuteFlag)
- suggestions.add(tmp);
- return true;
- }
- }
- else {
- if(tmp.isaLegalMove()==true && location == playerPiece.getLocation())
- {
- if(ExecuteFlag)
- System.out.println("Removing a " + colour + " cube from " + location.getName());
- location.removeCube(colour);
- if(ExecuteFlag) {
- pandemicBoard.addCube(colour);//add in pool of cube
- suggestions.add(tmp);
- decreasePlayerAction();
- }
- return true;
- }
- }
- return false;
- }
- // Drive action
- public boolean driveCity (City location, City destination)
- {
- // System.out.println(getPlayerName() + " current location is in " + location);
- // System.out.println("and he wants to go in " + destination);
- // System.out.println("attempting to move " + getPlayerName() + " to " + destination.getName() + " from "+ location.getName());
- driveCity tmp = new driveCity(location,destination);
- if (tmp.isaLegalMove())
- {
- if(ExecuteFlag)
- System.out.println(getPlayerName() + " drives from " + location.getName() + " to " + destination.getName() + ".");
- playerPiece.setLocation(destination);
- if(ExecuteFlag)
- suggestions.add(tmp);
- decreasePlayerAction();
- return true;
- }
- else
- {
- System.out.println("the location isn't connected");
- //decreasePlayerAction();
- }
- return false;
- }
- // Charter Flight action
- public boolean charterFlight(City location, City destination)
- {
- // System.out.println(getPlayerName() + " wants to flying from " +
- // location.getName() + " to "+ destination.getName() +
- // " on a charter flight");
- charterFlight tmp = new charterFlight(location,getHand(),destination);
- if (tmp.isaLegalMove() && playerPiece.getLocation().equals(location))
- {
- if(ExecuteFlag)
- System.out.println(getPlayerName() + " takes a charter flight to " +
- destination.getName() + " from " + location.getName() );
- discardCard(location.getName());
- playerPiece.setLocation(destination);
- if(ExecuteFlag) {
- suggestions.add(tmp);
- decreasePlayerAction();
- }
- return true;
- }
- return false;
- }
- //Direct flight
- public boolean directFlight(City location, City destination)
- {
- // System.out.println(getPlayerName() + " wants to flying from " +
- // location.getName() + " to "+ destination.getName() +
- // " on a direct flight");
- directFlight tmp = new directFlight(destination,getHand());
- if (tmp.isaLegalMove())
- {
- if(ExecuteFlag) {
- System.out.println(getPlayerName() + " takes a direct flight to " +
- destination.getName() + " from " + location.getName() );
- decreasePlayerAction();
- }
- discardCard(destination.getName());
- playerPiece.setLocation(destination);
- if(ExecuteFlag)
- suggestions.add(tmp);
- return true;
- }
- return false;
- }
- //ShuttleFlight
- public boolean shuttleFlight(City location, City destination)
- {
- // System.out.println(getPlayerName() + " wants to flying from " +
- // location.getName() + " to "+ destination.getName() +
- // " on a shuttle flight");
- shuttleFlight tmp = new shuttleFlight(location,destination);
- if(tmp.isaLegalMove())
- {
- if(ExecuteFlag)
- System.out.println(getPlayerName() + " takes a shuttle flight to " +
- destination.getName() + " from " + location.getName() );
- playerPiece.setLocation(destination);
- if(ExecuteFlag) {
- suggestions.add(tmp);
- decreasePlayerAction();
- }
- return true;
- }
- return false;
- }
- // Discover Cure action
- public boolean discoverCure(City location, String colour)
- {
- discoverCure tmp = new discoverCure(location,getHand(),colour);
- if (playerRole.equals("SCIENTIST")) {
- if (tmp.isaLegalMove(1))
- {
- if(ExecuteFlag)
- System.out.println("Its possible to discover a cure");
- discardCureCards(colour,pandemicBoard.getNeededForCure()-1);
- pandemicBoard.cureDisease(colour);
- if(ExecuteFlag) {
- suggestions.add(tmp);
- decreasePlayerAction();
- }
- return true;
- }
- }
- else
- {
- if (tmp.isaLegalMove(0))
- {
- if(ExecuteFlag)
- System.out.println("Its possible to discover a cure");
- discardCureCards(colour,pandemicBoard.getNeededForCure());
- pandemicBoard.cureDisease(colour);
- if(ExecuteFlag) {
- suggestions.add(tmp);
- decreasePlayerAction();
- }
- return true;
- }
- }
- return false;
- }
- /*
- *Below are implemented
- *1) --> OPERATIONS_EXPERT special ability (build research station without to discard a card) is implemented as if in the @BuildResearchStaion method
- *2) --> MEDIC special ability (treat all the cubes of a specific colour) implemented in @treatDisease as if statement
- *3) --> SCIENTIST special ability is implemented as if in the classic cure disease
- *4) --> QUARANTINE_SPECIALIST special ability is implemented in @infectCityPhase in @GameBoard class
- */
- //---------------------------------------------------------------------------------
- /**********************************************************************************
- ***************** These methods are used for AI controlled players.****************
- **********************************************************************************/
- //---------------------------------------------------------------------------------
- /** Design Doc: How should the other suggestions be evaluated
- - Run scoreDisese (enhanced) for each suggestions (if actions consist of buildResearch change the scoring)
- */
- @SuppressWarnings("unchecked")
- public void makeDecision(ArrayList<City> friend_hand,String Role)
- {
- ExecuteFlag = false;
- suggestions.clear();
- tmpLocation = playerPiece.getLocation();
- tmpFriendHand = (ArrayList<City>) hand.clone();
- playerAction = 4;
- tmpRole = playerRole;
- playerRole = Role;
- for(Piece p : pandemicBoard.playerPieces ){
- //System.out.println(p.owner.getPlayerRole() + " Location : " + p.owner.playerPiece.getLocation());
- if(p.owner.getPlayerRole().equals(playerRole)) {
- playerPiece.setLocation(p.getLocation());
- //System.out.println("Set new location : " + p.getLocation());
- //break;
- }
- }
- if(friend_hand != null) {
- //to set new location
- hand = (ArrayList<City>) friend_hand.clone();
- friendPlaying = true;
- }else{
- numOfRounds ++;
- friendPlaying = false;
- }
- if(friend_hand == null) {
- System.out.println("--------------------------------------------------------TURN: "+ numOfRounds+ " Actions left:" + playerAction );
- System.out.println(this.getPlayerName() + " is thinking..... "+ hand);
- }
- if(friend_hand == null){
- //TODO When its our turn(friend hand == null) Initialize/Renew Opponent modeling on Variables.Suggestions or Variables.Actions
- Float[] scoredSuggestions = new Float[4];
- int i = 0;
- for(ArrayList<Action> a : Variables.Suggestions ) {
- System.out.println(a);
- }
- System.out.println();
- for(ArrayList<Action> sequence : Variables.Suggestions) {
- if(sequence == null) {
- sequence = new ArrayList<Action>();
- }
- System.out.println();
- System.out.println(i);
- //System.out.println(sequence);
- if(sequence.size() != 0 ) {
- freeze();
- scoredSuggestions[i] = scoreSuggestion(sequence);
- unfreeze();
- System.out.println(scoredSuggestions[i]);
- }
- i++;
- }
- }
- /**********************************************************************************************************************************/
- max_score = -10000000;
- float temp_score = 0;
- playerAction = 4;
- max_score_sequence.clear();
- System.out.println("Location : " + playerPiece.getLocation().getName());
- if(friend_hand != null) {
- freeze();
- }
- ExecuteFlag = true;
- while (enoughCardsForCure() && playerAction>0) {// may need to cure more than one Disease
- // System.out.println("might be worth trying to find a cure. disatnce : "+ getDistanceResearch());
- if (discoverCure(playerPiece.getLocation(),tryCureCardColour())) {
- // System.out.println(this.getPlayerName() + " has discovered a cure!");
- temp_score += 100000;
- System.out.println("Yeah!!");
- System.out.println(" Yeah!!");
- System.out.println(" Yeah!!");
- }else
- { // enough cards for cure- drive to nearest research station
- // System.out.println("They need to go to a researh station.");
- while(playerAction >0) {
- tryDriveResearchStation();
- System.out.println(tryCureCardColour());
- if(playerAction > 0) {
- if(discoverCure(playerPiece.getLocation(),tryCureCardColour())) {
- //System.out.println(this.getPlayerName() + " has discovered a cure!");
- temp_score += 100000;
- System.out.println("Yeah!!");
- System.out.println(" Yeah!!");
- System.out.println(" Yeah!!");
- break;
- }
- }
- }
- }
- }
- //TODO check if we ever lose from not being able to drive to a research station
- ExecuteFlag = false;
- if(friend_hand == null) {
- freeze();
- }
- heuristic(4-playerAction, pandemicBoard.getCities(),hand,temp_score,max_score_sequence,Variables.CITY_WITH_RESEARCH_STATION);
- unfreeze();
- /**********************************************************************************************************************************/
- //TODO Consider Variables.Suggestions too and choose the best action with the help of opponent Modeling
- if(! friendPlaying )
- System.out.println("Max Score: "+ max_score + " " + max_score_sequence + " hand : "+ hand + " Research Staions: "+ Variables.CITY_WITH_RESEARCH_STATION + " Player Role: " + playerRole);
- hand = (ArrayList<City>) tmpFriendHand.clone();
- //System.out.println("Location : " + playerPiece.getLocation().getName());
- ExecuteFlag = true;
- //playerAction = 4;
- if(friend_hand == null) {
- for(Action act: max_score_sequence) {
- for(City c :pandemicBoard.cities){
- if(c.getName() == playerPiece.getLocation().getName()) {
- playerPiece.setLocation(c);
- break;
- }
- }
- //System.out.println("Current location : " + playerPiece.getLocation().getName());
- if(act instanceof driveCity){
- if(!driveCity(((Pandemic.actions.driveCity) act).getMoveFrom(),((Pandemic.actions.driveCity) act).getMoveTo())) {
- System.out.println("Drive city error");
- System.exit(1);
- }
- }else if(act instanceof charterFlight){
- if(! charterFlight(((charterFlight) act).getMoveFrom(),((charterFlight) act).getMoveTo()))
- System.exit(0);
- }else if(act instanceof directFlight){
- if(!directFlight(playerPiece.getLocation(), ((Pandemic.actions.directFlight) act).getMoveTo())){
- System.out.println("Direct flight error");
- System.out.println("Destination: " + ((Pandemic.actions.directFlight) act).getMoveTo());
- System.exit(1);
- }
- }else if(act instanceof buildResearchStation){
- if(!buildResearchStation()){
- System.out.println("buildResearchStation error");
- System.out.println("City to Build: " + ((buildResearchStation) act).getCityToResearchStation());
- System.exit(1);
- }
- }else if(act instanceof discoverCure){
- if(!discoverCure(playerPiece.getLocation(), ((Pandemic.actions.discoverCure) act).getColorOfDisease())){
- System.out.println("discoverCure error");
- System.exit(1);
- }
- }else if(act instanceof shuttleFlight){
- if(!shuttleFlight(((Pandemic.actions.shuttleFlight) act).getMoveFrom(), ((Pandemic.actions.shuttleFlight) act).getMoveTo())){
- System.out.println("shuttleFlight error");
- System.exit(1);
- }
- }else if(act instanceof treatDisease){
- if(!treatDisease(playerPiece.getLocation(), ((Pandemic.actions.treatDisease) act).getColour())){
- System.out.println("treatDisease error");
- for(City c :pandemicBoard.cities){
- if(c.getMaxCube() >0) {
- System.out.println("City "+ c.getName() + " " + c.getMaxCube());
- }
- }
- System.out.println(((treatDisease) act).getLocation().getCubeColour("Red"));
- System.out.println(((treatDisease) act).getLocation().getCubeColour("Black"));
- System.out.println(((treatDisease) act).getLocation().getCubeColour("Yellow"));
- System.out.println(((treatDisease) act).getLocation().getCubeColour("Blue"));
- System.exit(1);
- }
- }else{
- //not supposed to reach this
- System.out.println("...");
- System.exit(1);
- }
- }
- System.out.println("Done the Actions Correct? hand : "+ hand + " Research Staions: "+ Variables.CITY_WITH_RESEARCH_STATION + " Remaining Actions: ");
- //System.out.println("Suggestions: "+ suggestions);
- // for(City c :pandemicBoard.cities){
- // if(c.getMaxCube() >0) {
- // System.out.println("City "+ c.getName() + " " + c.getMaxCube());
- // }
- // }
- System.out.println("Red cubes: "+pandemicBoard.redCubes);
- System.out.println("Black cubes: "+pandemicBoard.blackCubes);
- System.out.println("Yellow cubes: "+pandemicBoard.yellowCubes);
- System.out.println("Blue cubes: "+pandemicBoard.blueCubes);
- System.out.println("Red Disease: " + pandemicBoard.getDisease("Red").cured);
- System.out.println("Black Disease: " + pandemicBoard.getDisease("Black").cured);
- System.out.println("Yellow Disease: " + pandemicBoard.getDisease("Yellow").cured);
- System.out.println("Blue Disease: " + pandemicBoard.getDisease("Blue").cured);
- System.out.println("Outbreak level: "+ Variables.OUTBREAK_MARKER);
- if(suggestions.size() < 4) {
- System.out.println("suggestions.size()");
- System.exit(1);
- }
- //System.exit(1);
- return;
- }else if(friend_hand != null) {
- suggestions.addAll(max_score_sequence);
- System.out.println("Suggestions from "+ playerName + " Actions : "+ suggestions);
- if(suggestions.size()<4 || suggestions.size()>4 ) {
- System.out.println("Suggetion error");
- System.exit(1);
- }
- //Revert back to normal player variables
- unfreeze();
- playerRole = tmpRole;
- hand = (ArrayList<City>) tmpFriendHand.clone();
- playerPiece.setLocation(tmpLocation);
- playerAction = 0;
- return;
- }
- }
- @SuppressWarnings("unchecked")
- public void freeze() {
- freezeCities = (ArrayList<City>) pandemicBoard.getCities().clone();
- freezeHand = (ArrayList<City>) hand.clone();
- f_redCube = pandemicBoard.redCubes;
- f_blueCubes = pandemicBoard.blueCubes;
- f_yellowCubes = pandemicBoard.yellowCubes;
- f_blackCubes = pandemicBoard.blackCubes;
- // f_diseases = (ArrayList<Disease>) pandemicBoard.getDiseases().clone();
- for(int i=0; i<4; i++) {
- diseasesCured[i] = pandemicBoard.diseases.get(i).getCured();
- }
- f_ResearchStation = (ArrayList<City>) pandemicBoard.getResearchCentre().clone();
- f1_location = pandemicBoard.playerPieces[0].getLocation();
- f2_location = pandemicBoard.playerPieces[1].getLocation();
- f3_location = pandemicBoard.playerPieces[2].getLocation();
- f4_location = pandemicBoard.playerPieces[3].getLocation();
- }
- public void unfreeze() {
- pandemicBoard.cities = (ArrayList<City>) freezeCities;
- hand = freezeHand;
- pandemicBoard.redCubes = f_redCube;
- pandemicBoard.blueCubes = f_blueCubes;
- pandemicBoard.yellowCubes = f_yellowCubes;
- pandemicBoard.blackCubes = f_blackCubes;
- //pandemicBoard.diseases = (ArrayList<Disease>) f_diseases.clone();
- for(int i=0; i<4; i++) {
- pandemicBoard.diseases.get(i).setCured(diseasesCured[i]);
- }
- pandemicBoard.playerPieces[0].setLocation(f1_location);
- pandemicBoard.playerPieces[1].setLocation(f2_location);
- pandemicBoard.playerPieces[2].setLocation(f3_location);
- pandemicBoard.playerPieces[3].setLocation(f4_location);
- Variables.CITY_WITH_RESEARCH_STATION = f_ResearchStation;
- }
- //an attempt to drive to nearest research station
- public void tryDriveResearchStation()
- {
- System.out.println("Searching cities with research stations as destinations.");
- getDistances(pandemicBoard.getResearchLocations());
- // System.out.println("Calculating destination");
- City toDriveTo = calculateDestination();
- System.out.println("I'll try to drive to " + toDriveTo.getName());
- driveCity(playerPiece.getLocation(),toDriveTo);
- }
- /**
- * Check to see if the count of cards in any color equal the number required
- * for a cure
- **/
- public String tryCureCardColour()
- {
- int n = pandemicBoard.getNeededForCure();
- if (playerRole == "SCIENTIST") {
- n --;
- }
- for (int i = 0; i < pandemicBoard.getNumberColours(); i ++)
- {
- if (getCountXCards(possibleColour[i]) >=n && !pandemicBoard.getDisease(possibleColour[i]).cured)
- {
- return possibleColour[i];
- }
- }
- return null;
- }
- //get distance
- public int getDistanceResearch()
- {
- getDistances(pandemicBoard.getResearchLocations());
- return playerPiece.getLocation().getDistance();
- }
- //get distances of the cities which a look to travel
- public void getDistances(ArrayList<City> destinations)
- {
- pandemicBoard.resetDistances();
- setDestination(destinations);
- int distance = 1;
- while (playerPiece.getLocation().getDistance() == 9999999)
- {
- // System.out.println("Looking for places distance of " + distance);
- for (int i = 0 ; i < pandemicBoard.cities.size() ; i ++)
- {
- for (int x = 0 ; x < pandemicBoard.cities.get(i).getNeighbors().size() ; x ++)
- {
- if (pandemicBoard.cities.get(i).getDistance() == (distance-1)
- && pandemicBoard.cities.get(i).getNeighbors().get(x).getDistance() > distance)
- {
- pandemicBoard.cities.get(i).getNeighbors().get(x).setDistance(distance);
- }
- }
- }
- distance ++;
- }
- }
- public void setDestination(ArrayList<City> destinations)
- {
- for (int i = 0 ; i < destinations.size() ; i ++)
- {
- destinations.get(i).setDistance(0);
- }
- }
- public City calculateDestination()
- {
- int closestDestination = 9999999;
- City toReturn = new City(0,0,0,0,0);
- //System.out.println("i have: " +playerPiece.getLocationConnections().size() + " neighbors");
- for (int i = 0 ; i < playerPiece.getLocationConnections().size(); i++)
- {
- if (playerPiece.getLocationConnections().get(i).getDistance() < closestDestination)
- {
- //System.out.println("Will probably go to " + playerPiece.getLocationConnections()[i].getName());
- toReturn = playerPiece.getLocationConnections().get(i);
- closestDestination = playerPiece.getLocationConnections().get(i).getDistance();
- }
- }
- return toReturn;
- }
- /*
- 1)Check if we should a build a research station before we move (OPERATIONS_EXPERT may favor just building a researh station)
- 2)Check if we should use some sort of flight (check all cards in hand for both types of flight)
- 3)Count how many actions you need to take to more there
- 4)
- */
- //Check if player has enough cards to cure
- public boolean enoughCardsForCure(){
- int n = 4;
- if (playerRole == "SCIENTIST")
- n--;
- int yellowcards = 0, blackcards = 0, redcards = 0, bluecards = 0;
- for(City i : hand)
- if (i.getColour() == "Red")
- redcards++;
- else if (i.getColour() == "Yellow")
- yellowcards++;
- else if (i.getColour() == "Blue")
- bluecards++;
- else
- blackcards++;
- return ((n <= redcards && !pandemicBoard.getDisease("Red").cured) ||(n <= blackcards && !pandemicBoard.getDisease("Black").cured)
- || (n <= bluecards && !pandemicBoard.getDisease("Blue").cured) ||(n <= yellowcards && !pandemicBoard.getDisease("Yellow").cured));
- }
- //Author: @Stavros
- //Write best possible moves in a List of Actions as the result
- @SuppressWarnings("unchecked")
- private void heuristic(int depth, ArrayList<City> cityBoard, ArrayList<City> currentHand, float score,ArrayList<Action> actionSequence,List<City> researchStations){
- LinkedList<Action> possibleActions;// = new LinkedList<Action>();
- int temp_weight = 0;
- int index = 0;
- int max_cube = 0;
- int cubesToAdd = 0;
- String max_cube_color = null;
- float tmp_score_before = 0;
- float tmp_score_after = 0;
- boolean worthToExpand = true;
- ArrayList<Action> temp_actionSequence;// = new ArrayList<Action>() ;
- //Freezing current Situation
- City playerCity = playerPiece.getLocation();
- ArrayList<City> freezeCities = (ArrayList<City>) pandemicBoard.getCities().clone();
- //ArrayList<City> freezeCities = (ArrayList<City>) cityBoard.clone();
- ArrayList<City> tmpHand = new ArrayList<City>();
- for(City c :pandemicBoard.cities){
- if(c.getName() == playerPiece.getLocation().getName()) {
- playerPiece.setLocation(c);
- break;
- }
- }
- max_cube = playerPiece.getLocation().getMaxCube();
- //exit condition
- if(depth < MAX_DEPTH){
- //Explore all possible actions (possibleActions)
- possibleActions = getPossibleActions(currentHand,playerPiece.getLocation());
- //System.out.println("----------Depth: "+ depth+ " Actions: "+ possibleActions + " Location: "+ playerPiece.getLocation().getName()+ " Hand: "+ currentHand +" Score: " + score);
- for(Action act : possibleActions){
- temp_actionSequence = (ArrayList<Action>) actionSequence.clone();
- tmpHand = (ArrayList<City>) currentHand.clone();
- temp_weight = 0;
- playerPiece.setLocation(playerCity);
- Variables.CITY_WITH_RESEARCH_STATION = new ArrayList<City>(researchStations);
- pandemicBoard.cities = (ArrayList<City>) cityBoard.clone();
- hand = (ArrayList<City>) currentHand.clone();
- worthToExpand = true;
- for(City c :pandemicBoard.cities){
- if(c.getMaxCube() >0) {
- // System.out.println("City "+ c.getName() + " " + c.getMaxCube());
- }
- if(c.getName() == playerPiece.getLocation().getName()) {
- //System.out.println("City "+ c.getName() + " " + c.getMaxCube());
- // System.out.println("MATCH");
- playerPiece.setLocation(c);
- break;
- }
- }
- //System.out.println("Depth: "+ depth+ " " + act + " Location: "+ playerPiece.getLocation().getName()+ " Hand: "+ currentHand);
- // Do the action !!!!!!!!!
- if(act instanceof driveCity){
- if(!driveCity(((Pandemic.actions.driveCity) act).getMoveFrom(),((Pandemic.actions.driveCity) act).getMoveTo())){
- System.out.println("driveCity error");
- System.exit(1);
- }
- }else if(act instanceof charterFlight){
- temp_weight += flightScoreCalculator(playerPiece.getLocation());
- if (temp_weight == -1000){
- worthToExpand = false;
- }
- if(! charterFlight(playerPiece.getLocation(), ((charterFlight) act).getMoveTo())) {
- System.out.println("Charter flight error Heuristic");
- System.exit(0);
- }
- tmpHand.remove( ((charterFlight) act).getMoveFrom());
- }else if(act instanceof directFlight){
- temp_weight += flightScoreCalculator(((directFlight) act).getMoveTo());
- if (temp_weight == -1000){
- worthToExpand = false;
- }
- //System.out.println(tmpHand);
- if(!directFlight(playerPiece.getLocation(), ((Pandemic.actions.directFlight) act).getMoveTo())){
- System.out.println("Direct flight error Heuristic");
- System.exit(1);
- }
- tmpHand.remove(((directFlight) act).getMoveTo());
- }else if(act instanceof buildResearchStation){
- temp_weight += score_ResearchStation(((buildResearchStation) act).getCityToResearchStation());
- if (temp_weight == -1000){
- worthToExpand = false;
- }
- //System.out.println("+++++++++++++++++++++ " + tmpHand);
- //System.out.println("research stations before : "+ Variables.CITY_WITH_RESEARCH_STATION);
- if(!buildResearchStation()){
- System.out.println("buildResearchStation error Heuristic");
- System.exit(1);
- }
- //System.out.println("research stations after : "+ Variables.CITY_WITH_RESEARCH_STATION);
- if( playerRole != "OPERATIONS_EXPERT") {
- index = 0;
- for(City c : tmpHand) {
- if(c.getName() == playerPiece.getLocation().getName()) {
- break;
- }
- index ++;
- }
- if(index == tmpHand.size()) {
- System.out.println("Error action build Heuristic");
- System.exit(1);
- }
- tmpHand.remove(index);
- }
- //System.out.println("+++--------++++++++++++++++++ " + tmpHand);
- }else if(act instanceof discoverCure){
- temp_weight += 0;
- if(!discoverCure(playerPiece.getLocation(), ((Pandemic.actions.discoverCure) act).getColorOfDisease())){
- System.out.println("discoverCure error Heuristic");
- System.exit(1);
- }
- }else if(act instanceof shuttleFlight){
- if(!shuttleFlight(((Pandemic.actions.shuttleFlight) act).getMoveFrom(), ((Pandemic.actions.shuttleFlight) act).getMoveTo())){
- System.out.println("shuttleFlight error Heuristic");
- System.exit(1);
- }
- }else if(act instanceof treatDisease){
- //int tmpCubesBefore = playerPiece.getLocation().getCubeColour(((Pandemic.actions.treatDisease) act).getColour());
- //System.out.println("Depth: "+ depth+ " " + act + " Location: "+ playerPiece.getLocation().getName()+ " Hand: "+ currentHand);
- //TODO MAY NEED TO CHECK FOR OTHER COLOR CUBES
- max_cube = playerPiece.getLocation().getMaxCube();
- max_cube_color = ((treatDisease) act).getColour();
- //System.out.println("INIT TREAT cubes: "+ playerPiece.getLocation().getMaxCube());
- //System.out.println(((treatDisease) act).getColour() + " CUBES: " + playerPiece.getLocation().getMaxCube());
- //tmp_score_before = scoreDiseases(((treatDisease) act).getLocation());
- if(!treatDisease(playerPiece.getLocation(), ((Pandemic.actions.treatDisease) act).getColour())){
- System.out.println("treatDisease error Heuristic");
- System.exit(1);
- }
- cubesToAdd = max_cube - playerPiece.getLocation().getMaxCube();
- //System.out.println("INIT TREAT cubes after: "+ playerPiece.getLocation().getMaxCube());
- //int tmpCubesAfter = playerPiece.getLocation().getCubeColour(((Pandemic.actions.treatDisease) act).getColour());
- //tmp_score_after = scoreDiseases(((treatDisease) act).getLocation());
- //temp_weight += (tmpCubesBefore-tmpCubesAfter) * (tmp__score - score_diseases());
- //System.out.println("INIT TREAT cubes: "+ playerPiece.getLocation().getMaxCube());
- //System.out.println("-----------"+ (tmp_score_before - tmp_score_after)+ " "+ ((Pandemic.actions.treatDisease) act).getLocation().getName());
- //temp_weight += tmp_score_before - tmp_score_after;
- temp_weight += 100;
- }else{
- //not supposed to reach this
- System.out.println("Howw?????");
- System.exit(1);
- }
- //Then explore the next level
- if(!temp_actionSequence.add(act)){
- System.out.println("Howw 2?????");
- System.exit(1);
- }
- if(worthToExpand)
- heuristic(depth + 1,freezeCities,tmpHand,score+temp_weight,temp_actionSequence,Variables.CITY_WITH_RESEARCH_STATION);
- }
- }else{
- //System.out.println("MAX DETH_________________score: " + score + " "+currentHand);
- //Evaluate board State
- int yellowcards = 0, blackcards = 0, redcards = 0, bluecards = 0;
- boolean flag = false;
- for(City i : hand)
- if (i.getColour() == "Red")
- redcards++;
- else if (i.getColour() == "Yellow")
- yellowcards++;
- else if (i.getColour() == "Blue")
- bluecards++;
- else
- blackcards++;
- int n = 4;
- int count = 0;
- if (playerRole == "SCIENTIST")
- n--;
- if(redcards >= n-1 && ! pandemicBoard.getDisease("Red").cured){
- count ++;
- flag = true;
- }else if(yellowcards >= n-1 && ! pandemicBoard.getDisease("Yellow").cured){
- count ++;
- flag = true;
- }else if(bluecards >= n-1 && ! pandemicBoard.getDisease("Blue").cured){
- count ++;
- flag = true;
- }else if(blackcards >= n-1 && ! pandemicBoard.getDisease("Black").cured ){
- count ++;
- flag = true;
- }
- if(flag && getDistanceResearch() <= 4-count){
- if(numOfRounds >= 4){
- score += 3000;
- //System.out.println("+++\n\n\n\n\n\n\n\n\n\n\n");
- }else{
- score += 30;
- }
- }
- if(playerRole == "QUARANTINE_SPECIALIST") {
- score += scoreQuarantineSpecialist(playerPiece.getLocation()); //function to score his standing position specifically
- }
- if(score > max_score){
- max_score_sequence = (ArrayList<Action>) actionSequence.clone();
- max_score = score;
- }
- return;
- }
- //Reverting the Board to the initial State
- pandemicBoard.cities = (ArrayList<City>) freezeCities.clone();
- playerPiece.setLocation(playerCity);
- for(City c :pandemicBoard.cities){
- if(c.getName() == playerCity.getName()) {
- for(int i=0; i<cubesToAdd && max_cube_color != null ;i++) {
- c.addCube(max_cube_color);
- }
- playerPiece.setLocation(c);
- break;
- }
- }
- Variables.CITY_WITH_RESEARCH_STATION = new ArrayList<City>(researchStations);
- //System.out.println("@kallinteris DEBUG1: hand" + tmpHand);
- hand = (ArrayList<City>) tmpHand.clone();
- }
- /** Authors: @kallinteris-Andreas && @Misokalos
- Returns: a list of all possible moves that can be executed by the player
- */
- private LinkedList<Action> getPossibleActions(ArrayList<City> currentHand, City currentLocation){
- LinkedList<Action> possibleAction = new LinkedList<Action>();
- //charterFlight @Kallinteris-Andreas
- if(hand.contains(currentLocation))
- for(City city : Variables.CITY_LIST) {
- if(! city.getName().equals(currentLocation.getName()))
- possibleAction.add(new charterFlight(currentLocation, hand,city));
- }
- //directFlight @Kallinteris-Andreas
- for (City destination : currentHand) {
- if(! destination.getName().equals(currentLocation.getName()))
- possibleAction.add(new directFlight(destination, currentHand));
- }
- //driveCity @Kallinteris-Andreas
- for(City neighbor : currentLocation.getNeighbors())
- possibleAction.add(new driveCity(currentLocation, neighbor));
- //shuttleFlight @kallinteris-Andreas
- if (Variables.CITY_WITH_RESEARCH_STATION.contains(currentLocation))
- for (City cityR : Variables.CITY_WITH_RESEARCH_STATION)
- if (cityR.getName() != currentLocation.getName())
- possibleAction.add(new shuttleFlight(currentLocation, cityR));
- //System.out.println("Location: " + currentLocation.getName() + " Cubes: " + currentLocation.getMaxCube() );
- if(currentLocation.getCubeColour("Red") != 0){
- if(checktreatDisease(currentLocation, "Red")){
- possibleAction.add(new treatDisease(currentLocation, "Red"));
- }
- }
- else if(currentLocation.getCubeColour("Blue") != 0){
- if(checktreatDisease(currentLocation, "Blue")){
- possibleAction.add(new treatDisease(currentLocation, "Blue"));
- }
- }
- else if(currentLocation.getCubeColour("Black") != 0){
- if(checktreatDisease(currentLocation, "Black")){
- possibleAction.add(new treatDisease(currentLocation, "Black"));
- }
- }
- else if(currentLocation.getCubeColour("Yellow") != 0){
- if(checktreatDisease(currentLocation, "Yellow")){
- possibleAction.add(new treatDisease(currentLocation, "Yellow"));
- }
- }
- //build station
- //TODO TIDY up
- if(checkbuildResearchStation(currentHand, currentLocation)){
- buildResearchStation a = new buildResearchStation(currentLocation,currentHand);
- possibleAction.add(a);
- //System.out.println("@Kallinteris DEBUG0: checkbuildResearchStation hand: " + currentHand + " Action : "+ a);
- }
- //discover cure
- // if(checkdiscoverCure(currentLocation, "Blue"))
- // possibleAction.add(new discoverCure(currentLocation,getHand(), "Blue"));
- // else if(checkdiscoverCure(currentLocation, "Black")){
- // possibleAction.add(new discoverCure(currentLocation,getHand(), "Black"));
- // }
- // else if(checkdiscoverCure(currentLocation, "Red")){
- // possibleAction.add(new discoverCure(currentLocation,getHand(), "Red"));
- // }
- // else if(checkdiscoverCure(currentLocation, "Yellow")){
- // possibleAction.add(new discoverCure(currentLocation,getHand(), "Yellow"));
- // }
- return possibleAction;
- }
- //checking for legal Actions/////////
- public boolean checkbuildResearchStation (ArrayList<City> currentHand, City currentLocation){
- buildResearchStation tmp = new buildResearchStation(currentLocation, currentHand );
- if (playerRole.equals("OPERATIONS_EXPERT") && !Variables.CITY_WITH_RESEARCH_STATION.contains(currentLocation)) {
- return true;
- }
- else{
- if (tmp.isaLegalMove()){
- return true;
- }
- }
- return false;
- }
- public boolean checktreatDisease (City location, String colour){
- treatDisease tmp = new treatDisease(location,colour);
- if (playerRole.equals("MEDIC")) {
- if(tmp.isaLegalMove()==true && location.getName() == playerPiece.getLocation().getName()){
- for (int i=0;i<location.getCubeColour(colour);i++) {
- }
- return true;
- }
- }
- else {
- if(tmp.isaLegalMove()==true && location.getName() == playerPiece.getLocation().getName()){
- return true;
- }
- }
- //System.out.println("--------------+============");
- return false;
- }
- public boolean checkdiscoverCure(City location, String colour){
- discoverCure tmp = new discoverCure(location,getHand(),colour);
- if (playerRole.equals("SCIENTIST")) {
- if (tmp.isaLegalMove(1)){
- return true;
- }
- }
- else{
- if (tmp.isaLegalMove(0)){
- return true;
- }
- }
- return false;
- }
- private float score_ResearchStation(City discCity){
- //reasearch Stations near x 40
- //one research station x 10
- //mutual coeff if players dont build increase
- // too many stations x 40
- //builder x 50
- float scoreOpExp = 0,scoreNoReasearchStations = 0;
- float scoreReasearchTooFar = 0, scoreTooMany = 0;
- int distance = getDistanceResearch();
- int n = 4;
- int helpStation = 0;
- // try build us a station if it is a good idea*
- if(friendPlaying){
- City currLocation = playerPiece.getLocation();
- playerPiece.setLocation(tmpLocation);
- ArrayList<City> nn = new ArrayList<City>();
- nn.add(currLocation);
- getDistances(nn);
- int distanceToOtherPlayer = playerPiece.getLocation().getDistance();
- int closestStation = getDistanceResearch();
- int yellowcards = 0, blackcards = 0, redcards = 0, bluecards = 0;
- boolean flag = false;
- for(City i : tmpFriendHand)
- if (i.getColour() == "Red")
- redcards++;
- else if (i.getColour() == "Yellow")
- yellowcards++;
- else if (i.getColour() == "Blue")
- bluecards++;
- else
- blackcards++;
- n = 4;
- if (tmpRole == "SCIENTIST")
- n--;
- if(discCity.getColour() == "Red" && (redcards > n ) && ! pandemicBoard.getDisease("Red").cured){
- flag = true;
- }else if(discCity.getColour() == "Yellow" && (yellowcards > n ) && ! pandemicBoard.getDisease("Yellow").cured){
- flag = true;
- }else if(discCity.getColour() == "Blue" && (bluecards > n) && ! pandemicBoard.getDisease("Blue").cured){
- flag = true;
- }else if(discCity.getColour() == "Black" && (blackcards > n ) && ! pandemicBoard.getDisease("Black").cured ){
- flag = true;
- }
- if(distanceToOtherPlayer < 4 && closestStation > 3 && flag){
- for (int i = 0; i < 1000 ; i++) {
- System.out.println("-------------------------------------------");
- }
- helpStation = 1000;
- }
- //if current is within 3 cities distance, consider building
- playerPiece.setLocation(currLocation);
- }
- // Never build when you have reasearch station too near
- if( distance <= 2)
- return -1000;
- n = 4;
- if (playerRole == "SCIENTIST")
- n--;
- int yellowcards = 0, blackcards = 0, redcards = 0, bluecards = 0;
- for(City i : hand)
- if (i.getColour() == "Red")
- redcards++;
- else if (i.getColour() == "Yellow")
- yellowcards++;
- else if (i.getColour() == "Blue")
- bluecards++;
- else
- blackcards++;
- String maxColor;
- int max_color_number = Math.max(Math.max(redcards, yellowcards), Math.max( bluecards, blackcards));
- if( max_color_number == redcards){
- maxColor = "Red";
- }else if(max_color_number == yellowcards){
- maxColor = "Yellow";
- }else if(max_color_number == bluecards){
- maxColor = "Blue";
- }else{
- maxColor = "Black";
- }
- if(discCity.getColour() == maxColor && max_color_number <= n){
- return -1000;
- }
- if(discCity.getNeighbors().size() <= 1) {
- return - 1000;
- }
- if( playerRole == "OPERATIONS_EXPERT")
- scoreOpExp = 4;
- //builder x 50
- else{
- scoreOpExp = 1;
- }
- if (pandemicBoard.getResearchCentre().size() <= 3 && distance > 3 && numOfRounds !=1)
- scoreNoReasearchStations = 1;
- //one research station x 10
- if( distance > 5)
- scoreReasearchTooFar = distance/4;
- //no reasearch near x 10 x
- if (pandemicBoard.getResearchCentre().size() > 3)
- scoreTooMany = (3-pandemicBoard.getResearchCentre().size());
- // too many stations x 40
- return scoreOpExp *(scoreNoReasearchStations*50 + scoreReasearchTooFar*40 + scoreTooMany*20 + helpStation ) ;
- // -40 --- 180
- //TODO callibrate this weight
- // TODO call opponent modelling
- }
- float MAX_DISEASE_SCORE = 65536;
- /**
- Author: @Kallinteris-Andreas
- checks the board evaluate dangers caused by existing diseases (cubes) on the board
- returns: a higher number for a more dangerours board
- (a really high number is returned in cases on game terminating dangers)
- TODO count turns?
- */
- private float scoreDiseases(City c){
- float score = 0;
- //Score based on the danger of outbreaks
- {
- //for (City c : pandemicBoard.get1CubeCities()){
- if (c.getCubeColour(c.getColour()) == 1){
- if (pandemicBoard.infectDeck.getInfectDiscardPile().contains(c)){
- if(epidemics_left() >= 3){
- float expectedTurnsTillNextOutbreak = (float) expectedSizeOfNextDiscardPile2()/ Variables.INFECTION_RATE + expectedTurnsTillEpidemicDraw()*2;
- score += scoreOutbreak(c) / expectedTurnsTillNextOutbreak;
- }
- if (epidemics_left() <= 2){ /*DO NO COUNT SINCE IT CAN NOT GET AN OUTBREAK*/}
- }
- else{
- float expectedTurnsTillNextOutbreak = (float) remaningReshufledInfectCards().size()/Variables.INFECTION_RATE + expectedSizeOfNextDiscardPile()/Variables.INFECTION_RATE + expectedSizeOfNextDiscardPile2()/Variables.INFECTION_RATE;
- score += scoreOutbreak(c) / expectedTurnsTillNextOutbreak;
- }
- }
- else if (c.getCubeColour(c.getColour()) == 2){
- //for (City c : pandemicBoard.get2CubeCities()){
- if (pandemicBoard.infectDeck.getInfectDiscardPile().contains(c)){
- if(epidemics_left() >= 3){
- float expectedTurnsTillNext2Outbreaks = (float) expectedSizeOfNextDiscardPile3()/Variables.INFECTION_RATE + expectedTurnsTillEpidemicDraw()*3;
- score += scoreOutbreak(c) / expectedTurnsTillNext2Outbreaks;
- }
- if(epidemics_left() >= 2){
- float expectedTurnsTillNextOutbreak = (float) expectedSizeOfNextDiscardPile2()/Variables.INFECTION_RATE + expectedTurnsTillEpidemicDraw()*2;
- score += scoreOutbreak(c) / expectedTurnsTillNextOutbreak;
- }
- if (epidemics_left() <= 1){ /*DO NO COUNT SINCE IT CAN NOT GET AN OUTBREAK*/}
- }
- else{
- float expectedTurnsTillNextOutbreak = (float) remaningReshufledInfectCards().size()/Variables.INFECTION_RATE + expectedSizeOfNextDiscardPile()/Variables.INFECTION_RATE;
- score += scoreOutbreak(c) / expectedTurnsTillNextOutbreak;
- }
- }
- else if (c.getCubeColour(c.getColour()) == 3){
- //for (City c : pandemicBoard.get3CubeCities()){
- if (pandemicBoard.infectDeck.getInfectDiscardPile().contains(c)){
- if(epidemics_left() > 2){
- float expectedTurnsTillNext3Outbreaks = (float) expectedSizeOfNextDiscardPile3()/Variables.INFECTION_RATE + expectedTurnsTillEpidemicDraw()*3;
- score += scoreOutbreak(c) / expectedTurnsTillNext3Outbreaks;
- }
- if(epidemics_left() > 1){
- float expectedTurnsTillNext2Outbreaks = (float) expectedSizeOfNextDiscardPile2()/Variables.INFECTION_RATE + expectedTurnsTillEpidemicDraw()*2;
- score += scoreOutbreak(c) / expectedTurnsTillNext2Outbreaks;
- }
- if(epidemics_left() > 0){
- float expectedTurnsTillNextOutbreak = (float) expectedSizeOfNextDiscardPile()/Variables.INFECTION_RATE + expectedTurnsTillEpidemicDraw();
- score += scoreOutbreak(c) / expectedTurnsTillNextOutbreak;
- }
- if (epidemics_left() == 0){ /*DO NO COUNT SINCE IT CAN NOT BE RE-INFECTED*/}
- }
- else{
- float expectedTurnsTillNextOutbreak = (float) remaningReshufledInfectCards().size()/Variables.INFECTION_RATE;
- score += scoreOutbreak(c) / expectedTurnsTillNextOutbreak;
- }
- }
- }
- //Score based on avoiding having 24 cubes in the map
- {
- float PANDEMIC_CUBE_WEIGHT = MAX_DISEASE_SCORE; //2^24
- if (epidemics_left() == 0){
- int redLeft = infectCardsOfColorXRemaining("Red") + pandemicBoard.redCubes;
- int blueLeft = infectCardsOfColorXRemaining("Blue") + pandemicBoard.blueCubes;
- int blackLeft = infectCardsOfColorXRemaining("Black") + pandemicBoard.blackCubes;
- int yellowLeft = infectCardsOfColorXRemaining("Yellow") + pandemicBoard.yellowCubes;
- if (redLeft >= 25)
- score += PANDEMIC_CUBE_WEIGHT*(redLeft-24)/pandemicBoard.infectDeck.getInfectPile().size()*4;
- if (blueLeft >= 25)
- score += PANDEMIC_CUBE_WEIGHT*(blueLeft-24)/pandemicBoard.infectDeck.getInfectPile().size()*4;
- if (blackLeft >= 25)
- score += PANDEMIC_CUBE_WEIGHT*(blackLeft-24)/pandemicBoard.infectDeck.getInfectPile().size()*4;
- if (yellowLeft >= 25)
- score += PANDEMIC_CUBE_WEIGHT*(yellowLeft-24)/pandemicBoard.infectDeck.getInfectPile().size()*4;
- }
- else if (epidemics_left() == 1){
- //very unlikely to happen has been commented out for compute resason
- /*
- int redLeft = infectCardsOfColorXRemaining("Red")/pandemicBoard.infectDeck.getInfectPile().size()/3;
- int blueLeft = infectCardsOfColorXRemaining("Blue") + pandemicBoard.blueCubes;
- int blackLeft = infectCardsOfColorXRemaining("Black") + pandemicBoard.blackCubes;
- int yellowLeft = infectCardsOfColorXRemaining("Yellow") + pandemicBoard.yellowCubes;
- if (pandemicBoard.redCubes > 18)
- score += Math.pow(CUBE_COUNT_WEIGHT, (pandemicBoard.redCubes - 18));
- if (pandemicBoard.blueCubes > 18)
- score += Math.pow(CUBE_COUNT_WEIGHT, (pandemicBoard.blueCubes - 18));
- if (pandemicBoard.yellowCubes > 18)
- score += Math.pow(CUBE_COUNT_WEIGHT, (pandemicBoard.yellowCubes - 18));
- if (pandemicBoard.blackCubes > 18);
- score += Math.pow(CUBE_COUNT_WEIGHT, (pandemicBoard.blackCubes - 18));
- */
- }
- else if (epidemics_left() >= 2){/*actually not possible*/}
- }
- return score;
- }
- /**
- Author: @Kallinteris-Andreas
- Evaluates the danger of a specific outbreak
- Returns: a number indicating that danger level
- */
- private float scoreOutbreak(City c){
- float score = 0;
- final float OUTBREAK_WEIGHT = MAX_DISEASE_SCORE/256;
- int outbreaksLeft = Variables.MAX_NUMBER_OF_OUTBREAK - Variables.OUTBREAK_MARKER;
- //Score based on OUTBREAK_MARKER
- //TODO change scaling (take into account number or turns)
- {
- switch(outbreaksLeft){
- case 1: score += MAX_DISEASE_SCORE; break;
- case 2: score += OUTBREAK_WEIGHT*7; break;
- case 3: score += OUTBREAK_WEIGHT*6; break;
- case 4: score += OUTBREAK_WEIGHT*5; break;
- case 5: score += OUTBREAK_WEIGHT*4; break;
- case 6: score += OUTBREAK_WEIGHT*3; break;
- case 7: score += OUTBREAK_WEIGHT*2; break;
- case 8: score += OUTBREAK_WEIGHT; break;
- }
- }
- //Score based on neighbors
- //Note: we ingnore the case of 4 outbreaks happening in the same city
- {
- for(City neighbor : c.getNeighbors())
- if (neighbor.getColour().equals(c.getColour()))
- switch(neighbor.getMaxCube()){
- case 0: score += 0; break;
- case 1: score += 0; break;
- case 2: score += OUTBREAK_WEIGHT; break;
- case 3: score = MAX_DISEASE_SCORE; break;
- }
- }
- //NOTE: it does take into account cities with 3 cure of a di
- //Note: can be improved at the cost of computation cost
- {
- if (c.getCubeColour(c.getColour()) == 3)
- if (c.getColour().equals("Red"))
- if (c.getNeighbors().size() + pandemicBoard.redCubes > 24)
- score += MAX_DISEASE_SCORE;
- else if (c.getColour().equals("Blue"))
- if (c.getNeighbors().size() + pandemicBoard.blueCubes > 24)
- score += MAX_DISEASE_SCORE;
- else if (c.getColour().equals("Black"))
- if (c.getNeighbors().size() + pandemicBoard.blackCubes > 24)
- score += MAX_DISEASE_SCORE;
- else if (c.getColour().equals("Yellow"))
- if (c.getNeighbors().size() + pandemicBoard.yellowCubes > 24)
- score += MAX_DISEASE_SCORE;
- }
- return score;
- }
- private float flightScoreCalculator (City discCity){
- int yellowcards = 0, blackcards = 0, redcards = 0, bluecards = 0;
- int n = 4;
- final float DISCARD_WEIGHT = 5;
- if(friendPlaying){
- if (tmpRole == "SCIENTIST")
- n--;
- for(City i : tmpFriendHand)
- if (i.getColour() == "Red")
- redcards++;
- else if (i.getColour() == "Yellow")
- yellowcards++;
- else if (i.getColour() == "Blue")
- bluecards++;
- else
- blackcards++;
- if(discCity.getColour() == "Red" && (redcards > n ) ){
- return DISCARD_WEIGHT;
- }else if(discCity.getColour() == "Yellow" && (yellowcards > n )){
- return DISCARD_WEIGHT;
- }else if(discCity.getColour() == "Blue" && (bluecards > n)){
- return DISCARD_WEIGHT;
- }else if(discCity.getColour() == "Black" && (blackcards > n ) ){
- return DISCARD_WEIGHT;
- }
- }
- for(int i = 0; i<4 ;i++){
- if(pandemicBoard.diseases.get(i).cured && (discCity.getColour() == pandemicBoard.diseases.get(i).getColour())){
- return DISCARD_WEIGHT;
- }
- }
- n = 4;
- if (playerRole == "SCIENTIST")
- n--;
- yellowcards = 0;
- blackcards = 0;
- redcards = 0;
- bluecards = 0;
- for(City i : hand)
- if (i.getColour() == "Red")
- redcards++;
- else if (i.getColour() == "Yellow")
- yellowcards++;
- else if (i.getColour() == "Blue")
- bluecards++;
- else
- blackcards++;
- String maxColor;
- int max_color_number = Math.max(Math.max(redcards, yellowcards), Math.max( bluecards, blackcards));
- if( max_color_number == redcards){
- maxColor = "Red";
- }else if(max_color_number == yellowcards){
- maxColor = "Yellow";
- }else if(max_color_number == bluecards){
- maxColor = "Blue";
- }else{
- maxColor = "Black";
- }
- if(discCity.getColour() == maxColor && max_color_number <= n){
- return -1000;
- }
- if(discCity.getColour() == "Red" && (redcards > n ) ){
- return DISCARD_WEIGHT;
- }else if(discCity.getColour() == "Yellow" && (yellowcards > n )){
- return DISCARD_WEIGHT;
- }else if(discCity.getColour() == "Blue" && (bluecards > n)){
- return DISCARD_WEIGHT;
- }else if(discCity.getColour() == "Black" && (blackcards > n ) ){
- return DISCARD_WEIGHT;
- }
- // if(discCity.getColour() == "Red" && (redcards > n || redcards <n-2) && numOfRounds > 2){
- // return -50;
- // }else if(discCity.getColour() == "Yellow" && (yellowcards > n || yellowcards <n-2) && numOfRounds > 2){
- // return -50;
- // }else if(discCity.getColour() == "Blue" && (bluecards > n || bluecards <n-2 ) && numOfRounds > 2){
- // return -50;
- // }else if(discCity.getColour() == "Black" && (blackcards > n || blackcards <n-2 ) && numOfRounds > 2){
- // return -50;
- // }
- return -1000;
- }
- /**
- TODO MAKE SOMETHING THAT MAKES SENSE
- */
- private float scoreQuarantineSpecialist (City curCity){
- int curInfectionLevel = 0;
- int infectionWeight = 3;
- float score = 0;
- for (City c : curCity.getNeighbors()){
- curInfectionLevel = 0;
- for (String i : possibleColour) {
- curInfectionLevel += c.getCubeColour(i);
- }
- if(curInfectionLevel == 1){
- score +=1 * infectionWeight;
- }else if(curInfectionLevel == 2){
- score +=3 * infectionWeight;
- }else if(curInfectionLevel == 3){
- score +=10 * infectionWeight;
- }else if(curInfectionLevel == 0){
- score +=0;
- }else{
- System.out.println("Error in cubes");
- System.exit(1);
- }
- }
- return score;
- }
- /** Author: @kallinteris-Andreas
- this function simulates the results of the suggested actions and
- returns: the a score indicating how good those actions were
- */
- @SuppressWarnings("unused")
- private float scoreSuggestion(ArrayList<Action> suggestion){
- //System.out.println("Location "+ playerPiece.getLocation().getName());
- City playerCity = playerPiece.getLocation();
- float temp_weight = 0,tmp_score_after,tmp_score_before;
- LinkedList<City> treats = new LinkedList<City>();
- LinkedList<Integer> numberOfTreats = new LinkedList<Integer>();
- LinkedList<String> colourOfTreat = new LinkedList<String>();
- for (Action act : suggestion){
- //System.out.println(act);
- if(act instanceof driveCity){
- if(!driveCity(((Pandemic.actions.driveCity) act).getMoveFrom(),((Pandemic.actions.driveCity) act).getMoveTo())){
- System.out.println("driveCity error suggestion");
- System.exit(1);
- }
- }else if(act instanceof charterFlight){
- temp_weight += flightScoreCalculator(playerPiece.getLocation());
- if(! charterFlight(playerPiece.getLocation(), ((charterFlight) act).getMoveTo())) {
- System.out.println("Charter flight error suggestion");
- System.exit(0);
- }
- }else if(act instanceof directFlight){
- temp_weight += flightScoreCalculator(((directFlight) act).getMoveTo());
- if(!directFlight(playerPiece.getLocation(), ((Pandemic.actions.directFlight) act).getMoveTo())){
- System.out.println("Direct flight error suggestion");
- System.exit(1);
- }
- }else if(act instanceof buildResearchStation){
- temp_weight += score_ResearchStation(((buildResearchStation) act).getCityToResearchStation());
- if(!buildResearchStation()){
- System.out.println("buildResearchStation error suggestion");
- System.exit(1);
- }
- }else if(act instanceof discoverCure){
- temp_weight += 100000;
- if(!discoverCure(playerPiece.getLocation(), ((Pandemic.actions.discoverCure) act).getColorOfDisease())){
- System.out.println("discoverCure error suggestion");
- System.exit(1);
- }
- }else if(act instanceof shuttleFlight){
- if(!shuttleFlight(((Pandemic.actions.shuttleFlight) act).getMoveFrom(), ((Pandemic.actions.shuttleFlight) act).getMoveTo())){
- System.out.println("shuttleFlight error suggestion");
- System.exit(1);
- }
- }else if(act instanceof treatDisease){
- treats.add(((treatDisease) act).getLocation());
- if(playerRole == "MEDIC") {
- numberOfTreats.add(((treatDisease) act).getLocation().getCubeColour(((treatDisease) act).getColour()));
- }else {
- numberOfTreats.add(1);
- }
- colourOfTreat.add(((treatDisease) act).getColour());
- //System.out.println("Location "+ playerPiece.getLocation().getName());
- tmp_score_before = scoreDiseases(((treatDisease) act).getLocation());
- if(!treatDisease(playerPiece.getLocation(), ((Pandemic.actions.treatDisease) act).getColour())){
- System.out.println("treatDisease error suggestion");
- System.exit(1);
- }
- tmp_score_after = scoreDiseases(((treatDisease) act).getLocation());
- temp_weight += tmp_score_before - tmp_score_after;
- }else{
- //not supposed to reach this
- System.out.println("Howw?????");
- System.exit(1);
- }
- }
- System.out.println("---------------------------");
- //Evaluate board State
- int yellowcards = 0, blackcards = 0, redcards = 0, bluecards = 0;
- boolean flag = false;
- for(City i : hand)
- if (i.getColour() == "Red")
- redcards++;
- else if (i.getColour() == "Yellow")
- yellowcards++;
- else if (i.getColour() == "Blue")
- bluecards++;
- else
- blackcards++;
- int n = 4;
- if (playerRole == "SCIENTIST")
- n--;
- if(redcards >= n-1 && ! pandemicBoard.getDisease("Red").cured){
- flag = true;
- }else if(yellowcards >= n-1 && ! pandemicBoard.getDisease("Yellow").cured){
- flag = true;
- }else if(bluecards >= n-1 && ! pandemicBoard.getDisease("Blue").cured){
- flag = true;
- }else if(blackcards >= n-1 && ! pandemicBoard.getDisease("Black").cured ){
- flag = true;
- }
- //score
- if(flag && getDistanceResearch() <=3){
- if(numOfRounds > 4){
- temp_weight += 300;
- //System.out.println("+++\n\n\n\n\n\n\n\n\n\n\n");
- }else{
- temp_weight += 30;
- }
- }
- if(playerRole == "QUARANTINE_SPECIALIST") {
- temp_weight += scoreQuarantineSpecialist(playerPiece.getLocation()); //function to score his standing position specifically
- }
- //Revert cubes
- for(int i =0; i<treats.size();i++) {
- for(int j=0;j<numberOfTreats.get(i);j++)
- treats.get(i).addCube(colourOfTreat.get(i));
- }
- return temp_weight;
- }
- //UTILITY FUNCTIONS
- //UTILITY FUNCTIONS
- //UTILITY FUNCTIONS
- /** Author: @kallinteris-Andreas
- returns: TRUE if there are epemic cards left in the deck
- */
- // private boolean epidemic_left(){
- // return epidemics_left() != 0;
- // }
- /** Author: @kallinteris-Andreas
- returns: the number of epemic cards that are left in the player deck
- */
- private int epidemics_left(){
- int t = 4;
- for (Object o : pandemicBoard.playerDeck.getPlayerDiscardPile())
- if(o.equals(true))
- t--;
- return t;
- }
- /**
- Note:assumes that there are epidemics lefts in the deck
- returns: the expected number of turns till it an epidemic card is drawn
- */
- private int expectedTurnsTillEpidemicDraw(){
- return pandemicBoard.playerDeck.getPlayerPile().size() / (2* epidemics_left()) ;
- }
- /** Author: @kallinteris-Andreas
- returns: the expected size of the next discard pile (after an epidemic card is drawn)
- */
- private int expectedSizeOfNextDiscardPile(){
- return expectedTurnsTillEpidemicDraw() * Variables.INFECTION_RATE + pandemicBoard.infectDeck.getInfectDiscardPile().size();
- }
- private int expectedSizeOfNextDiscardPile2(){
- return expectedSizeOfNextDiscardPile() + expectedTurnsTillEpidemicDraw()*Variables.INFECTION_RATE;
- }
- private int expectedSizeOfNextDiscardPile3(){
- return expectedSizeOfNextDiscardPile2() + expectedTurnsTillEpidemicDraw()*Variables.INFECTION_RATE;
- }
- /** Author: @kallinteris-Andreas
- returns: the expected size of the next discard pile (after an epidemic card is drawn)
- */
- private ArrayList<City> remaningReshufledInfectCards(){
- ArrayList<City> remainingCards = new ArrayList<>();
- for (City c : pandemicBoard.infectDeck.getInfectDiscardPileBeforeEpidemic())
- if (!pandemicBoard.infectDeck.getInfectDiscardPile().contains(c))
- remainingCards.add(c);
- return remainingCards;
- }
- /** Author: @kallinteris-Andreas
- returns: the number of cards in the infect deck of color X
- */
- private int infectCardsOfColorXRemaining(String X){
- int count = 12;
- for (City c : pandemicBoard.infectDeck.getInfectDiscardPile())
- if (c.getColour().equals(X))
- count--;
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement