Jump to content
  • Advertisement
Sign in to follow this  
doyleman

C++ and tic tac toe

This topic is 4895 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I picked up on C++ awhile ago and began working with it. I've already mastered 'hello world' and even entering in strings for things like a user entering in a name, and it displaying it. Aside from all of this, I have no where to start off for making tic tac toe... Im sure its possible to make without graphics, but I need somewhere to start (and yes, i've read most articles for beginners). If anyone could list how many variables, strings, booleans, etc needed for this, that'd be great :) Doyle

Share this post


Link to post
Share on other sites
Advertisement
first, think of all the steps required. wouldn't hurt to write a short list in notepad. for example you need to draw the board/grid, you need to ask for input, after placing an x or o you need to see if that x or o ended the game. you can easily do all this in a console using cin/cout, or you could learn to use the win32 api console functions. or if you decided to use graphics instead, try gdi or something like sdl or allegro. if you're lost, look around for some tic-tac-toe source code and see how others have done it.

Share this post


Link to post
Share on other sites
You could use pointers and references within your game. Yes, you can make a console based tic-tac-toe. Pretty much just map out your board and go through the logical steps like you were playing the game.

Share this post


Link to post
Share on other sites
#include <iostream>
#include <iostream>
#define C(v) cout<<(v?v==X?'X':'O':'.')

using namespace std;enum P{N,X,O}p=O,b[9],c;int d[
]={0,3,1,3,2,3,0,1,3,1,6,1,0,4,2,2},t,*u;int main(
){for(;;){y:for(t=0;t<9;++t)t=N;for(;;){z:for(t
=0;t<9;++t)C(t)<<((t+1)%3?' ':'\n');p=p==X?O:X;
for(;;){C(p)<<", enter your move (1-9): "<<flush;
cin.sync();t=cin.get()-'1';t=t%3+3*(2-t/3);if(t>-1
&&t<9&&!t)break;}t=p;for(u=d;u<d+16;u+=2)if(
(c=b[*u]))for(t=1;t<3;)if(c!=b[*u+t*u[1]]) break;
else if(++t>2){C(c)<<" Wins!\n";goto y;}for(t=0;t<
9;++t)if(!t)goto z;cout<<"Draw! \n";goto y;}}}



I hearby release this as public domain. I don't want anything to do with it.

Share this post


Link to post
Share on other sites
I forgot to mention that the book beginning c++ game programming has a whole chapter on coding a tic-tac-toe in c++ that is console based. The book is around $30 bucks. YOu might want to check it out.

Share this post


Link to post
Share on other sites
That's an excellent book when learning C++. The TTT example isn't OO but there is a OO example of poker in the last chapter that I used as a template to redo the TTT game with class/inheritence etc.

Definitely work a look and a buy..:)

Share this post


Link to post
Share on other sites
Quote:
Original post by doyleman
If anyone could list how many variables, strings, booleans, etc needed for this, that'd be great :)

Doyle


How much paint, how many brushes, do I need to paint a still life?

You are thinking about it in the wrong way. First, you must be able to describe the process. Start with a rough description and break down every step until you can see what it looks like in code. Roughly, tasks are put together from other tasks, and task descriptions suggest function names (and your data will be the things operated upon).

Share this post


Link to post
Share on other sites
Thanks for all the input.
I decided I should try making rock paper scissors instead first, but this will help, so thank you :)

Share this post


Link to post
Share on other sites
I just did a console based rock paper scissors like a month or so ago. If you run into any problems just post here and I'll help.

Share this post


Link to post
Share on other sites
Ok, i reviewed the commands done with numbers, but I can't find a randomize # code.
I noticed some tictactoe programs have 2 players running it, but i was looking at AI, which basically randomizes a variable 1~3, 1 being rock, 2 being paper, and 3 being scissors.
Basically, how do i randomize a set variable.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!