I have tried everything I know
You haven't tried to make use of your compiler, having it generate
warnings and reacting to them (
variable character
is unused). If it didn't generate this warning, use -Wall to make it so.
Also you should have tried a
debugger to see what is happening. Then you quickly would have realized that in your
champion
function, you never check the cells in the lower rows:
row
is always 0. And it is 0, because you calculate it as
index/3
, where
index
is a value between 0 and 2 ...
The mistake is, you should use
character
here, not
index
.
The lesson to be learned here is: learn to use the tools that you have available. If used well, they will help you a lot more than you you might imagine.
P.S.: another tip that could help you recognize bugs even before the compiler touches your code: use meaningful variable names.
This does include loop variables, especially when you have nested loops: using '
i
' and '
index
' isn't very useful. Naming them '
i_possibility
' and '
string_pos
' instead would be much clearer, and maybe you would have noticed that it isn't very sensible to determine your row and column index on the base of '
string_pos
'.
row = string_pos / GRID_SIZE;
Also the name '
character
' is very bad indead: character is it's type, not its meaning! What the value stored in this variable represents, is the index into your tic tac toe matrix, so why not name it
matrix_index
? Then the code to determine your row would be
row = matrix_index / GRID_SIZE;