0% found this document useful (0 votes)
24 views

C Report Final

The document describes how to create a Tic-Tac-Toe game using C programming. It discusses representing the board as a 2D array, taking user input, validating moves, checking for wins or draws, and switching between players. The game loops until there is a winner or draw, and an additional buzzer can be added to sound when a winner is declared.

Uploaded by

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

C Report Final

The document describes how to create a Tic-Tac-Toe game using C programming. It discusses representing the board as a 2D array, taking user input, validating moves, checking for wins or draws, and switching between players. The game loops until there is a winner or draw, and an additional buzzer can be added to sound when a winner is declared.

Uploaded by

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

Mini report on “Tic Tac Toe”

K. L. E. SOCIETY’S
K. L. E. INSTITUTE OF
TECHNOLOGY,
Opp. Airport, Gokul, Hubballi-580 027
Phone: 0836-2232681 Website: www.kleit.ac.in

A Mini Project Report on

“Tic Tac Toe”


Computer Science and Engineering
Semester II
Principles Of C Programming (BPOPS203)
Academic Year 2022-23

Submitted By
Ms. Aparna N H
(2KE22CS024)
Ms. Bhagyashree A
(2KE22CS030)
Ms. Chandrika K
(2KE22CS036)
Ms. Deepa G T
(2KE22CS043)

Under the Guidance of

Dr. Mahantesh Sajjan

Department of Computer Science & Engineering


1
Department of CSE K.L.E.I.T Hubballi
K. L. E. SOCIETY’S
K. L. E. INSTITUTE OF
Mini report on “Tic Tac Toe”

TECHNOLOGY,
Opp. Airport, Gokul, Hubballi-580 027
Phone: 0836-2232681 Website: www.kleit.ac.in

CERTIFICATE

Certified that the mini project work entitled” Tic Tac Toe” is a bonafide work carried
out by Ms. Aparna N H, bearing USN 2KE22CS024, Ms. Bhagyashree A, bearing USN
2KE22CS030, Ms. Chandrika K, bearing USN 2KE22CS036, Ms. Deepa G T, bearing USN
2KE22CS043 in partial fulfillment for the award of degree of Bachelor of Engineering in
Semester, Computer Science and Engineering of Visvesvaraya Technological
University, Belagavi, during the year 2022-23. It is certified that all corrections/suggestions
indicated for internal assessment have been incorporated in the report deposited in the
department library.

Signature of the Guide


(Prof. Mahantesh Sajjan)

2
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

ACKNOWLEDGEMENT

The mini project report on “Tic Tac Toe” is the outcome of guidance, moral support and
devotion bestowed on us throughout our work. For this we acknowledge and express our
profound sense of gratitude and thanks to everybody who have been a source of
inspiration during the project work.
First and foremost we offer our sincere phrases of thanks with innate humility to our
Principal Dr. Sharad G. Joshi who has been a constant source of support and
encouragement. We would like to thank our Dean Academics Dr. Manu T.M for his
constant support and guidance. We feel deeply indebted to our H.O.D. Dr. Yerriswamy
T. for the right help provided from the time of inception till date. We would take this
opportunity to acknowledge our Guide Prof. Mahantesh Sajjan, who not only stood by
us as a source of inspiration, but also dedicated his time for us to enable us to present the
project on time.
Last but not least, We would like to thank my parents, friends & well wishers who have
helped us in this work.

Aparna N H
Bhagyashree A
Chandrika K
Deepa G T

3
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

INTRODUCTION
1.1 Project overview:
Tic-Tac-Toe is a classic two-player game played on a 3x3 grid. The objective of the game
is to be the first player to form a horizontal, vertical, or diagonal line of their own symbol
(either "X" or "O") on the grid. Players take turns placing their symbols on the empty
cells of the grid until one player wins or the game ends in a draw.

Here's a brief introduction to creating a Tic-Tac-Toe game using the C programming


language:

1. *Board Representation*: In C, you can represent the Tic-Tac-Toe board using a 2D


array. Each cell of the array will hold either an "X", "O", or an empty space (" ") to
indicate the current state of the game.

2. *Displaying the Board*: You can create a function to display the current state of the
board by iterating through the array and printing the symbols in a grid format.

3. *Taking User Input*: Use the ` scanf ` function to take input from the players. They
will enter the row and column where they want to place their symbol.

4. *Validating Moves*: Check if the chosen cell is empty and falls within the valid range
(1-3) for both rows and columns.

5. *Checking for Win/Draw*: After each move, you'll need to check if the current player
has won the game or if the game has ended in a draw. Check for horizontal, vertical, and
diagonal lines of the same symbol.

