Specific array elements

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NaveedLife
Forum Newbie
Posts: 3
Joined: Sun Dec 05, 2010 9:44 pm

Specific array elements

Post by NaveedLife »

Hey guys, I am trying to figure out how I can access a specific element in the 2D array as I step through it, in reference to where I am at when stepping through. Here is the code I have and I just can't seem to get it. This is in PHP by the way. Thanks for any help and suggestions! :)

Code: Select all

for($actualRow = 0; $actualRow < $totalRows; ++$actualRow){
	for($actualCol = 0; $actualCol < $totalCols; ++$actualCol){
		
		/*$upCol += $actualCol;
		$upCol++;
		
		$upRow += $actualRow;
		$upRow++;
		
		$downCol = $actualCol;
		$downCol--;
		
		$downRow = $actualRow;
		$downRow--;*/
		
		
		
		if($actualGrid[$actualRow][$actualCol] == $bomb){
			if($actualRow == 1 && $actualCol == 10){ //top right				
				if($actualGrid[$actualRow][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow][$actualCol - 1] += 1;
				}
				
				if($actualGrid[$actualRow + 1][$actualCol] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol] += 1;
				}
				
				if($actualGrid[$actualRow + 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol - 1] += 1;
				}
			}
			
			elseif($actualRow == 1 && $actualCol != 1 && $actualCol != 10){ //top
				if($actualGrid[$actualRow - 1][$actualCol] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol] += 1; //adds to bottom
				}
				
				if($actualGrid[$actualRow + 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol + 1] += 1; //adds to bottom right
				}
				
				if($actualGrid[$actualRow + 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol - 1] += 1; //adds to bottom left
				}
			}
			
			elseif($actualRow == 1 && $actualCol == 1){ //top left
				if($actualGrid[$actualRow][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow][$actualCol + 1] += 1; //adds to right
				}
				
				if($actualGrid[$actualRow + 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol + 1] += 1; //adds to bottom right
				}
				
				if($actualGrid[$actualRow - 1][$actualCol] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol] += 1; //adds to bottom
				}
			}
			
			elseif($actualCol == 1 && $actualRow != 1 && $actualRow != 10){ //left
				if($actualGrid[$actualRow - 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol + 1] += 1; //adds to top right
				}
				
				if($actualGrid[$actualRow][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow][$actualCol + 1] += 1; //adds to right
				}
				
				if($actualGrid[$actualRow + 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol + 1] += 1; //adds to bottom right
				}
			}
			
			elseif($actualCol == 1 && $actualRow == 10){ //bottom left				
				if($actualGrid[$actualRow + 1][$actualCol] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol] += 1; //adds to top
				}
				
				if($actualGrid[$actualRow - 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol + 1] += 1; //adds to top right
				}
				
				if($actualGrid[$actualRow][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow][$actualCol + 1] += 1; //adds to right
				}
			} 
			
			elseif($actualRow == 10 && $actualCol != 1 && $actualCol != 10){ //bottom
				if($actualGrid[$actualRow + 1][$actualCol] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol] += 1; //adds to top
				}
				
				if($actualGrid[$actualRow - 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol - 1] += 1; //adds to top left
				}
				
				if($actualGrid[$actualRow - 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol + 1] += 1; //adds to top right
				}
			}
			
			elseif($actualRow == 10 && $actualCol == 10){ //bottom right
				if($actualGrid[$actualRow][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow][$actualCol - 1] += 1; //adds to left
				}
				
				if($actualGrid[$actualRow + 1][$actualCol] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol] += 1; //adds to top
				}
				
				if($actualGrid[$actualRow - 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol - 1] += 1; //adds to top left
				}
			}
			
			elseif($actualCol == 10 && $actualRow != 1 && $actualRow != 10){ //right
				if($actualGrid[$actualRow][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow][$actualCol - 1] += 1; //adds to left
				}
				
				if($actualGrid[$actualRow + 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol - 1] += 1; //adds to bottom left
				}
				
				if($actualGrid[$actualRow - 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol - 1] += 1; //adds to top left
				}
			}
			
			else{ //middle
				if($actualGrid[$actualRow + 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol + 1] += 1; //adds to bottom right
				}
				
				if($actualGrid[$actualRow + 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow + 1][$actualCol - 1] += 1; //adds to bottom left
				}
				
				if($actualGrid[$actualRow - 1][$actualCol - 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol - 1] += 1; //adds to top left
				}
				
				if($actualGrid[$actualRow - 1][$actualCol + 1] != $bomb){
					$actualGrid[$actualRow - 1][$actualCol + 1] += 1; //adds to top right
				}
				
				if($actualGrid[$actualRow][$upCol] != $bomb){
					$actualGrid[$actualRow][$upCol] += 1; //adds to right
				}
				
				if($actualGrid[$actualRow][$downCol] != $bomb){
					$actualGrid[$actualRow][$downCol] += 1; //adds to left
				}
				
				if($actualGrid[$upRow][$actualCol] != $bomb){
					$actualGrid[$upRow][$actualCol] += 1; //adds to top
				}
				
				if($actualGrid[$downRow][$actualCol] != $bomb){
					$actualGrid[$downRow][$actualCol] += 1; //adds to bottom
				}
			}
		}
	}
}
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Specific array elements

Post by curlybracket »

Can you mark the problem in your code in some way?
NaveedLife
Forum Newbie
Posts: 3
Joined: Sun Dec 05, 2010 9:44 pm

Re: Specific array elements

Post by NaveedLife »

Sure, sorry if I am being vague.

if($actualRow == 1 && $actualCol == 10){ //top right
if($actualGrid[$actualRow][$actualCol - 1] != $bomb){
$actualGrid[$actualRow][$actualCol - 1] += 1;
}

Any section like this. Before I added the line:

if($actualGrid[$actualRow][$actualCol - 1] != $bomb){

to check if its a bomb before adding, it "worked". By that I mean it did add, but I think it was doing it all wrong. Once I added that line to check if its a bomb before adding, it didnt even make it into the case, meaning the if statements were always false.
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Specific array elements

Post by curlybracket »

But it should be quite simple to solve for you: you just need to do echo $actualGrid[$actualRow][$actualCol - 1] and echo $bomb to find out which of those values is not as you expect. Technically $actualGrid[$actualRow][$actualCol - 1] is ok so if you have any value inside you should get it in this way.
NaveedLife
Forum Newbie
Posts: 3
Joined: Sun Dec 05, 2010 9:44 pm

Re: Specific array elements

Post by NaveedLife »

Maybe I am misunderstanding what you are saying, but that doesn't seem right. This is going into a program for minesweeper and I am trying to pinpoint elements in the array depending on what element I am currently "on" as I step through the array. When I run the program (command line) it says "undefined offset". Thank you for the help regardless.
Post Reply