Campbell Ritchie wrote:There are several different ways to arrange those nine squares, but try the individual squares out first.
Campbell Ritchie wrote:How are you going to set the symbol on each square? What are you going to do if somebody mistakenly tries to go for an occupied square?
Campbell Ritchie wrote:By the way: try these symbols: × is × and ○ is ○
Quite right. You want there to be exactly two values, o and x, so you can create an enum with those two elements. Then you get exactly those two values.Prasanna Raman wrote:I actually wanted to use enum, . . .
Why do you want a char? You can use an enum element. Read about enums in the Java Tutorials, and you should see how easy they are to use. You can enhance an enum by overriding toString or other methods, adding fields and constructors, etc., etc. But in this simple case you might not need to.Now, I don't know how to receive a 'char' as input and check if it is one of the enum constants. . . .
Yes, but it would be better to say you have designed a type to match the Square class and the game's rules, and you are using that.Prasanna Raman wrote: . . . So now, we are limiting what could be passed to the Square class as opposed to checking if the passed symbol is valid. Is my understanding correct?
That is because you have not tested your class properly.I don't understand what you've said about null. . . .
Prasanna Raman wrote: . . .
. . .
Yes, I was actually going to thank you for making me read up on enums. I did print the enum elements and it was all so easyCampbell Ritchie wrote:When you changed the Symbols enum, did you print all three elements? If you did, you will see how easily you can obtain the desired behaviour, don't you.
Oh, OK! You're referring to the IllegalArgumentException? So you had me include this exception instead of IllegalMoveException for BLANK inputs because this could be prevented by the programmer? Is that the difference we need to have in mind when creating exceptions to decide between checked and unchecked?Campbell Ritchie wrote:Don't include unchecked Exceptions in the throws clause. You should usually avoid catching unchecked Exceptions in real production code.
Yes, I am thinking along the lines of notifying the calling method by using boolean return values or exit status. I will continue to think about other ways if this is not what you had in mind but just wanted to ask you if I am way off.Campbell Ritchie wrote:Did you work out what the two alternatives to throwing an Exception were that I was thinking of?
Not yet. But should it be in the Game class?Campbell Ritchie wrote:Have you worked out where the most appropriate place to catch the IllegalMoveException is?
Thank youCampbell Ritchie wrote:I think you have got the Sqaure cracked; let's try Board. And it's getting better
You're welcomePrasanna Raman wrote: . . . thank you for making me read up on enums. . . .
Yes, I am thinking along the lines of notifying the calling method by using boolean return values or exit status. I will continue to think about other ways if this is not what you had in mind but just wanted to ask you if I am way off.Campbell Ritchie wrote:Have you worked out what I thought the alternatives to throwing those Exceptions were? If so, which do you think is the best solution?
In this case, I think only using exceptions allows us to notify the Game class of all possible errors (like IllegalMove, IllegalArgument etc.) If we use a boolean return type, the game would just know that the move has failed but not why it failed. In the other option that you showed, would the result of the game at the end not be wrong if we just allowed the "dumb" users to continue playing?Campbell Ritchie wrote:Which of the options do you think is the best?
I tried this, but unable to make it workCampbell Ritchie wrote:Did you consider this form of no‑args constructor for Symbols?
Prasanna Raman wrote: . . . In this case, I think only using exceptions allows us to notify the Game class of all possible errors (like IllegalMove, IllegalArgument etc.) If we use a boolean return type, the game would just know that the move has failed but not why it failed. In the other option that you showed, would the result of the game at the end not be wrong if we just allowed the "dumb" users to continue playing?
Is my thinking right? What would you do and why?
This is a question I've been asking myself several times in the last few days, so I will ask you now. When I've finished writing an application, should it work with any front end? What should be my approach at this stage? Should I try and cover all possible cases and test them in my code itself because I am just starting to learn object oriented programming? Or, should I leave the front end to do a bit of work with the inputs? I am asking not just for this game, but even in general. It's really confusing for me at this stage.Campbell Ritchie wrote:Another possibility is to put something in the ActionListener in the Swing app which tests the displayed square before passing the symbol to the Game in the first place.
So, whoever is writing the Game class must know from your documentation that this method returns false only when an illegal move has happened?Campbell Ritchie wrote:I might return false instead of IllegalMoveException but use IllegalArgumentException....
Yes. You should be writing that sort of thing in the documentation all the time.Prasanna Raman wrote: . . . So, whoever is writing the Game class must know from your documentation that this method returns false only when an illegal move has happened?
You're welcomeThank you for pointing out enum name and the documentation. I will make those changes. Learning a lot, can't thank you enough
Consider Paul's rocket mass heater. |