Directions: Write a program that places a mouse(“M”) in a maze.
The mouse has 2500 moves to find the cheese(“C”= -1). Create an int
matrix x[55][95] with a border of cells all equal to 30000(=”X”).
Then randomly fill the matrix with about 15% of its internal cells
equal to 40000 (=”#”).
Drop the cheese( -1) randomly in the lower right-hand quadrant.
Place the mouse in the upper right-hand corner. All other cells have
a zero value(=” “).
As the mouse moves, it leaves a train of ever-increasing integers( =
“.”).
The mouse moves only up, down, left and right. The mouse does not
move diagonally.
When the mouse moves, it choses the adjacent cell with the
smallest number.
If there are two-or-more cells with the smallest number, then the
mouse favors the directions
in some particular order(right, down, left, up)
-----------------------------------------------------------------------------------------
dere was an approach to this but not completely accomplished
the program we've tried is as follows
----------------------------------------------------------------------------------------------
import java.lang.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class Mouse extends JFrame
{
int mr = 1, mc = 1;
int move = 1;
int cc = 73, cr = 20;
int MAXMOVE = 2500;
int RMAX = 55, CMAX = 95;
int[][] x = new int [CMAX][RMAX];
//-----------------------------------------------------------------------------------------------
static void pause(int num)
{
long start = System.currentTimeMillis();
while(System.currentTimeMillis()-start<num);
}
//-----------------------------------------------------------------------------------------------
void printArray(Graphics2D g)
{
for(int r=0; r<RMAX; r++)
{
for(int c=0; c<CMAX; c++)
{
if (x[c][r] == 0)
g.drawString(" ", c*10+20, r*12+50);
else if (x[c][r] == 30000)
g.drawString("X", c*10+20, r*12+50);
else if (x[c][r] == 40000)
g.drawString("#", c*10+20, r*12+50);
else if(x[c][r] == -1)
{
g.setColor(Color.red);
g.drawString("C", c*10+20, r*12+50);
g.setColor(Color.yellow);
}
else
g.drawString(".", c*10+20, r*12+50);
}
}
}
//--------------------------------------------------------------------------------------------------
void initializeArray(Graphics2D g)
{
}
//--------------------------------------------------------------------------------------------------
void search(Graphics2D g)
{
while(move<MAXMOVE)
{
if(x[mc][mr] == -1)
break;
g.drawString("M", mc*10+20, mr*12+50);
pause(30);
//here are directions for the “search” method:
// You are at position x[c][r] in matrix x. You must move to either
the left cell x[c-1][r],
// the right cell x[c+1][r], the upper cell x[c][r-1], or the lower
cell x[c][r+1].
// You must move to the cell that has the smallest number value s. If
s is in more than one location,
// then you will favor the directions in this order: right, down,
left, and up.
// Write the code to give c and r their new values.
//
//
//
//
}
}
//---------------------------------------------------------------------------------------------------
public void paint(Graphics g1)
{
Graphics2D g = (Graphics2D) g1;
g.clearRect(0,0,getWidth(),getHeight());
initializeArray (g);
printArray (g);
g.setColor(Color.red);
search(g);
}
//----------------------------------------------------------------------------------------------------
public static void main(String[]args) throws IOException
{
System.out.println(" main begun ");
Mouse frame = new Mouse();
frame.setBackground(new Color(0,0,150));
frame.setForeground(Color.yellow);
frame.setSize(1280, 980);
frame.setTitle("Graphics Window");
frame.setFont(new Font("SansSerif", Font.PLAIN, 12));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
System.out.println("paint launched.");
frame.setVisible(true);
}
}
-----------------------------------------------------------------------------------------
i would be thankful for your valuable suggestions and efforts...
u can mail me to my id
thank u..