Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need to code this. Confused on how to do it. Prints example like shown on at end of page.thanks // ticTacShell.c // // Shell of

Need to code this. Confused on how to do it. Prints example like shown on at end of page.thanks image text in transcribed

// ticTacShell.c

//

// Shell of the game 'TicTacToe' for CpSc 1010/1011

//

#include

#include // rand(), srand()

#include // time()

// Size of the board (square)

const int BOARD_SIZE = 3;

// Symbols used for the board

const char BLANK_SYMBOL = ' ';

const char COMP_SYMBOL = 'O';

const char HUMAN_SYMBOL = 'X';

// Human goes first

const int HUMANS_TURN = 0;

const int COMPUTERS_TURN = 1;

// Function prototypes

void initializeBoard(char board[BOARD_SIZE][BOARD_SIZE]);

int hasWon(char board[BOARD_SIZE][BOARD_SIZE], char mark);

int hasWonHorizontal(char board[BOARD_SIZE][BOARD_SIZE], char mark);

int hasWonVertical(char board[BOARD_SIZE][BOARD_SIZE], char mark);

int hasWonDiagonal(char board[BOARD_SIZE][BOARD_SIZE], char mark);

void getComputerMove(char board[BOARD_SIZE][BOARD_SIZE]);

void getHumanMove(char board[BOARD_SIZE][BOARD_SIZE]);

void printBoard(char board[BOARD_SIZE][BOARD_SIZE]);

void clearScreen(void);

//

// The main function should not be changed

//

int main(void) {

char board[BOARD_SIZE][BOARD_SIZE];

int humanWon = 0; // boolean (0/1)

int computerWon = 0; // boolean (0/1)

int move = 0;

// Seed the random number generator

srand(time(0));

initializeBoard(board);

while ((move

clearScreen();

if ((move % 2) == COMPUTERS_TURN) {

getComputerMove(board);

} else {

printBoard(board);

getHumanMove(board);

}

computerWon = hasWon(board, COMP_SYMBOL);

humanWon = hasWon(board, HUMAN_SYMBOL);

move++;

}

clearScreen();

printBoard(board);

if (humanWon) {

printf(">>>> You won! ");

} else if (computerWon) {

printf("

} else { // move >= BOARD_SIZE * BOARD_SIZE

printf("==== A Draw ");

}

return 0;

}

//

// Initialized the board to all BLANK_SYMBOL

//

void initializeBoard(char board[BOARD_SIZE][BOARD_SIZE]) {

int row;

for (row = 0; row

int col;

for (col = 0; col

board[row][col] = BLANK_SYMBOL;

}

}

}

//

// Determines if the 'mark' completely fills a row, column, or diagonal

// returns 1 if yes, 0 if no

//

int hasWon(char board[BOARD_SIZE][BOARD_SIZE], char mark) {

return hasWonHorizontal(board, mark)

|| hasWonVertical(board, mark)

|| hasWonDiagonal(board, mark);

}

//

// Determines if the 'mark' completely fills a row

// returns 1 if yes, 0 if no

//

int hasWonHorizontal(char board[BOARD_SIZE][BOARD_SIZE], char mark) {

int won = 0; // boolean (0/1). Assume lost until proven true

int row;

for (row = 0; row

int match = 1; // boolean (0/1)

int col;

for (col = 0; col

if (board[row][col] != mark) {

match = 0;

}

}

won = match;

}

return won;

}

//

// Determines if the 'mark' completely fills a column

// returns 1 if yes, 0 if no

//

int hasWonVertical(char board[BOARD_SIZE][BOARD_SIZE], char mark) {

/* INSERT CODE HERE */

return 0; // Stub -- make this return the correct value

}

//

// Determines if the 'mark' completely fills a diagonal

// returns 1 if yes, 0 if no

//

int hasWonDiagonal(char board[BOARD_SIZE][BOARD_SIZE], char mark) {

/* INSERT CODE HERE */

return 0; // Stub -- make this return the correct value

}

//

// Gets computer move by randomly picking an unoccupied cell

//

void getComputerMove(char board[BOARD_SIZE][BOARD_SIZE]) {

int row;

int col;

do {

row = rand() % BOARD_SIZE;

col = rand() % BOARD_SIZE;

} while (board[row][col] != BLANK_SYMBOL);

board[row][col] = COMP_SYMBOL;

}

//

// Gets human move by prompting user for row and column numbers

//

void getHumanMove(char board[BOARD_SIZE][BOARD_SIZE]) {

/* INSERT CODE HERE */

}

void printBoard(char board[BOARD_SIZE][BOARD_SIZE]) {

/* INSERT CODE HERE */

}

//

// Clears the screen -- uses ANSI terminal control codes

//

void clearScreen(void) {

const char ESC = 27;

printf("%c[2J%c[H", ESC, ESC);

}

image text in transcribed
K Lab 09 lab9.pdf CPSC 1010/1011 Lab 9 2-D Arrays & Tic Tac Toe this week's lab, we will take a look at simple 2-dimensional arrays. These are used for tables ofdata They also b can ed to store the board in some game. ownload the shell of a game that plays TicTaxToecalled tic TacShelle from this lab assignment in Canvas. fter downloading the game, save it (or rename it as ticTacProg.c. should compile and run. The computer just akes moves at ral ndom, while the human player does nothing. And yet the game sometimes ends in draw. Why? or th lab, you must finish the tic TacProgeprogram. It is recommended that you write it in stages. Add the code to display the board. Run it and check it's working. Add the code to get the wser's move: read two ints with rows nambered top to bottom and columns left to right. Run and check that is working. Add the code that tests for a vertical three in a-row. (Hint: look at the code for herizontal three-inarew. Runitan check that it's working. Add the code that tests for adiagonal three-in-a-row. Run it and check that it's working urn in Your Work 1. Before tuming in your assignment, make sure you have followed all of the instructions stated in this assignment and any additional instructions by your lab instructor( s Always test, test, and retest that your program compiles and runs successfully on our Unix machines before submitting 2. Show your TA that you completed the assignment. Then submit and upload your ticTacProg.c Program source code to the CPSC 10 Labs website on Canvass under the comectlab assignment. After you submit filets on Canvas for this program and other programs that you write this semester, always double check that you submitted the correct file(so to do this, download your submission from Canvas, view it, and make sure the submitted file(s) compiles and runs on our Unix machines. tyle Formatting and Commenting Reguirements 120points) 5 points) Commenting your source code The top of your file should have a header comment, which should contain: our name (first and last Lab Section A brief description about what the program does Any other helpful information that you think would be good to have. All functions should be commented about what they do, and significant blocks of source codeshauld all describe its functional ity in plain English (which means someone who has no programming experience should be able to read your comments understand its functionality). 5 points] variables should be declared at the top of the main function Ewhen appropriatel. and variables should have meal ningful names. 5 points] Always indent your source code in a proper, consistent, and readable way (if you have questions abou how to indent your code comectly, then ask your lab instructor before you submit your assignment) points) Your code should compile without any warming on our Unix machines. Don't forget to use the flag when compiling to check for all compiler wannings. Correct any compiler warmings before submittin Wall

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

2. Provide recommendations for effective on-the-job training.

Answered: 1 week ago