Codingame Tron Battle : Java Bootstrap Code
Si vous souhaitez comme moi perdre votre temps en participant au concours de développement Tron Battle, voici un code Java qui vous permettra rapidement de développer votre robot Tron plus rapidement.
L'attribut checkerboard contient le damier avec les valeurs de chaque joueur, il ne vous reste à implémenter la méthode "Play()" avec la bonne logique pour bloquer les autres bots qui participent au concours.
Boostrap Code
import java.util.Scanner;
/**
* Tron Player
*
* Author : Ludovic Toinel
*/
class Player {
// Constants
private static final String UP = "UP";
private static final String DOWN = "DOWN";
private static final String LEFT = "LEFT";
private static final String RIGHT = "RIGHT";
private static final String[] directions = new String[]{UP,LEFT,DOWN,RIGHT};
private static final int X = 30;
private static final int Y = 20;
// checkerboard
public static int[][] checkerboard = new int[X][Y];
// Number of players
public static int nbPlayer;
// Id of this player
public static int idPlayer;
// Current players positions
public static Position[] players;
/**
* Play an action
*/
private static void action(String direction){
System.out.println(direction);
}
/**
* Debug a message
*/
private static void debug(String message){
System.err.println(message);
}
/**
* Main player class
* @param args
*/
public static void main(String args[]) {
// Init the scanner
Scanner in = new Scanner(System.in);
// Init coordinates
int x0, y0, x1, y1;
// Initialize the checkerBoard
removePlayer(0);
// Infinite while ...
while (true) {
// Count of players
nbPlayer = in.nextInt();
// Initialize players positions
if (players == null){
players = new Position[nbPlayer];
for(int i = 0; i < nbPlayer; i++){
players[i] = new Position(0,0);
}
}
// Retrieve my player ID
idPlayer = in.nextInt();
// For each players
for (int player = 0; player < nbPlayer; player++){
// Read the coordinates
x0 = in.nextInt();
y0 = in.nextInt();
x1 = in.nextInt();
y1 = in.nextInt();
// Player has lost
if (x0 == -1){
removePlayer(player);
continue;
}
// Update the checkerboard
checkerboard[x1][y1] = player;
// Save the current player position
players[player].update(x1,y1);
}
// Play the round
play();
}
}
/**
* Remove the player from the checkerboard
*/
private static void removePlayer(int id){
for(int i=0; i<checkerboard.length;i++){
for (int e = 0; e < checkerboard[i].length;e++){
if (checkerboard[i][e] == id ) {
checkerboard[i][e] = -1;
}
}
}
}
/**
* Play a round
*/
public static void play(){
// TODO CODE HERE !
action(LEFT);
}
/**
* Position class
*/
public static class Position{
public int x;
public int y;
public boolean me;
/** Constructor */
public Position(int x, int y){
this.x = x;
this.y = y;
}
/** Update the position */
public void update(int x, int y){
this.x = x;
this.y = y;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Position obj) {
return (obj != null && obj.x == x && obj.y == y);
}
}
}
Dépêchez-vous, il ne vous reste plus que 32 jours avant la fin du concours.