Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views10 pages

TIC TAC TOE

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 10

FIRST PAGE

Achowledgment
Certificate
Abstract

Abstract

Simple Calculator Project

This project involves developing a simple calculator application in the C programming


language to perform basic arithmetic operations: addition, subtraction, multiplication, and
division. The primary objective is to provide a user-friendly interface for performing these
calculations and to enhance fundamental programming skills.

The calculator operates through a console-based interface where users can select from a menu
of operations. The application continuously prompts the user to input their choice of
operation and the required operands until the user decides to exit. Error handling is
incorporated to manage division by zero and invalid menu selections, ensuring the program's
robustness and reliability.

Key features of the calculator include:

 Interactive Menu: A clear and concise menu allows users to choose between
addition, subtraction, multiplication, and division.
 Input Handling: Users can input two floating-point numbers and receive the result of
the selected arithmetic operation.
 Error Management: Includes checks for invalid menu choices and division by zero,
providing appropriate feedback to users.
 Continuous Operation: The calculator remains active, allowing multiple calculations
until the user opts to exit.
1. Introduction

1.1 Overview of Tic Tac Toe Game

Tic Tac Toe is a classic two-player game where players take turns marking a 3x3 grid with
either X or O. The objective is to align three of one's own marks in a row, column, or
diagonal. This document provides a detailed explanation of each module in the
implementation of Tic Tac Toe using the C programming language.

2. Project Structure

The project consists of several key modules:

1. Main Module
2. Board Display Module
3. Player Input Module
4. Game Logic Module
5. Error Handling Module

Each module is essential for creating a functional Tic Tac Toe game. The following sections
provide a detailed explanation of each module.

3. Main Module

3.1 Overview

The Main Module is responsible for initializing the game, managing turns, and controlling
the game flow. It ties together all other modules and handles the overall game logic.

3.2 Detailed Code Explanation


c
Copy code
#include <stdio.h>
#include "board.h"
#include "player.h"
#include "game.h"

int main() {
char board[3][3];
initializeBoard(board);

char currentPlayer = 'X';


int gameStatus;
do {
displayBoard(board);
playerMove(board, currentPlayer);
gameStatus = checkGameStatus(board);

if (gameStatus == 1) {
printf("Player %c wins!\n", currentPlayer);
} else if (gameStatus == 0) {
printf("It's a draw!\n");
}

currentPlayer = (currentPlayer == 'X') ? 'O' : 'X';


} while (gameStatus == -1);

displayBoard(board);
return 0;
}

3.3 Explanation

 Initialization: initializeBoard(board) sets up the game board with empty spaces.


 Game Loop: The do-while loop runs until the game status is determined (win, draw, or
ongoing).
 Display and Move: displayBoard(board) shows the current board, and
playerMove(board, currentPlayer) handles player input.
 Game Status Check: checkGameStatus(board) determines if there’s a winner or if the
game is a draw.

4. Board Display Module

4.1 Overview

This module is responsible for displaying the game board to the players. It provides a visual
representation of the game state.

4.2 Detailed Code Explanation


c
Copy code
#include <stdio.h>

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


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

 Board Layout: The board is printed row by row, with | separating columns and ---
separating rows.
 Visual Structure: printf statements manage the layout to ensure the board is clearly
presented to the player.

5. Player Input Module

5.1 Overview

This module handles input from the players. It ensures that the moves are valid and updates
the board accordingly.

5.2 Detailed Code Explanation


c
Copy code
#include <stdio.h>

void playerMove(char board[3][3], char currentPlayer) {


int row, col;
printf("Player %c, enter your move (row and column): ", currentPlayer);

while (1) {
scanf("%d %d", &row, &col);
if (row >= 0 && row < 3 && col >= 0 && col < 3 && board[row][col]
== ' ') {
board[row][col] = currentPlayer;
break;
} else {
printf("Invalid move. Try again: ");
}
}
}

5.3 Explanation

 Input Handling: scanf is used to take input for row and column.
 Validation: The while loop ensures the move is within bounds and the chosen cell is empty
before updating the board.

6. Game Logic Module

6.1 Overview

This module contains the core logic for determining the game state, including win conditions
and draw scenarios.

6.2 Detailed Code Explanation


c
Copy code
#include <stdio.h>

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


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

// Check diagonals
if (board[0][0] == board[1][1] && board[1][1] == board[2][2] &&
board[0][0] != ' ') return 1;
if (board[0][2] == board[1][1] && board[1][1] == board[2][0] &&
board[0][2] != ' ') return 1;

// Check for draw


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

return 0;
}

6.3 Explanation

 Win Conditions: Checks all rows, columns, and diagonals for three identical marks.
 Draw Condition: Checks if all cells are filled and no winner is found.

7. Error Handling Module

7.1 Overview

This module handles any errors or invalid states that arise during gameplay, ensuring a
smooth user experience.

7.2 Detailed Code Explanation


c
Copy code
#include <stdio.h>

void handleInvalidMove() {
printf("Error: Invalid move. Try again.\n");
}

7.3 Explanation
 Error Messages: Provides feedback to the user when an invalid move is made. This is a
placeholder function for more advanced error handling that could be implemented.

8. Integration and Testing

8.1 Integration

All modules are integrated into the main program. The main() function coordinates the game
flow, calling functions from each module as needed.

8.2 Testing

 Unit Testing: Each module is tested independently to ensure correct functionality.


 Integration Testing: The entire game is tested to ensure all modules work together
seamlessly.
 User Testing: The game is played by users to identify any usability issues or bugs.

9. Challenges and Solutions

9.1 Common Challenges

 Input Validation: Ensuring the input is within bounds and the cell is empty.
 Game Status Checking: Accurately determining win, loss, or draw conditions.

9.2 Solutions

 Enhanced Input Handling: Added checks to validate and prompt for correct input.
 Robust Game Logic: Thorough testing and validation of the game status checking logic.

10. Conclusion

10.1 Summary

The Tic Tac Toe project in C language is successfully implemented with key modules
handling different aspects of the game. Each module has been tested and integrated to create
a functional and interactive game.

10.2 Future Improvements

 AI Opponent: Implement an artificial intelligence player for single-player mode.


 Graphical Interface: Enhance the user interface with graphical representation.
11. References

 C Programming Language Books


 Online Resources and Tutorials

11.1 Appendix

 Full Code Listings: Include complete code files for all modules.
 Technical Notes: Any additional notes or explanations relevant to the project.

You might also like