Hey there programmers, I have written a oop version of Tic Tac Toe. If you could critique my code and let me know if there is a more efficient and better way of programming something in my code. Thank you.
Also your TicTacToe class is stateless. There is no real need to have an instance of it or to define constructrors and sestructors: all members would work better if made static. And in C++ it is often suggested that instead of making a class with all static membrs, it is better to just place them inside of namespace. So your class is really redundand here.
To make it OOP you have to delegate responsibility to manage board to class itself instead of doing it manually in main. Ideally your main function should look like:
int main() {
TicTacToe game;
game.play(); // run game loop
return 0;
}
or even
1 2 3 4 5
int main() {
TicTacToe game; // automatically run game loop
return 0;
}
A game loop could be useful, however, if you wanted to decouple the input/output code.
Meanwhile...
Both draw_board() and isGameOver() could be simplified with the use of loops (esp. if isGameOver() looks for rows and cols where all values are equal, rather than checking for specific values.)
I would not place game loop in constructor. Bad idea.
And loop is still going to be somewhere. It is only matter of for what extent your class manages game. For example we cam wrap my TicTacToe class with some other Game class which would just move that loop out of main inside play() ethod of that class. Again, depending on your needs and architecture of application.
I would not place game loop in constructor. Bad idea.
In a trivial game I don't see this to be an actual problem. I have come across the opinion that a truly object oriented C++ program's entrypoint should be the constructor of the "application" object. But I would probably construct the instance and then call a play() method.
As I already said, I only see the point of an external loop if the game object needs to interact with the system (i.e. there is i/p and o/p).
Tic-tac-toe (or Noughts and crosses, Xs and Os) is a paper-and-pencilcomputer game for two players, X and O, who take turns marking the spaces in a 3×3 grid.
so I would not expect it to be wrapped by a game object.
It is only matter of for what extent your class manages game.
From an OO point of view, the class is the game, it does not manage it.
Rather than checking if the game is over, you could search for a winner. The function lookk for a line of chars which are the same (so the assumption is their init to different chars as above.)
The instead of setting the int playerWins to 1 or 2 could make it a char and set it to 'X' or '0'.