Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 19th, 2004, 4:27 AM   #11
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,464
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 19th, 2004, 12:14 PM   #12
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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();
 }
 
 
	}
 
 
}
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Sep 19th, 2004, 12:16 PM   #13
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
For some reason the tabbing didn't work right when I posted. I''ll fix it later, but that's the gist of it.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Sep 19th, 2004, 11:17 PM   #14
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,464
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Sep 25th, 2004, 11:38 PM   #15
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
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.
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Sep 26th, 2004, 9:39 PM   #16
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,464
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:51 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC