Browsed this forum for a while, been a member but don't post, but I need some help with a project of mine.
I have been learning C++ off and on for several years now, and get tired of reading up on tutorials on stuff that I already know about, so I figured it's time to actually put my knowledge of the language to use and figure out myself how it works instead of reading how it works, and I'm starting by creating Tic-Tac toe for console. When I started yesterday, I was able to have a one player game and find a winner when the user reaches 3 in a row.
I was able to turn the game into a two player game with 'X' and 'O' values. The game can input and output the locations of the user input. Here's my problem though - I cannot for the life of me figure out how to end the game when an 'X' or an 'O' reaches 3 in a row. It was simpler to do it with 1 player. I have a variable called "result" that defaults to -1, and if a winning combination was found, return 1 for player1, or return 2 if player2 wins, but for whatever reason the compiler is always returning -1. I have a suspicion it's because I have result default to -1 but I tried changing that to 0 and NULL but that didn't work either.
I would appreciate any help out. Will post code shortly. It would be nice to not be given the answer, just an idea of what I need to be doing.
Well as it turns out I had the wrong combinations which explains why the game wouldn't figure out correctly who the winner is. That answers the first question.
Now, my next question is, When I print out who wins, cout << "Player1 wins" prints out infinitely. Not sure what's going on. Here's my updated code:
edit: Also realizing that my char[] array of spaces don't reset for a new game since I do not have code to do it... How would I do that?
edit2: am now realizing that I can't use brackets around initializers if I intend to reset them again in the future, so that solves that problem. But I still would like to let the program sleep for a few seconds to display who won the previous game before starting a new game. Is that possible?
So since stateOfGame never changes, it just keeps executing that part of the code.
I'd get rid of stateOfGame altogether. Almost every state simply causes the next state to execute. Instead, you could structure the code like this: I've used function calls to indicate the functionality, but you will probably just write the code inline:
I would have findWinner() return a char: either 0, 'X' or 'O'. You don't need to check for X and O separately, this code checks to see if someone has won in the top row and returns the winner if so:
1 2 3
if (tiles[1] == tiles[2] && tiles[2] == tiles[3]) {
return tiles[1]; // return 'X' or 'O'
}
If you put that in a loop, you can check each row.
A similar look can check each column.
That leaves just two special cases for the diagonals. Your entire findWinner() function should be less than 30 lines of code.
-1 is the default result to keep asking for user input. If Player1 wins, or Player2 wins, result will be either 1, or 2, but will return -1 if a winner has not been chosen.
Also of note, I need to write code to process draw results. But I think I can figure out how to do that...
And thanks for the advice, dhayden. I'll see what I can do.