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

Students will create a TTT game using the console app. Requirements: 1. The play

ID: 3719220 • Letter: S

Question

Students will create a TTT game using the console app.

Requirements:

1. The playing screen will be at least 40 x 40, be divided into nine squares and have a number in the top center or each square relevant to their location. (see above)

2. The x’s and o’x will occupy a significant portion of each square when the game decides to create them in the appropriate square

3. Game data (whether X or O was chosen for each square) will be stored in a 3x3 array.

4. The game must be designed so that only the integers 1 – 10 will be accepted as input.

5. The following functions must be present (passed parameters decided by coder):
                                    a. void draw_board()
                                    b. void draw_x()
                                    c. void draw_y()
                                    d. void place_marker()
                                                      - must pass pointer to game matrix.
                                          - must use a switch to place the appropriate marker.
                                    e. void check_for_win()
                                         - must use for loops to determine if a move won game.

                                                      

6. Other functions can be added as need especially the gotoxy() function.

7. Student should upload the .c file on the Google Drive.

8. NO GLOBAL VARIABLES ARE ALLOWED TO BE USED.  GLOBAL CONSTANTS ARE OK.

9. The game will be run as expected.  It will be opened to the game board and the command line at the bottom.  Input will be in integers, you do not have to account for non integer input.  If the box has already been chosen, obviously it cannot be used again and the player should be informed of this and asked to make a new choice.  After the appropriate number of inputs by players (you are not playing against the computer) the game should be checked for a victor.  If a victory occurs, the users should be told who won and offered the choice of playing again or exiting.

Explanation / Answer

//Im going to provide u whole tic tac toe game with AI implemented and user1 and user2 also support very well

// This is going to be crezy for u because this is a whole TTT code with AI aupport only u need to RUN PROPERLY

//How to Run> im going to attach two files one is "tictactow_header.h" file this will contain header and some function regarding with game u need to save this in one folder

//im attach this first

//Second im attach tictactoe.cpp file only u need to run on console

// g++ tictactoe.c++ and after ./a.out FOLLOW CONSOLE instruction then.

