Tic Tac Toe with AI :) + 1 more question

Started by
22 comments, last by superpig 16 years, 5 months ago
Quote:Original post by sheep19
where do I put int main() ? In the header or in the source file?


in the source file :)
Advertisement
Quote:Original post by dperfors
Quote:Original post by sheep19
where do I put int main() ? In the header or in the source file?


in the source file :)


thanks

bool winsP1 = false, winsPC = false;applyInputToBoard (*it, false, true);/*and so on...*/


Is this ok ? :)
Since the combination (true, true) is not possible, it would be better to use an

enum {
player_wins,
pc_wins,
no_one_wins
};

(Do not convert your variable "win" into bools.)
The new question is, does this program make me "eligible" to move to something more complicated? (like D3D). I know some SDL as well, but I really want to program with real graphics in. Also, I know nothing about pointers and their real uses (just some iterators, which I think are similar). What do you suggest to do?

Thanks in advance.
woah...no, not yet.

If you don't know much about pointers then you should learn about them well. Also, you really should get a good hang of the language, not only syntax but, learning how to program in OOP. Using a single class is not using OOP. Also, learning about data structures (queues, trees, linked lists), file processing, STL, operator overloading, etc. are all important. They may not all be used in game development but they will help you to get a strong hold in learning the language which will in turn help you to build better games.

Do you have an actual book on C++ programming? If you don't I suggest you pick one up. I recommed C++ How to Program (6th edition). It's pricey but it is an excellent book. If you do get it, make sure you get the 6th edition. Near the end of the book there is a chapter on how to make a graphical version of Pong.
Only you can decide your 'eligibility' for some arbitrary next step. Graphics aren't about knowing certain data structures anyway. Graphics present a project with an increase in code complexity [the parts that deal with the graphics and the resources thereof] and an increased reliance on tools [created by you or others] in exchange for having another medium to work with. Graphics are not 'hard', they just introduce more stuff. If you're ready to take more things into consideration, and have your project expand accordingly, then go for it.

Just keep in mind though that programs that use graphics range in complexity and 'hard'ness similarly to how programs without graphics range in complexity and 'hard'ness. Tic Tac Toe isn't complicated, and a simple graphic-using program won't be either. 'Text' projects can be just as complicated as graphics projects, or at least complicated to the extent that the complexity difference that would be introduced by having the 'Text-only' program be a graphic using program instead would be negligible.

In short, if you want to work with graphics, take a look at some API's, and then make your own decision about your readiness. Half of learning something is being able to judge your own skill and capacity accurately.
I've looked in this thread a bunch of times, and each time I start writing a big reply doing a full critique of your code, and each time I stop before I finish because I worry that what I'm saying will be too many changes for you to digest in one go.

The code you've written works, but there are some major problems with its design that imply a certain degree of confusion about the design. Also, putting the whole thing into one big class is something of a misuse of classes.

I'd recommend exploring the separation of the program into multiple classes, first and foremost - at the very least you could pull out one class for the Board, one class each for the Human and AI players and a base class that they both derive from.

Learn how to isolate the parts of the program from each other, formulate your invariants and learn how to guarantee them. Refactor what you've got here into something that is beautifully designed, and it will help you significantly when you move on to larger projects.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

Thanks a lot.

I will try to use more classes, as superpig suggested.

The book that I have is Beginning C++ Game Programming.
How about this as my header file ?

#include <iostream>#include <vector>#include <algorithm>#include <ctime>const char PLAYER_SYMBOL = 'X';const char PC_SYMBOL = 'O';const char DEFAULT_SYMBOL = '-';class TicTacToe{private:	std::vector <int> freeNodes; // the free nodes	std::vector <int> usedNodes; // the used nodes	char board [3] [3]; // set the board	class board 	{		void setBoard();		void setFreeNodes();		void printBoard();	};	class player	{		int getNode();		bool checkInput (int node);		void applyInputToBoard (int node, bool playsP1, bool playsPC);		void addRemoveNodes (int node);		int checkWin();	};		class pc 	{		int pcNode();		void applyInputToBoard (int node, bool playsP1, bool playsPC);		void undoInputToBoard (int node);		void addRemoveNodes (int node);		int checkWin();	};	void print();};

Also, do I have to use multiple source files ?
You don't have to use multiple source files, but you don't have to use for and while loops either (ie, goto). But as your project gets larger you will definitely want to break up your program into modules. Read this until you understand it and you'll do fine.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

This topic is closed to new replies.

Advertisement