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

Tic-Tac-Toe Victory Checker Overview In this assignment, the student will write

ID: 3710448 • Letter: T

Question

Tic-Tac-Toe Victory Checker

Overview

In this assignment, the student will write a program that implements and verifies the correctness of a state sensing algorithm within a two-dimensional array.

When completing this assignment, the student should demonstrate mastery of the following concepts:

· Visual Studio Project Creation

· C Formatted Output (printf())

· Conversion Characters

· Escape Sequences

Assignment

Write an implementation for the function VictoryCheck() in for your Tic-Tac-Toe program.

The function should take two arguments. The first argument is a single integer argument that contains the number of adjacent symbols a player must produce to win the game. The second argument should be a two-dimensional array representing the board. The number of adjacent symbols a player needs to produce the win the game is defined in the constant CONSECUTIVE_MARKS_REQUIRED.

VictoryCheck() should scan through the board and determine if a player has produced the appropriate number of adjacent symbols required to win the game. The adjacent symbols can be oriented in a horizontal, vertical or diagonal fashion. VictoryCheck() should return one of the provided victory codes to the driver. Please use the provided driver code to test your function for correctness. Take note that a scenario where the two players reach a draw in the game is not provided.

The provided driver code produces a basic test to give you an idea on how to develop a testing driver in main(). However, the driver can be modified if you wish to code a board population scenario to produce a TIE condition. Additionally, the return values from the functions are not being collected and used to display informative messages in main(). Remember, you should limit the interface message printing routines to main(), the functions should only be return code values that are later interpreted by main() (DisplayBoard() would be an exception to this rule).

Please read the provided driver code carefully before beginning to attack this problem to understand the organizational structure and identifier names you must use in your program. In particular, pay close attention to the function DisplayVictoryMessage(), which has been written for you to get you moving in the correct direction.

Start Up Code:

#include <stdio.h>
#include <conio.h>

#define TRUE   1
#define FALSE   0

#define ROWS   3
#define COLS   3

#define MARK_ONE  'X'
#define MARK_TWO  'O'
#define BLANK   '*'

#define MOVE_OK   1
#define MOVE_COLLISION 2
#define MOVE_OOB  3

#define IN_PROGRESS  1
#define X_WINS   2
#define O_WINS   3
#define TIE    4
#define ERROR   5

#define WIN_REQUIREMENT 3

void InitializeBoard(char[ROWS][COLS]);
void DisplayBoard(char[ROWS][COLS]);
int PlayerMove(int, int, char[ROWS][COLS], char);
int VictoryCheck(char[ROWS][COLS], int);

int main() {
char theBoard[ROWS][COLS];

InitializeBoard(theBoard);
DisplayBoard(theBoard);

_getch();
return 0;
}

void InitializeBoard(char initializeMe[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
  for (int j = 0; j < COLS; j++) {
   initializeMe[i][j] = BLANK;
  }
}
}
void DisplayBoard(char showMe[ROWS][COLS]) {
for (int i = 0; i < ROWS; i++) {
  for (int j = 0; j < COLS; j++) {
   printf("%-3c", showMe[i][j]);
  }
  printf(" ");
}
}
int PlayerMove(int requestedRow, int requestedColumn, char theBoard[ROWS][COLS], char symbol) {
int returnCode;

// Is this move a out of bounds?
if (requestedRow > ROWS || requestedColumn > COLS ||
  requestedRow <= 0 || requestedColumn <= 0) {
  returnCode = MOVE_OOB;
}
// Is the move a collision?
else if (theBoard[requestedRow][requestedColumn] != BLANK) {
  returnCode = MOVE_COLLISION;
}
else {
  theBoard[requestedRow - 1][requestedColumn - 1] = symbol;
  returnCode = MOVE_OK;
}

return returnCode;
}

int VictoryCheck(char checkMe[ROWS][COLS], int winRequirement) {
int blankSeen = FALSE;
int XPresents = FALSE;
int OPresents = FALSE;

// Is there a blank space on the board?
for (int i = 0; i < ROWS; i++) {
  for (int j = 0; j < COLS; j++) {
   if (checkMe[i][j] == BLANK)
    blankSeen = TRUE;
  }
}

// Check to see if X or O present victory.
for (int i = 0; i < ROWS; i++) {
  for (int j = 0; j < COLS; j++) {
   // To check and see if one of the tokens is presenting
   // a victory, write a loop that moves along the
   // spots indicated by the direction of the scan. As
   // you move to each spot, make sure it is logically
   // legal on the board. If you see the same symbol
   // at each point in the scan and the symbol isn't a
   // blank space, you can conclude that token has
   // presented victory and the appropriate flag
   // (XPresents, OPresents) can be set to TRUE.

   // Scan in a "vertical" direction.

   // Scan in a "horizontal" direction.

   // Scan in a "diagonal-up" direction.

   // Scan in a "diagonal-down" direction.
  }
}
return 0;
}

Explanation / Answer

//Here is my program will help u it support two player and AI also.

//tictactoe_header.h           HEADER FILE

#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);
           }
}


/////////////////////////////////////////////////////////////////

//MAIN PROGRAM FILE

#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;
}

///END need g++ compiler if u r intell person then compile it and use cause i sell this program for 5000 RS

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