Cna I get correct code? //FIXME file header comment /** * This file contains tes
ID: 3920620 • Letter: C
Question
Cna I get correct code?
//FIXME file header comment
/**
* This file contains testing methods for the WaTor project.
* These methods are intended to serve several objectives:
* 1) provide an example of a way to incrementally test your code
* 2) provide example method calls for the WaTor methods
* 3) provide examples of creating, accessing and modifying arrays
*
* Toward these objectives, the expectation is that part of the
* grade for the WaTor project is to write some tests and
* write header comments summarizing the tests that have been written.
* Specific places are noted with TODO but add any other comments
* you feel would be useful.
*
* Some of the provided comments within this file explain
* Java code as they are intended to help you learn Java. However,
* your comments and comments in professional code, should
* summarize the purpose of the code, not explain the meaning
* of the specific Java constructs.
*
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
/**
* This class contains a few methods for testing methods in the WaTor
* class as they are developed. These methods are all private as they are only
* intended for use within this class.
*
* @author Jim Williams
* @author TODO add your name here when you add tests and comment the tests
*
*/
public class TestWaTor {
/**
* This is the main method that runs the various tests. Uncomment the tests
* when you are ready for them to run.
*
* @param args (unused)
*/
public static void main(String []args) {
//milestone 1
testClearMoves();
testEmptyArray();
testCountCreatures();
//The best way to test the following is probably to compare
//output with the examples.
//showFishAndSharks
//placeFish
//placeSharks
/**
* Compares the lists to see if they are the same size and contain the same elements.
* @param list1 One list of coordinates.
* @param list2 Another list of coordinates
* @return Whether the lists contain the same coordinates or not.
*/
private static boolean matchingArrayLists(ArrayList<int[]>list1, ArrayList<int[]>list2) {
boolean result = true;
if ( list1.size() != list2.size()) {
System.err.println("list1 size: " + list1.size() + " list2 size:" + list2.size() + " should be the same");
result = false;
return result;
}
for ( int i = 0; i < list1.size(); i++) {
int[]move1 = list1.get(i);
int[]move2 = list2.get(i);
if ( move1[0] == move2[0] && move1[1] == move2[1]) {
//ok
} else {
result = false;
System.err.println( "list1("+ i+"):" + Arrays.toString(move1) + " doesn't match in list2: " + Arrays.toString( move2));
break;
}
}
return result;
}
/**
* This runs some tests on the unoccupiedPositions method.
* 1. TODO describe each test in your own words.
* 2.
*/
private static void testUnoccupiedPositions() {
boolean error = false;
int [][]fish = new int[][]{{-1,-1,-1},{-1,0,-1},{-1,-1,-1}};
int [][]sharks = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
ArrayList<int[]> positions = WaTor.unoccupiedPositions( fish, sharks, 1, 1);
ArrayList<int[]>expected = new ArrayList<>();
expected.add( new int[]{0,1});
expected.add( new int[]{2,1});
expected.add( new int[]{1,0});
expected.add( new int[]{1,2});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 1 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 1);
expected = new ArrayList<>();
expected.add( new int[]{2,1});
expected.add( new int[]{0,0});
expected.add( new int[]{0,2});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 2 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 0);
expected = new ArrayList<>();
expected.add( new int[]{2,0});
expected.add( new int[]{1,0});
expected.add( new int[]{0,2});
expected.add( new int[]{0,1});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 3 :" );
}
fish = new int[][]{{0,0,0},{0,0,0},{0,0,0}};
sharks = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
positions = WaTor.unoccupiedPositions( fish, sharks, 1, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 4 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 5 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 0);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 6 :" );
}
fish = new int[][]{{0,0,0},{-1,0,0},{0,0,0}};
sharks = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
positions = WaTor.unoccupiedPositions( fish, sharks, 1, 1);
expected = new ArrayList<>();
expected.add( new int[]{1,0});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 7 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 8 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 0);
expected = new ArrayList<>();
expected.add( new int[]{1,0});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 9 :" );
}
if ( error) {
System.err.println("testUnoccupiedPositions failed");
} else {
System.out.println("testUnoccupiedPositions passed");
}
}
/**
* This runs some tests on the fishPositions method.
* 1. TODO describe each test in your own words.
* 2.
*/
/**
* This runs some tests on the chooseMove method.
* 1. TODO describe each test in your own words.
* 2.
*/
private static void testChooseMove() {
boolean error = false;
Random randGen = new Random();
randGen.setSeed( 456);
ArrayList<int[]> input = new ArrayList<>();
int [] expected = null;
int [] result = WaTor.chooseMove( input, randGen);
if ( result != expected) {
error = true;
System.err.println("testChooseMove 0: result not null");
}
input.clear();
int [] int[] {0,1};
input.add(oneMove);
expected = oneMove;
result = WaTor.chooseMove( input, randGen);
if ( result != expected) {
error = true;
System.err.println("testChooseMove 1: result not " + Arrays.toString( oneMove));
}
input.clear();
int [] move1 = new int[] {0,1};
int [] move2 = new int[] {1,0};
input.add( move1);
input.add( move2);
int move1Count = 0;
int move2Count = 0;
int numTrials = 1000;
for ( int i = 0; i < numTrials; i++) {
result = WaTor.chooseMove( input, randGen);
if ( result == move1) move1Count++;
else if ( result == move2) move2Count++;
}
if ( move1Count != 513 || move2Count != 487 ) {
error = true;
System.err.println("testChooseMove 2: expected 513,487 move1Count=" + move1Count + " move2Count=" + move2Count);
}
input.clear();
move1 = new int[] {0,1};
move2 = new int[] {1,0};
int[] move3 = new int[] {2,1};
input.add( move1);
input.add( move2);
input.add( move3);
move1Count = 0;
move2Count = 0;
int move3Count = 0;
numTrials = 1000;
for ( int i = 0; i < numTrials; i++) {
result = WaTor.chooseMove( input, randGen);
if ( result == move1) move1Count++;
else if ( result == move2) move2Count++;
else if ( result == move3) move3Count++;
}
if ( move1Count != 325 || move2Count != 341 || move3Count != 334 ) {
error = true;
System.err.println("testChooseMove 3: expected 325,341,334 move1Count=" + move1Count + " move2Count=" + move2Count + " move3Count=" + move3Count);
}
input.clear();
move1 = new int[] {0,1};
move2 = new int[] {1,0};
move3 = new int[] {2,1};
int []move4 = new int[] {1,2};
input.add( move1);
input.add( move2);
input.add( move3);
input.add( move4);
move1Count = 0;
move2Count = 0;
move3Count = 0;
int move4Count = 0;
numTrials = 1000;
for ( int i = 0; i < numTrials; i++) {
result = WaTor.chooseMove( input, randGen);
if ( result == move1) move1Count++;
else if ( result == move2) move2Count++;
else if ( result == move3) move3Count++;
else if ( result == move4) move4Count++;
}
if ( move1Count != 273 || move2Count != 231 || move3Count != 234 || move4Count != 262 ) {
error = true;
System.err.println("testChooseMove 4: expected 325,341,334,262 move1Count=" + move1Count + " move2Count=" + move2Count + " move3Count=" + move3Count + " move4Count=" + move4Count);
}
if ( error) {
System.err.println("testChooseMove failed");
} else {
System.out.println("testChooseMove passed");
}
}
/**
* This runs some tests on the clearMoves method.
* 1. TODO describe each test in your own words.
*/
private static void testClearMoves() {
boolean error = false;
boolean [][] moves = new boolean[4][9];
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
moves[row][col] = true;
}
}
WaTor.clearMoves(moves);
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
if ( moves[row][col]) {
System.err.println("testClearMoves 0: move " + row + "," + col + " not false");
error = true;
break;
}
}
}
if ( error) {
System.err.println("testClearMoves failed");
} else {
System.out.println("testClearMoves passed");
}
}
/**
* This runs some tests on the emptyArray method.
* 1. TODO describe each test in your own words.
*/
private static void testEmptyArray() {
boolean error = false;
int [][] moves = new int[100][99];
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
moves[row][col] = Config.EMPTY + 2; //make sure not EMPTY
}
}
WaTor.emptyArray(moves);
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
if ( moves[row][col] != Config.EMPTY) {
System.err.println("testEmptyArray 0: move " + row + "," + col + " not EMPTY");
error = true;
break;
}
}
}
if ( error) {
System.err.println("testEmptyArray failed");
} else {
System.out.println("testEmptyArray passed");
}
}
/**
* This runs some tests on the countFish method.
* 1. TODO describe each test in your own words.
* 2.
*/
private static void testCountCreatures() {
boolean error = false;
int[][] fish = new int[7][3];
WaTor.emptyArray(fish);
fish[0][0] = 1;
fish[6][2] = 2;
fish[0][2] = 3;
fish[6][0] = 4;
fish[3][1] = 5;
int result = WaTor.countCreatures( fish);
if ( result != 5) {
System.err.println("testCountCreatures 0: expected 5 found " + result );
error = true;
}
if ( error) {
System.err.println("testCountCreatures failed");
} else {
System.out.println("testCountCreatures passed");
}
}
}
Explanation / Answer
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class TestWaTor {
public static void main(String []args) {
//milestone 1
testClearMoves();
testEmptyArray();
testCountCreatures();
//milestone 2
testUnoccupiedPositions();
testChooseMove();
testFishPositions();
//milestone 3
//comparing results of either inputting from or outputting to files
//may be the easiest way to test.
}
// Compares the lists to see if they are the same size and contain the same elements.
private static boolean matchingArrayLists(ArrayList<int[]>list1, ArrayList<int[]>list2) {
boolean result = true;
if ( list1.size() != list2.size()) {// compares size
System.err.println("list1 size: " + list1.size() + " list2 size:" + list2.size() + " should be the same");
result = false;
return result;
}
for ( int i = 0; i < list1.size(); i++) {
int[]move1 = list1.get(i);
int[]move2 = list2.get(i);
if ( move1[0] == move2[0] && move1[1] == move2[1]) {// compares values
} else {
result = false;
System.err.println( "list1("+ i+"):" + Arrays.toString(move1) + " doesn't match in list2: " + Arrays.toString( move2));
break;
}
}
return result;
}
// Checks the immediate surrounding coordinates of a selected row and col to see if there are fish or sharks at that location or not.
private static void testUnoccupiedPositions() {
boolean error = false;
int [][]fish = new int[][]{{-1,-1,-1},{-1,0,-1},{-1,-1,-1}};
int [][]sharks = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
ArrayList<int[]> positions = WaTor.unoccupiedPositions( fish, sharks, 1, 1);
ArrayList<int[]>expected = new ArrayList<>();
expected.add( new int[]{0,1});
expected.add( new int[]{2,1});// expected result
expected.add( new int[]{1,0});
expected.add( new int[]{1,2});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 1 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 1);
expected = new ArrayList<>();//expected result
expected.add( new int[]{2,1});
expected.add( new int[]{0,0});
expected.add( new int[]{0,2});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 2 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 0);
expected = new ArrayList<>();
expected.add( new int[]{2,0});
expected.add( new int[]{1,0});
expected.add( new int[]{0,2});
expected.add( new int[]{0,1});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 3 :" );
}
fish = new int[][]{{0,0,0},{0,0,0},{0,0,0}};
sharks = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
positions = WaTor.unoccupiedPositions( fish, sharks, 1, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 4 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 5 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 0);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 6 :" );
}
fish = new int[][]{{0,0,0},{-1,0,0},{0,0,0}};
sharks = new int[][]{{-1,-1,-1},{-1,-1,-1},{-1,-1,-1}};
positions = WaTor.unoccupiedPositions( fish, sharks, 1, 1);
expected = new ArrayList<>();
expected.add( new int[]{1,0});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 7 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 8 :" );
}
positions = WaTor.unoccupiedPositions( fish, sharks, 0, 0);
expected = new ArrayList<>();
expected.add( new int[]{1,0});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testUnoccupiedPositions 9 :" );
}
if ( error) {
System.err.println("testUnoccupiedPositions failed");
} else {
System.out.println("testUnoccupiedPositions passed");
}
}
//Checks the immediate surrounding coordinates of a selected row and col that contains a fish to make sure the surrounding location is precise.
private static void testFishPositions() {
boolean error = false;
int [][]fish = new int[][]{{-1,-1,-1},{-1,0,-1},{-1,-1,-1}};
ArrayList<int[]> positions = WaTor.fishPositions(fish, 1, 1);
ArrayList<int[]>expected = new ArrayList<>();
expected.add( new int[]{0,1});
expected.add( new int[]{2,1});
expected.add( new int[]{1,0});
expected.add( new int[]{1,2});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 1 :" );
}
positions = WaTor.fishPositions( fish, 0, 1);
expected = new ArrayList<>();
expected.add( new int[]{2,1});
expected.add( new int[]{0,0});
expected.add( new int[]{0,2});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 2 :" );
}
positions = WaTor.fishPositions(fish, 0, 0);
expected = new ArrayList<>();
expected.add( new int[]{2,0});
expected.add( new int[]{1,0});
expected.add( new int[]{0,2});
expected.add( new int[]{0,1});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 3 :" );
}
fish = new int[][]{{0,0,0},{0,0,0},{0,0,0}};
positions = WaTor.fishPositions(fish, 1, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 4 :" );
}
positions = WaTor.fishPositions( fish, 0, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 5 :" );
}
positions = WaTor.fishPositions( fish, 0, 0);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 6 :" );
}
fish = new int[][]{{0,0,0},{-1,0,0},{0,0,0}};
positions = WaTor.fishPositions(fish, 1, 1);
expected = new ArrayList<>();
expected.add(new int[]{1,0});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 7 :" );
}
positions = WaTor.fishPositions(fish, 0, 1);
expected = new ArrayList<>();
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 8 :" );
}
positions = WaTor.fishPositions( fish, 0, 0);
expected = new ArrayList<>();
expected.add( new int[]{1,0});
if ( !matchingArrayLists( expected, positions)) {
error = true;
System.err.println("testFishPositions 9 :" );
}
if (error) {
System.err.println("testFishPositions failed");
} else {
System.out.println("testFishPositions passed");
}
//TODO Implement in Milestone 2
}
// Checks the neighbor ArrrayList for different situations.
private static void testChooseMove() {
boolean error = false;
Random randGen = new Random();
randGen.setSeed( 456);
ArrayList<int[]> input = new ArrayList<>();
int [] expected = null;
int [] result = WaTor.chooseMove( input, randGen);
if ( result != expected) {
error = true;
System.err.println("testChooseMove 0: result not null");
}
input.clear();
int [] int[] {0,1};
input.add(oneMove);
expected = oneMove;
result = WaTor.chooseMove( input, randGen);
if ( result != expected) {
error = true;
System.err.println("testChooseMove 1: result not " + Arrays.toString( oneMove));
}
input.clear();
int [] move1 = new int[] {0,1};
int [] move2 = new int[] {1,0};
input.add( move1);
input.add( move2);
int move1Count = 0;
int move2Count = 0;
int numTrials = 1000;
for ( int i = 0; i < numTrials; i++) {
result = WaTor.chooseMove( input, randGen);
if ( result == move1) move1Count++;
else if ( result == move2) move2Count++;
}
if ( move1Count != 513 || move2Count != 487 ) {
error = true;
System.err.println("testChooseMove 2: expected 513,487 move1Count=" + move1Count + " move2Count=" + move2Count);
}
input.clear();
move1 = new int[] {0,1};
move2 = new int[] {1,0};
int[] move3 = new int[] {2,1};
input.add( move1);
input.add( move2);
input.add( move3);
move1Count = 0;
move2Count = 0;
int move3Count = 0;
numTrials = 1000;
for ( int i = 0; i < numTrials; i++) {
result = WaTor.chooseMove( input, randGen);
if ( result == move1) move1Count++;
else if ( result == move2) move2Count++;
else if ( result == move3) move3Count++;
}
if ( move1Count != 325 || move2Count != 341 || move3Count != 334 ) {
error = true;
System.err.println("testChooseMove 3: expected 325,341,334 move1Count=" + move1Count + " move2Count=" + move2Count + " move3Count=" + move3Count);
}
input.clear();
move1 = new int[] {0,1};
move2 = new int[] {1,0};
move3 = new int[] {2,1};
int []move4 = new int[] {1,2};
input.add( move1);
input.add( move2);
input.add( move3);
input.add( move4);
move1Count = 0;
move2Count = 0;
move3Count = 0;
int move4Count = 0;
numTrials = 1000;
for ( int i = 0; i < numTrials; i++) {
result = WaTor.chooseMove( input, randGen);
if ( result == move1) move1Count++;
else if ( result == move2) move2Count++;
else if ( result == move3) move3Count++;
else if ( result == move4) move4Count++;
}
if ( move1Count != 273 || move2Count != 231 || move3Count != 234 || move4Count != 262 ) {
error = true;
System.err.println("testChooseMove 4: expected 325,341,334,262 move1Count=" + move1Count + " move2Count=" + move2Count + " move3Count=" + move3Count + " move4Count=" + move4Count);
}
if ( error) {
System.err.println("testChooseMove failed");
} else {
System.out.println("testChooseMove passed");
}
}
/// Checks to see if the elements of the array moves all have the value of false.
private static void testClearMoves() {
boolean error = false;
boolean [][] moves = new boolean[4][9];
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
moves[row][col] = true;
}
}
WaTor.clearMoves(moves);
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
if ( moves[row][col]) {
System.err.println("testClearMoves 0: move " + row + "," + col + " not false");
error = true;
break;
}
}
}
if ( error) {
System.err.println("testClearMoves failed");
} else {
System.out.println("testClearMoves passed");
}
}
private static void testEmptyArray() {
boolean error = false;
int [][] moves = new int[100][99];
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
moves[row][col] = Config.EMPTY + 2;
}
}
WaTor.emptyArray(moves);
for (int row = 0; row < moves.length; row++) {
for (int col = 0; col < moves[row].length; col++) {
if ( moves[row][col] != Config.EMPTY) {
System.err.println("testEmptyArray 0: move " + row + "," + col + " not EMPTY");
error = true;
break;
}
}
}
if ( error) {
System.err.println("testEmptyArray failed");
} else {
System.out.println("testEmptyArray passed");
}
}
//Counts the number of non config.empty characters.
private static void testCountCreatures() {
boolean error = false;
int[][] fish = new int[7][3];
WaTor.emptyArray(fish);
fish[0][0] = 1;
fish[6][2] = 2;
fish[0][2] = 3;
fish[6][0] = 4;
int result = WaTor.countCreatures( fish);
if ( result != 4) {
System.err.println("testCountCreatures 0: expected 4 found " + result );
error = true;
}
if ( error) {
System.err.println("testCountCreatures failed");
} else {
System.out.println("testCountCreatures passed");
}
}
}
..........................................................................................
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
//FIXME class header comment
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;
System.out.println("Welcome to Wa-Tor");
char userInput;
String fileName;
System.out.print("Do you want to load simulation parameters from a file (y/n): ");
userInput = input.nextLine().charAt(0);
if (userInput == 'y' || userInput == 'Y') {
System.out.print("Enter filename to load: ");
fileName = input.nextLine();
loadSimulationParameters(fileName);
}
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")];
// 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)
emptyArray(fish);
emptyArray(sharks);
emptyArray(starve);
// place the initial fish and sharks and print out the number
int numFish = placeFish(fish, startingFish, fishBreed, randGen);
System.out.println("Placed " + numFish + " fish.");
int numSharks = placeSharks(fish, sharks, startingSharks, sharksBreed, randGen);
System.out.println("Placed " + numSharks + " sharks.");
int currentChronon = 1;
ArrayList<int[]> history = new ArrayList<int[]>(); // The ArrayList
// simulation ends when no more sharks or fish remain
boolean Endcondition = (numFish <= 0 || numSharks <= 0);
while (!Endcondition) {
// print out the locations of the fish and sharks
showFishAndSharks(currentChronon, fish, sharks);
System.out.println("fish:" + numFish + " sharks:" + numSharks);
System.out.print("Press Enter, # of chronon, or 'end': ");
String response = input.nextLine().trim();
if (response.equalsIgnoreCase("end")) {
break; // leave simulation loop
}
int numChronon = 1;
try {
numChronon = Integer.parseInt(response);
} catch (Exception e) {
numChronon = 1;
}
for (int counter = 0; counter < numChronon; counter++) {
if (numSharks <= 0 || numFish <= 0) {
break;
}
// clear fishMoved and sharksMoved from previous chronon
// TODO Milestone 1
clearMoves(fishMoved);
clearMoves(sharksMoved);
// call fishSwimAndBreed
int fswimandbreed = fishSwimAndBreed(fish, sharks, fishMoved, fishBreed, randGen);
// TODO Milestone 2
// call sharksHuntAndBreed
int shuntandbreed = sharksHuntAndBreed(fish, sharks, fishMoved, sharksMoved, sharksBreed, starve,
sharksStarve, randGen);
int[] historyValues = new int[Config.NUM_HISTORY_ITEMS];
historyValues[Config.HISTORY_CHRONON_INDEX] = currentChronon;
historyValues[Config.HISTORY_NUM_FISH_INDEX] = numFish;
historyValues[Config.HISTORY_NUM_SHARKS_INDEX] = numSharks;
history.add(historyValues);
// increment current chronon and count the current number of
// fish and sharks
currentChronon++;
numFish = countCreatures(fish);
numSharks = countCreatures(sharks);
// if all the fish or sharks are gone then end simulation
Endcondition = numFish <= 0 || numSharks <= 0;
}
}
;
int[] finalhistoryvalue = new int[Config.NUM_HISTORY_ITEMS];
finalhistoryvalue[Config.HISTORY_CHRONON_INDEX] = currentChronon;
finalhistoryvalue[Config.HISTORY_NUM_FISH_INDEX] = numFish;
finalhistoryvalue[Config.HISTORY_NUM_SHARKS_INDEX] = numSharks;
history.add(finalhistoryvalue);
// print the final ocean contents
showFishAndSharks(currentChronon, fish, sharks);
System.out.println("fish:" + numFish + " sharks:" + numSharks);
// 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.");
}
boolean errorCondition = false;
do {
System.out.print("Save simulation parameters (y/n): ");
userInput = input.nextLine().charAt(0);
if (userInput == 'y' || userInput == 'Y') {
System.out.print("Enter filename: ");
String filename = input.nextLine().trim();
try {
saveSimulationParameters(simulationParameters, filename);
errorCondition = false;
} catch (Exception e) {
errorCondition = true;
System.out.print("Unable to save to: " + filename);
}
} else {
errorCondition = false;
}
} while (errorCondition == true);
boolean errorCondition2 = false;
do {
System.out.print("Save population chart (y/n): ");
userInput = input.nextLine().charAt(0);
if (userInput == 'y' || userInput == 'Y') {
System.out.print("Enter filename: ");
String filename = input.nextLine();
try {
savePopulationChart(simulationParameters, history, oceanWidth, oceanHeight, filename);
errorCondition2 = false;
} catch (Exception e) {
errorCondition2 = true;
System.out.print("Unable to save to: " + filename);
}
} else {
errorCondition2 = false;
}
} while (errorCondition2 == true);
input.close();
}
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]++; // increments the age of the fish
fishMove[row][col] = true;
}
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);
}
// moves fish from 1 set of coords to another
fish[toRow][toCol] = fish[fromRow][fromCol] + 1; // increments the fish
// age
fishMove[toRow][toCol] = true;
// sets prev location to false as to clear it
fish[fromRow][fromCol] = Config.EMPTY;
fishMove[fromRow][fromCol] = false;
}
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 to new locatoin while setting age to 0
fish[toRow][toCol] = 0;
fishMove[toRow][toCol] = true;
// breeds the fish
fish[fromRow][fromCol] = 0; // new fish at old coords
fishMove[fromRow][fromCol] = true;
}
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);
}
// shark starves and dies by clearing position
sharks[row][col] = Config.EMPTY;
starve[row][col] = Config.EMPTY;
sharksMove[row][col] = false;
}
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
starve[row][col]++; // increment time since last meal
sharksMove[row][col] = true;
}
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);
}
// moves position
sharks[toRow][toCol] = sharks[fromRow][fromCol] + 1;
sharksMove[toRow][toCol] = true;
starve[toRow][toCol] = starve[fromRow][fromCol] + 1;
// clears prev position
sharks[fromRow][fromCol] = Config.EMPTY;
sharksMove[fromRow][fromCol] = false;
starve[fromRow][fromCol] = 0;
}
public static void sharkMovesAndBreeds(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 and breeds ", fromRow, fromCol, toRow, toCol);
}
sharks[toRow][toCol] = 0;
sharks[fromRow][fromCol] = 0;
sharksMove[toRow][toCol] = true;
sharksMove[fromRow][fromCol] = true;
starve[toRow][toCol] = starve[fromRow][fromCol] + 1;
starve[fromRow][fromCol] = 0;
}
public static void sharkEatsFish(int[][] sharks, boolean[][] sharksMove, int[][] starve, int[][] fish,
boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) {
if (Config.DEBUG) {
System.out.printf("DEBUG shark moved from %d,%d and ate fish %d,%d ", fromRow, fromCol, toRow, toCol);
}
// eats fish
fish[toRow][toCol] = Config.EMPTY;
fishMove[toRow][toCol] = false;
// moves shark
sharks[toRow][toCol] = sharks[fromRow][fromCol] + 1;
sharksMove[toRow][toCol] = true;
starve[toRow][toCol] = starve[fromRow][fromCol] = 0;
// clear old coords
sharks[fromRow][fromCol] = Config.EMPTY;
sharksMove[fromRow][fromCol] = true;
starve[fromRow][fromCol] = 0;
}
public static void sharkEatsFishAndBreeds(int[][] sharks, boolean[][] sharksMove, int[][] starve, int[][] fish,
boolean[][] fishMove, int fromRow, int fromCol, int toRow, int toCol) {
if (Config.DEBUG) {
System.out.printf("DEBUG shark moved from %d,%d and ate fish %d,%d and breed ", fromRow, fromCol, toRow,
toCol);
}
// eat
fish[toRow][toCol] = Config.EMPTY;
fishMove[toRow][toCol] = false;
// move
sharks[toRow][toCol] = 0;
sharksMove[toRow][toCol] = true;
starve[toRow][toCol] = 0;
// breed
sharks[fromRow][fromCol] = 0;
sharksMove[fromRow][fromCol] = true;
starve[fromRow][fromCol] = 0;
}
public static void emptyArray(int[][] arr) {
if (arr == null) {
System.out.println("emptyArray arr is null");
return;
}
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
arr[row][col] = Config.EMPTY;
}
}
}
public static void clearMoves(boolean[][] arr) {
if (arr == null) {
System.out.println("clearMoves arr is null");
return;
}
for (int row = 0; row < arr.length; row++) {
for (int col = 0; col < arr[row].length; col++) {
arr[row][col] = false;
}
}
}
public static void showFishAndSharks(int chronon, int[][] fish, int[][] sharks) {
System.out.println("Chronon: " + chronon);
int fishcount = 0;
int sharkcount = 0;
for (int i = 0; i < fish.length; i++) {
for (int j = 0; j < fish[0].length; j++) {
if (fish[i][j] != Config.EMPTY) {
System.out.print(Config.FISH_MARK + " ");
fishcount++;
} else if (sharks[i][j] != Config.EMPTY) {
System.out.print(Config.SHARK_MARK + " ");
sharkcount++;
} else
System.out.print(Config.WATER_MARK + " ");
}
System.out.println();
}
}
public static int placeFish(int[][] fish, int startingFish, int fishBreed, Random randGen) {
int numFishPlaced = 0;
for (int placeAttempts = 1; placeAttempts <= Config.MAX_PLACE_ATTEMPTS; placeAttempts++) {
int xcoord = randGen.nextInt(fish.length);
int ycoord = randGen.nextInt(fish[0].length);
if (fish[xcoord][ycoord] == Config.EMPTY) {
fish[xcoord][ycoord] = randGen.nextInt(fishBreed + 1);
numFishPlaced++;
if (placeAttempts == Config.MAX_PLACE_ATTEMPTS)
break;
else
placeAttempts = 0;
}
if (numFishPlaced == startingFish)
break;
else
continue;
}
return numFishPlaced;
}
public static int placeSharks(int[][] fish, int[][] sharks, int startingSharks, int sharksBreed, Random randGen) {
int numSharksPlaced = 0;
for (int placeAttempts = 1; placeAttempts <= Config.MAX_PLACE_ATTEMPTS; placeAttempts++) {
int ycoord = randGen.nextInt(sharks.length);
int xcoord = randGen.nextInt(sharks[0].length);
if (Config.DEBUG) {
System.out.println("DEBUG: placeSharks location " + ycoord + ", " + xcoord);
}
if ((sharks[ycoord][xcoord] == Config.EMPTY) && (fish[ycoord][xcoord] == Config.EMPTY)) {
sharks[ycoord][xcoord] = randGen.nextInt(sharksBreed + 1);
numSharksPlaced++;
if (placeAttempts == Config.MAX_PLACE_ATTEMPTS)
break;
else
placeAttempts = 0;
}
if (numSharksPlaced == startingSharks)
return numSharksPlaced;
else
continue;
}
return numSharksPlaced;
}
public static int countCreatures(int[][] fishOrSharks) {
int numCreatures = 0;
for (int y = 0; y < fishOrSharks.length; y++) {
for (int x = 0; x < fishOrSharks[0].length; x++) {
if (fishOrSharks[y][x] != Config.EMPTY)
numCreatures++;
}
}
return numCreatures;
}
public static ArrayList<int[]> unoccupiedPositions(int[][] fish, int[][] sharks, int row, int col) {
ArrayList<int[]> unoccupied = new ArrayList<>();
int r = row;
int c = col;
// Check above
if (row - 1 < 0) {
r = sharks.length - 1;
c = col;
} else {
r = row - 1;
c = col;
}
if ((fish[r][c] == Config.EMPTY) && (sharks[r][c] == Config.EMPTY)) {
unoccupied.add(new int[] { r, c });
if (Config.DEBUG) {
System.out.println("DEBUG: Succesfully added to unPos up --> " + r + "," + c + ".");
}
}
// check below
if (row == sharks.length - 1) {
r = 0;
c = col;
} else {
r = row + 1;
c = col;
}
if ((fish[r][c] == Config.EMPTY) && (sharks[r][c] == Config.EMPTY)) {
unoccupied.add(new int[] { r, c });
if (Config.DEBUG) {
System.out.println("DEBUG: Succesfully added to unPos down --> " + r + "," + c + ".");
}
}
// check left
if (col - 1 < 0) {
r = row;
c = sharks[0].length - 1;
} else {
r = row;
c = col - 1;
}
if ((fish[r][c] == Config.EMPTY) && (sharks[r][c] == Config.EMPTY)) {
unoccupied.add(new int[] { r, c });
if (Config.DEBUG) {
System.out.println("DEBUG: Succesfully added to unPos left --> " + r + "," + c + ".");
}
}
// check right
if (col == sharks[0].length - 1) {
r = row;
c = 0;
} else {
r = row;
c = col + 1;
}
if ((fish[r][c] == Config.EMPTY) && (sharks[r][c] == Config.EMPTY)) {
unoccupied.add(new int[] { r, c });
if (Config.DEBUG) {
System.out.println("DEBUG: Succesfully added to unPos right --> " + r + "," + c + ".");
}
}
if (Config.DEBUG) {
System.out.print("DEBUG: Unoccupied for " + row + "," + col + " :");
for (int i = 0; i < unoccupied.size(); i++)
System.out.print(Arrays.toString(unoccupied.get(i)) + ", ");
System.out.println();
}
return unoccupied;
}
public static int[] chooseMove(ArrayList<int[]> neighbors, Random randGen) {
// make sure parameters are valid
if (neighbors.size() == 0)
return null;
else if (neighbors == null || randGen == null) {
System.err.println("Neighbor is empty");
return null;
} else if (neighbors.size() == 1) {
return neighbors.get(0);
} else {
// randomly pick a spot
int randInt = randGen.nextInt(neighbors.size());
return neighbors.get(randInt);
}
}
public static int fishSwimAndBreed(int[][] fish, int[][] sharks, boolean[][] fishMove, int fishBreed,
Random randGen) {
// make sure parameters are valid
if (fish == null || fish.length < 1 || fish[0].length < 1) {
System.err.println("fishSwimAndBreed Invalid fish array: Null or not at least 1 in each dimension.");
return -1;
}
if (sharks == null || sharks.length < 1 || sharks[0].length < 1) {
System.err.println("fishSwimAndBreed Invalid sharks array: Null or not at least 1 in each dimension.");
return -1;
}
if (fishMove == null) {
System.err.println("fishSwimAndBreed Invalid fishMove array: Null or not at least 1 in each dimension.");
return -1;
}
if (fishBreed < 0) {
System.err.println("fishSwimAndBreed Invalid fishBreed, fishBreed less than 0 ");
return -2;
}
if (randGen == null) {
System.err.println("fishSwimAndBreed Invalid randGen, randGen is null");
return -3;
}
int row = 0;
int col = 0;
ArrayList<int[]> unoccupied = new ArrayList<>();
int coords[];
for (row = 0; row < fish.length; row++) {
for (col = 0; col < fish[0].length; col++) {
if (!fishMove[row][col] && fish[row][col] != Config.EMPTY) {
unoccupied = unoccupiedPositions(fish, sharks, row, col);
coords = chooseMove(unoccupied, randGen);
if (coords == null) {
aFishStays(fish, fishMove, row, col);
if (Config.DEBUG)
showFishAndSharks(1, fish, sharks);
}
else if (fish[row][col] >= fishBreed) {
aFishMovesAndBreeds(fish, fishMove, row, col, coords[0], coords[1]);
} else // else just move
aFishMoves(fish, fishMove, row, col, coords[0], coords[1]);
if (Config.DEBUG)
showFishAndSharks(1, fish, sharks);
}
}
}
return 0;
}
public static ArrayList<int[]> fishPositions(int[][] fish, int row, int col) {
ArrayList<int[]> fishPositions = new ArrayList<>();
int fishrow;
int fishcol;
//check above
if (row - 1 < 0) {
fishrow = fish.length - 1;
fishcol = col;
} else {
fishrow = row - 1;
fishcol = col;
}
if (fish[fishrow][fishcol] != Config.EMPTY)
fishPositions.add(new int[] { fishrow, fishcol });
// check below
if (row == fish.length - 1) {
fishrow = 0;
fishcol = col;
} else {
fishrow = row + 1;
fishcol = col;
}
if (fish[fishrow][fishcol] != Config.EMPTY)
fishPositions.add(new int[] { fishrow, fishcol });
// check left
if (col - 1 < 0) {
fishrow = row;
fishcol = fish[0].length - 1;
} else {
fishrow = row;
fishcol = col - 1;
}
if (fish[fishrow][fishcol] != Config.EMPTY) {
fishPositions.add(new int[] { fishrow, fishcol });
if (Config.DEBUG) {
System.out.println("DEBUG: Succesfully added to fPos Left --> " + fishrow + "," + fishcol + ".");
}
}
// check right
if (col == fish[0].length - 1) {
fishrow = row;
fishcol = 0;
} else {
fishrow = row;
fishcol = col + 1;
}
if (fish[fishrow][fishcol] != Config.EMPTY) {
fishPositions.add(new int[] { fishrow, fishcol });
if (Config.DEBUG) {
System.out.println("DEBUG: Succesfully added to fPos Right --> " + fishrow + "," + fishcol + ".");
}
}
return fishPositions;
}
public static int sharksHuntAndBreed(int[][] fish, int[][] sharks, boolean[][] fishMove, boolean[][] sharksMove,
int sharksBreed, int[][] starve, int sharksStarve, Random randGen) {
//make sure parameters are valid
if (fish == null || fish.length < 1 || fish[0].length < 1) {
System.err.println("sharksHuntAndBreed Invalid fish array: Null or not at least 1 in each dimension.");
return -1;
}
if (sharks == null || sharks.length < 1 || sharks[0].length < 1) {
System.err.println("sharksHuntAndBreed Invalid sharks array: Null or not at least 1 in each dimension.");
return -1;
}
if (fishMove == null) {
System.err.println("sharksHuntAndBreed Invalid fishMove array: Null or not at least 1 in each dimension.");
return -1;
}
if (sharksMove == null) {
System.err
.println("sharksHuntAndBreed Invalid sharksMove array: Null or not at least 1 in each dimension.");
return -1;
}
if (sharksBreed < 0) {
System.err.println("sharksHuntAndBreed Invalid sharksBreed, sharksBreed less than 0 ");
return -2;
}
if (sharksStarve < 0) {
System.err.println("sharksHuntAndBreed Invalid sharksStarve, sharksStarve less than 0 ");
return -2;
}
if (randGen == null) {
System.err.println("sharksHuntAndBreed Invalid randGen, randGen is null");
return -3;
}
int row = 0;
int col = 0;
ArrayList<int[]> unoccupied = new ArrayList<>();
ArrayList<int[]> fishpositions = new ArrayList<>();
int coords[];
for (row = 0; row < fish.length; row++) {
for (col = 0; col < fish[0].length; col++) {
if ((!sharksMove[row][col]) && (sharks[row][col] != Config.EMPTY)) {
if (starve[row][col] >= sharksStarve)
sharkStarves(sharks, sharksMove, starve, row, col);
else {
fishpositions = fishPositions(fish, row, col);
if (fishpositions == null || (fishpositions.size() == 0)) {
unoccupied = unoccupiedPositions(fish, sharks, row, col);
coords = chooseMove(unoccupied, randGen);
if (coords == null)
sharkStays(sharks, sharksMove, starve, row, col);
else if (sharks[row][col] >= sharksBreed)
sharkMovesAndBreeds(sharks, sharksMove, starve, row, col, coords[0], coords[1]);
else// else move
sharkMoves(sharks, sharksMove, starve, row, col, coords[0], coords[1]);
} else {// if there is a fish
if (Config.DEBUG)
showFishAndSharks(1, fish, sharks);
coords = chooseMove(fishpositions, randGen);
if ((coords == null) || (coords.length == 0))
sharkStays(sharks, sharksMove, starve, row, col);
else if (sharks[row][col] >= sharksBreed)
sharkEatsFishAndBreeds(sharks, sharksMove, starve, fish, fishMove, row, col, coords[0],
coords[1]);
else// else move
sharkEatsFish(sharks, sharksMove, starve, fish, fishMove, row, col, coords[0], coords[1]);
}
}
}
}
}
return 0;
}
public static int indexForParam(String paramName) {
for (int i = 0; i < Config.SIM_PARAMS.length; i++) {
if (paramName.equalsIgnoreCase(Config.SIM_PARAMS[i])) {
return i;
}
}
return -1;
}
public static void saveSimulationParameters(int[] simulationParameters, String filename) throws IOException {
// create File and a file writer
File file = new File(filename);
FileWriter fwriter = new FileWriter(file);
for (int i = 0; i < simulationParameters.length; i++) {
int a = simulationParameters[i];
fwriter.write(Config.SIM_PARAMS[i] + "=" + a + " ");
}
fwriter.close();
}
public static int[] loadSimulationParameters(String filename) {
int[] parameters = null;
parameters = new int[15];
FileInputStream fileByteStream = null;
Scanner reader = null;
int i = 0;
String name;
try {
fileByteStream = new FileInputStream(filename);
reader = new Scanner(fileByteStream);
reader.useDelimiter("=");
for (i = 0; i < Config.SIM_PARAMS.length; i++) {
name = reader.nextLine();
parameters[i] = indexForParam(name);
reader.nextLine();
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + filename);
return null;
}
reader.close();
return parameters;
}
public static void savePopulationChart(int[] simulationParameters, ArrayList<int[]> history, int oceanWidth,
int oceanHeight, String filename) throws IOException {
//checks if paramaters are valid
if (history == null || simulationParameters == null) {
System.out.println("Error");
return;
}
int uSize = oceanWidth * oceanHeight / Config.POPULATION_CHART_WIDTH;//creates unitsize
if (uSize == 0)
uSize = 1;
File file = new File(filename);
FileWriter fwriter = new FileWriter(file);
PrintWriter pwriter = new PrintWriter(fwriter);
for (int i = 0; i < simulationParameters.length; i++) {
pwriter.println(Config.SIM_PARAMS[i] + "=" + simulationParameters[i]);
}
pwriter.println();
pwriter.println("Population Chart");
pwriter.println("Numbers of fish(" + Config.FISH_MARK + ") and sharks(" + Config.SHARK_MARK + ") in units of "
+ uSize + ".");
int fcount = 0;
int scount = 0;
for (int i = 0; i < history.size(); i++) {
pwriter.printf("F%3d", history.get(i)[Config.HISTORY_NUM_FISH_INDEX]);
pwriter.print(",");
pwriter.printf("S%3d", history.get(i)[Config.HISTORY_NUM_SHARKS_INDEX]);
pwriter.printf("%5d)", history.get(i)[Config.HISTORY_CHRONON_INDEX]);
if (history.get(i)[1] >= history.get(i)[2]) {
scount = (int) Math.ceil((double) history.get(i)[Config.HISTORY_NUM_SHARKS_INDEX] / uSize);
fcount = (int) Math.ceil((double) history.get(i)[Config.HISTORY_NUM_FISH_INDEX] / uSize) - scount;
if (Config.DEBUG) {
pwriter.print("SharkCount = " + scount + " FishCount = " + fcount);
}
for (int c = 0; c < scount; c++) {
pwriter.print(Config.SHARK_MARK);
}
for (int c = 0; c < fcount; c++) {
pwriter.print(Config.FISH_MARK);
}
} else {
fcount = (int) Math.ceil((double) history.get(i)[Config.HISTORY_NUM_FISH_INDEX] / uSize);
scount = (int) Math.ceil((double) history.get(i)[Config.HISTORY_NUM_SHARKS_INDEX] / uSize) - fcount;
if (Config.DEBUG) {
pwriter.print("SharkCount = " + scount + " FishCount = " + fcount);
}
for (int c = 0; c < fcount; c++) {
pwriter.print(Config.FISH_MARK);
}
for (int m = 0; m < scount; m++) {
pwriter.print(Config.SHARK_MARK);
}
}
for (int n = 0; n < Config.POPULATION_CHART_WIDTH - fcount - scount; n++) {
pwriter.print(" ");
}
pwriter.println();
}
pwriter.close();
}
}
--------------------------------------------------------------------------------------------------------------------------
public class Config {
// Character constants used to show what is in each ocean location.
final static char FISH_MARK = '.';
final static char SHARK_MARK = 'O';
final static char WATER_MARK = ' ';
// Constant to initialize all the empty locations in the fish shark and starve arrays.
final static int EMPTY = -1;
//maximum attempts to try to randomly place a fish or shark.
final static int MAX_PLACE_ATTEMPTS = 5;
//width of the population chart.
final static int POPULATION_CHART_WIDTH = 50;
//The number of history indexes and then the constants for the indexes into the history array for chronon, fish and shark numbers.
final static int NUM_HISTORY_ITEMS = 3;
final static int HISTORY_CHRONON_INDEX = 0;
final static int HISTORY_NUM_FISH_INDEX = 1;
final static int HISTORY_NUM_SHARKS_INDEX = 2;
// Names of the simulation parameters.
final static String [] SIM_PARAMS = {
"seed", "ocean_width", "ocean_height", "starting_fish", "starting_sharks",
"fish_breed", "sharks_breed", "sharks_starve" };
//Checked within if statements to see whether to print out debug information or not.
final static boolean DEBUG = false;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.