Tic-Tac-Toe

Started by
1 comment, last by bjgamer 11 years, 7 months ago
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
Advertisement
A few things to look into...

A board could be represented as arrays--perhaps a row array, and then a board is a collection of rows.

Read into object oriented design.
SimLab Developments
[twitter]simlabdev[/twitter]
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.

This topic is closed to new replies.

Advertisement