Question

In: Computer Science

Mice are often used in science experiments because both mice and humans are mammals. Because of...

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, int rowNumber, 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, int rowNumber, 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();

}

}

}

Solutions

Expert Solution

Java code for the same has been provided below along with comments throughout for better understanding...

//Java program to simulate a mouse maze experiment

import java.util.Random;

import java.text.DecimalFormat;

import java.util.Scanner;

public class mouse_Exp

{

private static final Scanner sc = new Scanner(System.in);

private static DecimalFormat deci = new DecimalFormat("#.##");

public static void main(String[] args)

{

deci.setGroupingUsed(true);

deci.setGroupingSize(3);

int row_no, col_no;

String ch = "yes";

System.out.println("This program fills and prints a two-dimensional array.");

while(ch.equalsIgnoreCase("yes")) {

System.out.println("Please enter the number of rows you would like.");

row_no = scan.nextInt();

System.out.println("Please enter the number of columns you would like.");

col_no = scan.nextInt();

perform_Maze(row_no, col_no);

System.out.println("Would you like to create a new array?");

System.out.println("Plz choose yes/no");

sc.nextLine();

ch = sc.nextLine();

}

}

//Everything related to maze happens here

private static void perform_Maze(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 mouse_Row = 0, mouse_Column = 0, mouse_Move = 0;

//variable to store cat info

int cat_Row = 0, cat_Column = 0;

//variable to store stats related to solving the maze

int attempt_Count = 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)

{

attempt_Count ++;

mouse_Row = 0;

mouse_Column = 0;

mouse_Move = 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 = mouse_Row - 1;

newMouseColumn = mouse_Column - 1;

break;

}

case 2 : {

newMouseRow = mouse_Row - 1;

newMouseColumn = mouse_Column;

break;

}

case 3 : {

newMouseRow = mouse_Row - 1;

newMouseColumn = mouse_Column + 1;

break;

}

case 4 : {

newMouseRow = mouse_Row ;

newMouseColumn = mouse_Column + 1;

break;

}

case 5 : {

newMouseRow = mouse_Row + 1;

newMouseColumn = mouse_Column + 1;

break;

}

case 6 : {

newMouseRow = mouse_Row + 1;

newMouseColumn = mouse_Column;

break;

}

case 7 : {

newMouseRow = mouse_Row + 1;

newMouseColumn = mouse_Column - 1;

break;

}

case 8 : {

newMouseRow = mouse_Row;

newMouseColumn = mouse_Column - 1;

break;

}

}

//Introduce a cat spanning 4 blocks in a square

//it ranges 1 less than the limits as it accounts for width = 2

cat_Row = random.nextInt(rowNumber-1);

cat_Column = 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((cat_Row==newMouseRow && cat_Column==newMouseColumn) ||

(cat_Row+1==newMouseRow && cat_Column==newMouseColumn) ||

(cat_Row==newMouseRow && cat_Column+1==newMouseColumn) ||

(cat_Row+1==newMouseRow && cat_Column+1==newMouseColumn))

{

//Cat has got the mouse

gotKilledByCatCount++;

isMouseAlive = false;

}

else

{

//the move is legal

mouse_Column = newMouseColumn;

mouse_Row = newMouseRow;

mouse_Move++;

maze[mouse_Row][mouse_Column] = mouse_Move;

mouseVisitedPosition[mouse_Row][mouse_Column] = 2;

//if mouse has reached the final block

//maze is completed and no new attempts are needed to be made

if(mouse_Row == 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[cat_Row][cat_Column] == 0)

maze[cat_Row][cat_Column] = -1;

if(maze[cat_Row+1][cat_Column] == 0)

maze[cat_Row+1][cat_Column] = -1;

if(maze[cat_Row][cat_Column+1] == 0)

maze[cat_Row][cat_Column+1] = -1;

if(maze[cat_Row+1][cat_Column+1] == 0)

maze[cat_Row+1][cat_Column+1] = -1;

//Printing the info

System.out.println("It took "+decimalFormat.format(attempt_Count)+" 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();

}

}

Some sample outputs are here :


Related Solutions

Mice are often used in science experiments because both mice and humans are mammals. Because of...
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...
The amino acid glycine is often used as a component of buffers in biochemical experiments. The...
The amino acid glycine is often used as a component of buffers in biochemical experiments. The amino group of glycine has a pKa of 9.6, which can exist in the protonated form -NH3+ or the free base -NH2 as shown in this equilibrium:         R-NH3+ ⇌ R-NH2 + H+ (a) Using glycine as a buffering agent requires specific pH ranges. Identify the buffer range for glycine’s amine group and describe how this molecule provides buffer capabilities as small amounts...
1. In all mammals except for humans physical and chemical digestion begins where? Why are humans...
1. In all mammals except for humans physical and chemical digestion begins where? Why are humans the exception? 2. Write these in the order of innermost (touching digesting food) to outermost (touching the inside of your body) and provide a brief description of each: connective tissue, epithelium, layer of muscle (thin), layer of muscle (thick) submucosa, subserosa, serosa 3. What are segmentation, peristalsis and reverse peristalsis and what is each used for in the body
According to Zimmels (1983), the sizes of particles used in sedimentation experiments often have a uniform...
According to Zimmels (1983), the sizes of particles used in sedimentation experiments often have a uniform distribution. In sedimentation involving mixtures of particles of various sizes, the larger particles hinder the movements of the smaller ones. Thus, it is important to study both the mean and the variance of particle sizes. Suppose that spherical particles have diameters that are uniformly distributed between 0.02 and 0.07 centimeters. Find the mean and variance of the volumes of these particles. (Recall that the...
This video is a brief tale of two mice and two humans who live in a...
This video is a brief tale of two mice and two humans who live in a maze and one day are faced with change: someone moves their cheese. This story is about adjusting attitudes toward change in life, especially at work. Change occurs whether a person is ready or not, but the author affirms that it can be positive. His principles are to anticipate change, let go of the old, and do what you would do if you were not...
7) H-Y antigens are only found in cells of male mammals such as mice. Double positive...
7) H-Y antigens are only found in cells of male mammals such as mice. Double positive T cells with a transgenic TCR specific for H-Y (male) antigen on an MHC class I protein present in that given strain of mouse A. would develop normally in male and female mice     B. would develop into both CD4+ T cells and CD8+ T cells in female mice but would not develop into mature T cells in male mice.     C. would develop...
Humans are referred to as the “ideal mammal”. a) What features of mammals have made them...
Humans are referred to as the “ideal mammal”. a) What features of mammals have made them adaptable as a class? b) Which adaptations have humans created to the mammal plan to be called “ideal”? c) Compare development observed in the mammals to the Orthinodelphia and Metatheria. d) What does development of these groups tell one about their evolutionary relationships with one another & the other tetrapod groups?
Preferred stock is often referred to as hybrid security because it has many characteristics of both...
Preferred stock is often referred to as hybrid security because it has many characteristics of both common stock and bonds. What are the characteristics similar to common stocks; what are the characteristics similar to bonds? Explain.
Describe & explain & discuss experiments / observations in humans and monkeys that demonstrate the role...
Describe & explain & discuss experiments / observations in humans and monkeys that demonstrate the role of the parietal lobe in attention and attention disorders
The genetic code is (almost) universal, meaning that ribosomes from bacteria, fish, mice or humans will...
The genetic code is (almost) universal, meaning that ribosomes from bacteria, fish, mice or humans will translate a particular open reading frame into identical proteins. However, it is not possible to place a message from bacteria into a human cell (or vice versa) and expect to see a protein produced. Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT