I'm trying to make a very simple Tic Tac Toe for a class I have where instead of X and O you color the cells interior blue (user) and red (macro) and no AI.
but whenever I think I got it it goes into an infinite loop
Sub Tic()
Dim r1 As Integer
Dim r2 As Integer
Do
r1 = Int(Rnd * 3) + 1
r2 = Int(Rnd * 3) + 1
If Cells(r1, r2).Interior.Color = xlNone Then 'with colorindex instead of color it fills before crashing
Cells(r1, r2).Interior.Color = vbRed
End If
Loop While Cells(r1, r2).Interior.Color = vbBlue Or Cells(r1, r2).Interior.Color <> vbRed
'tried removing the first condition but the result is the same, same with changing the second to equal
End Sub
Now Im only allowed to use the functions I'm using so I cant change too much
so what I'm thinking it should do is check if the cells are not filled with any color then it should color it red , if its blue or red then it should do nothing and look for another unfilled cell.
no matter how I look at it I just dont know what part is wrong so if you cant answer id appreciate it if someone just pointed out which part is wrong so I can focus on it
If the loop hits a blue cell, then your
If
statement isn't going to execute and yourWhile
condition will be met, meaning you get stuck in a loop.Perhaps you would be better off setting a counter of the number of non-coloured cells before you enter the loop, and then decrement it each time you colour a cell. Then you can exit the loop when the counter is 0.
So if you have 9 cells, you could use