Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fishy problem

This topic is 5650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ok i am making a fish program that displays 5 fish in a 2d environment (i already made the 1D fish one) (also this is not graphics, just text, cout and such). The fish can move left, right, up, and down, as long as nothing is blocking it (ie another fish, or wall). So, i have one fish moving in an environment and when i get that fish moving without fault i will add the other fish movments. The problem is that,usually, when the fish moves from 0,0 to anywhere, and then back to 0,0 it draws another fish somewhere else in the matrix. I cannot figure out why it does that. Thats the usual problem, sometimes it can do that movement and nothing happens. Also i cant figure out how to tell the fish if it is surrounded not to move, because it can either be surrounded by 4 fish, or the fish could be in the corner and surround by ownly 2 fish. Once i figure that out i can add the other fish. Im gonna give the source, its kinda big. But once i get this program working fine I plan on making an entirely new program fully user based, meaning they can change the size of the matrix, how many fish, and the starting fish positions.
    
void TwoDFish()
{
	int ntimes = 0,i,j,LorR,cmove = 0;  //LorR (left or right), cmove (cant move)

	int fish1row = 0;
	int fish1col = 0;
	int fish2row = 1;
	int fish2col = 1;
	int fish3row = 2;       //intializes each fish row and column

	int fish3col = 2;
	int fish4row = 3;
	int fish4col = 3;
	int fish5row = 4;
	int fish5col = 4;
	int temp1,temp2,temp3,temp4;
	//tells how many times the fish bumped a wall or another fish

	int fish1bump = 0,fish2bump = 0,fish3bump = 0,fish4bump = 0,fish5bump = 0;
	
		
	char fmatrix[5][5] = {
		{'F','X','X','X','X'},
		{'X','A','X','X','X'},
		{'X','X','B','X','X'},         ///shows the matrix

		{'X','X','X','C','X'},
		{'X','X','X','X','D'}
		};

//random numbers each time

	srand((unsigned)time(NULL));

	//initial matrix

	cout<<"Initial matrix";
	for(i=0; i<=4; i++)
	{ 
		cout<<"\n";
		for(j =0; j<=4; j++)
		{
			cout<<fmatrix[i][j];
		}
	}
cout<<"\n";
          //start loop

        do
		{
			for(i=0; i<=4; i++)
			{
				cout<<"\n";
				for(j=0; j<=4; j++)
				{

                 
					LorR = rand() % 4;  //random between 0(right) 1(left),2(up),3(down)

			//fish 1 loop

              for(i=0; i<=4; i++)
				   {
				for(j=0; j<=4; j++)
					{
                    cout <<"\n";
					//go right

					if(LorR == 0)
					{
                      //it went right

						cout <<"Fish 1 moved right!\n";

					   //if at the end of the matrix					

					  if(fmatrix[fish1row][fish1col] == fmatrix[fish1row][4])
					  {
						  fish1bump = fish1bump + 1;
						  fmatrix[fish1row][fish1col] = 'F';
						  fish1col = 4;
					  }
                      //if not at the end of the matrix perform the right switch

					  else
					  {
                          //make sure there is not a fish next to it, if so make fish do another move

						  if(fmatrix[fish1row][fish1col + 1] != 'X')
						  {
							  do
							  { 
								 LorR = rand() % 4;   //try to move a new direction

								 cmove = cmove + 1;   //add to cant move

							  }while(LorR = 0);
						  fish1bump = fish1bump + 1;
						  }
						  else
						  {
						  temp1 = fmatrix[fish1row][fish1col];
						  fmatrix[fish1row][fish1col + 1] = 'F';
						  fmatrix[fish1row][fish1col] =temp1;
						  fmatrix[fish1row][fish1col] = 'X';
						  fish1col = fish1col + 1;
						  
						  }
					  }
					ntimes = ntimes + 1;  //add number of rotations

					}//close big if

					//go left

					if(LorR == 1)
					{
					   //it went left

						cout <<"Fish 1 moved Left!\n";

						//check to see if at beg of matrix

						if(fmatrix[fish1row][fish1col] ==  fmatrix[fish1row][0])
					   {
						   fish1bump = fish1bump + 1;
						   fmatrix[fish1row][fish1col] = 'F';
						   fish1col = 0;
						}
						//if not at end of matrix perform the right switch

						else
						{
							//make sure there is not a fish next to it, if so make fish do another move

							if(fmatrix[fish1row][fish1col - 1] != 'X')
							{
								do
								{
								  	LorR = rand() % 4;   //try a new direction

									cmove = cmove + 1;  //add to cant move

								}while(LorR = 1);
							 fish1bump = fish1bump + 1;
							}
							//if not perform right switch

							else
							{
								temp1 = fmatrix[fish1row][fish1col];
								fmatrix[fish1row][fish1col - 1] = 'F';
								fmatrix[fish1row][fish1col] = temp1;
								fmatrix[fish1row][fish1col] = 'X';
								fish1col = fish1col + 1;
							
							}
						}
					ntimes = ntimes + 1;   //add to number of rotations

					}//close big if

				//go up		

				if(LorR == 2)
				   {
					   //it went up

						cout <<"Fish 1 moved up!\n";

					    //check to see if at top of matrix

					   	if(fmatrix[fish1row][fish1col] ==  fmatrix[0][fish1col])
						{
						   fish1bump = fish1bump + 1;
						   fmatrix[fish1row][fish1col] = 'F';
						   fish1row = 0;
					   }
						//if not at top of matrix perform right switch

						else
						{
							//make sure ther is not a fish above it, if so make fish do another move

							if(fmatrix[fish1row - 1][fish1col]  != 'X')
							{
								do
								{
									LorR = rand() % 4;   //try a new direction

									cmove = cmove + 1;     //add to cant move

								}while (LorR = 2);
								fish1bump = fish1bump + 1;
							}
							//if not perform right switch

							else
							{
								temp1 = fmatrix[fish1row][fish1col];
								fmatrix[fish1row - 1][fish1col] = 'F';
								fmatrix[fish1row][fish1col] = temp1;
								fmatrix[fish1row][fish1col] = 'X';
								fish1row = fish1row - 1;
								  
							} 
						}
				ntimes = ntimes + 1;   //add to number of rotations

				}//close big if

				//go down

				  if(LorR == 3)
				   {
					  //it went down

						cout <<"Fish 1 moved Down!\n";
 
					  //check to see if at bottom of matrix

					   	if(fmatrix[fish1row][fish1col] ==  fmatrix[4][fish1col])
					   {
						   fish1bump = fish1bump + 1;
						   fmatrix[fish1row][fish1col] = 'F';
						   fish1row = 4;
					   }
				  	
				        //if not at bottom of matrix perform right switch

						else
						{
							//make sure ther is not a fish above it, if so make fish do another move

							if(fmatrix[fish1row + 1][fish1col]  != 'X')
							{
								do
								{
									LorR = rand() % 4;       //try a new direction

									cmove = cmove + 1;  //add to cant move

								}while (LorR = 3);
								fish1bump = fish1bump + 1;
							}
							//if not perform right switch

							else
							{
								temp1 = fmatrix[fish1row][fish1col];
								fmatrix[fish1row + 1][fish1col] = 'F';
								fmatrix[fish1row][fish1col] = temp1;
								fmatrix[fish1row][fish1col] = 'X';
								fish1row = fish1row + 1;
						  
							}

						}
				  ntimes = ntimes + 1;  //add to number of rotations

				  }//close big if


				  //if fish cant move stay where it is

				  if(cmove == 4)
				  {
					  fmatrix[fish1row][fish1col] = fmatrix[fish1row][fish1col];
				  }
              
                     //show matrix

				  for(i = 0; i<=4; i++)
				  {
					  cout<<"\n";
					  for(j = 0; j<=4; j++)
					  {
						  cout<<fmatrix[i][j];
					  }
				  }
		 
          }//close for 2 i loop

          }// close for 2 j loop

	}//close for big i loop

}//close for big j loop

}while(ntimes != 10);

}
   
