Simple Tic Tac Toe on excel VBA -
i'm trying make simple tic tac toe class have instead of x , o color cells interior blue (user) , red (macro) , no ai.
but whenever think got it goes infinite loop
sub tic() dim r1 integer dim r2 integer r1 = int(rnd * 3) + 1 r2 = int(rnd * 3) + 1 if cells(r1, r2).interior.color = xlnone 'with colorindex instead of color 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 first condition result same, same changing second equal end sub
now im allowed use functions i'm using cant change
so i'm thinking should check if cells not filled color should color red , if blue or red should nothing , unfilled cell.
no matter how @ dont know part wrong if cant answer id appreciate if pointed out part wrong can focus on it
if loop hits blue cell, if
statement isn't going execute , while
condition met, meaning stuck in loop.
perhaps better off setting counter of number of non-coloured cells before enter loop, , decrement each time colour cell. can exit loop when counter 0.
so if have 9 cells, use
dim r1 integer dim r2 integer dim blankcells integer blankcells = 9 r1 = int(rnd * 3) + 1 r2 = int(rnd * 3) + 1 if cells(r1, r2).interior.color = xlnone 'with colorindex instead of color fills before crashing blankcells = blankcells - 1 cells(r1, r2).interior.color = vbred end if loop while blankcells > 0
Comments
Post a Comment