Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a (C++) Tic Tac Toe game. I am almost done I just need to make a fe

ID: 3665394 • Letter: I

Question

I am writing a (C++) Tic Tac Toe game. I am almost done I just need to make a few improvement and I need some help from the Chegg community. I need to validate whitespace in all types of input. I also want to give feedback to the players when they try choosing a square that has already been chosen. Any help is very appreciated. If you would like to tidy up my program so that most of int main() is composed mainly of function calls, I would also very much appreciate that.

#include <iostream>
#include <string>

using namespace std;
int checkwin(char square[]);
void board(string player1Name, string player2Name, char square[]);
bool validateUseInput(char input[]);
bool validatePlayerName(string playerName);
void splash();
int main()
{

   char userChoice;
   do
   {
       int player = 1, i, choice;
       char square[9] = { '0','1','2','3','4','5','6','7','8' };
       splash();
       string player1Name;
       string player2Name;

       cout << "Enter Player 1 Name: " << endl;
       cout << "Enter a single word with alpha characters only!" << endl;
       while (true)
       {
           cin >> player1Name;
           bool valid = validatePlayerName(player1Name);
           if (valid)
               break;
           else
           {
               cout << "--Invalid Player Name , Must be characters only -- ";
               cout << "--Please Re-Enter-- ";
           }
       }
       cout << "Enter Player 2 Name: " << endl;
       cout << "Enter a single word with alpha characters only!" << endl;

       while (true)
       {
           cin >> player2Name;
           bool valid = validatePlayerName(player2Name);
           if (valid)
               break;
           else
           {
               cout << "--Invalid Player Name , Must be characters only -- ";
               cout << "--Please Re-Enter-- ";
           }

       }

      
       char mark;
       do
       {
           board(player1Name, player2Name, square);
           player = (player % 2) ? 1 : 2;

           if (player == 1)
           {
               cout << "Player " << player1Name << ", you are 'X' " << endl;
               cout << " It's your move! Enter a number: " << endl;
           }
           else if (player == 2)
           {
               cout << "Player " << player2Name << " you are 'O' " << endl;
               cout << " It's your move! Enter a number: " << endl;
           }
           char input[10];
           while (true)
           {
               cin >> input;
               bool valid = validateUseInput(input);
               if (valid == true)
                   break;
           }
           choice = input[0] - 48;

           mark = (player == 1) ? 'X' : 'O';

           if (choice == 0 && square[0] == '0')

               square[0] = mark;
           else if (choice == 1 && square[1] == '1')

               square[1] = mark;
           else if (choice == 2 && square[2] == '2')

               square[2] = mark;
           else if (choice == 3 && square[3] == '3')

               square[3] = mark;
           else if (choice == 4 && square[4] == '4')

               square[4] = mark;
           else if (choice == 5 && square[5] == '5')

               square[5] = mark;
           else if (choice == 6 && square[6] == '6')

               square[6] = mark;
           else if (choice == 7 && square[7] == '7')

               square[7] = mark;
           else if (choice == 8 && square[8] == '8')

               square[8] = mark;
           else
           {
               cout << "Invalid move ";
               player--;
           }
           i = checkwin(square);
           player++;
       } while (i == -1);
       board(player1Name, player2Name, square);
       if (i == 1)
       {
           int playerNumber = --player;
           if (playerNumber == 1)
               cout << "==>Player " << player1Name << " win ";
           else if (playerNumber == 2)
               cout << "==>Player " << player2Name << " win ";
       }
       else
           cout << "==>Game draw";

       cout << endl;

       cout << "Do you want to continue press y or Y :";

       cin >> userChoice;
   } while (userChoice == 'y' || userChoice == 'Y');

   cout << "Bye ";
   return 0;
}

/*********************************************

FUNCTION TO RETURN GAME STATUS
1 FOR GAME IS OVER WITH RESULT
-1 FOR GAME IS IN PROGRESS
O GAME IS OVER AND NO RESULT
**********************************************/

int checkwin(char square[])
{
   if (square[0] == square[1] && square[1] == square[2])

       return 1;
   else if (square[3] == square[4] && square[4] == square[5])

       return 1;
   else if (square[6] == square[7] && square[7] == square[8])

       return 1;
   else if (square[0] == square[3] && square[3] == square[6])

       return 1;
   else if (square[1] == square[4] && square[4] == square[7])

       return 1;
   else if (square[2] == square[5] && square[5] == square[8])

       return 1;
   else if (square[0] == square[4] && square[4] == square[8])

       return 1;
   else if (square[2] == square[4] && square[4] == square[6])

       return 1;
   else if (square[0] != '0' && square[1] != '1' && square[2] != '2' && square[3] != '3'
       && square[4] != '4' && square[5] != '5' && square[6] != '6'
       && square[7] != '7' && square[8] != '8')

       return 0;
   else
       return -1;
}


