C++ and tic tac toe
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
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.
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.
#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.
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.
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..:)
Definitely work a look and a buy..:)
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).
Thanks for all the input.
I decided I should try making rock paper scissors instead first, but this will help, so thank you :)
I decided I should try making rock paper scissors instead first, but this will help, so thank you :)
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement