// GameEngine.java // Description: Game Engine for Terriers and Squirrels // Author: Chris Wilcox // Date: 4/28/2015 package myPackage; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; import myPackage.Interface.eMove; public class GameEngine { // Board data private char board[][]; private int width; private int height; // Animal data private ArrayList terriers = new ArrayList(); private ArrayList squirrels = new ArrayList(); // Constructor public GameEngine(String filename) { // Read file readFile(filename); // Setup board for (int row = 0; row < height; ++row) { for (int col = 0; col < width; ++col) { if (board[row][col] == 'T') { // Make a Terrier Terrier terrier = new Terrier(row, col, board); terriers.add(terrier); } else if (board[row][col] == 'S') { // Make a Squirrel Squirrel squirrel = new Squirrel(row, col, board); squirrels.add(squirrel); } } } } // Returns board public char[][] data() { return board; } // Next move public boolean next() { // Move squirrels for (Squirrel s : squirrels) { moveSquirrel(s); } // Move terriers for (Terrier t : terriers) { // No need to continue if (squirrels.size() == 0) break; moveTerrier(t); // Remove squirrel, if necessary if (t.ateSquirrel) { for (Squirrel s : squirrels) { if ((s.getCurrentRow() == t.getCurrentRow()) && (s.getCurrentCol() == t.getCurrentCol())) { System.err.print("Terrier[" + terriers.indexOf(t) + "] ate "); System.err.println("Squirrel[" + squirrels.indexOf(s) + "]!"); squirrels.remove(s); break; } } } } printBoard(); return (squirrels.size() == 0) ? true : false; } // Move squirrel private void moveSquirrel(Squirrel s) { // Call student code to move squirrel eMove e = s.move(); updateBoard(s, e); System.err.print("Squirrel[" + squirrels.indexOf(s) + "] moved to "); System.err.println("(" + s.getCurrentRow() + "," + s.getCurrentCol() + ")"); } // Move terrier private void moveTerrier(Terrier t) { // Call instructor code to move terrier t.move(); System.err.print("Terrier[" + terriers.indexOf(t) + "] moved to "); System.err.println("(" + t.getCurrentRow() + "," + t.getCurrentCol() + ")"); } // Update board with current location public void updateBoard(Squirrel s, eMove e) { // Get current location int currentRow = s.getCurrentRow(); int currentCol = s.getCurrentCol(); // Remove squirrel board[currentRow][currentCol] = '-'; // Save previous position int saveRow = currentRow; int saveCol = currentCol; // Move squirrel switch (e) { case LEFT: currentCol--; break; case UP_LEFT: currentRow--; currentCol--; break; case UP: currentRow--; break; case UP_RIGHT: currentRow--; currentCol++; break; case RIGHT: currentCol++; break; case DOWN_RIGHT: currentRow++; currentCol++; break; case DOWN: currentRow++; break; case DOWN_LEFT: currentRow++; currentCol--; break; default: } // Don't collide with anything if (board[currentRow][currentCol] != '-') { currentRow = saveRow; currentCol = saveCol; } // Update location of squirrel s.setCurrentRow(currentRow); s.setCurrentCol(currentCol); // Replace squirrel board[currentRow][currentCol] = 'S'; } // Read board private void readFile(String filename) { try { Scanner in = new Scanner(new File(filename)); height = in.nextInt(); width = in.nextInt(); board = new char[height][width]; for (int row = 0; row < height; row++) for (int col = 0; col < width; col++) board[row][col] = in.next().charAt(0); in.close(); } catch (IOException e) { System.err.println("Cannot read " + filename); System.exit(0); } printBoard(); } // Display board private void printBoard() { for (int row = 0; row < height; ++row) { System.err.print("Row " + row + ": "); for (int col = 0; col < width; ++col) { System.err.print(board[row][col] + " "); } System.err.println(); } } }