![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Programming Guru
![]() ![]() ![]() |
I put a randomizer in the code to randomly go through the moves to give a sense of automation... but it is definitely a poor way to do it, that part could definitely use some work... but I think I've grown bored with it... so I will see what other people come up with.
Just to give you a few hints... make a struct to hold maze multi-dimensional array, location of mouse and cheese, etc. Here are some of my function declarations, this should give you an idea of whay you may need.: void ReadMaze (string mazeFile); void WriteMaze (string mazeFile, maze m); void FindRatAndCheese(string a[MAX][MAX]); void DisplayMaze (string a[MAX][MAX], int r, int c); void PopulateMaze(string a[MAX][MAX], int r, int c); int MoveMouse (string direction); int RandomRange(int lowest_number, int highest_number); void CopyArray(string source[MAX][MAX], string dest[MAX][MAX]); void PlayHuman (void); void PlayComputer (void);
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#12 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Here's my (almost) finalized code. It seems to be working. Thanks for all your help. All I have left to do is output to a file.
import java.util.*;
import java.io.*;
public class Mouse
{
public static void main(String[]args) throws IOException
{
Scanner keyboard = new Scanner(new File("PROG13.IN"));
int square = keyboard.nextInt();
boolean cheeseFound=false;
char bufferedMatrix [][] = new char[square][square];
keyboard.nextLine();
int rOfCheese=20000;
int cOfCheese=20000;
int finalValue = 0;
for(int r = 0; r<square; r++)
{
String [] buffer = keyboard.nextLine().split(" ");
for(int c = 0; c<square; c++)
{
bufferedMatrix[c][r] = buffer[c].charAt(0);
}
}
int matrix[][] = new int[square][square];
for(int r = 0; r<square; r++)
{
for(int c = 0; c<square; c++)
{
if(bufferedMatrix[c][r]=='#')
{
matrix[c][r] = -1;
}
else if(bufferedMatrix[c][r] == '.')
{
matrix[c][r] = 0;
}
else if(bufferedMatrix[c][r] == 'M')
{
matrix[c][r] = 1;
}
else if(bufferedMatrix[c][r] == 'C')
{
matrix[c][r] = 5000;
rOfCheese = r;
cOfCheese = c;
}
}
}
// System.out.println("1");
int currentValueToSearchFor = 1;
while(!cheeseFound)
{
for(int r = 0; r<square; r++)
{
for(int c = 0; c<square; c++)
{
if(matrix[c][r] == currentValueToSearchFor)
{
if(matrix[c+1][r] == 0)
{
matrix[c+1][r] = currentValueToSearchFor + 1;
}
if(matrix[c-1][r] == 0)
{
matrix[c-1][r] = currentValueToSearchFor + 1;
}
if(matrix[c][r+1] == 0)
{
matrix[c][r+1] = currentValueToSearchFor + 1;
}
if(matrix[c][r-1] == 0)
{
matrix[c][r-1] = currentValueToSearchFor + 1;
}
if(matrix[c][r+1] == 5000 || matrix[c][r-1] == 5000 || matrix[c+1][r] == 5000 ||
matrix[c-1][r] == 5000)
{
cheeseFound = true;
finalValue = currentValueToSearchFor;
}
}
}
}
currentValueToSearchFor++;
}
while(finalValue > 1)
{
if(matrix[cOfCheese+1][rOfCheese] == finalValue)
{
matrix[cOfCheese+1][rOfCheese] = 10000;
cOfCheese++;
}
if(matrix[cOfCheese-1][rOfCheese] == finalValue)
{
matrix[cOfCheese-1][rOfCheese] = 10000;
cOfCheese--;
}
if(matrix[cOfCheese][rOfCheese+1] == finalValue)
{
matrix[cOfCheese][rOfCheese+1] = 10000;
rOfCheese++;
}
if(matrix[cOfCheese][rOfCheese-1] == finalValue)
{
matrix[cOfCheese][rOfCheese-1] = 10000;
rOfCheese--;
}
finalValue--;
}
for(int r = 0; r<square; r++)
{
for(int c = 0; c<square; c++)
{
if(matrix[c][r] == 10000)
{
bufferedMatrix[c][r] = '+';
}
}
}
for(int r = 0; r<square; r++)
{
for(int c = 0; c<square; c++)
{
System.out.print(bufferedMatrix[c][r] + " ");
}
System.out.println();
}
}
}
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#13 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
For some reason the tabbing didn't work right when I posted. I''ll fix it later, but that's the gist of it.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#14 |
|
Programming Guru
![]() ![]() ![]() |
Ok. I'm retarded... I wrote the code in C++... forgot to look at the language type / thread section... ahhhh! I'll compile your code tomorrow to see it work... If time permits, I'm going to rewrite my version from C++ to Java... If you are still having difficulty, let me know.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#15 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
Well, just in case anyone wanted to know, I got a free 100 points on this project. I was also the first one to turn this in.
Thanks for your help again.
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#16 |
|
Programming Guru
![]() ![]() ![]() |
Good deal man, glad you got it working....
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|