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

//this program has to be sloved in c++ and strictly follow the instractions . Wr

ID: 3722901 • Letter: #

Question

//this program has to be sloved in c++ and strictly follow the instractions .

Write a modular program that allows two players to play a game of tic-tac-toe. Use a two dimensional char array with 3 rows and 3 columns as the game board. Each element of

the array should be initialized with an asterisk (*). The program should display the initial

board configuration and then start a loop that does the following:

• Allow player 1 to select a location on the board for an X by entering a row and column

number. Then redisplay the board with an X replacing the * in the chosen location.

• If there is no winner yet and the board is not yet full, allow player 2 to select a location

on the board for an O by entering a row and column number. Then redisplay the

board with an O replacing the * in the chosen location.

The loop should continue until a player has won or a tie has occurred, then display a message

indicating who won, or reporting that a tie occurred.

• Player 1 wins when there are three Xs in a row, a column, or a diagonal on the game

Board.

• Player 2 wins when there are three Os in a row, a column, or a diagonal on the game

Board.

• A tie occurs when all of the locations on the board are full, but there is no winner.

Input Validation: Only allow legal moves to be entered. The row must be 1, 2, or 3.

The column must be 1, 2, or 3. The (row, column) position entered must currently be

empty (i.e., still have an asterisk in it).

Explanation / Answer

here is your program : --------------->>>>>>>>>>>>>

#include<stdio.h>

void initialize(char arr[3][3]){
int i,j;
for(i = 0;i<3;i++){
  for(j = 0;j<3;j++){
   arr[i][j] = '*';
  }
}
}

void printArray(char arr[3][3]){
system("cls");
printf("***************************** Board *************************** ");
int i,j;
printf(" ___________________ ");
for(i = 0;i<3;i++){
  printf(" ");
  for(j = 0;j<3;j++){
   printf("| %c ",arr[i][j]);
  }
  printf("| ");
  printf(" ___________________ ");
}
}
char playerChoice(){
char ch;
while(1){
  printf(" Enter your choice 'O' or 'X' : ");
  ch = getchar();
  if(ch == 'O' || ch == 'X'){
   break;
  }else{
   printf(" Wrong choice : ");
  }
}

return ch;
}
void computerMove(char player,char arr[3][3]){
if(arr[1][1] == ' '){
  arr[1][1] = player;
}else if(arr[0][0] == ' '){
  arr[0][0] = player;
}else if(arr[0][2] == ' '){
  arr[0][2] = player;
}else if(arr[2][0] == ' '){
  arr[2][0] = player;
}else if(arr[2][2] == ' '){
  arr[2][2] = player;
}else if(arr[0][1] == ' '){
  arr[0][1] = player;
}else if(arr[1][0] == ' '){
  arr[1][0] = player;
}else if(arr[1][2] == ' '){
  arr[1][2] = player;
}else if(arr[2][1] == ' '){
  arr[2][1] = player;
}
}

void getPlayerMove(char player,char arr[3][3]){
int i,j;
while(1){
  printf(" Enter row(0-2) : ");
  scanf("%d",&i);
  printf(" Enter col(0-2) : ");
  scanf("%d",&j);
  if(arr[i][j] == '*'){
   arr[i][j] = player;
   break;
  }else{
   printf(" Space Occupied : ");
  }
}
}

int checkWinning(char player,char ttt[3][3]){
if(ttt[0][0] == player && ttt[0][1] == player && ttt[0][2] == player){
            return 1;
        }
        if(ttt[0][0] == player && ttt[1][0] == player && ttt[2][0] == player){
            return 1;
        }
        if(ttt[0][0] == player && ttt[1][1] == player && ttt[2][2] == player){
            return 1;
        }
        if(ttt[1][0] == player && ttt[1][1] == player && ttt[1][2] == player){
            return 1;
        }
        if(ttt[2][0] == player && ttt[2][1] == player && ttt[2][2] == player){
            return 1;
        }
        if(ttt[0][2] == player && ttt[1][1] == player && ttt[2][0] == player){
            return 1;
        }
        if(ttt[0][1] == player && ttt[1][1] == player && ttt[2][1] == player){
            return 1;
        }
        if(ttt[0][2] == player && ttt[1][2] == player && ttt[2][2] == player){
            return 1;
        }

        return 0;
}
int checkDraw(char arr[3][3]){
int i,j;
for(i = 0;i<3;i++){
  for(j = 0;j<3;j++){
   if(arr[i][j] == '*'){
    return 0;
   }
  }
}

return 1;
}

int main(){
char arr[3][3];
char player1;
char player2;
int st = 0;
player1 = playerChoice();
if(player1 == 'O'){
  player2 = 'X';
}else{
  player2 = 'O';
}
printf(" Player = %c,Computer = %c",player1,player2);
initialize(arr);
while(!checkDraw(arr)){
  printArray(arr);
  if(st == 0){
   printf(" player 1 : ");
   getPlayerMove(player1,arr);
   if(checkWinning(player1,arr)){
    printArray(arr);
    printf(" Player 1 Wins");
    break;
   }
   st = 1;
  }else if(st == 1){
   printf(" player 2 : ");
   getPlayerMove(player2,arr);
   if(checkWinning(player2,arr)){
    printArray(arr);
    printf(" Player2 Wins");
    break;
   }
   st = 0;
  }
}
if(checkDraw(arr)){
  printf(" Game Draw");
}

return 0;
}