Search code examples
c++tic-tac-toe

Is it possible to convert an element of a char arrary into a const char?


I dont want to covert the char array into const char array, but to make an element constant after it has got a particular value. This, I require for a tic tac toe game. When a grid has got the value of X or O, it needs to be unchangeable, to ensure that players can't put X and O on a grid which has been taken.

I searched about it(on google and stackoverflow), but so far it seems impossible. Is this approach wrong? I dont want to alter the whole program to overcome this problem, so I need some other way to ensure that players dont change a grid after its taken.


Solution

  • const is made for developers. It just says to the compiler that a value is unchangeable, allowing the compiler to make some optimizations and protecting from programmer errors.

    In your case, you must write your own code to protect your field. For example, you can initialize each field with the space symbol (' ') and, before changing the cell's value, check the current value… allowing a change only if the value is the space symbol.