Recursive tictactoe game help

My goal right now is to create a bool function which tells me if winning a tictactoe game is possible(assuming you're vs the worst possible AI.) Basically if the possibility for you to win exists(i.e. playing vs someone who's trying to lose) it should return true.

So right now my code is at step 1, I check if putting a piece at any of the 9 spots on the board will make me win. I return true if it does.

Now I want to use recursion to do the next part but I'm stuck. Here's what my function kind of looks like, the checkB function just checks if the board wins, and the copyB function just copys the array.



So right now my code basically just tells me if I can win on a given board configuration on the first move. How can I make it do more then this?
Last edited on
That user wins if:
1. there's a vertical match
2. there's a horizontal match
3. there's a diagonal match

The function shouldn't care which side is playing, the check is the same, so it needs to know the mark to check for (O or X).

So the parameters I'd expect it to take are:
1. the board
2. the mark

The actual types passed in depends on your implementation.
I have a function that tells me if the board is a winning combination, I called it in the function I created. it's called checkB.

The function I'm trying to make should just return true if the computer can win(i.e. if vs the worst possible player) and false if it's not possible to win(i.e. the boards full, or the only moves left can't result in a win)

Right now I can see if there's a winning single move I can make that results in a win. I want it to tell me if there's multiple moves I can make that can result in a win(again, assuming your opponent is the worst player ever/trying to lose.)

So for instance, right now if the board was "X" "None" "X" in the top row my code would return true since X can win. If the top row of the board was completely empty(i.e.) all None's it would return False, even though you can definitely win still. My function should return true if it's POSSIBLE to win(i.e. if your opponent plays terribly) but right now it's only returning if I can win on that single move.

It's hard to explain what I mean, but I want to use recursion. Basically if there's enough turns left and it's possible to win, I should return true. It's assuming your opponent is also playing, but purposely trying to lose(i.e. worst player ever.)

Last edited on
I see. It will still go thru the possible ways to will and check if it can be achieved by a single move. But again, I would expect it to be callable from either side--the algorithm doesn't change, so why hard code it to work for just one side?

EDIT
And the algorithm isn't recursive.
Last edited on
Topic archived. No new replies allowed.