I am making a TicTacToe game in Java I am required to use a two-dimensional array and to use recursion to check if there is a winner.
I feel like I could easily do a non-recursive check for a winner, but I don’t know where to start if I were to use recursion to do it because I am very new at recursion. Could someone guide me on where to start the process of such an algorithm?
Assuming your board looks something like:
One way is to simply check adjacent cells recursively (in a single direction). For example (pseudo-code):
Then, we just have to check the eight possible start-point/direction values:
Now, granted that’s a fairly limited (and contrived) use of recursion but, as you say, it’s not really a situation suited to recursion anyway. Far better is just to use eight
ifstatements, provided you’re not planning of expanding this to more than standard 3×3 tic-tac-toe.