• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Tic-tac-toe Design

 
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks good.
If you think you are not using the Player#play() method, change it to private access or comment it out completely and see whether you get any errors.
 
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I'll start coding my Game class then. So, my Player class as it stands right now just serves to generate Player objects and doesn't do much else.
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about a record wins method in Player?
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I'm away today and tomorrow. Will post in about 24 hours.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the structure of my Game class. Please review.


And here's my GameStatus enum:
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What about a record wins method in Player?



Should I have player name and other details about the players and write them to a file along with their win/loss count every time to achieve this?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do that if you wish. I would suggest that as a later enhancement, though.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. If you feel my Game class is OK, I will start working on the implementation.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above Game class I've written doesn't make use of the play() method in the Player class. So I have just removed it for now. Can I now work on the implementation of the Game class?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Work on the game and see what happens.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my Player class:
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the implementation of my Game class. Please review and comment.
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not at all, I am afraid.
Why are you calling methods recursively from inside catch blocks? That looks like very poor design.
Why are you repeatedly creating reader objects and allowing them to go out of scope?
Why are you swapping 2 and MAX_PLAYERS? Why have you got a for loop without .length?
Why are you using a reader rather than a Scanner?
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for reviewing. Sorry, I'll work on these About the recursive calls from catch block, I just thought of one scenario. Will the program run out of space if it keeps throwing exceptions because the methods below never stop? Is there any other reason why it shouldn't be done?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using exceptions instead of ordinary control structures.
Search my posts for Scanner and hasNextInt and nextint and you will see (as Rob Spoor told me ages ago) that you can run a loop which can be guaranteed to return an int from the keyboard. Look here as well as searching. That means you can be sure there is no need to do any exception handling.
There is a much simpler way to keep the people taking turns. When you play noughts and crosses, you don't stop because an Exception occurs, do you? Think when you stop and when you continue.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I'll search through the old posts. In general, you said calling a method from a catch block is poor design because it might result in the program running out of memory if the exception keeps happening? Is that the (only) reason or are there other issues that might come out of it?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasanna Raman wrote: . . . you said calling a method from a catch block is poor design because it might result in the program running out of memory . . .

No, I didn't.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry! I just meant to ask why you said it (calling methods recursively from inside catch blocks) was bad design.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote this utility class:Then I've modified my start() method in the Game class as below: Does this look OK?
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still thinking about how to call the same method again when I encounter an exception, but I think I'm going nowhere Could you please give me a hint?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasanna Raman wrote: . . . Does this look OK?

Yes, that looks good, except for two tiny things.
You have one unnecessary pair of () in the utility class.
You should say default value of 3. I suggest you set a maximum size for the board, unless you want to play on a 97×97 board.


You realise you can enhance the utility class to return a number in a particular range?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you play the game with a real pencil and paper, how do you tell whether you need to take more turns? You have actually said that, or something similar, a long time ago.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I suggest you set a maximum size for the board, unless you want to play on a 97×97 board.


I definitely don't want to play on a 97*97 board I'll set a maximum size.

Campbell Ritchie wrote:You realise you can enhance the utility class to return a number in a particular range?


Sorry if I'm mistaken, but do you mean I can enhance the utility class to make it return a number less than the maximum size of the board? If so, should I not do that in my application (Game class)?
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is my updated MyUtilites class:
And my Game class:
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking better

Prasanna Raman wrote: . . .. . .

You can simplify that method. I think you have put that class in the wrong package.
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prasanna Raman wrote: . . .

There is something untidy about the way you have set up the board.
Pleased to see you have got a while for continuing the game (), but you can simplify its condition a lot.
I do not like the regular expression for verifying the entry. If you are going to verify the entry with a regular expression, do so before you try splitting it.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Looking better

Thank you

Campbell Ritchie wrote:You can simplify that method. I think you have put that class in the wrong package.


Sorry, yes. I'll move this class to some package called utility. In what way can I simplify this method?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get rid of the initial assignment. If the line entered contains “real” input, there is no need to enter the loop. I posted what I thought was a simpler version about a week ago.

And you were right to put that class in the utility package. Remember you will be using it again and again.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:There is something untidy about the way you have set up the board.


Are you referring to the while loop being untidy? Or, can I clean up somewhere else too?

Campbell Ritchie wrote:Pleased to see you have got a while for continuing the game (), but you can simplify its condition a lot.


I can't believe how I didn't use a while loop earlier! Terrible mistake Thank you for pointing me in the right direction.

Campbell Ritchie wrote:I do not like the regular expression for verifying the entry. If you are going to verify the entry with a regular expression, do so before you try splitting it.


OK, I'll use regex on the whole string before splitting. Would you have also used regex? I was thinking about this for a long time before settling on regex. Is there something else that can be done?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I probably wouldn't have used a regex there. The way you wrote it, you are confined to a single‑digit number, and restricted from changing the largest permissible square to 10×10.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Is there something else I can try?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can put a regex on the whole input: one letter, up to two digits, and the square must not be > 26.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. For the play() while loop, is this better?
I set the gameStatus field to ONGOING when I declare it initially.
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A lot better
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you

Campbell Ritchie wrote:There is something untidy about the way you have set up the board.

Sorry, I don't understand what you're referring to here Could you please clarify?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Asking for 0 to get 3. Maybe System.out.print("Do you want to change the board size from default = 3?: yes/no");
Then you can ask for the size, remembering 1×1 and 2×2 are impossible sizes.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can get rid of the initial assignment. If the line entered contains “real” input, there is no need to enter the loop. I posted what I thought was a simpler version about a week ago.


Is this the one you're referring to? I tried to use this one as it is, but since you say "System.out.print(message)", the user types the input immediately after the message but the first line in the while loop assigns "line" to the next line, which will be empty. So I am having to enter the location twice to make it work. That's why I tweaked it a little bit. Am I missing something?
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you are right. You are going to get alternate lines like that. Sorry, I was wrong there.
 
Prasanna Raman
Ranch Hand
Posts: 546
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are my updated methods, please take a look.
 
Campbell Ritchie
Marshal
Posts: 80637
471
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better; does the output appear nice and tidy to the user?
I do not think your regex will work, however.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic