Hi guys; i need help with this please: Write a modular C program that will allow
ID: 3604269 • Letter: H
Question
Hi guys; i need help with this please:
Write a modular C program that will allow two players to play the game of tic-tac-toe. Players take turns to place their token (a ‘O’ or a ‘X’) on the board by choosing a number 1-9. The game is won if one player can place three of their tokens in a row, horizontally, vertically or diagonally.
display the game board
player 1 enters the number of the square they wish to choose
the game board is updated
the win status of the game is tested and the game finishes if the player has won
player 2 enters the number of the square they wish to choose
the game board is updated
the win status of the game is tested and the game finishes if the player has won
and so on... thanks.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
//Board
char **board;
// Initializing board
void initialize()
{
auto char **p;
auto char *q;
//Outer loop for rows
for (p = board; p != board + 3; ++p)
{
//Inner loop for columns
for (q = *p; q != *p + 3; ++q)
{
*q = '*';
}
}
}
// print prints the board in nice format
void print_board ()
{
auto char **p;
auto char *q;
printf(" The board is: ");
printf(" |---|---|---| ");
//Outer loop for rows
for (p = board; p != board + 3; ++p)
{
//Inner loop for columns
for (q = *p; q != *p + 3; ++q)
{
//Printing character
printf(" | %c", *q);
}
printf(" |");
//Printing footer
printf(" |---|---|---| ");
}
}
//Function that checks whether board is full or not
int isBoardFull()
{
auto char **p;
auto char *q;
//Outer loop for rows
for (p = board; p != board + 3; ++p)
{
//Inner loop for columns
for (q = *p; q != *p + 3; ++q)
{
//Board is not Full
if(*q == '*')
return 0;
}
}
//Board Full
return 1;
}
// win returns true if the given player has won on the
// given board, else it returns false
int win (char player)
{
return (*(*(board + 0) + 0) == player && *(*(board + 0) + 1) == player && *(*(board + 0) + 2) == player) ||
(*(*(board + 1) + 0) == player && *(*(board + 2) + 1) == player && *(*(board + 2) + 2) == player) ||
(*(*(board + 2) + 0) == player && *(*(board + 2) + 1) == player && *(*(board + 2) + 2) == player) ||
(*(*(board + 0) + 0) == player && *(*(board + 1) + 0) == player && *(*(board + 2) + 0) == player) ||
(*(*(board + 0) + 1) == player && *(*(board + 1) + 1) == player && *(*(board + 2) + 1) == player) ||
(*(*(board + 0) + 2) == player && *(*(board + 1) + 2) == player && *(*(board + 2) + 2) == player) ||
(*(*(board + 0) + 0) == player && *(*(board + 1) + 1) == player && *(*(board + 2) + 2) == player) ||
(*(*(board + 0) + 2) == player && *(*(board + 1) + 1) == player && *(*(board + 2) + 0) == player);
}
//Function that checks for winner
int check_board(char player1, char player2)
{
//Checking for winning of player
if(win('X') == 1 && win('O') == 1)
{
return 2;
}
//Checking for winning of player
else if(win('X') == 1)
{
return 0;
}
//Checking for winning of computer
else if(win('O') == 1)
{
return 1;
}
//Tie
else
{
return -1;
}
}
//Function that plays the game
void playGame(char player1, char player2)
{
int row, col;
int winner;
//Loop till board is full
while(isBoardFull() != 1)
{
//Player turn
while(1)
{
//Reading positions from user
printf(" Enter the row, column pair for inserting %c : ", player1);
scanf("%d %d", &row, &col);
//Making suitable for array indexing
row = row - 1;
col = col - 1;
//Checking for empty position
if(*(*(board + row) + col) == '*')
break;
else
printf(" Invalid choice... Try again!! ");
}
//Storing in array
*(*(board + row) + col) = player1;
//Printing board
print_board();
//Finding winner
winner = check_board(player1, player2);
//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: printf(" Player 1 won the game... "); break;
case 1: printf(" Player 2 won the game... "); break;
case 2: printf(" Game Tie ... "); break;
}
return;
}
//Player turn
while(1)
{
//Reading positions from user
printf(" Enter the row, column pair for inserting %c : ", player2);
scanf("%d %d", &row, &col);
//Making suitable for array indexing
row = row - 1;
col = col - 1;
//Checking for empty position
if(*(*(board + row) + col) == '*')
break;
else
printf(" Invalid choice... Try again!! ");
}
//Storing in array
*(*(board + row) + col) = player2;
//Printing board
print_board();
//Finding winner
winner = check_board(player1, player2);
//If either of winner is found
if(winner >= 0 && winner <= 2)
{
//Printing winner
switch(winner)
{
//Displaying winner
case 0: printf(" Player 1 won the game... "); break;
case 1: printf(" Player 2 won the game... "); break;
case 2: printf(" Game Tie ... "); break;
}
return;
}
}
}
//Main function
int main()
{
int i;
char ch;
char *temp;
//Player characters
char player1Character='X', player2Character='O';
//Dynamically allocating space
board = (char **)malloc(sizeof(char*) * 3);
temp = (char *)malloc(3 * 3 * sizeof(char));
for(i=0; i<3; ++i)
{
board[i] = temp + i * 3;
}
//Initializing random function
srand(time(NULL));
do
{
//Initializing board
initialize();
//Printing board
print_board();
//Playing game
playGame(player1Character, player2Character);
//Prompting user for new game
printf(" Do you want to play another game? (Y/N): ");
scanf(" %c", &ch);
}while(ch=='y' || ch=='Y');
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.