6. *Switching Players*: After each move, switch the current player to the other player's
symbol ("X" to "O" or vice versa).

7. *Looping the Game*: Use a loop to keep the game running until there's a winner or
the game ends in a draw. Break the loop and display the result when the game is over.

8. *Additional buzzer*:With the help of the buzzer we can enhance the game tic tac toe,
When winner is declared the buzzer is activated.

9. *Final Touches*: You can add extra features like displaying the winner's name,
allowing players to restart the game, or improving the user interface.

4
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

1.2 Introduction to C programming:


C programming is a popular and widely-used programming language that is used for
developing a wide range of applications, from operating systems to game development.
It was created in the 1970s by Dennis Ritchie at Bell Labs and has since become one of
the most influential programming languages.

One of the key features of C programming is its ability to provide low-level access to
memory and system resources. This makes it an excellent choice for writing systems
software, such as device drivers and operating systems. Additionally, C programming is
highly efficient and can be used to write high-performance applications.

C programming follows a structured approach and utilizes a set of standardized syntax


and libraries. It is a procedural language, meaning that programs are made up of a series
of functions that perform specific tasks.

Some of the important concepts in C programming include variables, data types, control
structures (such as if-else statements and loops), functions, and arrays. C also supports
pointers, which allow direct manipulation of memory addresses.

5
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

1.3 Applications of C programming:


C programming has a wide range of applications in various fields. Some of the key areas
where C programming is commonly used include:

1. Operating Systems: C programming is heavily used in the development of operating


systems because of its ability to interact with hardware and low-level system functions.

2. Embedded Systems: C programming is widely used in the development of embedded


systems like microcontrollers, where code efficiency and accessing hardware resources
are critical.

3. System Software: Many system software components, such as device drivers, network
protocols, and file systems, are implemented using C due to its low-level capabilities.

4. Game Development: C programming is used in the development of gaming engines,


graphics libraries, and game logic due to its efficiency and ability to interact with
hardware effectively.

5. Database Systems: C programming is used to develop backend database systems,


where efficiency and fast processing of large amounts of data are essential.

6. Web Development: C programming can also be used for server-side web development
using frameworks like CGI

6
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

1.4.A Flow Chat:

7
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

1.4.B Algorithm:

▪ Step A: Display a Message letting the user know the:


• Welcome and Rules
▪ Step B: Initialize the Game with an empty board
• Display the Board with 2 vertical and 2 horizontal lines
• Clear the Board making sure there are no ‘X’ s or ‘O’ s in the game
positions clear memory that will be needed so that it is an empty slate.
• Select player to start: Give them a choice of an X or an O
▪ Step C: start playing until there is a Winner or cat scratch
• Display a Message:
• Tell the current player to select a location
• Choose Mouse Location (Wait for the mouse button to be hit)
• Find the Location on the Board
• Cheak to see if that Location is Available
• If available, Move piece and Switch Players
• Otherwise display a message to retry
▪ Display a winner’s message and end the game!

8
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

2.project definition:
2.1 purpose and scope:

*Purpose: *
The purpose of creating a Tic Tac Toe game using a C programming language is
multifaceted. It serves as an excellent learning tool, an introduction to programming
concepts, and a practical application of logic and decision-making. The primary purposes
are:

1. *Educational Tool: * Tic Tac Toe provides a simple yet comprehensive environment
for beginners to understand and practice programming concepts such as arrays, loops,
conditionals, and functions. It offers an engaging way to introduce programming to
newcomers.

2. *Problem Solving: * Developing a Tic Tac Toe game involves tackling challenges like
user input validation, win-detection algorithms, and managing game states. These
challenges foster problem-solving skills and logical thinking.

3. *Logic and Decision Making: * The game emphasizes decision-making processes as


players must strategically choose where to place their symbols. Implementing win-
detection and draw-detection algorithms also involves complex logical checks.

4. *Hands-On Experience: * Aspiring programmers gain practical experience by


translating conceptual knowledge into a tangible project. It allows them to experiment,
troubleshoot, and refine their coding skills.

*Scope: *

1. *Basic Gameplay: * The primary focus is on creating a functional Tic Tac Toe game
where two players can take turns and place their symbols ('X' and 'O') on the grid.

2. *Game Logic: * Implementing the core game logic, including checking for a win or a
draw, ensures a complete gaming experience.
9
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

3. *User Interaction: * The game provides a basic user interface for players to input their
moves and see the state of the game board.

4. *Limited Error Handling: * The scope includes basic input validation to ensure players
select valid moves. For example, preventing players from selecting an already occupied
cell.

5. *Single-Device Gameplay: * The game is designed for local multiplayer, where both
players interact with the same device.

