Computing a square-root is unnecessary; further, it only works when
kBoardSize
is a perfect square. Preferconst int kBoardLength = 3; const int kBoardSize = kBoardLength * kBoardLength;
Your test for unreachability is not complete. You verify that O has made no more moves than X, but you really need to check that the winner made the last move. If X won, then should have
numberOfX = numberOfO + 1
; if O won then should havenumberOfO = numberOfX
. It should be clear how to add these checks to yourCheckTicTacToeBoard
method.A more overarching tension in this code is: should the board size really be a constant? The code is written so abstractly as to support any board size. But then the board size is fixed to a constant. I would choose one of the following directions to proceed in:
Commit to a board size of 9. In this case, I would recommend hard-coding the win conditions in
winDetection
. There are only 8 ways to win, so the resulting code would be a lot cleaner.Allow arbitrary board sizes. In this case, you should accept a
boardLength
argument. Further, you will have to refine the notion of anUnreachableState
. For example, in 45-by-45 tic tac toe, the following state is unreachable, but not detected by your code would returnXwins
:x x x x x o o. o . o x x x x x o . o . o o o x o o