dougal85 Posted April 26, 2006 Share Posted April 26, 2006 [!--fonto:Verdana--][span style=\"font-family:Verdana\"][!--/fonto--]Hi,[b]First[/b] post! :)I am having a problem with my PHP.I am trying to make a [b]8x8[/b] grid, with boolean values that i can then check and count how many true values are in each column, row and diagonal.At the moment i have managed to get the grid/matrix successfully and also i have managed to check the rows and columns. So thats all cool.However i am having trouble working out the best way to check the diagonals.I have noticed that there is a bit of a patern with them, for example some of them as you follow it through either the x or the y value is going down and the other is going up.I just dont know how best to put this into code.Any suggestions would be greatly appreciated!ThanksDougal[!--fontc--][/span][!--/fontc--] Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/ Share on other sites More sharing options...
ToonMariner Posted April 26, 2006 Share Posted April 26, 2006 to traverse a 2d array(must be a i x i array)[code]<?php$array = array(array(1,0,1,0),array(1,0,0,0),array(0,0,1,1),array(1,0,1,1);$arrsize = count($array[0]);$tlbrcount = NULL; var to store number of true values in tl - br diag$bltrcount = NULL; var to store number of true values in bl - tr diagfor ($i = 0; $i < $arrsize; $i++) {//tl - br loop $tlbrcont = $array[$i][$i] == true ? $tlbrcont + 1 : $tlbrcont;//bl - tr loop $bltrcont = $array[$i][($arrsize - 1) - $i] == true ? $bltrcont + 1 : $bltrcont;}?>[/code]There you go - took me 5 minutes to post that!!!!!!!! should start charging. Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30793 Share on other sites More sharing options...
dougal85 Posted April 26, 2006 Author Share Posted April 26, 2006 Cool! thats really good! thanksHowever, am i right in thinking thats the diagonals from the opposite corners.I also need to check the other diagonals in the array - they are whats getting me stuck the most!Any further suggestions? thats a cool start! thanksDougal Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30794 Share on other sites More sharing options...
ToonMariner Posted April 26, 2006 Share Posted April 26, 2006 so ALL diagonlas?-ie any diagonal starting from an element on the outside of the matrix?from top left to bottom right? or in all directions? Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30804 Share on other sites More sharing options...
dougal85 Posted April 26, 2006 Author Share Posted April 26, 2006 [!--quoteo(post=368698:date=Apr 26 2006, 03:04 AM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 26 2006, 03:04 AM) [snapback]368698[/snapback][/div][div class=\'quotemain\'][!--quotec--]so ALL diagonlas?-ie any diagonal starting from an element on the outside of the matrix?from top left to bottom right? or in all directions?[/quote]all directions.basically, items need to be placed into the matrix... however no more than two can be in any diagonal line same with horizontal and vertical.ie. this example has a diagonal with 30 0 1 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 1 0 00 0 0 0 0 0 1 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 0*EDIT* i realise that example is quite hard to see, but you should get the idea :) Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30890 Share on other sites More sharing options...
gerkintrigg Posted April 26, 2006 Share Posted April 26, 2006 Welcome to PHP freaks Dougal.From Neil The Runner Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30892 Share on other sites More sharing options...
samshel Posted April 26, 2006 Share Posted April 26, 2006 Hello,I am not sure if i understand it correctly. still no harm in trying....to check for any diagonal elements, you will have to navigate thru each element of the array and check diagonal elements for each.example : suppose the current array element u r checking is arr( x , y )the possible digonal elements are arr ( x-1, y-1 ) arr ( x+1, y-1 ) arr ( x-1, y+1 ) arr ( x+1, y+1 )out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonalshthsorry if i confused u more :) was just trying to help :) Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30897 Share on other sites More sharing options...
dougal85 Posted April 26, 2006 Author Share Posted April 26, 2006 [!--quoteo(post=368792:date=Apr 26 2006, 12:22 PM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ Apr 26 2006, 12:22 PM) [snapback]368792[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hello,I am not sure if i understand it correctly. still no harm in trying....to check for any diagonal elements, you will have to navigate thru each element of the array and check diagonal elements for each.example : suppose the current array element u r checking is arr( x , y )the possible digonal elements are arr ( x-1, y-1 ) arr ( x+1, y-1 ) arr ( x-1, y+1 ) arr ( x+1, y+1 )out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonalshthsorry if i confused u more :) was just trying to help :)[/quote]omg, thats actually a really good idea. I think i can see how that would works, since i know the upper and lower limits i think i should be able to get some sort of loop to go through all that.The only thing is it will probably end up checking all the diagonals multiple times but i could put it in my documentation that its doing that just to be sure! hehe :)hmm, guess i could try and work it out to see what the minimum number of elements i would need to check for it.Thanks! I'm gonna give this a shot tonight. Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30910 Share on other sites More sharing options...
samshel Posted April 26, 2006 Share Posted April 26, 2006 Hello,as i said,[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]out of which i think you need not check for arr ( x-1, y-1 ) and arr ( x+1, y-1 ) as while navigating if you start from x=0,y=0 these 2 elements would have been already checked. so u need to check only the remaining 2 elements. if any of these 2 is same as the arr( x , y ) then you have ur diagonals[/quote]u need not check arr ( x-1, y-1 ) and arr ( x+1, y-1 ) , so u will not check repeatedly....:) ...give it a shot, i think it should work.hth Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30911 Share on other sites More sharing options...
dougal85 Posted April 26, 2006 Author Share Posted April 26, 2006 [!--quoteo(post=368807:date=Apr 26 2006, 01:19 PM:name=samshel)--][div class=\'quotetop\']QUOTE(samshel @ Apr 26 2006, 01:19 PM) [snapback]368807[/snapback][/div][div class=\'quotemain\'][!--quotec--]Hello,as i said,u need not check arr ( x-1, y-1 ) and arr ( x+1, y-1 ) , so u will not check repeatedly....:) ...give it a shot, i think it should work.hth[/quote]sorry i didnt see that!Thanks, i'm just going to give it a shot now - i'll let you know how i get on. Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30943 Share on other sites More sharing options...
dougal85 Posted April 26, 2006 Author Share Posted April 26, 2006 also realised that;(x-1, y+1) only needs start from the elements on the left and top(x+1, y+1) only needs to start on the elements on the left and bottom.just coding it now, fingers crossed Link to comment https://forums.phpfreaks.com/topic/8417-2d-arraymatrix/#findComment-30957 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.