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

Here is the format to follow: // INCLUDES #include <stdio.h> #include <stdlib.h>

ID: 3659266 • Letter: H

Question

Here is the format to follow:

// INCLUDES

#include <stdio.h>

#include <stdlib.h>

// DEFINES

#ifndef __TRUE_FALSE__

#define __TRUE_FALSE__

#define TRUE 1

#define FALSE 0

#endif

// ROWS and COLS must be between 1 and 9

#define ROWS 7

#define COLS 7

// MARKER CODES

#define MARKONE 'X'

#define MARKTWO 'O'

#define BLANK ' '

// VICTORY CODES

#define NOWIN 0

#define MARKONEVICTORY 1

#define MARKTWOVICTORY 2

#define TIE 3

#define ERROR 4

#define EPIC_FAIL 5

// GAME PARAMETER CODES

#define CONSECUTIVE_MARKS_REQUIRED 3

// PROTOTYPES

void InitializeBoard(char[ROWS][COLS]);

void DisplayBoard(char[ROWS][COLS]);

int PlayerMove(int, int, char[ROWS][COLS], char);

int VictoryCheck(int, char[ROWS][COLS]);

void DisplayVictoryMessage(int);



//helper fxn

int helpcheckWin(int[],int, int);

int movesleft(char[ROWS][COLS]);



// MAIN

int main() {

// THE CORE LOGIC FOR YOUR GAME GOES HERE

// declare variables

char board[ROWS][COLS]; // Size of board



//exit program

return 0;

}

// FUNCTION IMPLEMENTATIONS

void InitializeBoard(char board[ROWS][COLS]) {

// INITIALIZE BOARD WITH BLANK SPACES


for(int i=0; i<ROWS; i++ ) {

for(int j=0; j<COLS; j++ ) {

board[i][j]= BLANK; // create blank squares on board

}

}

}

void DisplayBoard(char board[ROWS][COLS]) {

// DISPLAY BOARD ON SCREEN


printf("*************TIC-TAC-TOE********** ");


printf( " " );

for(int i=0; i<COLS; i++ ) {

printf( "%d ", i + 1 );

}

printf( " " );

for(int i=0; i<ROWS; i++ ) {

printf( "%d ", i + 1);

for(int j=0; j<COLS-1; j++ ) {

printf( "%c|", board[i][j] );

}


printf( " -" );

for(int j=0; j<COLS-1; j++ ) {

printf( "+-" , board[i][j]);

}

printf( " " );


}

}

int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {

// DESCRIBE PLAYERMOVES


if ( row <= 0 || col <= 0 || row >= ROWS || col >= COLS ) { // check for moves off of board

printf( "THE MOVE IS NOT ON THE BOARD " );

return FALSE;

}


else if (board[row-1][col-1] == MARKONE || // check for occupied spaces

board[row-1][col-1] == MARKTWO) {

printf( "THAT SPACE IS ALREADY OCCUPIED " );

return FALSE;

}


else {

board[row-1][col-1]=symbol; // check for good moves

return TRUE;

}


return FALSE;

}

int VictoryCheck(int winRequirement, char board[ROWS][COLS]) {

// CHECK IF WIN HAS OCCURRED


winRequirement = CONSECUTIVE_MARKS_REQUIRED;

int P1Wins=0,P2Wins=0; // Player1 and Player2 winnings

int cnt[2]={0}; //count, 0-P1, 1-P2

int i,j,k;

//horizontal

for(int i=0;i<ROWS;i++){

for(int j=0;j<COLS;j++){

if(board[i][j]==MARKONE){

P1Wins+=helpcheckWin(cnt,0,winRequirement);

}

else if(board[i][j]==MARKTWO){

P2Wins+=helpcheckWin(cnt,1,winRequirement);

}

else{

cnt[0]=0;

cnt[1]=0;

}


}//end for j

} //end for i


//vertical

cnt[0]=0;

cnt[1]=0;

for(int j=0;j<COLS;j++){

for(int i=0;i<ROWS;i++){

if(board[i][j]==MARKONE){

P1Wins+=helpcheckWin(cnt,0,winRequirement);

}

else if(board[i][j]==MARKTWO){

P2Wins+=helpcheckWin(cnt,1,winRequirement);

}

else{

cnt[0]=0;

cnt[1]=0;

}

}//end for j

} //end for i


//FILE *fo;

//fo=fopen("trace.txt","w");


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

//diagonal left

cnt[0]=0;

cnt[1]=0;


i=0;


for(int j=0;j<=COLS-winRequirement;j++){

// fprintf(fo," %d ",i);

for(int k=0;k+j<COLS &&k+i<ROWS;k++){

// fprintf(fo," Checking [%d,%d]",i+k,j+k);

if(board[i+k][j+k]==MARKONE){

P1Wins+=helpcheckWin(cnt,0,winRequirement);

}

else if(board[i+k][j+k]==MARKTWO){

P2Wins+=helpcheckWin(cnt,1,winRequirement);

}

else{

cnt[0]=0;

cnt[1]=0;

}

}


}

cnt[0]=0;

cnt[1]=0;

j=0;

for(int i=1;i<=ROWS-winRequirement;i++){

//fprintf(fo," %d ",i);

for(int k=0;k+j<COLS &&k+i<ROWS;k++){

// fprintf(fo," Checking [%d,%d]",i+k,j+k);

if(board[i+k][j+k]==MARKONE){

P1Wins+=helpcheckWin(cnt,0,winRequirement);

}

else if(board[i+k][j+k]==MARKTWO){

P2Wins+=helpcheckWin(cnt,1,winRequirement);

}

else {

cnt[0]=0;

cnt[1]=0;

}

}


}


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

//diagonal right


cnt[0]=0;

cnt[1]=0;


i=0;


for(j=COLS-1;j-winRequirement+1>=0;j--){

//fprintf(fo," %d ",i);

for(k=0;j-k>=0 &&k+i<ROWS;k++){

// fprintf(fo," Checking [%d,%d]",i+k,j-k);

if(board[i+k][j-k]==MARKONE){

P1Wins+=helpcheckWin(cnt,0,winRequirement);

}

else if(board[i+k][j-k]==MARKTWO){

P2Wins+=helpcheckWin(cnt,1,winRequirement);

}

else {

cnt[0]=0;

cnt[1]=0;

}

}


}

cnt[0]=0;

cnt[1]=0;


j=COLS-1;

for(i=1;i<=ROWS-winRequirement;i++){

//fprintf(fo," %d ",i);

for(k=0;j-k>=0 &&k+i<ROWS;k++){

//fprintf(fo," Checking [%d,%d]",i+k,j-k);

if(board[i+k][j-k]==MARKONE){

P1Wins+=helpcheckWin(cnt,0,winRequirement);

}

else if(board[i+k][j-k]==MARKTWO){

P2Wins+=helpcheckWin(cnt,1,winRequirement);

}

else {

cnt[0]=0;

cnt[1]=0;

}

}


}

//fclose(fo);


//__________________________________________

if(P1Wins==0&&P2Wins==0){

if(movesleft(board)==1)

return NOWIN;

else

return TIE;

}

else if(P1Wins>0&&P2Wins==0){return MARKONEVICTORY;}

else if(P1Wins==0&&P2Wins>0){return MARKTWOVICTORY;}

else if(P1Wins>0&&P2Wins>0){return ERROR;}

else

return EPIC_FAIL;


}//end ck