6. *Console-Based: * The scope is limited to a console-based interface, displaying the


game board and messages through the terminal.

7. *Learning and Expansion: * The project encourages learners to experiment and expand
on their own. They can enhance the project by adding features like improved user
interfaces, AI opponents, and error handling.

10
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

3.Program Tracing

1.Initialize Board(): This function initializes the game board by populating the board
array with empty spaces (' '). It also prints out the cell numbers and a visual representation
of the empty board.

2.Show Board(int x, int y): This function displays the current state of the game board. It
prints the symbols ('X' or 'O') stored in the board array and draws the grid lines between
cells.

3.Update Board(int cell, char player Sign): This function updates the game board based
on the player's move. It calculates the row and column indices corresponding to the
selected cell number. If the cell is already filled, it prints an error message. Otherwise, it
updates the cell with the player's symbol and calls showBoard() to display the updated
board. It returns isValid, which indicates whether the move was valid or not.

4.Check Winner(char sg): This function checks if a player has won the game. It checks
for three-in-a-row matches in rows, columns, and diagonals. If a match is found, it returns
1, indicating a winner. Otherwise, it returns 0.

Play Tic Tac Toe(): This is the main function that controls the game flow. It alternates
between players' turns, takes input for cell selection, and checks for a win or draw
condition. It also calls the other functions as needed.

main(): This is the entry point of the program. It displays instructions, initializes the
game, and manages the game loop. Players can restart the game or exit.

Overall, the program uses a 2D array called board to represent the Tic Tac Toe grid and
implements various functions to handle different aspects of the game, from initialization
to checking for a winner or draw. The main() function ties everything together, allowing
players to interact with the game and control its flow.

11
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

4.Requirements:

4.1 Hardware and Software:


To run a Tic Tac Toe program on your computer, you will need the following hardware
and software requirements:

Hardware Requirements:
1. Computer: Any standard desktop or laptop computer can run a Tic Tac Toe program.
2. Processor: A basic processor like Intel Core i3 or equivalent should be sufficient.
3. Memory (RAM): Minimum 4GB RAM should be enough.
4. Display: Any standard monitor or display will work fine.

Software Requirements:
1. Operating System: The Tic Tac Toe program can run on various operating systems,
such as Windows, macOS, or Linux.
2. Development Environment: You will need a suitable programming language and
development environment installed on your computer. Some common choices for
implementing Tic Tac Toe programs are Python, Java, C++, or C#.
3. Compiler/Interpreter: If you choose a compiled language like C++ or Java, you will
need a compatible compiler installed to compile the source code. Alternatively,
interpreted languages like Python or C# would require an compiler.

12
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

5.Implementation:

5.1 program and output:


#include <stdio.h>

// Globally declared 2D-array


char board[3][3];

// Function to initialize the game board


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

13
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

printf("\n\n\n");
}

// Function shows the game board


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

// Function to update the game board


int updateBoard(int cell, char playerSign)
{
int row = (cell - 1) / 3;
int col = (cell - 1) % 3;
int isValid = 1;

// accessing already played cell number


if (board[row][col] != ' ')
{
printf("\nInvalid: Cell is already Filled!\n");
14
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

isValid = 0;
}

// valid cell position


else
{
board[row][col] = playerSign;
}
showBoard(row, col);

return isValid;
}

// Function to check the winner of the game


int checkWinner(char sg)
{
// check all rows
if (board[0][0] == sg && board[0][1] == sg && board[0][2] == sg ||
board[1][0] == sg && board[1][1] == sg && board[1][2] == sg ||
board[2][0] == sg && board[2][1] == sg && board[2][2] == sg)
{
return 1;
}
// check all columns
else if (board[0][0] == sg && board[1][0] == sg && board[2][0] == sg ||
board[0][1] == sg && board[1][1] == sg && board[2][1] == sg ||
board[0][2] == sg && board[1][2] == sg && board[2][2] == sg)
{
return 1;
}
// check both diagonals
else if (board[0][0] == sg && board[1][1] == sg && board[2][2] == sg ||
board[0][2] == sg && board[1][1] == sg && board[2][0] == sg)
{
15
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

return 1;
}

// There is no winner
return 0;
}

// Start your game from here


