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

TIC TAC TOE C++ , need to using getline and check all the values are correct. Hu

ID: 663580 • Letter: T

Question

TIC TAC TOE

C++ , need to using getline and check all the values are correct.

Human plays against the computer (use one-dimensional arrays

In this option you will develop a program in which a human plays against the computer.
1. Validate user input at every opportunity.
a. Do not allow number entries less than 0
b. Do not allow number entries greater than 8
c. Do not allow non-numeric entries
2. Do not use global variables in the development of the program
3. You may use global constants in the development of the program
4. Use one-dimensional arrays to keep track of the game:
a. Computer moves
b. Human moves
5. Use functions to pass arrays and implement other program requirements.
6. The program must be developed using functions so that the main() function consists mostly of function calls
7. Computer must be aggressive and take every opportunity to win the game if and when the human makes errors
8. The main() function must use a loop to keep the user in the program until he/she wants to quit.

splashScreen()//displays game and developer’s information
askForUserNames()//requests for username
validateUserName()//validate username
switchPlayer()//switch from one player to another
resetGame()//reset the game when one concludes; this includes filling the array with vales 0-8
displayGrid()//display the grid after each player makes a move
playerMakeMove()//prompts player to make a move, invokes validatePlayersMove, checkPositionAvailability
validatePlayersMove()//validates that user entry X is such that 0<=X<=8
checkPositionAvailability()//check that the position selected by the user is available
checkWin()//check for a winning player
checkTie()//check for a tie

makeBestMove()//select best option
computerMakeMove()//used to make the move, in other words populate the array

Explanation / Answer

#include <iostream>
using namespace std;

int square[9] = {-1,-1,-1,-1,-1,-1,-1,-1,-1};

int checkwin(){
   if (square[1] == square[2] && square[2] == square[3]) return 1;
   else if (square[4] == square[5] && square[5] == square[6])   return 1;
   else if (square[7] == square[8] && square[8] == square[9])   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[3] == square[6] && square[6] == square[9])   return 1;
   else if (square[1] == square[5] && square[5] == square[9])   return 1;
   else if (square[3] == square[5] && square[5] == square[7])   return 1;
   else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')
return 0;
   else
       return -1;
}

void board()
{
   system("cls");
   cout << " Tic Tac Toe ";

   cout << "Player 1 (O) - Player 2 (X)" << endl << endl;
   cout << endl;

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

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

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

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

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

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

int main()
{
   int player = 1,i,choice;

   char mark;
   do
   {
       board();
       player=(player%2)?1:2;

       cout << "Player " << player << ", enter a number: ";
       cin >> choice;

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

       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 if (choice == 9 && square[9] == '9')

           square[9] = mark;
       else
       {
           cout<<"Invalid move ";

           player--;
           cin.ignore();
           cin.get();
       }
       i=checkwin();

       player++;
   }while(i==-1);
   board();
   if(i==1)

       cout<<"==>Player "<<--player<<" win ";
   else
       cout<<"==>Game draw";

   cin.ignore();
   cin.get();
   return 0;
}