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

Overview In this assignment, the student will write a series of function definit

ID: 3702953 • Letter: O

Question

Overview

In this assignment, the student will write a series of function definitions for a Tic-Tac-Toe game interface based on pre-provided function specifications.

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

· Functions

· Interface Design

· Function Interface Descriptions

· Modular Design

Assignment

Write a program that displays a tic-tac-toe board and allows arbitrary moves to be made with a function call. Your program should include three functions and the provided driver code in main. One function should initialize your board with blank spaces. One function should neatly display the board on the screen, and the last function should allow an arbitrary move to be made for a given player’s symbol. You must write your code to manipulate a board of arbitrary dimensions from 1x1 up to 9x9. Your output should match the provided output as closely as possible. You may add additional functions and features at your discretion. However, in a minimal capacity, your program should contain the requested functionality and adhere to the spirit of the illustrated design pattern.

// INCLUDES

#include <stdio.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

#define MARKONE 'X'

#define MARKTWO 'O'

#define BLANK ' '

// PROTOTYPES

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

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

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

// MAIN

int main() {

// declare variables

char board[ROWS][COLS];

// initialize board

InitializeBoard(board);

// populate the board with moves

PlayerMove(1, 1, board, MARKONE); PlayerMove(1, 2, board, MARKONE); PlayerMove(4, 3, board, MARKONE); PlayerMove(1, 1, board, MARKTWO); PlayerMove(6, 2, board, MARKTWO); PlayerMove(4, 12, board, MARKTWO);

// display the board

DisplayBoard(board);

// exit program

return 0;

}

// FUNCTION IMPLEMENTATIONS

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

// YOUR IMPLEMENTATION GOES HERE

}

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

// YOUR IMPLEMENTATION GOES HERE

}

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

// YOUR IMPLEMENTATION GOES HERE

}

Explanation / Answer

void InitializeBoard(char board[ROWS][COLS])
{
char index = '1';
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
board[i][j] = index;
index++;
}
}
}


void DisplayBoard(char board[ROWS][COLS])
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%c", board[i][j]);
if (j != 2)
printf("|");
}
if (i != 2)
printf(" ---------");
printf(" ");
}
}

//Calculate the position after the moves

int PositionCalculation(char board[3][3], int selection, char input)
{
int column = selection % 3;
int row = selection / 3;

if (column == 0)
{
column += 3;
row -= 1;
}

if (board[row][column - 1] == 'o' || board[row][column - 1] == 'x')
return -1;

board[row][column - 1] = input;
return 0;
}


// Condition check for a win

bool ROWSCheck(char board[ROWS][COLS])
{
for (int i = 0; i < ROWS; i++)
{
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
return true;
}
}

bool COLSCheck(char board[ROWS][COLS])
{
for (int i = 0; i < COLS; i++)
{
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
return true;
}
}

bool DiagonalCheck(char board[ROWS][COLS])
{
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
return true;
}


// Main Program

#define ROWS 3
#define COLS 3

void TicTacToe()
{

int selection = 0;
int counter = 0;
int checker = 0;
char board[ROWS][COLS];


BoardInitializer(board);
DrawBoard(board);

while (counter != 9)
{
if (counter % 2 == 0)
{
printf(" Please input where you want to place X: ");
scanf(" %d", &selection);

SCANX:
while (selection < 0 || selection > 10)
{
printf("Invalid input, please try again: ");
scanf(" %d", &selection);
}

printf(" ");
checker = PositionCalculation(board, selection, 'x');

if (checker == -1)
{
selection = -1;
goto SCANX;
}

DrawBoard(board);
counter++;

if (counter > 3)
{
if (ROWSCheck(board) == true || COLSCheck(board) == true || DiagonalCheck(board) == true)
goto XWINS;
}
}
else
{
printf(" Please input where you want to place O: ");
scanf(" %d", &selection);

SCANO:
while (selection < 0 || selection > 10)
{
printf("Invalid input, please try again: ");
scanf(" %d", &selection);
}
printf(" ");

checker = PositionCalculation(board, selection, 'o');
if (checker == -1)
{
selection = -1;
goto SCANO;
}

DrawBoard(board);
counter++;

if (counter > 3)
{
if (ROWSCheck(board) == true || COLSCheck(board) == true || DiagonalCheck(board) == true)
goto OWINS;

}
}
}
printf("Its a draw ");
goto ENDPROGRAM;

XWINS:
printf(" X wins! ");
goto ENDPROGRAM;
OWINS:
printf(" O wins ");
goto ENDPROGRAM;

ENDPROGRAM:
return;
}