0% found this document useful (0 votes)
5 views6 pages

Lab 07

this gives you the code

Uploaded by

Pushpa Kaladevi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

Lab 07

this gives you the code

Uploaded by

Pushpa Kaladevi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab -07

Source code:

#include <stdio.h>
#include <string.h>

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


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
board[i][j] = ' ';
}
}
}

void displayBoard(char board[3][3]) {


printf(" 0 1 2\n");
for (int i = 0; i < 3; i++) {
printf("%d ", i);
for (int j = 0; j < 3; j++) {
printf("%c", board[i][j]);
if (j < 2) {
printf("|");
}
}
printf("\n");
if (i < 2) {
printf(" -----\n");
}
}
}

void makeMove(char board[3][3], char player) {


int row, col;

while (1) {
printf("Enter row and column (0, 1, or 2): ");
scanf("%d %d", &row, &col);

if (row >= 0 && row < 3 && col >= 0 && col < 3 &&
board[row][col] == ' ') {
board[row][col] = player;
break;
} else {
printf("Invalid move, try again.\n");
}
}
}

int checkWinner(char board[3][3]) {


for (int i = 0; i < 3; i++) {
if (board[i][0] != ' ' && board[i][0] == board[i][1] &&
board[i][1] == board[i][2]) {
return 1;
}
if (board[0][i] != ' ' && board[0][i] == board[1][i] &&
board[1][i] == board[2][i]) {
return 1;
}
}

if (board[0][0] != ' ' && board[0][0] == board[1][1] &&


board[1][1] == board[2][2]) {
return 1;
}
if (board[0][2] != ' ' && board[0][2] == board[1][1] &&
board[1][1] == board[2][0]) {
return 1;
}

return 0;
}

int main() {
char board[3][3];
char player1[50], player2[50];
int winner = 0;
int moves = 0;

printf("Enter Player 1 name: ");


fgets(player1, sizeof(player1), stdin);
player1[strcspn(player1, "\n")] = '\0';

printf("Enter Player 2 name: ");


fgets(player2, sizeof(player2), stdin);
player2[strcspn(player2, "\n")] = '\0';

initializeBoard(board);
while (winner == 0 && moves < 9) {
displayBoard(board);

if (moves % 2 == 0) {
printf("%s's turn (X):\n", player1);
makeMove(board, 'X');
} else {
printf("%s's turn (O):\n", player2);
makeMove(board, 'O');
}

winner = checkWinner(board);
moves++;
}

displayBoard(board);

if (winner == 1) {
printf("Congratulations %s, you are the winner!\n",
(moves % 2 == 0) ? player2 : player1);
} else {
printf("It's a draw!\n");
}

return 0;
}
Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy