import java.util.Scanner; public class World { // World is a 2D array. // Each element is a reference to a Tardis object // All elements but one will be null // One element will refer to a Tardis object (the Doctor's tardis). private Tardis [][] grid; // These two values determine the size of the world private int maxRow; private int maxColumn; // Current location of the Doctor's tardis private Location tardisLocation; public World() { final int START_ROW = 0; final int START_COLUMN = 0; Scanner in = new Scanner(System.in); System.out.println("Creating the game world"); System.out.print("Max rows: "); maxRow = in.nextInt(); System.out.print("Max columns: "); maxColumn = in.nextInt(); grid = new Tardis[maxRow][maxColumn]; wipe(); // Empties the world grid[START_ROW][START_COLUMN] = new Tardis(10); // Put the Doctor's Tardis here. tardisLocation = new Location(START_ROW,START_COLUMN); display(); } public void display() { int r; int c; for (r = 0; r < maxRow; r++) { for (c = 0; c < maxColumn; c++) { if (grid[r][c] == null) System.out.print("."); else System.out.print("T"); } System.out.println(); } } /* Queries the element referring to the Tardis object to determine if it has energy remaining. */ public boolean energyRemains() { boolean isThereEnergy; isThereEnergy = grid[tardisLocation.getRow()][tardisLocation.getColumn()].hasEnergy(); return(isThereEnergy); } public void move() { // Get the current Row/Col coordinates of the Tardis int currentRow = tardisLocation.getRow(); int currentColumn = tardisLocation.getColumn(); int oldRow = currentRow; int oldColumn = currentColumn; System.out.println("Tardis dematerializing"); tardisLocation = grid[currentRow][currentColumn].calculateCoordinates(maxRow,maxColumn); // Update temporary values with current location currentRow = tardisLocation.getRow(); currentColumn = tardisLocation.getColumn(); // Copy the reference to the tardis from the old location to the new one. grid[currentRow][currentColumn] = grid[oldRow][oldColumn]; // Check if tardis trying to move onto same square, don't 'wipe' if this is the case or // tardis will be lost in the temporal void! if ((currentRow == oldRow) && (currentColumn == oldColumn)) { System.out.println("Rematerializing in same location"); } else { // old location no longer refers to the tardis because the tardis has 'moved' off of it. grid[oldRow][oldColumn] = null; } System.out.println("Tardis re-materializing"); display(); } public void wipe() { int r; int c; for (r = 0; r < maxRow; r++) { for (c = 0; c < maxColumn; c++) { grid[r][c] = null; } } } }