Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming Overview In this assignment, the student will write a program that implements and verifies the correctness of a state sensing algorithm within a

C Programming

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

#include

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

// TEST TEST TEST!!!!

InitializeBoard(theBoard);

DisplayBoard(theBoard);

PlayerMove(1, 1, theBoard, MARK_TWO);

PlayerMove(1, 2, theBoard, MARK_TWO);

PlayerMove(1, 3, theBoard, MARK_TWO);

DisplayBoard(theBoard);

int returnCode = VictoryCheck(theBoard, WIN_REQUIREMENT);

printf("The return code is: %d ", returnCode);

_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.

int possiblePresentation = FALSE;

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.

possiblePresentation = TRUE;

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

// Make sure the forged index doesn't go out of bounds

if (j + k >= COLS)

possiblePresentation = FALSE;

if (checkMe[i][j] != checkMe[i][j + k])

possiblePresentation = FALSE;

}

// If there was a potential presentation, determine

// which symbol is showing it (X or O).

if (possiblePresentation == TRUE) {

if (checkMe[i][j] == MARK_ONE)

XPresents = TRUE;

else if (checkMe[i][j] == MARK_TWO)

OPresents = TRUE;

}

?

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

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

}

}

// At this point... blankseem, XPresents and OPresents have valid values.

// Determine which code to return.

int returnCode = 0;

if (blankSeen == TRUE && XPresents == FALSE && OPresents == FALSE) {

returnCode = IN_PROGRESS;

}

else if (blankSeen == TRUE && XPresents == FALSE && OPresents == FALSE) {

returnCode = TIE;

}

else if (XPresents == TRUE && OPresents == FALSE) {

returnCode = X_WINS;

}

else if (XPresents == FALSE && OPresents == TRUE) {

returnCode = O_WINS;

}

else if (XPresents == TRUE && OPresents == TRUE) {

returnCode = ERROR;

}

return returnCode;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions