// Program Name:
MouseOnIsland.java
// Course:
CSE 1302J
// Student Name:
Bradley Shedd
// Assignment Number:
Project#2
// Due Date:
10/04/2010
// Purpose:
This program models an island with a mouse
//
on it. Also, it implements a random generator
//
for the bridge placements and mouse moves. It
//
also calls the print method to list how many
//
times the mouse dies(rose) or escapes(smily).
import
java.util.Random;
public
class MouseOnIsland
{
private
int[][] island =
new
int[10][10];
private
int count;
private
int mC = 1;//move
count
private
int dC = 0;//drown
count
private
int sC = 0;//starve
count
private
int eC = 0;//escape
count
Random generator =
new
Random();
public
MouseOnIsland(int[][] isl)
{
island = isl;
count = isl.length;
}
public
int getSC()//
gets starve count
{
return
sC;
}
public
int getDC()//
gets drown count
{
return
dC;
}
public
int getEC()//
gets escape count
{
return
eC;
}
public
void printList()
{
for
(int i = 0; i < island.length; i++)
{
for
(int j = 0; j < island[i].length;
j++)
{
if
(island[i][j] == -1 || island[i][j] == 99)
System.out.print(island[i][j] +
" | ");
else
if (island[i][j] >= 10)
System.out.print(island[i][j] +
" | ");
else
System.out.print("
" + island[i][j] +
" | ");
}
System.out.println();
}
}
public
void randomStart()//
where the mouse starts inside the board
{
int
x = (int)generator.nextInt(7) + 2;
int
y = (int)generator.nextInt(7) + 2;
island[x][y] = 1;
}
public
void
randomBridge()// location of the two
bridges
{
for
(int i =0; i < 2; i++)
{
int
x = (Math.abs(generator.nextInt())%9);
int
y = (Math.abs(generator.nextInt())%9);
island[x][y] = 99;
}
}
public
void MouseMov()//
mouse walks across the island
{
int
moveP=0;//states can't backtrack previous move
int
move = (Math.abs(generator.nextInt())%4+1);
for(int
k = 0; k < 20; k++)
{
for
(int i = 0; i < island.length; i++)
{
for
(int j = 0; j < island[i].length;
j++)
{
if
(island[i][j] == mC && mC <= 20)
{
move = (Math.abs(generator.nextInt())%4+1);
// mouse can't backtrack previous move
while
(move==moveP-1 || move==moveP+1)
{
move = (Math.abs(generator.nextInt())%4+1);
}
moveP=move;//mouse
can't backtrack
if
(move == 1)
{
if
(mC == 20)
{
// System.out.println("________________________" +
// "_________________________");
System.out.println("~~The mouse wandered"
+
"
around and starved!<--<@");
sC++;
mC = 21;
break;
}
else
if (island[i][j-1] == -1)
{
// System.out.println("________________________" +
//"_________________________");
System.out.println("~~The mouse drowned in" +
" the water!<--<@");
dC++;
mC++;
island[i][j-1] = mC;
mC = 21;
break;
}
else
if (island[i][j-1] == 99)
{
// System.out.println("________________________" +
// "_________________________");
System.out.println("~~The mouse found a
bridge" +
" and escaped! :) ");
eC++;
mC++;
island[i][j-1] = mC;
mC = 21;
break;
}
else
{
mC++;
island[i][j-1] = mC;
}
}
else
if (move == 2)
{
if
(mC == 20)
{
//System.out.println("________________________" +
// "_________________________");
System.out.println("~~The mouse wandered"
+
" around and starved!<--<@");
sC++;
mC = 21;
break;
}
else
if (island[i][j+1] == -1)
{
//
System.out.println("________________________" +
// "_________________________");
System.out.println("~~The mouse drowned in" +
" the
water!<--<@");
dC++;
mC++;
island[i][j+1] = mC;
mC = 21;
break;
}
else
if (island[i][j+1] == 99)
{
// System.out.println("________________________" +
// "_________________________");
System.out.println("~~The mouse found a
bridge" +
" and escaped! :) ");
eC++;
mC++;
island[i][j+1] = mC;
mC = 21;
break;
}
else
{
mC++;
island[i][j+1] = mC;
}
}
else
if (move == 3)
{
if
(mC == 20)
{
// System.out.println("________________________" +
//
"_________________________");
System.out.println("~~The
mouse wandered" +
" around and starved!<--<@");
sC++;
mC = 21;
break;
}
else
if (island[i-1][j] == -1)
{
//
System.out.println("________________________" +
// "_________________________");
System.out.println("~~The mouse drowned in" +
" the water!<--<@");
dC++;
mC++;
island[i-1][j] = mC;
mC = 21;
break;
}
else
if (island[i-1][j] == 99)
{
//
System.out.println("________________________" +
// "_________________________");
System.out.println("~~The
mouse found a bridge" +
" and escaped! :) ");
eC++;
mC++;
island[i-1][j] = mC;
mC = 21;
break;
}
else
{
mC++;
island[i-1][j] = mC;
}
}
else
{
if
(mC == 20)
{
//
System.out.println("________________________" +
//
"_________________________");
System.out.println("~~The
mouse wandered" +
" around and starved!<--<@");
sC++;
mC = 21;
break;
}
else
if (island[i+1][j] == -1)
{
//
System.out.println("________________________" +
// "_________________________");
System.out.println("~~The mouse drowned in" +
" the water!<--<@");
dC++;
mC++;
island[i+1][j] = mC;
mC = 21;
break;
}
else
if (island[i+1][j] == 99)
{
//
System.out.println("________________________" +
// "_________________________");
System.out.println("~~The
mouse found a bridge" +
" and escaped! :) ");
eC++;
mC++;
island[i+1][j] = mC;
mC = 21;
break;
}
else
{
mC++;
island[i+1][j] = mC;
}
}
}
}
}
}
}
}