I am starting a software engineering degree soon and have been practicing the last couple months. I have posted some code I wrote for a basic Tic Tac Toe game below (C++). I have read the worst thing to do is to get into bad habits early on, so please can you read over the code and tell me what I should be doing differently? Be as brutally honest as possible, I care more about my future than feelings. Bad practices, bad code, bad formatting, anything.
#include <iostream>
#include <string>
void playGame();
void printBoard(char board[3][3]);
int nextMove(char symbol, int plays, int playedMoves[9]);
bool checkWinner(char board[3][3], char symbol, int plays);
int main()
{
using namespace std;
cout << "Tic Tac Toe by Kevin\n"
<< "____________________\n\n";
playGame();
char yn = 'a';
//Check if players want another game
while (true)
{
while (tolower(yn) != 'y' && tolower(yn) != 'n')
{
cout << "Do you want to play again? (y/n): ";
cin >> yn;
//following 2 lines are to handle incorrect inputs
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
if (tolower(yn) == 'y')
playGame();
else
break;
}
return 0;
}
//Where the game starts. Sets up board and runs 9 plays
void playGame()
{
using namespace std;
char board[3][3] =
{
{ ' ', ' ', ' ', },
{ ' ', ' ', ' ', },
{ ' ', ' ', ' ' }
};
printBoard(board);
char symbol = ' ';
int move = 0;
int playedMoves[9] = { 0 };
for (int plays = 1; plays < 10; plays++)
{
if (!(plays % 2))
symbol = 'O';
else
symbol = 'X';
move = nextMove(symbol, plays, playedMoves);
playedMoves[plays - 1] = move;
int x = 0, y = 0;
x = (move + 2) % 3;
y = (move - 1) / 3; //math to convert 1-9 to [][]
board[x][y] = symbol;
printBoard(board);
if (plays > 4)
if (checkWinner(board, symbol, plays)) break;
}
}
//Prints the board
void printBoard (char board[3][3])
{
using namespace std;
string sLine = "";
cout << endl;
for (int k = 0; k < 3; k++)
{
sLine = "";
for (int l = 0; l < 3; l++)
{
sLine += board[l][k];
if (l < 2) sLine += "|";
}
cout << sLine << endl;
if (k < 2) cout << "-----" << endl;
}
cout << endl;
}
//Takes next move and checks for errors
int nextMove (char symbol, int plays, int playedMoves[9])
{
using namespace std;
bool validPlay = false;
int move = 0;
while (!validPlay)
{
while (move < 1 || move > 9)
{
cout << "Player " << symbol << " enter your move: ";
cin >> move;
if (!cin)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
validPlay = true;
for (int k = 0; k < plays; k++)
{
if (move == playedMoves[k]) validPlay = false;
}
}
return move;
}
//Check to see if a player has won or if there is a tie
bool checkWinner(char board[3][3], char symbol, int plays)
{
using namespace std;
bool winner = false;
//Check vertical and horizontal
for (int k = 0; k < 3; k++)
{
if (board[k][0] == symbol && board[k][1] == symbol && board[k][2] == symbol)
winner = true;
if (board[0][k] == symbol && board[1][k] == symbol && board[2][k] == symbol)
winner = true;
}
//Check diagonal
if (board[0][0] == symbol && board[1][1] == symbol && board[2][2] == symbol)
winner = true;
if (board[2][0] == symbol && board[1][1] == symbol && board[0][2] == symbol)
winner = true;
if (winner)
cout << "Congratulations! The winner is " << symbol << "'s!\n\n";
if (!(winner) && (plays == 9))
cout << "Unfortunately it was a tie!\n\n";
return winner;
}