I recently completed a command line tictactoe, and was wondering what I could change to make it better, besides making it object orientated which I am just starting to work on. I realize that some of the programming logic is badly done, which is why I'm asking for opinions :) Any help is greatly appreciated.
Here is the source code:
Commands:
1.Place piece
2.Exit
1 2 3
1 - - -
2 - - -
3 - - -
1
Player 1, its your turn.
Please select a location (ROW COL):
2 2
1 2 3
1 - - -
2 - X -
3 - - -
Player 2, its your turn.
Please select a location (ROW COL):
1 1
1 2 3
1 O - -
2 - X -
3 - - -
Player 1, its your turn.
Please select a location (ROW COL):
3 2
1 2 3
1 O - -
2 - X -
3 - X -
Player 2, its your turn.
Please select a location (ROW COL):
3 3
1 2 3
1 O - -
2 - X -
3 - X O
Player 1, its your turn.
Please select a location (ROW COL):
1 2
1 2 3
1 O X -
2 - X -
3 - X O
Player 1 Wins
I realize that some of the programming logic is badly done
I would say it's pretty good. What parts are you unhappy with?
I think you can go without int IBoard[3][3], but not exactly sure. For example:
1 2 3 4 5 6 7 8 9 10 11 12 13
//Check Horizontal Win
for (int i = 0; i < 3; i++) {
int TotalValue = 0;
for (int j = 0; j < 3; j++) {
TotalValue += Board[i][j]; // This is just Board[3][3]
}
if (TotalValue == 3 * 'X') {
WinX = true;
}
elseif (TotalValue == 3 * 'O') {
WinO = true;
}
}
You could also try making checkWin() return bool, and have this be the condition of the while loop. The game must be over if the total of the Board is (5 * 'X') + (4 * 'O') as well, so checkWin() can also handle the case of a cat's game.
Thanks for the reply.
One part I would say I'm not pleased with is how it decides what piece to place. Would it be better to store the players piece with the player itself? but then it would be object orrentated.
Also what do you mean about not needing the IBoard array, wouldn't TotalValue += Board[i][j];
cause an error? as Board[3][3] is a char array.
I do agree that the CheckWin() should handle the full board scenario.
One last thing, off topic. Are there any other little 'mini games' I can make. As I want more practice, but I find it more fun to create games, applying what I learn, rather then just doing examples I can't really think of a scenario to use. So far I've made: Number Guessing(Both Player and Computer Guesses), RockPaperScissors, and TicTacToe.
Instead of having the user input the row and col you could have the board start out with numbers and fill in the X's and O's based on input. I think it would be easier to play, just a matter of taste though :)
Thanks for replying. I actually did think about doing it that way at first, but never ended up doing it. But it is a good idea for making it more usable, thanks!