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 int [] currentLocation; public World() { // Stores info about the current location of the tardis in the world // Element 0: current row the tardis is located // Element 1: current column the tardis is located currentLocation = new int[2]; 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[0][0] = new Tardis(10); // Put the Doctor's Tardis here. currentLocation[0] = 0; // Tardis row = 0 currentLocation[1] = 0; // Tardis col = 0 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(); } } public void move() { // Get the current Row/Col coordinates of the Tardis int currentRow = currentLocation[0]; int currentColumn = currentLocation[1]; int oldRow = currentRow; int oldColumn = currentColumn; System.out.println("Tardis dematerializing"); // Why pass in the max number of rows/columns? // Tardis.calculateCoordinates(): randomly generates a new row/column // The new coordinates have to fall within the bounds of the world (dicated by the // max number of rows, columns). // So the max nmumber of rows and columns must be passed to the calculateCoordinates // method. // Why are the coordinats store in 2 element array instead of two integers // Coordinates are stored as a 2 element integer array (currentLocation) because // calculate Coordinates will generate and return the new row AND column location // (two values). Methods can at most return only one thing. So an array is used to // store the pair of coordinates. currentLocation = grid[currentRow] [currentColumn].calculateCoordinates(maxRow,maxColumn); // Update temporary values with current location currentRow = currentLocation[0]; currentColumn = currentLocation[1]; // 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; } } } }