void playTicTacToe()
{
int gameResult = 0;
int cell = 0;
int playCount = 0;
int updationResult = 1;

char playerSign = ' ';

// start playing the game


while (!gameResult && playCount < 9)
{
if (playCount % 2 == 0)
{
// player 1
printf("\nPlayer 1 [ X ] : ");
playerSign = 'X';
}
else
{
// player 2
printf("\nPlayer 2 [ O ] : ");
playerSign = 'O';
}
scanf("%d", &cell);
if (cell > 0 && cell < 10)
16
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

{
updationResult = updateBoard(cell, playerSign);
// if updation is possible
if (updationResult)
{
gameResult = checkWinner(playerSign);
// print the winner of the game
if (gameResult)
{
printf("\t * Player %d Won!! *\n", playerSign == 'X' ? 1 : 2);
}
playCount++;
}
}
else if (cell == -1)
{
printf("\n\tGame Terminated\n");
return;
}
else
{
printf("\nPlease Enter a valid cell value\n");
}
}

// no one won the game


if (!gameResult && playCount == 9)
{
printf("\n\t * Draw... *\n");
}
printf("\n\t --- Game Over --- \n");
}

int main()
17
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

{
printf("--------- Tic Tac Toe ----------\n\n");

printf("\n* Instructions \n\n");


printf("\tPlayer 1 sign = X\n");
printf("\tPlayer 2 sign = O");
printf("\n\tTo exit from game, Enter -1\n");

printf("\n\n* Cell Numbers on Board\n");


initializeBoard();

char start = ' ';


printf("\n> Press Enter to start...");

scanf("%c", &start);

if (start)
{
int userChoice = 1;
// restart the game
while (userChoice)
{
playTicTacToe();
printf("\n* Menu\n");
printf("\nPress 1 to Restart");
printf("\nPress 0 for Exit");
printf("\n\nChoice: ");
scanf("%d", &userChoice);
if (userChoice)
{
initializeBoard();
}
printf("\n");
}
18
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

}
printf("\n :: Thanks for playing Tic Tac Toe game! :: \n");
return 0;
}

Output:
* Instructions

Player 1 sign = X
Player 2 sign = O
To exit from game, Enter -1

* Cell Numbers on Board

1 | 2 | 3
----------------
4 | 5 | 6
----------------
7 | 8 | 9

> Press Enter to start...

Player 1 [ X ] : 1

X | |
----------------
| |
----------------

19
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

| |

Player 2 [ O ] : 4

X | |
----------------
O | |
----------------
| |

Player 1 [ X ] : 5

X | |
----------------
O | X |
----------------
| |

Player 2 [ O ] : 6

X | |
----------------
O | X | O
----------------
| |
20
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

Player 1 [ X ] : 9

X | |
----------------
O | X | O
----------------
| | X

* Player 1 Won!! *

--- Game Over ---

* Menu

Press 1 to Restart
Press 0 for Exit

Choice: 0

Thanks for playing Tic Tac Toe game!

21
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

6.Snapshots:
Here are a few snapshots of a tic-tac-toe game progressing from an empty board
to a won game:

Snapshot 1:

|X|
-----------
| |
-----------
| |

Snapshot 2:

|X|
-----------
|O|
-----------
| |

Snapshot 3:

X|X|
-----------
|O|
-----------
| |

22
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

Snapshot 4:

X|X|O
-----------
|O|
-----------
| |

Snapshot 5:

X|X|O
-----------
|O|
-----------
X| |

Snapshot 6:

X|X|O
-----------
O|O|
-----------
X| |

This snapshot shows a player X winning the game by forming a line of X's
horizontally.

23
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

7.Conclusion:

The conclusion of a C program for a tic-tac-toe game would typically involve printing
the final game board, determining the winner (if any), and displaying the appropriate
message. Additionally, you might want to offer the option to play again or exit the game.
If you have specific code or questions, feel free to share, and I can assist you further!

24
Department of CSE K.L.E.I.T Hubballi
Mini report on “Tic Tac Toe”

REFERENCES:
Certainly, here are three C programming books that you can use to learn and implement
a Tic Tac Toe program:

1. "C Programming Absolute Beginner's Guide (3rd Edition)" by Perry and Miller
This book is designed for beginners and covers the fundamentals of C programming. It
provides clear explanations and practical examples that will help you build the skills
needed to create a Tic Tac Toe program.

2. "C Programming for the Absolute Beginner, Second Edition" by Vine


Another excellent choice for beginners, this book introduces C programming concepts
step by step. It's a great resource to learn the basics and apply them to coding projects
like a Tic Tac Toe game.

3. "C Programming for the Absolute Beginner" by Vine and Vine


This book offers a gentle introduction to C programming, making it suitable for those
who are new to programming. It covers foundational concepts and can guide you through
building a simple game like Tic Tac Toe.

Remember, while these books may not have a specific chapter dedicated to a Tic Tac
Toe program, they will provide the necessary knowledge to implement the game using
C programming. Additionally, you might want to explore online tutorials and resources
to supplement your learning and get hands-on with creating the game.

25
Department of CSE K.L.E.I.T Hubballi

You might also like