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
function, you never check the cells in the lower rows:
row is always 0
. And it is 0, because you calculate it as
is a value between 0 and 2 ...
The mistake is, you should use
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 '
' and '
' isn't very useful. Naming them '
' and '
' 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 '
row = string_pos / GRID_SIZE;
Also the name '
' 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
? Then the code to determine your row would be
row = matrix_index / GRID_SIZE;