/*FILE ONE tictactoe_header.h save it

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;

typedef struct TicTacToe
{
   int current_mov;//which user moved this state || index number
    int status;
   int tic[3][3];
   struct TicTacToe *node[9];
}tictactoe;

char name1[80];           //User Entry
char symbol_1;
char name2[80];
char symbol_2;

tictactoe *newTree()
{
   tictactoe *newNode;
   newNode=(tictactoe *)malloc(sizeof(tictactoe));
   newNode->current_mov=-1;
        newNode->status=0;
   for(int i=0;i<3;i++)
       for(int j=0;j<3;j++)
           newNode->tic[i][j]=0;
   for(int i=0;i<9;i++)
       newNode->node[i]=NULL;
   return newNode;
}

//Name: newTree
//Disc:   It creates new node of tic tac toe
//Date:   14/9/2017

void Display(tictactoe *tree,int turn)
{
   if(tree==NULL){
       printf("NULL ");return ;}
   //system("clear");
   printf(" Player1-[%c] [%s] Player2-[%c] [%s] ",symbol_1,name1,symbol_2,name2);
   for(int k=0;k<13;k++)
       printf("-");
   printf(" ");
   for(int i=0;i<3;i++){
       printf("|");
       for(int j=0;j<3;j++){
           if(tree->tic[i][j]==1)
               printf(" %c |",symbol_1);
           else if(tree->tic[i][j]==2)
               printf(" %c |",symbol_2);
           else
               printf("   |");
       }
   printf(" ");
   for(int k=0;k<13;k++)
       printf("-");
   printf(" ");
   }
   if(turn>0)
       printf(" Player %d turn- ",turn);
}
void ChooseSymbol(int user)
{
   char sym[3]={'O','X'};
   printf(" 1.   O 2.   X ");
   int choice;
   printf("Chhose symbol- ");
   scanf("%d",&choice);
   if(choice==1){
       symbol_1=sym[0];
       symbol_2=sym[1];
   }
   else
   {
       symbol_1=sym[1];
       symbol_2=sym[0];
   }

}

//Disc:   Check win or continue
//Return 1   Win Player1
//Return 2   Win Player2
//Return 4   Continue (rest of game)
//Return 3   End   Tie

int Check(tictactoe *t)
{
   int no;
   no=t->tic[0][0];
   if(t->tic[0][0]==no && t->tic[0][1]==no && t->tic[0][2]==no)
       return no;
   no=t->tic[1][0];
   if(t->tic[1][0]==no && t->tic[1][1]==no && t->tic[1][2]==no)
       return no;
   no=t->tic[2][0];
   if(t->tic[2][0]==no && t->tic[2][1]==no && t->tic[2][2]==no)
       return no;
   no=t->tic[0][0];
   if(t->tic[0][0]==no && t->tic[1][0]==no && t->tic[2][0]==no)
       return no;
   no=t->tic[0][1];
   if(t->tic[0][1]==no && t->tic[1][1]==no && t->tic[2][1]==no)
       return no;
   no=t->tic[0][2];
   if(t->tic[0][2]==no && t->tic[1][2]==no && t->tic[2][2]==no)
       return no;
   no=t->tic[0][0];
   if(t->tic[0][0]==no && t->tic[1][1]==no && t->tic[2][2]==no)
       return no;
   no=t->tic[1][1];
   if(t->tic[0][2]==no && t->tic[1][1]==no && t->tic[2][0]==no)
       return no;
   else
   {
       int flag=0;
       for(int i=0;i<3;i++)
           for(int j=0;j<3;j++)
               if(t->tic[i][j]==0)
                   flag=1;
       if(flag==1)
           return 4;//continue to play
       else
           return 3;//tie game
   }
}

void DisplayWin(tictactoe *t,int id)
{
   system("clear");
   Display(t,-1);
   printf(" Player %d Win",id);
   char ch[10];
   printf(" You wan to play again (y/n)- ");
   scanf("%s",ch);
   if(strcmp(ch,"n")==0)
       exit(0);
   free(t);
   t=newTree();
}


int convertMove(int i,int j)
{
   if(i==0 && j==0)
       return 1;
   else if(i==0 && j==1)
       return 2;
   else if(i==0 && j==2)
       return 3;
   else if(i==1 && j==0)
       return 4;
   else if(i==1 && j==1)
       return 5;
   else if(i==1 && j==2)
       return 6;
   else if(i==2 && j==0)
       return 7;
   else if(i==2 && j==1)
       return 8;
   else if(i==2 && j==2)
       return 9;
}
int addMove(tictactoe *t,int turn,int user)       //return 1 for laready one and 0 for update turn
{
   if(turn==1 && t->tic[0][0]==0){
       t->tic[0][0]=user;
       return 0;}
   else if(turn==2 && t->tic[0][1]==0){
       t->tic[0][1]=user;
       return 0;}
   else if(turn==3 && t->tic[0][2]==0){
       t->tic[0][2]=user;
       return 0;}
   else if(turn==4 && t->tic[1][0]==0){
       t->tic[1][0]=user;
       return 0;}
   else if(turn==5 && t->tic[1][1]==0){
       t->tic[1][1]=user;
       return 0;}
   else if(turn==6 && t->tic[1][2]==0){
       t->tic[1][2]=user;
       return 0;}
   else if(turn==7 && t->tic[2][0]==0){
       t->tic[2][0]=user;
       return 0;}
   else if(turn==8 && t->tic[2][1]==0){
       t->tic[2][1]=user;
       return 0;}
   else if(turn==9 && t->tic[2][2]==0){
       t->tic[2][2]=user;
       return 0;}
   else
       return 1;
}


//Disc:   use for user1 and user2 game only
void U1U2(tictactoe *t)
{
   Display(t,0);
   while(1)
   {
       int flag=Check(t);
       if(flag==1 || flag==2){
           DisplayWin(t,1);break;}
       if(flag==4){
           DisplayWin(t,4);break;}
       int turn;
       Display(t,1);
       scanf("%d",&turn);
       addMove(t,turn,1);

       flag=Check(t);
       if(flag==1 || flag==2){
           DisplayWin(t,1);break;}
       if(flag==4){
           DisplayWin(t,4);break;}
       Display(t,2);
       scanf("%d",&turn);
       addMove(t,turn,2);
   }
}
int getMax_arr_value_index(int *arr)
{
   int max=0,index=0;
   for(int i=0;i<9;i++)
       if(arr[i]>max){
           max=arr[i];index=i;}
   return index;
}
int getSum_arr(int *arr,int index)
{
   if(index==9)
       return 0;
   else
       return arr[index]+getSum_arr(arr,index+1);
}
int Height(tictactoe *t)
{
           if(t==NULL)
               return 0;
           else
           {
               int arr[9];
               for(int i=0;i<9;i++)
                   arr[i]=1+Height(t->node[i]);
               return getMax_arr_value_index(arr);
           }
}

*/

