I've been learning C++ as a hobby for the previous month and recently i started on a project to make this Tic Tac Toe game. I've written the source code below. It isn't complete - i've only just started on it. The problem is that I can't get the functions called "putX" and "isLegal" to work - the program just freezes.
I'd appreciate it if someone could tell me where i've gone wrong and also how i can make the program more efficient.
Please remember that this is not the final form of the program.
*getMove takes a cellnumber as an argument, but the cellnumber in getMove() is only a copy of cellnumber in main(). There is no reason to pass this argument. But don't forget do declare a local variable 'cellnumber' in getMove().
*On line 50 you want to check whether input was legal. I don't know what you do there, nor why it compiles, but you need while(!isLegal(cellnumber, playGrid)); note that isLegal requires playGrid so you'll have to pass it to getMove.
*getMove returns the correct value for cellnumber, but you never assign that value to cellnumber. line 19 should be cellnumber = getMove(playGrid);
*playGrid is an array of 8 chars, but you need 9. setGrid accesses 9'th element of 8 element array. This could cause errors.
*On line 49 you check whether cellnumber is in correct range. However note that 9 should be out of range.
*In C++, when passing array to function you don't need to specify its length. int getMove(char playGrid[])
Thanks alot! That worked perfectly.
Would you say that the code is readable - I know it's not the most efficient bit of programming in the world - still I did try to make it easy to follow.
for me it was readable enough :) try to add comments on what stuff does though.. In here it's obvious, but when the code gets more nested loops etc, it'll become harder to understand :)
And instead of using system("pause") try using something like std::cin.get. That's quiker and it's better (see: http://cplusplus.com/forum/articles/11153/ for more info)