// ticTacShell.c // // Shell of the game \'TicTacToe\' for CpSc 1010/1011 // #in
ID: 3805456 • Letter: #
Question
// ticTacShell.c
//
// Shell of the game 'TicTacToe' for CpSc 1010/1011
//
#include <stdio.h>
#include <stdlib.h> // rand(), srand()
#include <time.h> // 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 < (BOARD_SIZE * BOARD_SIZE)) && !humanWon && !computerWon) {
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("<<<< I won! ");
} 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 < BOARD_SIZE; row++) {
int col;
for (col = 0; col < BOARD_SIZE; 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 < BOARD_SIZE && !won; row++) {
int match = 1; // boolean (0/1)
int col;
for (col = 0; col < BOARD_SIZE; 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);
}
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 WallExplanation / Answer
Here is the code for above scenario:
void showBoard(char board[][SIDE])
{
printf(" ");
printf(" %c | %c | %c ", board[0][0],
board[0][1], board[0][2]);
printf(" -------------- ");
printf(" %c | %c | %c ", board[1][0],
board[1][1], board[1][2]);
printf(" -------------- ");
printf(" %c | %c | %c ", board[2][0],
board[2][1], board[2][2]);
return;
}
// A function to show the instructions
void showInstructions()
{
printf(" Tic-Tac-Toe ");
printf("Choose a cell numbered from 1 to 9 as below"
" and play ");
printf(" 1 | 2 | 3 ");
printf(" -------------- ");
printf(" 4 | 5 | 6 ");
printf(" -------------- ");
printf(" 7 | 8 | 9 ");
printf("- - - - - - - - - - ");
return;
}
// A function to initialise the game
void initialise(char board[][SIDE], int moves[])
{
// Initiate the random number generator so that
// the same configuration doesn't arises
srand(time(NULL));
// Initially the board is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = ' ';
}
// Fill the moves with numbers
for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;
// randomise the moves
random_shuffle(moves, moves + SIDE*SIDE);
return;
}
// A function to declare the winner of the game
void declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
printf("COMPUTER has won ");
else
printf("HUMAN has won ");
return;
}
// A function that returns true if any of the row
// is crossed with the same player's move
bool rowCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != ' ')
return (true);
}
return(false);
}
// A function that returns true if any of the column
// is crossed with the same player's move
bool columnCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != ' ')
return (true);
}
return(false);
}
// A function that returns true if any of the diagonal
// is crossed with the same player's move
bool diagonalCrossed(char board[][SIDE])
{
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != ' ')
return(true);
if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != ' ')
return(true);
return(false);
}
// A function that returns true if the game is over
// else it returns a false
bool gameOver(char board[][SIDE])
{
return(rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board) );
}
// A function to play Tic-Tac-Toe
void playTicTacToe(int whoseTurn)
{
// A 3*3 Tic-Tac-Toe board for playing
char board[SIDE][SIDE];
int moves[SIDE*SIDE];
// Initialise the game
initialise(board, moves);
// Show the instructions before playing
showInstructions();
int moveIndex = 0, x, y;
// Keep playing till the game is over or it is a draw
while (gameOver(board) == false &&
moveIndex != SIDE*SIDE)
{
if (whoseTurn == COMPUTER)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = COMPUTERMOVE;
printf("COMPUTER has put a %c in cell %d ",
COMPUTERMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = HUMAN;
}
else if (whoseTurn == HUMAN)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = HUMANMOVE;
printf ("HUMAN has put a %c in cell %d ",
HUMANMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = COMPUTER;
}
}
// If the game has drawn
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw ");
else
{
// Toggling the user to declare the actual
// winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;
// Declare the winner
declareWinner(whoseTurn);
}
return;
}
// Driver program
int main()
{
// Let us play the game with COMPUTER starting first
playTicTacToe(COMPUTER);
return (0);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.