Writing code for a tic tac toe game in C++

O

osmium

I was wondering if anyone knows how to write a tic tac toe game in c++?

I would expect that any C++ programmer who knew the rules of tic tac toe
could write such a program.
 
J

Jeremy Jurksztowicz

No, sadly no one has yet been able to solve the so called 'tic-tac-toe'
paradox. Despite my best efforts during 12 years of research, I have
still not come up with a satisfactory solution. There is currently a
group of scientists at MIT, who claim to be close to a solution, you
may want to contact them at (e-mail address removed).
 
A

Alf P. Steinbach

* (e-mail address removed):
I got to do this for a final in my C++ class and to be honest i'm
completely clueless. Could you please enlighten me?

Start by writing a "guess a random integer number by being informed
whether each guess is too low or high or correct" program A, just to get
a feel for how it is to write an interactive program.

Then write a program B that systematically solves this problem on its
own, acting like the user; no theory needed, just common sense.

Then write a program C that let's the user solve a simple problem such
as crossing a river in a boat with limited capacity (two most popular
variants being 3 nuns and 3 cannibals, and farmer+goat+wolf+wheat).

Then write a program D that solves this problem on its own; you'll need
to look into min-max algorithm.

Then write a program E that let's the user place dots and crosses on the
tic-tac-toe board.

Then write a program F where the user plays tic-tac-toe against the
computer (since 3x3 tic-tac-toe is easily analyzed completely on paper
there's no real need to use e.g. min-max, but probably easiest to do).
 
A

Alf P. Steinbach

* Alf P. Steinbach:
* (e-mail address removed):

Start by writing a "guess a random integer number by being informed
whether each guess is too low or high or correct" program A, just to get
a feel for how it is to write an interactive program.

Then write a program B that systematically solves this problem on its
own, acting like the user; no theory needed, just common sense.

Then write a program C that let's the user solve a simple problem such
as crossing a river in a boat with limited capacity (two most popular
variants being 3 nuns and 3 cannibals, and farmer+goat+wolf+wheat).

Then write a program D that solves this problem on its own; you'll need
to look into min-max algorithm.

I meant, search algorithms. min-max is for program F.
 
J

Jim Langston

I got to do this for a final in my C++ class and to be honest i'm
completely clueless. Could you please enlighten me?

This is your final assignemtn and you're clueless? Didn't you do any of the
homework the instructor gave you? I find it hard to believe that you could
go through an entire class on c++ and not know how to do this.

Can you display a tic tac toe board in C++? Write code to just display a
tic tac toe board (without X's or O's).

Okay, now you're going to need to put in X's, O's or blanks depending on...
what? Hmm.. seems you're going to have to store the move for each square.
Lets look at a tic tac toe board:

| |
-------
| |
-------
| |

It seems to be 3 by 3. How would you store in memory 3 elements by 3
elements? There are a few ways (vector of vector, 9 variables, a 2d array).
Decide which way you want to go (for something simple like this I might
choose a simple 2d array, I.E. char Board[3][3]; Although you might want
std::vector<std::vector<char> > Board; You decide. There is no "right"
answer, though some may argue that a vector should always be used instead of
a static array, I'm not one of them.

Okay, now you have storage, intialize them to spaces.

Now fill in some X's and O's in your array, and get them to print out in the
board.

Okay, so there are 3 things that programs do, Input, processing and output.
So we have the output so far. Lets look at the input. Write a
class/method/function/routine to accept a players move. You decide how the
player picks a square. Common solutions are 1 thorugh 9, or 1,1 through
3,3.

But you'll need processing. Is the players move legal? Does it fall within
bounds? Is there already a move there? Did the player win?

Alternate players.

Okay, once you're done, you have a tic tac toe game.

Now you can go farther, have the player play against the computer.

Show me code, when you get stuck ask.
 
D

David Harmon

On 16 Nov 2006 19:33:30 -0800 in comp.lang.c++, (e-mail address removed)
wrote,
I got to do this for a final in my C++ class and to be honest i'm
completely clueless. Could you please enlighten me?

How did you manage the previous assignments? Be advised that it's
against policy in almost all online groups to do your work _for_ you.
Show what you have got done so far. Describe where you are stuck.
Ask _specific_ questions.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"# [5.2] How do I get other people to do my homework problem for me?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/

See the welcome message posted twice per week in comp.lang.c++ under the
subject "Welcome to comp.lang.c++! Read this first." or available at
http://www.slack.net/~shiva/welcome.txt
 
V

VJ

Jeremy said:
No, sadly no one has yet been able to solve the so called 'tic-tac-toe'
paradox. Despite my best efforts during 12 years of research, I have
still not come up with a satisfactory solution. There is currently a
group of scientists at MIT, who claim to be close to a solution, you
may want to contact them at (e-mail address removed).

Invalid email address. You probably meant (e-mail address removed)
 
M

Marcus Kwok

Alf P. Steinbach said:
Then write a program E that let's the user place dots and crosses on the
tic-tac-toe board.

Then write a program F where the user plays tic-tac-toe against the
computer (since 3x3 tic-tac-toe is easily analyzed completely on paper
there's no real need to use e.g. min-max, but probably easiest to do).

Next write a program G: Global Thermonuclear War.
 
P

Puppet_Sock

I got to do this for a final in my C++ class and to be honest i'm
completely clueless. Could you please enlighten me?

Repeat after me:

Want fries with that?

Socks
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top