/* FILE @ tictactoe.cpp

#include<string.h>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include "tictactoe_header.h"
using namespace std;
static int counter=0;

tictactoe *copyData(tictactoe *old,tictactoe *t)
{
   for(int i=0;i<3;i++)
       for(int j=0;j<3;j++)
       {
           t->tic[i][j]=old->tic[i][j];
       }
   return t;
}
void D(tictactoe *t)
{
   printf(" ");
   for(int i=0;i<3;i++){
       for(int j=0;j<3;j++){
           printf("%d ",t->tic[i][j]);}printf(" ");}

}
tictactoe *genTree(tictactoe *t,tictactoe *old,int times,int turn)
{
   if(times==0)
       return t;
   if(t==NULL)
   {
       t=newTree();
   }
   if(old!=NULL)
   {
       t=copyData(old,t);
   }
   //Display(t,-1);
   int i=0;
   //printf("(%d) ",counter);
   for(int k=0;k<9;k++){
       t->node[k]=genTree(t->node[k],t,times-1,turn);
   for(int i=0;i<3;i++){
       for(int j=0;j<3;j++){
           if(t->tic[i][j]==0)
           {
               int mov=convertMove(i,j);
               int temp=addMove(t,mov,turn);
               //printf(" [%d]-[%d] ",mov,t->tic[i][j]);
               if(turn==0){
                   t->node[mov-1]=genTree(t->node[mov-1],t,times-1,2);
                   }
               if(turn==1){
                       t->node[mov-1]=genTree(t->node[mov-1],t,times-1,2);
                   }
               else{
                       t->node[mov-1]=genTree(t->node[mov-1],t,times-1,1);
                   }
               counter++;
   //printf("(%d) ",counter);
           }
       }
   }
   return t;
   }
   return t;
}

void U1AI()
{
   //Display(t,-1);
   tictactoe *t=newTree();
   t=genTree(t,t,9,0);
   printf("Total Count-[%d]",counter);
   Display(t,-1);
}

void Start()
{
   tictactoe *t;
   printf("Size of tree-%d",sizeof(struct TicTacToe));
   t=newTree();
  
   do{
       //system("clear");
       printf(" 1.User1 with User2 2.User with AI 3.Replay 4.Exit ");
       int choice;
       scanf("%d",&choice);
       switch(choice)
       {
           case 1://User1 with User2
               printf(" Enter user1 name-");
               scanf("%s",name1);
               ChooseSymbol(1);
               printf(" Enter user2 name-");
               scanf("%s",name2);
               U1U2(t);  
               break;
           case 2:
               /*printf(" Enter user1 name-");
               scanf("%s",name1);
               ChooseSymbol(1);
               strcpy(name2,"Computer");*/
               ChooseSymbol(1);
               U1AI();
               break;
           case 3:
               system("clear");
               free(t);
               t=newTree();                              
               break;
           default:
               printf(" Thank You for Played ");
               exit(0);
              
       }
   }while(1);
}


int main()
{
   Start();
   return 0;
}

*/