Hello, I’m here with my Tic Tac Toe game I have been working on for a udemy course. I have the game running and almost everything seems to work. I was trying to implement some protections so that the program doesn’t crash when the player gives it an invalid input. These do not work as intended and they effectively break the game. Could I get some guidance on how to clean up input and insure that the program doesn’t break when an invalid input is entered? A valid input should be a string value 1-9.
Another problem I am facing is my game loop is supposed to end with a stalemate if there is no winner and all of the spaces are filled. Instead of ending when all 9 spaces are filled my game seems to end when only 8 spaces are filled. This bug is replicable by filling all the spaces sequentially, when you enter the number 8 the program closes without finishing the Main() method.
I also would like to restart the game after the game is finished, if somebody could give me an idea for how to make this happen I would appreciate it.
General feedback is definitely welcome, I’m sure there are better/ cleaner ways to do things.
Here is my code:
May want to put this into pastebin, github or something of the sort, very hard to read long code like this on reddit and makes it hard for us to help.
I second this. And the method GameLoop returns a boolean, completely ambiguous method name and isn’t even a loop at all, a better name would be Frame or GameFrame which suggests this method is called every frame and handles a majority of the work.
Okay, I will look into that. I thought that using the markdown thing on reddit was how I was supposed to post it.
On your valid input question what I would do is use make a boolean method to return if the input is valid or not. You can use int.TryParse since the only valid inputs are integers so that you know if it fails the TryParse you can return false (then display something about invalid input) and if it passes you can check if the number parsed is between 1-9 if so then it is valid, otherwise false. Hope that helps without writing it out for you, I can give examples on anything specific if you’d like, just let me know.