int helpcheckWin(int arr[],int idx, int req){ //returns either 1 for win or 0 for not win

arr[idx]++; //increment this idx

arr[(idx+1)%2]=0; //set other idx to 0


if(arr[idx]==req){

arr[idx]=0; //reset to 0

return 1; //return 1 to be added to win

}

return 0;//if not a win, return 0

}

int movesleft(char b[ROWS][COLS]){

int i,j;


for(i=0;i<ROWS;i++){

for(j=0;j<COLS;j++){

if(b[i][j]==BLANK)

return 1;

}

}

return 0;//if no BLANK found



}

void DisplayVictoryMessage(int victoryCode) {

// AN IMPLEMENTATION FOR THIS FUNCTION WAS PROVIDED IN A WEEKLY ASSIGNMENT


switch(victoryCode) {

case NOWIN:

printf("There is still no winner. ");

break;

case MARKONEVICTORY:

printf("MARKONE has won the game. ");

break;

case MARKTWOVICTORY:

printf("MARKTWO has won the game. ");

break;

case TIE:

printf("The game is a draw. ");

break;

case ERROR:

printf("Something bad happened... MARKONE and MARKTWO have both won. ");

break;

case EPIC_FAIL:

printf("Something bad happened... VictoryCheck() has produced an impossible combination of return code indicators. ");

break;

default:

printf("DisplayVictoryMessage() was passed an invalid victoryCode. ");

}

}

Explanation / Answer

// INCLUDES #include // DEFINES #ifndef __TRUE_FALSE__ #define __TRUE_FALSE__ #define TRUE 1 #define FALSE 0 #endif // ROWS and COLS must be between 1 and 9 #define ROWS 7 #define COLS 7 // MARKER CODES #define MARKONE 'X' #define MARKTWO 'O' #define BLANK ' ' // VICTORY CODES #define NOWIN 0 #define MARKONEVICTORY 1 #define MARKTWOVICTORY 2 #define TIE 3 #define ERROR 4 #define EPIC_FAIL 5 // GAME PARAMETER CODES #define CONSECUTIVE_MARKS_REQUIRED 3 // PROTOTYPES void InitializeBoard(char[ROWS][COLS]); void DisplayBoard(char[ROWS][COLS]); int PlayerMove(int, int, char[ROWS][COLS], char); int VictoryCheck(int, char[ROWS][COLS]); void DisplayVictoryMessage(int); //helper fxn int helpcheckWin(int[],int, int); int movesleft(char[ROWS][COLS]); // MAIN int main() { char board[ROWS][COLS]; char marker[2]={MARKONE,MARKTWO}; char player[2][8]={"MARKONE","MARKTWO"}; int rChoice,cChoice; int turn=0; int code; InitializeBoard(board); do{ DisplayBoard(board); printf(" MARKONE: %c MARKTWO: %c",MARKONE,MARKTWO); do{ printf(" %s's turn ",player[turn%2]); printf("Enter a move (Row,Column): "); scanf("%d,%d",&rChoice,&cChoice); }while(PlayerMove(rChoice,cChoice,board,marker[turn%2])==0); turn++; code=VictoryCheck(CONSECUTIVE_MARKS_REQUIRED,board); DisplayVictoryMessage(code); }while(code==0); printf("Press any key to continue . . . "); getch(); // exit program return 0; } // FUNCTION IMPLEMENTATIONS void InitializeBoard(char board[ROWS][COLS]) { // INITIALIZE BOARD WITH BLANK SPACES int i,j; for( i=0; i