In: Computer Science
Mice are often used in science experiments because both mice and humans are mammals. Because of this close relationship with humans and because they are easy to take care of and they have a high reproduction rate, they are used for research in genetics and other fields of study. The genome of the mouse has been sequenced and many mouse genes correspond to human genes. This project has a mouse running in a maze and your job is to create a path for the mouse to travel in the maze. Use a 2 dimensional array and start the mouse in location array[0][0]. The mouse must find its way to the opposite corner. Repeatedly get a random number representing one of 8 possible moves. A legal move is one that moves forward, does not run off the “edge” of the maze and does not land on a previous move. If the move is illegal the poor mouse must start over with location [0][0]. Going forward is defined as the sum of the two array indexes either increasing or staying the same. With each safe mouse move introduce a cat that may eat the mouse. The cat is a block of 4 maze locations forming a square. With each safe move made by the mouse create a cat. The random number generator generates a location that serves as the upper left corner location of the cat. The random number is dependent on the size and shape of the maze when the maze is first created. If the cat “catches” the mouse the mouse must begin again. Think through the operations I have described and make those operations methods that can be called in order to accomplish the cat aspect of the problem. I used four small methods to implement the cat part of the project. Allow the mouse to repeatedly run the maze and choose the size of the two-dimensional maze. The output consists of five numbers. 1) The first is the number of times the mouse must start over before he finds a path from beginning to the end, 2) the second is the number of times he falls off the maze 3) the third is the number of times the cat catches the mouse, 4) the number of times he goes backwards and 5) the number of times the mouse lands on a previous position of the path. Then print the array to the screen showing the path that was successful and the last cat position. The mouse path is numbered starting with one in the upper left corner and ending with the number of moves in the lower right corner. On my website is a file that shows what the output should look like.(HINT: use a two dimensional integer array, record the cat as 4 negative ones in the maze. When you print out the maze replace a -1 with the char ‘C’.) Further in the program when you know the number of rows and columns from the client an array declaration would look like this.
import java.util.Scanner;
public class TwoDementionalArrayExample
{
static Scanner scan = new Scanner(System.in);
public static void main(String[] args)
{
int rowNumber,
columnNumber;
String choice = "yes";
System.out.println("This program fills and prints a two-dimensional array.");
while(choice.equalsIgnoreCase("yes"))
{
System.out.println("Please enter the number of rows you would like.");
rowNumber = scan.nextInt();
System.out.println("Please enter the number of columns you would like.");
columnNumber = scan.nextInt();
int[][] array = new int[rowNumber][columnNumber];
readArray(array,rowNumber,columnNumber);
printArray(array, rowNumber, columnNumber);
System.out.println(" Would you like to create a new array?");
System.out.println(" Please choose yes/no");
scan.nextLine();//enter key
choice = scan.nextLine();
}
}
private static void readArray(int[][] array, introwNumber, int columnNumber)
{
//*** these two for loops fill the array
int move = 1;
for(int k = 0; k < rowNumber; k++)
{
for(int j = 0; j < columnNumber; j++)
{
array[k][j] = move;
move++;
}
}
}
private static void printArray(int[][] array, introwNumber, int columnNumber)
{
int move;
for(int k = 0; k < rowNumber; k++)
{
for(int j = 0; j < columnNumber; j++)
{
move =array[k][j];
if(move < 10)
System.out.print( " ");
System.out.print( move + " ");
move++;
}
System.out.println();
}
}
}
Using the DecimalFormat class found in Appendix 4 add commas to the output. Example: 1,564,678. Duplicate the output on my website.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR
YOU
//Java program to simulate a mouse maze experiment
import java.text.DecimalFormat;
import java.util.Random;
import java.util.Scanner;
class mouseExperiment {
private static final Scanner scan = new Scanner(System.in);
private static DecimalFormat decimalFormat = new DecimalFormat("#.##");
public static void main(String[] args) {
decimalFormat.setGroupingUsed(true);
decimalFormat.setGroupingSize(3);
int rowNumber, columnNumber;
String choice = "yes";
System.out.println(
"This program fills and prints a two-dimensional array."
);
while (choice.equalsIgnoreCase("yes")) {
System.out.println("Please enter the number of rows you would like.");
rowNumber = scan.nextInt();
System.out.println("Please enter the number of columns you would like.");
columnNumber = scan.nextInt();
performMaze(rowNumber, columnNumber);
System.out.println("Would you like to create a new array?");
System.out.println("Please choose yes/no");
scan.nextLine(); //enter key
choice = scan.nextLine();
}
}
//Everything related to maze happens here
private static void performMaze(int rowNumber, int columnNumber) {
//maze will be maintained so that it can be displayed afterwards
int maze[][] = new int[rowNumber][columnNumber];
//mouseVisitedPosition will contain the details of previous moves in every attempt
int mouseVisitedPosition[][] = new int[rowNumber][columnNumber];
//variables to store mouse info
int mouseRow = 0, mouseColumn = 0, mouseMove = 0;
//variable to store cat info
int catRow = 0, catColumn = 0;
//variable to store stats related to solving the maze
int attemptCount = 0;
int fallOffMazeCount = 0, goBackwardsCount = 0, landOnPreviousMoveCount =
0, gotKilledByCatCount = 0;
//boolean to check when to stop performing the operations
boolean mazeCompleted = false;
//This loop will be responsible for every attempt
while (!mazeCompleted) {
attemptCount++;
mouseRow = 0;
mouseColumn = 0;
mouseMove = 1;
maze = new int[rowNumber][columnNumber];
mouseVisitedPosition = new int[rowNumber][columnNumber];
//The first move at 0,0 is pre-made for every mouse
maze[0][0] = 1;
mouseVisitedPosition[0][0] = 2; // 2 means visited
//boolean to check whether mouse can still move in this attempt
boolean isMouseAlive = true;
//This loop will be responsible for every move in each attempt
while (isMouseAlive && !mazeCompleted) {
//moves will be arranged like
// 1 2 3
// 8 M 4
// 7 6 5
//Choose a random number to figure mouse move
Random random = new Random();
int randomMoveChooser = random.nextInt(8) + 1;
//The function generates random between 0 - bound-1 (both inclusive)
int newMouseRow = 0;
int newMouseColumn = 0;
//get a new mouse position with respect to the random number that came in the above process
//The orientation of moves is given above
switch (randomMoveChooser) {
case 1:
{
newMouseRow = mouseRow - 1;
newMouseColumn = mouseColumn - 1;
break;
}
case 2:
{
newMouseRow = mouseRow - 1;
newMouseColumn = mouseColumn;
break;
}
case 3:
{
newMouseRow = mouseRow - 1;
newMouseColumn = mouseColumn + 1;
break;
}
case 4:
{
newMouseRow = mouseRow;
newMouseColumn = mouseColumn + 1;
break;
}
case 5:
{
newMouseRow = mouseRow + 1;
newMouseColumn = mouseColumn + 1;
break;
}
case 6:
{
newMouseRow = mouseRow + 1;
newMouseColumn = mouseColumn;
break;
}
case 7:
{
newMouseRow = mouseRow + 1;
newMouseColumn = mouseColumn - 1;
break;
}
case 8:
{
newMouseRow = mouseRow;
newMouseColumn = mouseColumn - 1;
break;
}
}
//Introduce a cat spanning 4 blocks in a square
//it ranges 1 less than the limits as it accounts for width = 2
catRow = random.nextInt(rowNumber - 1);
catColumn = random.nextInt(columnNumber - 1);
if (
(newMouseRow < 0) ||
(newMouseRow >= rowNumber) ||
(newMouseColumn < 0) ||
(newMouseColumn >= columnNumber)
) {
//mouse has fallen off edge
fallOffMazeCount++;
isMouseAlive = false;
} else if (mouseVisitedPosition[newMouseRow][newMouseColumn] == 2) {
//Mouse has landed on a previous move
landOnPreviousMoveCount++;
isMouseAlive = false;
} else if ((newMouseColumn + newMouseRow) < (mouseColumn + mouseRow)) {
//Has Moved Backwards
goBackwardsCount++;
isMouseAlive = false;
} else if (
(catRow == newMouseRow && catColumn == newMouseColumn) ||
(catRow + 1 == newMouseRow && catColumn == newMouseColumn) ||
(catRow == newMouseRow && catColumn + 1 == newMouseColumn) ||
(catRow + 1 == newMouseRow && catColumn + 1 == newMouseColumn)
) {
//Cat has got the mouse
gotKilledByCatCount++;
isMouseAlive = false;
} else {
//the move is legal
mouseColumn = newMouseColumn;
mouseRow = newMouseRow;
mouseMove++;
maze[mouseRow][mouseColumn] = mouseMove;
mouseVisitedPosition[mouseRow][mouseColumn] = 2;
//if mouse has reached the final block
//maze is completed and no new attempts are needed to be made
if (mouseRow == rowNumber - 1 && mouseColumn == columnNumber - 1) {
mazeCompleted = true;
}
}
}
}
//Setting last position of cat
//If at the cat position, a move was not played, only then display cat at that point
if (maze[catRow][catColumn] == 0) maze[catRow][catColumn] = -1;
if (maze[catRow + 1][catColumn] == 0) maze[catRow + 1][catColumn] = -1;
if (maze[catRow][catColumn + 1] == 0) maze[catRow][catColumn + 1] = -1;
if (maze[catRow + 1][catColumn + 1] == 0) maze[catRow + 1][catColumn + 1] =
-1;
//Printing the info
System.out.println(
"It took " +
decimalFormat.format(attemptCount) +
" attempts to find a path."
);
System.out.println(
"The cat got the mouse " +
decimalFormat.format(gotKilledByCatCount) +
" times."
);
System.out.println(
"The mouse fell off the maze " +
decimalFormat.format(fallOffMazeCount) +
" times."
);
System.out.println(
"The mouse went backwards " +
decimalFormat.format(goBackwardsCount) +
" times."
);
System.out.println(
"The mouse went to a former move " +
decimalFormat.format(landOnPreviousMoveCount) +
" times."
);
System.out.println();
for (int i = 0; i < rowNumber; i++) {
for (int j = 0; j < columnNumber; j++) {
int k = maze[i][j];
if (k == -1) System.out.print(" C "); else if (
k < 10
) System.out.print(" " + k + " "); else System.out.print(
" " + k + " "
);
}
System.out.println();
}
System.out.println();
}
}