Checking for rows and columns

Blitz3D Forums/Blitz3D Programming/Checking for rows and columns

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Someone asked for something similar to this, back along. Here's the code I came up with:

	SeedRnd MilliSecs() 

	Const END_ROW = 9
	Const END_COL = 9
	Dim grid(END_ROW,END_COL) ; A 10x10 grid.

	Repeat

		Print
		For r = 0 To END_ROW
			For c = 0 To END_COL
				grid(r,c) = Rand(0,1)
				Write grid(r,c) + " "
			Next
			Print
		Next
	
		most_in_a_row% = 0
		
		For r = 0 To END_ROW
			For c = 0 To END_COL
				connected = in_a_row(r,c)
				If connected > most_in_a_row Then most_in_a_row = connected
			Next
		Next
	
		Print : Print "Most in-a-row values " + most_in_a_row
		WaitKey()
	
	Until KeyHit(1)
	
	End

;
; Finds the largest amount of in-a-row values connected
; to the value at row,col in grid() array.
;
; Params:
; row,col - Position in grid() array of where to find largest
;           amount of matching, in-a-row values.
;
; Returns:
; Largest amount of matching in_a_row values found.
;
Function in_a_row(row%, col%)
	
	match = grid(row,col)
	
	For n = 0 To 3	; 4 directions to check: horz, vert & 2 diags.
	
		dx = Sgn(n) : dy = Sgn(n-2)		
		connected = 1
		
		For j = 1 To 2
			trow = row : tcol = col

			Repeat
				trow = trow + dx : tcol = tcol + dy
				out_of_bounds = (trow<0)+(trow>END_ROW)+(tcol<0)+(tcol>END_COL)
				If out_of_bounds Then Exit
				If grid(trow,tcol) <> match Then Exit
				connected = connected + 1
			Forever
					
			dx = -dx : dy = -dy ; Reverse checking direction.
		Next

		If connected > most_in_a_row Then most_in_a_row = connected
						
	Next
	
	Return most_in_a_row
	
End Function