import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Set; /** * PicthView (for Voronoi Diagram) * - Tracking positions of all the team * Using the x,y cordinates of ball and players you can work out * situations where a goal is likely or there is a chance * @author Akbar Sherwani * @date 30th November * @version 0.7 */ public class PitchView { /** * Constructor for PitchView (Veronoi Diagram) */ private HashMap positions; private HashMap temp; private ArrayList arrayteaml; private ArrayList arrayteamr; public Logger log; private int time; private CommentaryProducer cp; private String teaml; private String teamr; public PitchView() { // initialise hasmap and arraylists positions = new HashMap(); temp = new HashMap(); arrayteaml = new ArrayList(); arrayteamr = new ArrayList(); // Logger File file = new File("veroni.txt"); try { log = new Logger(file); } catch (NullPointerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * PlayerKick * Register kick or touch from player (dribble) * Then after check it then starts two veronio methods below * @param int player */ public void PlayerKick(int player, int time) { // get players current position Position posa = (Position) positions.get(player); double x; boolean left; if(player > 11) { // cordinates to -53 x = -53.0; left = true; arrayteamr.add(posa); } else { x = 53.0; left = false; arrayteaml.add(posa); } // Start main methods Checkothers(x,player,posa.getPosx(), left); // Checks who else is round CheckTeam(player, time, posa.getPosy()); // Now checks who is left spaceAndTime(player, time, posa); // Checks space and time // log.write("Dribble player " + player + " players to go " + temp.size(), time); // Clear temp for next run temp.clear(); } /** * CheckTeam * brings in the players pos and works out the team advantage * and also checks where the player is in terms of y axis * @param player int * @param time int * @param posy double */ private void CheckTeam(int player, int time1, double posy) { // Works out which team the player is on String team; if(player > 11) { team = teamr; } else { team = teaml; } // go through temp hashmap Set set= temp.keySet(); Iterator iter = set.iterator() ; Integer number; int teamAttack = 0; int teamDefend = 0; // Go through each position while ( iter.hasNext () ) { number = (Integer) iter.next(); if(sameteam(player,number.intValue())) { // Increment values teamAttack++; } else { // Increment values teamDefend++; } } // Check for counter attack, important check time check as well if(teamAttack > teamDefend && checktime(time1)) { log.write("Chance to Counter Attack " + teamAttack + " against " + teamDefend, time1); cp.counterAttack(team); timesave(time1); } // Open goal or cross, important check time check as well if(teamDefend == 0 && checktime(time1)) { if(posy > -17.50 && posy < 17.50) { log.write("Open goal/ Gone round keeper", time1); cp.openGoal(); timesave(time1); } else { log.write("Needs a cross", time1); cp.crossChance(team); timesave(time1); } } // Checking if there is one hence must be goakeeper, important check time check as well if(teamDefend == 1 && checktime(time1)) { if(posy > -17.50 && posy < 17.50) { log.write("Great Chance, beaten the last defender", time1); cp.pastLastDefender(player); timesave(time1); } else { log.write("Needs a good cross", time1); cp.crossChance(team); timesave(time1); } } // this.time = time1; } /** * Playerpos * Adds player positions in position hashmap * @param posx double * @param posy double */ public void Playerpos(int player_no, double posx, double posy) { Position pos = new Position(posx, posy); if(positions.get(player_no) == null) { // do nothing } else { // remove entry in hasmap positions.remove(player_no); } positions.put(player_no, pos); } /** * Checkothers * Who is in view after the players x position * @param x double * @param player in * @param player pos double * @param left boolean */ public void Checkothers(double x, int player, double player_pos, boolean left) { Set set= positions.keySet(); Iterator iter = set.iterator() ; Integer number; // Go through each position while ( iter.hasNext () ) { number = (Integer) iter.next(); Position a = positions.get(number.intValue()); if(number.intValue() != player) { if(left) { if(a.getPosx() > x && a.getPosx() < player_pos ) { // if the player is infront add temp.put(number, a); } } else { if(a.getPosx() < x && a.getPosx() > player_pos ) { // if the player is infront add temp.put(number, a); } } } } } /** * sameteam * Check on same team * @param a first player * @param b second player */ public boolean sameteam(int a, int b) { if(a <= 11) { if(b <= 11) { return true; } } else { if(b > 11 && b <= 22) { return true; } } return false; } /** * Saves time when a veronio is recorded * @param time */ public void timesave(int time) { this.time = time; } /** * check if next comment can be made * @param timev * @return boolean */ public boolean checktime(int timev) { if(timev > (time + 10) ) { return true; } else { return false; } } /** * Set CP * @param cp CommentaryProducer */ public void setCP(CommentaryProducer cp) { this.cp = cp; } /** * Set Teams * @param teaml * @param teamr */ public void setTeams(String teaml, String teamr) { this.teaml = teaml; this.teamr = teamr; } /** * CheckFinalPos * Goes through after halftime and give view on where they had the ball in last * 1000 game time */ public void checkFinalPos() { int x =0; while(arrayteaml.size() > x) { Position a = arrayteaml.get(x); double posx = a.getPosx(); double posy = a.getPosy(); x++; } } /** * Space and Time * When attacker has the ball check whether any of the opposite * team are around him and if they are further than 25 yards away he has * space and time * @param player int player number * @param time int time of game * @param pos Position of player */ public void spaceAndTime(int player, int time, Position pos) { // Work out what player number in terms of team int cplayer = 0;; if(player > 11) { cplayer = player - 11; } else { cplayer = player; } // If player is an attacker he or mid field he must be 7 or higher Set set= temp.keySet(); Iterator iter = set.iterator() ; Integer number; int close = 0; if(cplayer > 7) { // Go through all player around while ( iter.hasNext () ) { number = (Integer) iter.next(); if(sameteam(player,number.intValue())) { // ignore on same team } else { // Get position of other player Position other = temp.get(number.intValue()); // Get Distance double check = Distance(pos, other); if(check < 20) { // If player is close by then increment close close++; } } } } // Therefore final check if no one close pass cp and log if(close == 0 && checktime(time) && (cplayer > 7)) { log.write("Player " + player + " has space/time", time); // cp.hastime(); } } /** * Pythag * Find pass distance * @param a position of each * @param b position of each * @return pythag value of the two points */ public double Distance(Position a, Position b) { if(a != null || b != null) { double diffx = Math.abs(a.getPosx() - b.getPosx()); double diffy = Math.abs(a.getPosy() - b.getPosy()); return pythag(diffx, diffy); } else { return 0; } } /** * Pythag * Find pass distance * @param a position of each * @param b position of each * @return pythag value of the two points */ public double pythag(double a, double b) { double absa,absb; absa=Math.abs(a); absb=Math.abs(b); if(absa > absb) return absa*Math.sqrt(1.+Math.pow((absb/absa),2)); else if(absb==0.) return 0.; else return absb*Math.sqrt(1.+Math.pow((absa/absb),2)); } }