patterncppMinor
Tic-Tac-Toe Implementation in C++
Viewed 0 times
tacimplementationtictoe
Problem
First full program in C++. I'm having trouble understanding when I should use classes and when I should not. Ultimately, after this review, I would love to have a better idea of what circumstances I should use classes and how I can more efficiently write in c++.
I plan to rewrite the program using classes and re-post for further review. At the minimum, I assume I could create a player class to more efficiently track which symbol belongs to who and so forth. Written using Eclipse and MinGW GCC
main.cpp:
```
//TicTac Toe game for C++ dev practice
//attempting to utilize various things I have learned to produce a replayable tictac toe with drawn out game board on the console
//Lets do this
//LEEERRROOOYYYYYY JEENNNKIIINNNNSSSSS
#include
#include "ticTacToeFunctions.h"
using namespace std;
int main()
{
//assumes no initial play before requesting input from the user
bool play;
play = getUserWantToPlay(play);
// initializes loop for game play..allows for users to play multiple times
while(play)
{
char squareArray[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int playerTurn = 1;
int playerWin = 0;
//Round iterations
while(playerWin == 0)
{
drawGameBoard(squareArray);
cout << "Player " << playerTurn << "'s turn!" << endl;
//Gets player move from user and alters squareArray for iteration through drawGameBoard
getPlayerMove(playerTurn, squareArray);
//Checks for player win
playerWin = checkForWin(squareArray, playerTurn);
if(playerWin == 0)
{
//Alternates player turn via conditional operator
playerTurn = playerTurn == 1 ? 2 : 1;
}
}
drawGameBoard(squareArray);
cout << "Congratulations player " << playerWin << "! You Win!" << endl;
//retest if player wants to play a new game
play = getUserWantToPlay(p
I plan to rewrite the program using classes and re-post for further review. At the minimum, I assume I could create a player class to more efficiently track which symbol belongs to who and so forth. Written using Eclipse and MinGW GCC
main.cpp:
```
//TicTac Toe game for C++ dev practice
//attempting to utilize various things I have learned to produce a replayable tictac toe with drawn out game board on the console
//Lets do this
//LEEERRROOOYYYYYY JEENNNKIIINNNNSSSSS
#include
#include "ticTacToeFunctions.h"
using namespace std;
int main()
{
//assumes no initial play before requesting input from the user
bool play;
play = getUserWantToPlay(play);
// initializes loop for game play..allows for users to play multiple times
while(play)
{
char squareArray[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
int playerTurn = 1;
int playerWin = 0;
//Round iterations
while(playerWin == 0)
{
drawGameBoard(squareArray);
cout << "Player " << playerTurn << "'s turn!" << endl;
//Gets player move from user and alters squareArray for iteration through drawGameBoard
getPlayerMove(playerTurn, squareArray);
//Checks for player win
playerWin = checkForWin(squareArray, playerTurn);
if(playerWin == 0)
{
//Alternates player turn via conditional operator
playerTurn = playerTurn == 1 ? 2 : 1;
}
}
drawGameBoard(squareArray);
cout << "Congratulations player " << playerWin << "! You Win!" << endl;
//retest if player wants to play a new game
play = getUserWantToPlay(p
Solution
Learning how to use classes (or types) in C++ is definitely a great idea. Just keep on mind that you don't always need to use classes everywhere. It is just a tool and there are many more tools in C++. It is definitely possible and definitely ok to implement some minimalistic tic-tac-toe game without any class definition (well you just did it).
If you want to practice using types (classes) you could try to think about what (which entity) you need to represent in your code (maybe player, maybe board, maybe something totally different) and create class for it. You might also benefit from reading some nice book to understand basic concepts.
This question might be helpful:
https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
You can find some ideas regarding current code below.
Regarding game rules
tie
Other possible end-state apart from victory is tie. You should probably check for that too and end the game accordingly.
occupied squares
You probably don't want to allow moves to already occupied squares.
What happens when players inputs 'x' as his/her new move?
Regarding design
interface
Why the reference? Wouldn't it be sufficient to just return the value?
Regarding use of C++
infamous
This is probably not a good idea as you are cluttering your scope with names you probably don't need. Try to google "c++ using namespace std" to get the idea.
player representation
Player is represented by
passing array to function
Check how you are passing the
This answer might help:
https://stackoverflow.com/a/14309142
initialization
Uninitialized variables are bugs waiting to happen. If you forgot to initialize those before reading it is undefined behaviour, about the ugliest thing in C++.
Instead of separate definition and initialization (possible several code lines apart) it is better to do it at once (safer, sometimes more efficient):
If you want to practice using types (classes) you could try to think about what (which entity) you need to represent in your code (maybe player, maybe board, maybe something totally different) and create class for it. You might also benefit from reading some nice book to understand basic concepts.
This question might be helpful:
https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
You can find some ideas regarding current code below.
Regarding game rules
tie
Other possible end-state apart from victory is tie. You should probably check for that too and end the game accordingly.
occupied squares
You probably don't want to allow moves to already occupied squares.
What happens when players inputs 'x' as his/her new move?
Regarding design
interface
bool getUserWantToPlay(bool &play)Why the reference? Wouldn't it be sufficient to just return the value?
bool getUserWantToPlay()Regarding use of C++
infamous
using namespace stdThis is probably not a good idea as you are cluttering your scope with names you probably don't need. Try to google "c++ using namespace std" to get the idea.
player representation
Player is represented by
int (e. g. playerTurn, playerWin) that is a type with at least 2 ^ 32 possible values out of which only 2 are valid. That is a huge space for bugs. Wouldn't be using something like enum or bool better?passing array to function
Check how you are passing the
squareArray array around.void drawGameBoard(char squareArray[9]);
void getPlayerMove(int playerTurn, char (&squareArray)[9]);
int checkForWin(char squareArray[9], int playerTurn);This answer might help:
https://stackoverflow.com/a/14309142
initialization
Uninitialized variables are bugs waiting to happen. If you forgot to initialize those before reading it is undefined behaviour, about the ugliest thing in C++.
bool play;
play = getUserWantToPlay(play);Instead of separate definition and initialization (possible several code lines apart) it is better to do it at once (safer, sometimes more efficient):
bool play = getUserWantToPlay(play);Code Snippets
bool getUserWantToPlay(bool &play)bool getUserWantToPlay()void drawGameBoard(char squareArray[9]);
void getPlayerMove(int playerTurn, char (&squareArray)[9]);
int checkForWin(char squareArray[9], int playerTurn);bool play;
play = getUserWantToPlay(play);bool play = getUserWantToPlay(play);Context
StackExchange Code Review Q#131565, answer score: 3
Revisions (0)
No revisions yet.