Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can\'t seem to figure out how to scan the board as requested in the first step

ID: 3770202 • Letter: I

Question

I can't seem to figure out how to scan the board as requested in the first step of the instructions which is bolded below:

The purpose of the assignment is to finish two Java classes that implement the behavior of 1) a squirrel that is trying to evade terriers, and 2) a terrier that feels compelled to chase squirrels. The game is played on a board, with some number of terriers and squirrels whose placement is determined by a file. The board is a two-dimensional array, with 'S' for Squirrel, 'D' for Terrier, 'T' for Tree, 'F' for Fence, and '-' for empty squares with Grass.

The game engine reads the file specified on the command line, builds a user interface. Approximately every couple of seconds it asks each squirrel and terrier which way they want to move. You are going to code the behavior of both animals.

Terrier behavior is defined in Terrier.java. Terriers always try to chase after the nearest squirrel and eat it. Terriers must avoid running into other terriers or going off the board, and they cannot climb trees or pass through Fences.

Squirrel behavior is defined in Squirrel.java. Squirrels look for the closest terrier and always move in the opposite direction. Squirrel must avoid running into other squirrels or terriers, but they can pass through fences or climb trees. If they make it to a Tree, they are safe and disappear from the board.

The game continues until all squirrels are safe or eaten, or for 30 iterations, whichever comes first. None of the supplied fields require that many iterations. The game engine reports all movements and significant events.

Write the findClosest method to find which Terrier is the closest. To do so you must scan the board from top to bottom and left to right, computing the Euclidean distance to each Terrier that you find. The Euclidean distance calculation is shown here.

Store the row and column of the closest terrier so that you can return it when getClosestRow and getClosestCol are called.

If more than one Terriers are at an equal distance, store the row and column for the first Terrier you find.

Copy the current position to the previous position.

Here is the code I have so far:

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.Math;
import java.util.Scanner;

public class Squirrel implements AnimalInterface {

//
// DO NOT MODIFY BELOW
//
private int currentRow;
private int currentCol;
private int previousRow = -1;
private int previousCol = -1;
private int closestRow;
private int closestCol;
private char[][] field;

// Initializes position and field
public Squirrel(int row, int col, char[][] field){
this.currentRow = row;
this.currentCol = col;
this.field = field;
}

// Getters
public int getCurrentRow(){ return currentRow; }
public int getCurrentCol(){ return currentCol; }
public int getPreviousRow(){ return previousRow; }
public int getPreviousCol(){ return previousCol; }
public int getClosestRow(){ return closestRow; }
public int getClosestCol(){ return closestCol; }
//
// DO NOT MODIFY ABOVE
//

// Find closest terrier
public void findClosest(){
   int rows = 0; int col = 0;
   rows = field[0][0]; col = field[1][0];
  
// TO DO: Replace with code to find closest
closestRow = -1;
closestCol = -1;

}
  
// Move squirrel according to the rules
public void moveAnimal() {

eMove move;

// Store previous position
previousRow = currentRow;
previousCol = currentCol;

// TO DO: replace with code to select move (Step 1)
move = eMove.RIGHT;

// TO DO: replace with code to adjust move (Step 2)
move = move;

// TO DO: replace with code to make move (Step 3)
currentCol++;
}
  
//
// Private Methods, if you need them
//

}

Terrier.java code:

import java.lang.Math;

public class Terrier implements AnimalInterface {

//
// DO NOT MODIFY BELOW
//
private int currentRow;
private int currentCol;
private int previousRow = -1;
private int previousCol = -1;
private int closestRow;
private int closestCol;
private char[][] field;

// Initializes position and field
public Terrier(int row, int col, char[][] field){
this.currentRow = row;
this.currentCol = col;
this.field = field;
}

// Getters
public int getCurrentRow(){ return currentRow; }
public int getCurrentCol(){ return currentCol; }
public int getPreviousRow(){ return previousRow; }
public int getPreviousCol(){ return previousCol; }
public int getClosestRow(){ return closestRow; }
public int getClosestCol(){ return closestCol; }
//
// DO NOT MODIFY ABOVE
//

// Find closest squirrel
public void findClosest(){

// TO DO: Replace with code to find closest
closestRow = -1;
closestCol = -1;

}
  
// Move terrier according to the rules
public void moveAnimal() {

eMove move;

// Store previous position
previousRow = currentRow;
previousCol = currentCol;

// TO DO: replace with code to select move (Step 1)
move = eMove.LEFT;

// TO DO: replace with code to adjust move (Step 2)
move = move;

// TO DO: replace with code to make move (Step 3)
currentCol--;
}
  
//
// Private Methods, if you need them
//

}

Thanks for any help!

Explanation / Answer

For Squirrel/Terrier same logic code applies. Here I am finding the distance of the current location of the animal with each and every location. For efficient finding, division the matrix into 4 parts from currentRow and currentCol

//For Squirrel

public void findClosest(){
   int rows = 0; int cols = 0;
// rows = field[0][0]; col = field[1][0];


// TO DO: Replace with code to find closest
closestRow = -1;
closestCol = -1;

rows = field.length;
cols = field[0].length;

//Traverse from the location of squirrel/Terrier
double dist = 0, newDist = 0;

for(int i = currentRow; i < rows; i++) {
   for(int j =0; j < cols; j++) {
            //if its 'D' for Terrier
           if(i != currentRow && j != currentCol && field[i][j] == 'D') {
               //find Euclidean distance
               newDist = Math.sqrt( Math.pow(currentRow - i, 2)+Math.pow(currentCol - j, 2));
               if(dist == 0) {
                   dist = newDist;
                   closestRow = i;
                   closestCol = j;
               } else if(newDist < dist) {
                   dist = newDist;
                   closestRow = i;
                   closestCol = j;
               }
              
           }
   }
}

}

//For Terrier

public void findClosest(){
   int rows = 0; int cols = 0;
// rows = field[0][0]; col = field[1][0];


// TO DO: Replace with code to find closest
closestRow = -1;
closestCol = -1;

rows = field.length;
cols = field[0].length;

//Traverse from the location of squirrel/Terrier
double dist = 0, newDist = 0;

for(int i = currentRow; i < rows; i++) {
   for(int j =0; j < cols; j++) {
            //if its 'D' for Terrier. 'S' fr squirrel
           if(i != currentRow && j != currentCol && field[i][j] == 'S') {
               //find Euclidean distance
               newDist = Math.sqrt( Math.pow(currentRow - i, 2)+Math.pow(currentCol - j, 2));
               if(dist == 0) {
                   dist = newDist;
                   closestRow = i;
                   closestCol = j;
               } else if(newDist < dist) {
                   dist = newDist;
                   closestRow = i;
                   closestCol = j;
               }
              
           }
   }
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote