Tic Tac Toe using recursion

J

jbutler67

Does anyone have C++ source code for a Tic Tac Toe game where
recursion is used?

I am just learning C++ and have been trying to write a program where
the user plays against the computer. The program uses recursion to
assess all possible moves before making a move.

This is not for a course... it is a starting program that I am using
to learn C++ basics on my own.

Thanks in advance.
 
J

Jonathan Turkanis

Does anyone have C++ source code for a Tic Tac Toe game where
recursion is used?
....

This is not for a course... it is a starting program that I am using
to learn C++ basics on my own.

If you're really trying to learn, you shouldn't be asking for
completed source code. You should try to solve it yourself, and post
here when you get stuck.

Jonathan
 
J

jbutler67

Is the following approach on the right track?

Use a recursive function to finish out all possible remaining moves to
assess the end result as one of three possible options:
1) -1 if end result would be a loss
2) 0 if end result would be a tie
3) +1 if end result would be a win

Then choose one of the possible remaining moves that would result in
eitther a +1 or 0. (If more than one possible "win" moves remains,
use a random number to pick the move to go with

In the first few moves of a game, assuming the human player goes
first, the program would use recursion to assess all 8 follow up
moves, with all folow ups to each reply move.

Example, a starting move of the following:

X | |
-----------------
| |
-----------------
| |

would be possible countered by one of 8 follow up moves


X | O |
-----------------
| |
-----------------
| |

or...

X | | O
-----------------
| |
-----------------
| |

or...

X | |
-----------------
O | |
-----------------
| |

or...
X | |
-----------------
| O |
-----------------
| |

etc.....


And for EACH of the 8 possible follow up moves, there will be 7 reply
moves, with each of the 7 reply moves having 6 follow up moves....
etc.
 
J

Jonathan Turkanis

Is the following approach on the right track?

Use a recursive function to finish out all possible remaining moves to
assess the end result as one of three possible options:
1) -1 if end result would be a loss
2) 0 if end result would be a tie
3) +1 if end result would be a win

Sound reasonable so far.
Then choose one of the possible remaining moves that would result in
eitther a +1 or 0.

Generally, as soon as you find a winning move, you can stop searching.
(If more than one possible "win" moves remains,
use a random number to pick the move to go with

Okay, but it doesn't have to be random. You could just use the first
acceptable move discovered.
In the first few moves of a game, assuming the human player goes
first, the program would use recursion to assess all 8 follow up
moves, with all folow ups to each reply move.

Why don't you start by writing down the signature for the function,
describing exactly what it does, and writing code which uses the
function to select the computer's next move.

Jonathan
 
B

Buster

(e-mail address removed) wrote:

The algorithm to find an optimal move in a two-player zero-sum
perfect-information game like Tic Tac Toe or chess is called "minimax".
There are plenty of examples in code on the web. Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top