| derrikjb93 (15) | |||
Hi, I have just written my first Tic Tac Toe game. I was just looking for some input on how to make it a little more compact/easy to read. Also looking possibly an easier way to switch between first and second player that may lead to an easier way to check winning. Thank-You in advanced.
I also completely understand that most of this is noobish and in sure it's complete poop. | |||
|
|
|||
| James2250 (323) | |||||
Instead of using ints to check between player1 and player2 I would just use a single bool to check which players turn it currently is. You also don't need to check every number a user can enter with a case statement you could make it simpler with something like this.
Then at the end of every turn you could use something this to change players.
There are several other ways to do it, but hopefully that gives you some ideas. (On a side note you are missing 0 on your case statements as arrays start at 0.) | |||||
|
|
|||||
| derrikjb93 (15) | |||
Does this look any better?
I also inserted a game checking function which turned out ok. But it was interesting to find a way to close the program when when the one of the two different conditions i wanted to check was inside of a function. Any more input would be greatly appreciated. Thank-You in advanced. | |||
|
|
|||
| Imadatobanisa (647) | ||||
Sorry, but here's my board (old) :
Be more detailed? (You may explain or post an example here...) | ||||
|
Last edited on
|
||||
| derrikjb93 (15) | |||||
I wanted to take the variable from my function
and use it as a variable in my while loop. Like so.
So that whenever int game_check(); did check that one of the players had one (hehe it's kind of punny) the program would end.
| |||||
|
Last edited on
|
|||||
| Imadatobanisa (647) | |||
|
Firstly, remove int game_check;. It's duplicate (name). You should return value - because assigning a function with a value is totally wrong (Did you look at the compling error log?) return 1;Rather than : game_check = 1;//error And how to catch the result from the function game_check() :
Listen to the compiler. When you had any trouble just ask. | |||
|
Last edited on
|
|||
| JLBorges (1754) | ||||
|
> Firstly, remove int game_check;. It's duplicate (name). It is bad style; but it is not a duplicate name - the scopes are different. > I wanted to take the variable from my function > and use it as a variable in my while loop Have the function return the value. Also, prefer bool over int for predicates (either true or false). The program logic is the same as before, but with some re-factoring:
This won't prevent a player from cheating; for instance right at the first turn, player one can enter
and win before player two has had a chance. (To take care of that, we need to empty the input buffer after reading just one integer). | ||||
|
|
||||
| derrikjb93 (15) | |||
|
I really appreciate all of the input! Some of if I don't necessarily understand completely but im trying. Here is my latest addition, adding a computer option and adding a (very) simple AI. Well here is half of it. Im sure it's way longer than it needs to be. What do you guys think?
| |||
|
|
|||
| derrikjb93 (15) | |||
And the other half....
| |||
|
|
|||