How to create a tic tac toe game in C?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to create a tic tac toe game in C??

Actually tic tac toe game is something that is usually played in free time but I know that even this simple time pass game takes a lot of logics and expressions but I want to learn it as I think it is a basic game that can be developed using C please help me with a detailed and easy method to solve this problem

11th Mar 2019, 12:48 PM
Vsuniltagore
Vsuniltagore - avatar
2 Answers
+ 3
Just like any other program you grab a pen and paper or notepad and a keyboard and write down whatever you think you need step by step. Instead of thinking of a solution that solves the entire program at once, think of solutions that solve each minor case. Combined together they form the entire solution. It also makes it much easier to find help/reference to a specific problem than a broad one. For example 1. You need a way to represent the game state. Tic tac toe is game with a 3x3 grid where each cell has only 3 possible states; empty, X or O. You can choose between a 2d array of 3x3 or a 1D array of 9 elements. 3 possible states means that you can get away with a char as data type where a specific character represents which state the cell is in. ' ', 'X', 'O' or whatever. 2. You need user input, which functions come to mind that can take/verify user input? What should the user input? 0 through 8 where 4 is the center cell? 2 numbers? 1 1 being the center? How do you tell the user? 3. You need to display the state of the board to the user, so you want a function that reads the state of the board and displays it. How to handle the situation when you want to draw again? Draw it underneath the previous one? Or refresh the entire screen and then draw it? 4. When and how to handle the game logic? Probably after user input and before displaying the board. What steps to take to check if the board is in a winning state or if the board is full? Check every row then column and then diagonals could be an option but there might be better ways out there. No win and no empty cells left? How to check for that? Maybe a step 4.1? :)
11th Mar 2019, 1:58 PM
Dennis
Dennis - avatar
0
Thanks for your advice I will definitely think all the possible ways to solve it
11th Mar 2019, 2:30 PM
Vsuniltagore
Vsuniltagore - avatar