I bring a part from the problem. Please help me this part // //FIXME file header
ID: 3920279 • Letter: I
Question
I bring a part from the problem. Please help me this part
//
//FIXME file header comment
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;
//FIXME class header comment
public class WaTor {
/**
* This is the main method for WaTor simulation.
* Based on: http://home.cc.gatech.edu/biocs1/uploads/2/wator_dewdney.pdf
* This method contains the main simulation loop. In main the Scanner
* for System.in is allocated and used to interact with the user.
*
* @param args (unused)
*/
public static void main(String[] args) {
//scanner and random number generator for use throughout
Scanner input = new Scanner(System.in);
Random randGen = new Random();
//values at the same index in these parallel arrays correspond to the
//same creature
//a value equal or greater than 0 at a location indicates a fish of
//that age at that location.
int[][] fish = null;
//true at a location indicates that the fish moved during the current
//chronon
boolean[][] fishMoved = null;
//a value equal or greater than 0 at a location indicates a shark of
//that age at that location
int[][] sharks = null;
//true at a location indicates that the shark moved during the current
//chronon
boolean[][] sharksMoved = null;
//a value equal or greater than 0 indicates the number of chronon
//since the shark last ate.
int[][] starve = null;
//an array for simulation parameters
//to be used when saving or loading parameters in Milestone 3
int[] simulationParameters = null;
//welcome message
//Ask user if they would like to load simulation parameters from a file.
//If the user enters a y or Y as the only non-whitespace characters
//then prompt for filename and call loadSimulationParameters
//TODO in Milestone 3
//prompts the user to enter the simulation parameters
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 seed is > 0 then set the random number generator to seed
if (simulationParameters[indexForParam("seed")] > 0) {
randGen.setSeed(simulationParameters[indexForParam("seed")]);
}
//save simulation parameters in local variables to help make code
//more readable.
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")];
//create parallel arrays to track fish and sharks
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];
//make sure fish, sharks and starve arrays are empty (call emptyArray)
//TODO Milestone 1
//place the initial fish and sharks and print out the number
//placed
int numFish = 0;
int numSharks = 0;
//TODO Milestone 1
int currentChronon = 1;
//simulation ends when no more sharks or fish remain
boolean simulationEnd = numFish <= 0 || numSharks <= 0;
while ( !simulationEnd){
//print out the locations of the fish and sharks
//TODO Milestone 1
//prompt user for Enter, # of chronon, or 'end'
//Enter advances to next chronon, a number
//entered means run that many chronon, (TODO Milestone 2),
//'end' will end the simulation
System.out.print("Press Enter, # of chronon, or 'end': ");
String response = input.nextLine().trim();
if (response.equalsIgnoreCase("end")) {
break; //leave simulation loop
}
//clear fishMoved and sharksMoved from previous chronon
//TODO Milestone 1
//call fishSwimAndBreed
//TODO Milestone 2
//call sharksHuntAndBreed
//TODO Milestone 2
//increment current chronon and count the current number of fish and sharks
//TODO Milestone 1
//if all the fish or sharks are gone then end simulation
simulationEnd = numFish <= 0 || numSharks <= 0;
};
//print the final ocean contents
//TODO Milestone 1
//Print out why the simulation ended.
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.");
}
//If the user was prompted to enter simulation parameters
//then prompt the user to see if they would like to save them.
//If the user enters a y or Y as the only non-whitespace characters
//then prompt for filename and save, otherwise don't save parameters.
//call saveSimulationParameters to actually save the parameters to the file.
//If saveSimulationParameters throws an IOException then catch it and
//repeat the code to prompt asking the user if they want to save
//the simulation parameters.
//TODO Milestone 3
//Always prompt the user to see if they would like to save a
//population chart of the simulation.
//If the user enters a y or Y as the only non-whitespace characters
//then prompt for filename and save, otherwise don't save chart.
//call savePopulationChart to save the parameters to the file.
//If savePopulationChart throws an IOException then catch it and
//repeat the code to prompt asking the user if they want to save
//the population chart.
//TODO Milestone 3
input.close();
}
Explanation / Answer
SOLUTION:
According to the given data the following code follows;
CODE:
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<Actor> 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<Actor>(rows, cols);
reset(fractionFish, fractionSharks);
}
public void reset(double fractionFish, double fractionSharks){
ArrayList<Location> 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<Location> locs = new ArrayList<Location>();
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<Location> locsUsed = new ArrayList<Location>();
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<Location> 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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.