Sign in to follow this  
vsg1990

Tic-Tac-Toe

Recommended Posts

Hi,

I've been reading these forums for a while. This is my first post. I've been learning DirectX for a while and understand enough code to fill a window with a quad and color it. However, its getting very complicated very fast and I feel like I don't know enough about 'organizing' code to write a game with DirectX. So I decided to write a game with C++ in the console. Someone suggested Tic-Tac-Toe, so I decided to give it a whirl. I would like as many opinions as I can get about optimizing/organizing this code. You can be very critical, it won't hurt my feelings. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img]

[url="http://pastebin.com/c2puhfFy"]http://pastebin.com/c2puhfFy[/url] - main.cpp
[url="http://pastebin.com/bxQP7sB2"]http://pastebin.com/bxQP7sB2[/url] - main.h
[url="http://pastebin.com/2bn6LxaL"]http://pastebin.com/2bn6LxaL[/url] - board.cpp
[url="http://pastebin.com/mbsKZhYE"]http://pastebin.com/mbsKZhYE[/url] - board.h

Thanks,
Vince

Share this post


Link to post
Share on other sites
Your code is kinda lengthy for what your trying to do. It might be easier to organize if you condense it a bit. I'd suggest making a node class with the status of the tile (blank, X, or O) as a variable, and then made an 3x3 array made up of those nodes. Also to make checking if someone won easier I'd make 8 different empty strings that represented each of the 8 possible win combinations and a count variable. Whenever a player makes a move, just append it to the corresponding string (for example if a move is made in the top left corner by player one place an X in the [i]topRow[/i], le[i]ftColumn, and leftDiagonal[/i] strings. The order is unimportant since 3 same characters in the string means a victory) and check them for a winning combination (you really only have to start checking on the 5th move since that's when the first player is able to have 3 moves on the board, hence the count variable). After that it's as simple as waiting for someone to make a winning move.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this