import org.RoboBall.Player; import org.RoboBall.SoccerAction; import java.awt.Point; import java.net.*; import java.io.*; public class LogPlayer extends Player { private int m_iterationCount = 0; private FileOutputStream m_logFile; // creates the log file // log file name: .log public LogPlayer() { try { m_logFile = new FileOutputStream( this.getClass().getName()+".log"); System.out.println("new LogFile"); } catch (Exception e) { System.err.println ("Unable to create file"); System.exit(-1); } } // closes the log file protected void finalize() { try { m_logFile.close(); System.out.println("LogFile closed"); } catch (Exception e) { System.err.println ("Unable to close file"); System.exit(-1); } } // writes a message to the log file // format: [: ] protected void writeLogFile( String message ) { try { new PrintStream(m_logFile).println( m_iterationCount + " ["+this.getClass().getName()+": "+ getMyXPos()+","+getMyYPos()+"] "+message ); } catch (Exception e) { System.err.println ("Unable to write to file"); System.exit(-1); } } public SoccerAction makeMove() { m_iterationCount++; // sample for a log file entry writeLogFile( "new move" ); writeLogFile( "Robots of Class LogPlayer: " + findRobotsOfClass("LogPlayer").length ); writeLogFile( "Robots of Class TestPlayer: " + findRobotsOfClass("TestPlayer").length ); Point myPos = getMyPos(); Point ballPos = getBallPos(); switch (selfBallAround()) { case UP: if (isSelfPassPossible(UP)) return SoccerAction.pass(UP); else { switch (lookTo(UP, 2)) { case FREE: case WALL: case OTHER_TEAM: return SoccerAction.move(UP); case MY_TEAM: if (lookTo(LEFT) == FREE || lookTo(LEFT, 2) == FREE) return SoccerAction.move(LEFT); else if (lookTo(RIGHT) == FREE || lookTo(RIGHT, 2) == FREE) return SoccerAction.move(RIGHT); } if (lookTo(LEFT) == FREE && nextOwnRobotAround(LEFT) <= nextOtherRobotAround(LEFT)) return SoccerAction.dribble(LEFT); else if (lookTo(RIGHT) == FREE && nextOwnRobotAround(RIGHT) <= nextOtherRobotAround(RIGHT)) return SoccerAction.dribble(RIGHT); else if (lookTo(DOWN) == FREE && nextOtherRobotAround(DOWN) == Integer.MAX_VALUE) return SoccerAction.dribble(DOWN); return SoccerAction.nothing(); } case DOWN: if (lookTo(UP) == FREE && (nextOwnRobotAround(UP) <= nextOtherRobotAround(UP))) return SoccerAction.dribble(UP); else if (lookTo(LEFT) == FREE && (nextOwnRobotAround(LEFT) <= nextOtherRobotAround(LEFT))) return SoccerAction.dribble(LEFT); else if (lookTo(RIGHT) == FREE && (nextOwnRobotAround(RIGHT) <= nextOtherRobotAround(RIGHT))) return SoccerAction.dribble(RIGHT); else { if (lookToRel(ballPos, DOWN) == MY_TEAM) return SoccerAction.move(UP); else if (lookToRel(ballPos, DOWN) == OTHER_TEAM) return SoccerAction.move(DOWN); if (ballPos.y < getFieldHeight() - 3 && nextOwnRobotAround(offsetPoint(ballPos, DOWN)) <= nextOtherRobotAround(offsetPoint(ballPos, DOWN))) return SoccerAction.move(DOWN); else if (lookTo(LEFT) == OTHER_TEAM && lookTo(LEFT, 2) == FREE) return SoccerAction.move(LEFT); else if (lookTo(RIGHT) == OTHER_TEAM && lookTo(RIGHT, 2) == FREE) return SoccerAction.move(RIGHT); else if (lookTo(LEFT) == OTHER_TEAM) return SoccerAction.move(LEFT); else if (lookTo(RIGHT) == OTHER_TEAM) return SoccerAction.move(RIGHT); else return SoccerAction.nothing(); } case LEFT: if (lookTo(UP) == FREE && nextOwnRobotAround(UP) <= nextOtherRobotAround(UP)) return SoccerAction.dribble(UP); else if (myPos.x > getFieldWidth() / 4 && lookTo(LEFT, 2) == FREE) return SoccerAction.move(LEFT); else if (lookTo(RIGHT) == FREE && nextOwnRobotAround(RIGHT) <= nextOtherRobotAround(RIGHT)) return SoccerAction.dribble(RIGHT); else if (lookTo(DOWN) == FREE) { if (ballPos.y < getFieldHeight() - 3 && nextOwnRobotAround(DOWN) <= nextOtherRobotAround(DOWN)) return SoccerAction.dribble(DOWN); else return SoccerAction.move(DOWN); } else return SoccerAction.nothing(); case RIGHT: if (lookTo(UP) == FREE && nextOwnRobotAround(UP) <= nextOtherRobotAround(UP)) return SoccerAction.dribble(UP); else if (myPos.x < getFieldWidth() * 3/4 && lookTo(RIGHT, 2) == FREE) return SoccerAction.move(RIGHT); else if (lookTo(LEFT) == FREE && nextOwnRobotAround(LEFT) <= nextOtherRobotAround(LEFT)) return SoccerAction.dribble(LEFT); else if (lookTo(DOWN) == FREE) { if (ballPos.y < getFieldHeight() - 3 && nextOwnRobotAround(DOWN) <= nextOtherRobotAround(DOWN)) return SoccerAction.dribble(DOWN); else return SoccerAction.move(DOWN); } else return SoccerAction.nothing(); case NOTHING: { int ballDirection = getRelativeDirection(myPos, ballPos); if (ballPos.y == myPos.y - 1) { if ( Math.abs(ballPos.x - myPos.x) == 1 ) { if (lookAt(ballPos.x, myPos.y) == FREE) return SoccerAction.move(ballDirection & HORIZONTAL); else if (lookTo(UP) == FREE) return SoccerAction.move(UP); else return SoccerAction.move(DOWN); } else if ( Math.abs(ballPos.x - myPos.x) == 2 ) { if (lookAt(ballPos.x, myPos.y) == FREE) return SoccerAction.run(ballDirection & HORIZONTAL); else if (lookTo(UP) == FREE) return SoccerAction.move(UP); else return SoccerAction.move(DOWN); } } else if ((ballDirection & VERTICAL) != NOTHING) { if ( lookTo(ballDirection & VERTICAL) == FREE ) return SoccerAction.run(ballDirection & VERTICAL); else return SoccerAction.move(ballDirection & HORIZONTAL); } else { if (lookTo(ballDirection & HORIZONTAL) == FREE) return SoccerAction.run(ballDirection & HORIZONTAL); else if (lookTo(DOWN) == FREE) return SoccerAction.run(DOWN); else return SoccerAction.move(UP); } } default: return SoccerAction.nothing(); } } }