In: Computer Science
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 :