simple tic tac toe game
| anthony830121 (13) | |||
| Hi, recently i made a program during my spare time.It's tic tac toe. I was wondering if anyone could help me improve it in terms of coding, or organization, because I think I've over-complicated the codes. >.> I am in high school and started C++ 3 weeks ago, my knowledge isn't very deep, so please post codes that I can understand. >.< What I've included in the programs are what I can comprehend so...=3 Any help would be appreciated. Thanks.
| |||
| Duoas (1159) | |||||
| Yeah, you got lost. It is because you are trying to comprehend it all at once. No one can do that. You have to take it a piece at a time. You started out quite nicely. You wrote functions to: - display the current state of the game (progress) - determine if someone has won the game (and, supposedly, determine if the game is over) - check for a valid move - determine whether or not to play the game again However, you began mixing things up. For example, go_on() is supposed to tell you whether or not the user wants to play again. But the way it is currently written, it doesn't tell you anything you didn't already know. You could have just skipped the function and written:
(Don't do that with main(), BTW. It is illegal in C++ and very bad karma otherwise --like running around with a sharp knife in your hand. Sooner or later you or someone else will get cut.) Another thing is that you are using int for boolean values, when C++ provides you with a bool type. Always use types to improve clarity. So, with that preamble, I'll help you refine your design goals and you can start fixing things up. Let's start with the over-all structure. main() This is the "controller" of the program. It doesn't actually run the game --we'll palm that off to another function. What it does is greet the user, initialize things, call the function that runs the game, tell the user who won and who lost (and keep track of how many times each), and ask the user if he/she wants to play again. We'll also have to define the way the gameboard is represented to the user. I'll say that we do it naturally for people, and number rows and columns 1..3. We'll just subtract one from whatever they give us to access the actual item. We'll store the gameboard the same way you did (nice job, BTW), as a 3 x 3 array of char, with values ' ', 'X', or 'O'. Let's write that out in a simple version of code:
Well, I've given you that... Now that we've got top-level stuff at a good idea state, let's flesh-out some of those functions we made-up. playgame() This function actually plays a game. It runs until either someone wins or until there are no more moves (and a draw occurs). It displays the current gameboard, asks each player for his move in turn (and keeps asking until he enters a valid one), updates the gameboard, looks for a winner, and if none, repeats (suggesting another loop). Before returning, it displays the gameboard one more time (so the players can see the winning move). It returns who won as an integer value of draw, player1, or player2. (Notice how more functions were suggested: your progress() and winlose() and check, etc. Don't worry about how they work inside, figure out what they should do for this function--what they should return and what arguments they need to do it. display_winner_stats() Does what it says. Congradulates the winner and also tells how many times each player has won and how many draws there were. ask_to_play_again() Does what it says. Asks the user if he/she wants to play again and returns a boolean value: true if yes and false if no. Notice how we can keep things simple by considering it abstractly from the top. (This is called the "top-down" approach.) Whew. I've got to get some sleep. I'll help more later. | |||||
| anthony830121 (13) | |||
| lol thanks a lot. I'll try and fix it up. xD | |||
| anilpanicker (58) | |||
| That was a wonderful explanation !!, how to write a neat,readable and maintanable code | |||
| exception (185) | |||
It's amazing that it compiled. It was a microsoft compiler, wasn't it? Not quite conform to the standard. From which I quote (sect. 3.6.1):
That is, (a) your "main()" has no return type (not good, but likely to compile since MS uses it) and (b) you call your main() (it really amazes me that there exists an compiler which does not complain about that) | |||
| Evote (17) | |||
| Lol Well.... I'm trying to fix it :D what compiler you use cause if i fix on wrong one it won't work for you lol! I assume microsoft cause i got lots errors not microsoft but which one? | |||
Registered users can reply in this forum.