/*******************************************************************
FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
********************************************************************/


void board(string player1Name, string player2Name, char square[])
{
   system("cls");

   cout << " Tic Tac Toe ";

   cout << "Player " << player1Name << " (X) - Player " << player2Name << " (O)" << endl << endl;
   cout << endl;

   cout << " | | " << endl;
   cout << " " << square[0] << " | " << square[1] << " | " << square[2] << endl;

   cout << "_____|_____|_____" << endl;
   cout << " | | " << endl;

   cout << " " << square[3] << " | " << square[4] << " | " << square[5] << endl;

   cout << "_____|_____|_____" << endl;
   cout << " | | " << endl;

   cout << " " << square[6] << " | " << square[7] << " | " << square[8] << endl;

   cout << " | | " << endl << endl;
}

bool validateUseInput(char input[])
{
   int inputLength = strlen(input);
if (inputLength>1)
   {
       cout << "----Invalid input Please enter valid input---- ";
       return false;
   }
   else if (input[0] >= 48 && input[0]<57)
   {
       return true;
   }
   else {
       cout << "----Invalid input Please enter valid input---- ";
       return false;
   }
}

bool validatePlayerName(string playerName)
{
   bool isValid = true;

  
  

  
   for (int i = 0; i<playerName.length(); i++)
   {
      
  
   if (!((playerName[i] >= 65 && playerName[i] <= 90) || (playerName[i] >= 97 && playerName[i] <= 122)))
       {
           isValid = false;
           break;
       }
   }
   return isValid;
}

void splash()
{

   system("cls");
   //splash
   cout << "**********************" << endl;
   cout << "**********************" << endl;
   cout << "**********************" << endl;

   cout << "**********************" << endl;
   cout << "**********************" << endl;
   cout << "**********************" << endl;
  
   system("pause");
   system("cls");
}

Explanation / Answer

If you dont mind you can use this code.

#include <iostream>

// This program allows user(s) to play a game of tic-tac-toe.


using namespace std;

// constants
const char SIGIL[3] = { '.', 'X', 'O' };

// prototypes
int winner(int board[3][3]);
void drawBoard(int board[3][3]);
bool isMoveLegal(int board[3][3]);
bool isGameOver(int board[3][3]);

// return 1 if player 1 'X' has won
// return 2 if player 2 'O' has won
// return 0 if neither player has won
int winner(int board[3][3]){
int win = 0;

// Checks for:
// X X X
for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
win = board[i][0];

// Checks for:
// X
// X
// X
for (int i = 0; i < 3; i++)
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
win = board[0][i];

// Checks for:
// X X
// X or X
// X X
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
win = board[1][1];

return win;
}


// using this board as a guide
// draw the board using "." for empty squares
// or 'X' or 'O' for player 1 or player 2
// constants
const char SIGIL[3] = { '.', 'X', 'O' };

// ...

void drawBoard(int board[3][3])
{
cout << " 0 1 2 ";
for (int i = 0; i < 3; ++i)
{
cout << i ;

for (int j = 0; j < 3; ++j)
{
cout << SIGIL[board[i][j]] << " ";
}
cout << endl;;
}
}


// return false if row or column are out of bounds
// or if that spot on the board is already taken
// otherwise return true
bool isMoveLegal(int board[3][3], int row, int column){
if (row >= 0 && row < 3 && column < 3 && column >= 0) {
if (board[row][column] == 0)
return true;
else return false;
}


// if any player has three in a row or if the board is full
// return true otherwise return false
bool isGameOver(int board[3][3]){
if (winner(board) == 1 || winner(board) == 2) return true;
for (int r = 0; r <= 2; r++)
for (int c = 0; c <= 2; c++)
if (board[r][c] == 0)
return false;
return true;
}


int main(){
int board[3][3] = { { 0 } }; // 0 for empty square, 1 or 2 for taken squares
int player = 1;
int row, column, result;
bool legalMove;

// starting board
drawBoard(board);
while (!isGameOver(board)){
cout << "Player " << player << "(" << SIGIL[player] << "), your move?";
cin >> row >> column;
legalMove = isMoveLegal(board, row, column);
while (!legalMove){
cout << "Player " << player << "(" << SIGIL[player] << "), your move?";
cin >> row >> column;
legalMove = isMoveLegal(board, row, column);
}
board[row][column] = player;
drawBoard(board);
player = 3 - player;
}
// game over
result = winner(board);
if (result == 0){
cout << "Tie Game" << endl;
}
else {
cout << "Player " << result << "(" << SIGIL[result] << ") wins!" << endl;
}

return 0;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote