Newer
Older
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
private int[][] board;
private Car[] cars;
private Position exit;
public Board(int size, int nbcars){
initTab(size);
cars = new Car[nbcars];
}
private void initTab(int size){
for ( int i=0 ; i < size ; i++){
for ( int j=0 ; j< size ; j++){
board[i][j] = -1;
}
}
}
/**
* Load the board from a file.
* Structure of the file:
* 1st line: size of the board (the board is a square)
* 2nd line: x positition and y position of the goal
* 3rd line: number of cars in the level
* Following lines: every line defines a car: values of xFront<SPACE>yFront<SPACE>xBack<SPACE>yBack<SPACE>orientation
*
* @param filelevel The name of the file
* @throws IOException On file reading errors
*/
public Board(String filelevel) throws IOException{
BufferedReader br = new BufferedReader( new FileReader(filelevel) );
int size = Integer.valueOf(br.readLine());
initTab(size);
String[]line2 = br.readLine().split(" ");
exit = new Position(Integer.valueOf(line2[0]), Integer.valueOf(line2[1]));
int car_number = Integer.valueOf(br.readLine());
cars = new Car[car_number];
int i = 0;
String l;
while((l = br.readLine()) != null){
String[] splitl = l.split(" ");
Position pos1 = new Position(Integer.valueOf(splitl[0]), Integer.valueOf(splitl[1]));
Position pos2 = new Position(Integer.valueOf(splitl[2]), Integer.valueOf(splitl[3]));
Car car = new Car(i, pos1, pos2, Car.Orientation.valueOf(splitl[4]));
Alexis BUSSENEAU
committed
if(car.getOrientation() == Car.Orientation.HORIZONTAL)
for(int x = car.getBack().getX(); x <= car.getFront().getX(); x++)
board[x][car.getFront().getY()] = car.getNumber();
if(car.getOrientation() == Car.Orientation.VERTICAL)
for(int y = car.getBack().getY(); y <= car.getFront().getY(); y++)
board[car.getFront().getX()][y] = car.getNumber();
i++;
}
// Play a move on the board
public void play(Move move)
{
if(move.isForward())
move.getCar().moveForward(this, move.getMovement_size());
else
move.getCar().moveBackwards(this, move.getMovement_size());
}
return board[exit.getX()][exit.getY()] == 0;
}
public int getSize() {
return board.length;
}
public int getCarsNumber() {
return cars.length;
}
public int getValue(Position pos) {
return board[pos.getX()][pos.getY()];
}
public boolean isEmpty(int x, int y)
{
return board[x][y] == -1;
}
public boolean isEmpty(Position pos)
{
return board[pos.getX()][pos.getY()] == -1;
}
public void setValue(int x, int y, int value) {
board[x][y] = value;
}
public void setValue(Position pos, int value) {
board[pos.getX()][pos.getY()] = value;
}
Alexis BUSSENEAU
committed
public void setEmpty(int x, int y)
{
board[x][y] = -1;
}
public void setEmpty(Position pos)
{
board[pos.getX()][pos.getY()] = -1;
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("Goal:" + exit.toString() + "\n");
for (int y = 0; y < board.length; y++)
for (int x = 0; x < board.length; x++)
{
if(board[x][y] == -1)
sb.append(String.format("%3s ", board[x][y]));
sb.append("\n");
}
return sb.toString();
}