Can we use array of integer while creating tic tac toe game in c programming or is it mandatory to declare array as character char array[9]={1,2,3,4,5,6,7,8,9};
CodePudding user response:
Nothing is mandatory. But arrays are very convenient as you can use another variable to access elements of the array.
The type of array elements is also not mandatory. Programmers tend to save memory by using the smallest type which can accommodate the values required for the task.
You can use any type you want - it can be long long
, int
, double
etc.
CodePudding user response:
Because that's the most simple and obvious choice for this particular problem. Programming is problem solving, just like math, and process does not always matter as long as you get the results. However, humans are lazy and tend to the most simple and obvious solutions, and for a good reason. Writing code is not enough, code is for humans, not machines, and so you need to be able to read it and understand it even after an indeterminate amount of time of not interacting with it.
Tic-tac-toe is a game that involves 3x3 board and any 2 symbols to differentiate the player moves, most common of which are O
and X
. Board slots with no moves are empty and player can make a move there, in computer text we represent this as whitespace
.
We have no other special state in this simple game, just symbols on the board. In C, symbol unit is char, and while it does not exactly represent real human symbols, as long as we stay in ASCII, it does. O
and X
luckily are a part of ASCII and we don't need to worry about unicode. O
and X
is already a char in C, so why not just use char
? Anything else would become more complicated for no discernible gain.
Most simple C programs also are executed and debugged inside a terminal, which is a textual interface, and easiest way to visualize the board is to simply print it in your terminal. And there's nothing that's easier to print than an array of characters.
To answer your specific question, you can, it just doesn't make sense to.