Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

There are some error, but I don't know how to fix it. Please let me know import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import

There are some error, but I don't know how to fix it. Please let me know

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Random;

import java.util.Scanner;

public class WaTor { public static void main(String[] args) { Scanner input = new Scanner(System.in); Random randGen = new Random();

int[][] fish = null; boolean[][] fishMoved = null;

int[][] sharks = null; boolean[][] sharksMoved = null; int[][] starve = null;

int[] simulationParameters = null; if ( simulationParameters == null) { simulationParameters = new int[Config.SIM_PARAMS.length]; for ( int i = 0; i < Config.SIM_PARAMS.length; i++) { System.out.print("Enter " + Config.SIM_PARAMS[i] + ": "); simulationParameters[i] = input.nextInt(); } input.nextLine(); //read and ignore remaining newline } if (simulationParameters[indexForParam("seed")] > 0) { randGen.setSeed(simulationParameters[indexForParam("seed")]); } int oceanWidth = simulationParameters[indexForParam("ocean_width")]; int oceanHeight = simulationParameters[indexForParam("ocean_height")]; int startingFish = simulationParameters[indexForParam("starting_fish")]; int startingSharks = simulationParameters[indexForParam("starting_sharks")]; int fishBreed = simulationParameters[indexForParam("fish_breed")]; int sharksBreed = simulationParameters[indexForParam("sharks_breed")]; int sharksStarve = simulationParameters[indexForParam("sharks_starve")];

fish = new int[oceanHeight][oceanWidth]; fishMoved = new boolean[oceanHeight][oceanWidth]; sharks = new int[oceanHeight][oceanWidth]; sharksMoved = new boolean[oceanHeight][oceanWidth]; starve = new int[oceanHeight][oceanWidth];

int numFish = 0; int numSharks = 0; int currentChronon = 1; boolean simulationEnd = numFish <= 0 || numSharks <= 0; while ( !simulationEnd)

System.out.print("Press Enter, # of chronon, or 'end': "); String response = input.nextLine().trim(); if (response.equalsIgnoreCase("end")) { break;

} simulationEnd = numFish <= 0 || numSharks <= 0; }; if ( numSharks <= 0 ) { System.out.println("Wa-Tor simulation ended since no sharks remain."); } else if ( numFish <= 0 ){ System.out.println("Wa-Tor simulation ended since no fish remain."); } else { System.out.println("Wa-Tor simulation ended at user request."); }

input.close(); }

public class WatorWorld { private Grid theWorld; private static Color oceanColor; static { oceanColor = new Color(50, 50, 255); } public static Color getOceanColor() { return oceanColor; } public WatorWorld(int rows, int cols, double fractionFish, double fractionSharks){ theWorld = new WrappedBoundedGrid(rows, cols); reset(fractionFish, fractionSharks); } public void reset(double fractionFish, double fractionSharks){ ArrayList occupiedCells = theWorld.getOccupiedLocations(); for(Location loc : occupiedCells){ theWorld.get(loc).removeSelfFromGrid(); } Shark.resetNumSharks(); Fish.resetNumFish(); populateWorld(fractionFish, fractionSharks); } private void populateWorld(double fractionFish, double fractionSharks){ double totalFractionCreatures = fractionFish + fractionSharks; if(totalFractionCreatures > 1) { // too many creatures. scale each fractionFish = fractionFish / totalFractionCreatures; fractionSharks= fractionSharks / totalFractionCreatures; } int numFish = (int) (fractionFish * getNumSpots()); int numSharks = (int) (fractionSharks * getNumSpots()); if(numFish + numSharks < getNumSpots() / 2) populateDefinite(numFish, numSharks); else populateIndefinite(numFish, numSharks); }

private void populateIndefinite(int numFish, int numSharks) { ArrayList locs = new ArrayList(); for(int r = 0, rLimit = theWorld.getNumRows(); r < rLimit; r++) for(int c = 0, cLimit = theWorld.getNumCols(); c < cLimit; c++) locs.add(new Location(r, c)); Collections.shuffle(locs); for(int i = 0; i < numFish; i++) new Fish().putSelfInGrid(theWorld, locs.get(i)); for(int i = numFish, limit = numFish + numSharks; i < limit; i++) new Shark().putSelfInGrid(theWorld, locs.get(i)); }

private void populateDefinite(int numFish, int numSharks) { loopToPlace(numFish, true); loopToPlace(numSharks, false); } private void loopToPlace(int num, boolean placeFish){ int placed = 0; Random r = new Random(); ArrayList locsUsed = new ArrayList(); while(placed < num){ int row = r.nextInt(theWorld.getNumRows()); int col = r.nextInt(theWorld.getNumCols()); Location loc = new Location(row, col); if(theWorld.get(loc) == null){ placed++; Actor result = placeFish ? new Fish() : new Shark(); result.putSelfInGrid(theWorld, loc); locsUsed.add(loc); } } } public void step(){ ArrayList occupiedLocations = theWorld.getOccupiedLocations(); Collections.shuffle(occupiedLocations); for(Location loc : occupiedLocations){ Actor a = theWorld.get(loc); // in case eaten if(a != null) a.act(); } } public int getNumFish(){ return Fish.getNumFish(); }

public int getNumSharks(){ return Shark.getNumSharks(); } public Color getColor(int row, int col){ Actor a = theWorld.get(new Location(row, col)); if(a == null) return oceanColor; else return a.getColor(); } getNumRows(){ return theWorld.getNumRows(); }

public int getNumCols(){ return theWorld.getNumCols(); } public String toString(){ StringBuilder b = new StringBuilder(); for(int r = 0, rLimit = theWorld.getNumRows(); r < rLimit; r++){ for(int c = 0, cLimit = theWorld.getNumCols(); c < cLimit; c++){ Actor a = theWorld.get(new Location(r, c)); if(a == null) b.append('.'); else b.append( a.getChar() ); } b.append(' '); } return b.toString(); } public int getNumSpots(){ return theWorld.getNumRows() * theWorld.getNumCols(); } }

/** * This is called when a fish cannot move. This increments the fish's age and notes in * the fishMove array that it has been updated this chronon. * * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param row The row of the fish that is staying. * @param col The col of the fish that is staying. */ public static void aFishStays(int[][] fish, boolean[][] fishMove, int row, int col) { if (Config.DEBUG) { System.out.printf("DEBUG fish %d,%d stays ", row, col); } fish[row][col]++; // increment age of fish fishMove[row][col] = true; } /** * The fish moves from fromRow,fromCol to toRow,toCol. The age of the fish is incremented. The * fishMove array records that this fish has moved this chronon. * * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param fromRow The row the fish is moving from. * @param fromCol The column the fish is moving from. * @param toRow The row the fish is moving to. * @param toCol The column the fish is moving to. */ public static void aFishMoves(int[][] fish, boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG fish moved from %d,%d to %d,%d ", fromRow, fromCol, toRow, toCol); } // just move fish fish[toRow][toCol] = fish[fromRow][fromCol] + 1; // increment age fishMove[toRow][toCol] = true;

// clear previous location fish[fromRow][fromCol] = Config.EMPTY; fishMove[fromRow][fromCol] = false; } /** * The fish moves from fromRow,fromCol to toRow,toCol. This fish breeds so its * age is reset to 0. The new fish is put in the fromRow,fromCol with an age of 0. The * fishMove array records that both fish moved this chronon. * * @param fish The array containing all the ages of all the fish. * @param fishMove The array containing the indicator of whether each fish moved this * chronon. * @param fromRow The row the fish is moving from and where the new fish is located. * @param fromCol The column the fish is moving from and where the new fish is located. * @param toRow The row the fish is moving to. * @param toCol The column the fish is moving to. */ public static void aFishMovesAndBreeds(int[][] fish, boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG fish moved from %d,%d to %d,%d and breed ", fromRow, fromCol, toRow, toCol); } // move fish, resetting age in new location fish[toRow][toCol] = 0; fishMove[toRow][toCol] = true;

// breed fish[fromRow][fromCol] = 0; // new fish in previous location fishMove[fromRow][fromCol] = true; }

/** * This removes the shark from the sharks, sharksMove and starve arrays. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param row The row the shark is in. * @param col The column the shark is in. */ public static void sharkStarves(int[][] sharks, boolean[][] sharksMove, int[][] starve, int row, int col) { if (Config.DEBUG) { System.out.printf("DEBUG shark %d,%d starves ", row, col); } sharks[row][col] = Config.EMPTY; starve[row][col] = Config.EMPTY; sharksMove[row][col] = false; }

/** * This is called when a shark cannot move. This increments the shark's age and time since * the shark last ate and notes in the sharkMove array that it has been updated this chronon. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param row The row the shark is in. * @param col The column the shark is in. */ public static void sharkStays(int[][] sharks, boolean[][] sharksMove, int[][] starve, int row, int col) { if (Config.DEBUG) { System.out.printf("DEBUG shark %d,%d can't move ", row, col); } sharks[row][col]++; // increment age of shark starve[row][col]++; // increment time since last ate sharksMove[row][col] = true; } /** * This moves a shark from fromRow,fromCol to toRow,toCol. This increments the age and time * since the shark last ate and notes that this shark has moved this chronon. * * @param sharks The array containing all the ages of all the sharks. * @param sharksMove The array containing the indicator of whether each shark moved this * chronon. * @param starve The array containing the time in chronon since the sharks last ate. * @param fromRow The row the shark is moving from. * @param fromCol The column the shark is moving from. * @param toRow The row the shark is moving to. * @param toCol The column the shark is moving to. */ public static void sharkMoves(int[][] sharks, boolean[][] sharksMove, int[][] starve, int fromRow, int fromCol, int toRow, int toCol) { if (Config.DEBUG) { System.out.printf("DEBUG shark moved from %d,%d to %d,%d ", fromRow, fromCol, toRow, toCol); } // just move shark sharks[toRow][toCol] = sharks[fromRow][fromCol] + 1; // move age sharksMove[toRow][toCol] = true; starve[toRow][toCol] = starve[fromRow][fromCol] + 1;

sharks[fromRow][fromCol] = Config.EMPTY; sharksMove[fromRow][fromCol] = false; starve[fromRow][fromCol] = 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ai And The Lottery Defying Odds With Intelligent Prediction

Authors: Gary Covella Ph D

1st Edition

B0CND1ZB98, 979-8223302568

More Books

Students also viewed these Databases questions

Question

What mental processes allow you to perceive a lemon as yellow?

Answered: 1 week ago