# Algorithm question

I’m learning programming using Xojo with a program that I’ve inherited. This is more of an algorithm question than a Xojo question, but I hope that someone can help me.

I have a square array containing little squares of value s(i,j)=1 or s(i,j)=-1 whose color I would like to change to one of two possible colors. Basically, if s(i,j)=1 and s(i+1,j)=-1 then I’d like to have both of those squares be the same color, if s(i,j)=1 and s(i+1,j)=1 then both of those squares need to be different colors, etc. Then, I compare the next two squares, then the next two, etc. It gets a little tricky with an odd array (say, 3 x 3 or something) so I thought I’d start with an even array, as given below.

My questions are: is there a simpler way to code it? Naively, two for loops followed by three if statements doesn’t seem very efficient to me. Also, how do I specify in the case of, for example, s(i,j)=1 and s(i+1,j)=1 that those squares need to be two separate colors?

[code] Dim x, y, n as Integer

for i=0 to size-1
for j=0 to size-1
if size = 2 * n then // if even array (it gets tricky with an odd array)
if i = 2 * n then // comparing first two squares, then next two, etc.
if s(i,j)=1 and s(i+1,j)=-1 then
GraphicsWindow.Graphics.ForeColor = Color1
if s(i,j)=1 and s(i+1,j)=1 then
// ???

``````      end if
end if
end if
next
``````

next

x = i * SquareWidth
y = j * SquareWidth
GraphicsWindow.Graphics.FillRect x, y, SquareWidth, SquareWidth[/code]

Not sure what should happen when s(i,j)=-1 and s(i+1,j)=-1, but here is some code that might help (doesn’t cater of odd sizes):

``````  i = 0
while (i + 1) < size

j = 0
while j < size

// set up color

if s(i,j) = s(i+1, j) then
Color1 = &cff5555
Color2 = &cff5555
else
Color1 = &cff5555
Color2 = &c55ff55
end if

// draw blocks

x = i * SquareWidth
y = j * SquareWidth
g.ForeColor = Color1
g.FillRect x, y, SquareWidth, SquareWidth
x = (i + 1) * SquareWidth
g.ForeColor = Color2
g.FillRect x, y, SquareWidth, SquareWidth

j = j + 1

wend

i = i + 2

wend``````

The following pattern emerges for a 10x10 array filled with random data: