0% found this document useful (0 votes)
22 views2 pages

Tic Tac Toe Program

The document contains code for a Tic Tac Toe game. It includes functions to display the game board, check for a win, and hold the game logic and flow. The main function runs a loop allowing two players to take turns placing markers until there is a win or a tie.

Uploaded by

haroon ali
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)
22 views2 pages

Tic Tac Toe Program

The document contains code for a Tic Tac Toe game. It includes functions to display the game board, check for a win, and hold the game logic and flow. The main function runs a loop allowing two players to take turns placing markers until there is a win or a tie.

Uploaded by

haroon ali
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/ 2

#include <stdio.

h>
#include <stdlib.h>

// Function to display the Tic Tac Toe board


// Declares a function that takes a 3x3 grid of characters
// as input and does not return any value.
void displayBoard(char board[3][3]) {
printf("\nTic Tac Toe\n\n");

printf(" %c | %c | %c \n", board[0][0], board[0][1], board[0][2]);


printf("---+---+---\n");
printf(" %c | %c | %c \n", board[1][0], board[1][1], board[1][2]);
printf("---+---+---\n");
printf(" %c | %c | %c \n", board[2][0], board[2][1], board[2][2]);
}

// Implementation of win checking logic


// Return appropriate value(which can be used in the main program)
// based on the win condition and function takes two arguments
int checkWin(char board[3][3], char player) {
for (int row = 0; row < 3; row++) {
if (board[row][0] == player && board[row][1] == player && board[row][2] == player) {
return 1; // Player wins horizontally
}
}

for (int col = 0; col < 3; col++) {


if (board[0][col] == player && board[1][col] == player && board[2][col] == player) {
return 1; // Player wins vertically
}
}

if ((board[0][0] == player && board[1][1] == player && board[2][2] == player) ||


(board[0][2] == player && board[1][1] == player && board[2][0] == player)) {
return 1; // Player wins diagonally
}

return 0; // No winner yet


}

int main() {
char board[3][3] = { //Declares board with numbers
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}
};
int currentPlayer = 1; // Player 1's turn
int moves = 0; // Looping variable
int choice; // Board position choice
char mark;

displayBoard(board);
// Loop until the number of moves is less than 9
// (i.e., there are still empty cells on the game board)
while (moves < 9) {
mark = (currentPlayer == 1) ? 'X' : 'O'; //If currentPlayer is equal to 1, assign 'X' to mark; otherwise,
assign 'O' to mark."

printf("\nPlayer %d, enter a number (1-9) to place %c: ", currentPlayer, mark);
scanf("%d", &choice);

if (choice < 1 || choice > 9) {


printf("Invalid input! Please enter a number between 1 and 9.\n");
continue;
}

int row = (choice - 1) / 3;


int col = (choice - 1) % 3;

if (board[row][col] == 'X' || board[row][col] == 'O') {


printf("That cell is already taken! Please choose another.\n");
continue;
}

board[row][col] = mark;
displayBoard(board);

if (checkWin(board, mark)) {
printf("\nPlayer %d wins!\n", currentPlayer);
break;
}

currentPlayer = (currentPlayer == 1) ? 2 : 1;
moves++;
}

if (moves == 9) {
printf("\nIt's a tie!\n");
}

return 0;
}

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