I've got a char 3x3 array full of asterisks that I want to replace with x's and o's as a tic tac toe game. How can I have the player input a row and column to replace an asterisk in that place?
And with array's starting at 0 what would I have to modify to make it 1-3 instead of 0-2? Is there a way to subtract one from the value so when someone inputs 1, 2 the grid see's it as 0, 1?
Also, how would I validate that a space is not already taken? Some sort of if statement?
Alright right now I'm just testing some things out with a switch statement to converse 1 2 and 3 to 0 1 and 2 for array's so they don't have to ype 0 1 and 2. Anyways, when I put it into function form I get linking errors, but when I post it in main it works fine. Note: I know the do while loop won't stop, I'm just testing other things before I get to it and I know it isn't causing any problems.
Assuming you have the array set up correctly, if the user inputs row and col for the location of where they want to put an 'x', you can do the following:
board[row-1][col-1] = 'x';
To check if a space is taken you can use an if statement as you thought. If the space is not an asterisk it must be taken.
1 2 3
if( board[row-1][col-1] != '*' ) {
// space is taken
}
That looks alot easier than using a switch for each one. I'll try it out and hope it allieviates the problems :)
Work's 90%, Only problem which I'm looking into is when I input a row/column that is already inserted and post another it doesn't post the O/X in the program.
Alright, I've fixed it, now all I need to do is have it calculate if a players won or tied ect. Would I do that in the while loop or do another if statement to see if someone's won?
If I were to do an if statement to check if a row is occupied by X's or O's, could I have it add the total of the X's(88) and see if that work? Although I don't know how I would do it with the diaganols
This code is giving my a happy face instead of O for player 2?