• Advertisement
Sign in to follow this  

Tic-Tac-Toe

This topic is 2039 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

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. tongue.png

http://pastebin.com/c2puhfFy - main.cpp
http://pastebin.com/bxQP7sB2 - main.h
http://pastebin.com/2bn6LxaL - board.cpp
http://pastebin.com/mbsKZhYE - board.h

Thanks,
Vince

Share this post


Link to post
Share on other sites
Advertisement
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 topRow, leftColumn, and leftDiagonal 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
Sign in to follow this  

  • Advertisement