[edited by - Nothingness on December 1, 2002 6:00:25 PM]

Share this post


Link to post
Share on other sites
Advertisement
Ok, first off, don't base the fish positions off of the matrix. Group each fish into a struct at least, class at best, and use an array, as opposed to variables for each fish. Maybe even a vector, although that may be a little advanced.

I didn't check this for errors, but you get the idea...



struct FISH
{
int x; // instead of 'column'
int y; // instead of 'row'
int bumps; // # times fish has bumped into something
// any other members...
}



then later, in your code, you can go:



#define MAX_FISH 5

FISH fishies[MAX_FISH]



... and then to reference the fish, you just go:



fishies[4].x = 4;
fishies[4].y = 4;
.... etc, etc ...



Now if you wanted to do this really nice, you would make each fish a class, and include a collusion-testing member... but we'll keep it simple. Just have some sort of function like:



#define NORTH 0
#define EAST 1
#defi... etc.

bool Move(FISH &f1, int moveDirection, const FISH * const fishes, int maxFishes, int tankWidth, int tankHeight)
{
FISH f1move = f1; // where the fish will be if it moves
switch(moveDirection)
{
case NORTH:
f1move.x--;
break;
case EAST:
f1move.x++;
break;

... etc. etc. etc.
}

if((f1move.x > width) || (f1move.x > height) ||
(f1move.x < 0) || (f1move.x < 0))
return false;

for(int i = 0; i < maxFishes; i++)
{
if((f1move.X == fishes.x) && (f1move.Y == fishes.Y))
{
++f1.bumps; // fish bumped into another fish
return false;
}
}
f1 = f1move; // move ok
return true;
}



Anyway, hope that helps. When you render, copy the original, fishless matrix, and then on the copy, add the fish. That's the cleanest way to do it. Hope this helped you out. Keep in mind my code may need some tweaking, but it should give you some ideas.


[edited by - Beowulf_ on December 1, 2002 6:45:31 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!