Win a copy of Programmers Guide to Apache Thrift this week in the Open Source forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Tic-tac-toe Design  RSS feed

 
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I like it. But I still don't think you need to run the loop at all for some of the diagonals.
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are these better?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I think I was mistaken earlier when I first wrote these methods. I assumed that you wanted me to check all the rows, columns and diagonals irrespective of the square used by the player. But, did you already approve of this method where we check only the required rows and columns?
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I tried a noughts and crosses game myslef, I checked all rows and columns and diagonals. A long time ago, you said you would only check the rows which had changed. So you only need to check the diagonals when your square is on a diagonal. Which you have achieved with row == column Well done.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, those methods are better.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. What should I do next?
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Get the Game class running so you print “X's turn” followed by a pretty‑print of the board. Give it an interface so you can pass instructions from a GUI later.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I have these methods in the Board class, do I need the getBoard() method? Can I remove geBoard(), make those 4 methods public but not static and use the board instance?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Get the Game class running so you print “X's turn” followed by a pretty‑print of the board.


Shouldn't the Game class have Player as one of its attributes? If so, when should I do Player?
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote: . . . do I need the getBoard() method? . . . Can I remove geBoard(), make those 4 methods public but not static and use the board instance?

Don't know. I think those four methods should be public instance methods and not take the board/grid as a parameter. Yes, use the board field.
The fact that you made that suggestion makes me think you are getting to think object‑oriented. ()
For the time being give getBoard() private access, then you can change it back to public later if you find you actually need it. If you print the state of the game from the Game class, then you will still need getBoard() as not private.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The fact that you made that suggestion makes me think you are getting to think object‑oriented. ()

Thank you very much I've actually been very worried over the last few days and started to wonder if I will ever get the hang of this.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you might have missed my previous post about the Game class.

Campbell Ritchie wrote:Get the Game class running so you print “X's turn” followed by a pretty‑print of the board.


Shouldn't the Game class have Player as one of its attributes? If so, when should I do Player?
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Player? Now. You somehow must ensure there are only two Player instances at any time. You can have Player X and Player O. Each Player can have a count of games played and won and lost.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prasanna Raman wrote: . . . Shouldn't the Game class have Player as one of its attributes? . . .

Two Players, surely.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Player? Now. You somehow must ensure there are only two Player instances at any time. You can have Player X and Player O. Each Player can have a count of games played and won and lost.

Thank you! I'll work on the Player class then.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my Player class as it is now; I still need to implement the play() method. I think I also need to do something if someone tries to call this method more than once, but I wanted to check with you if the approach is right before going too deep into it.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Posting this just in case you didn't receive notification for my previous post.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for delay; have been busy.
i am not sure whether that method is good or not. If you call that method several times, you will get several arrays. Is that the best way to ensure there are two Player objects? Should you have a Player[] array as a private static field instead. Should that array be in the Game class or the Player class?


I would have put it in the Game class, myself.

Are you going to be able to count how many games a particular Player has won or lost?
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I haven't been able to work on this today. I'll actually be away for quite a while from today; visting family. I'll be back on December 2. I am not sure if I can connect to the internet from there I'll try to work on this if I can when I'm there, but will definitely post as soon as I'm back in December. Thank you for all your help so far
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome and have a good time.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Happy New Year!! Sorry, I had to extend my stay by a month, and I couldn't get regular internet access there I came back just yesterday. I will continue to start working from where we'd left off and will post soon.
 
Campbell Ritchie
Marshal
Posts: 64471
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome back
Please start by reminding us all how far you have got, otherwise we shall forget what you have achieved so far.
 
Prasanna Raman
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is what I've done so far:

  • Enum - symbol: For the 3 symbols X,O and Blank
  • Square class: To represent the squares in the board
  • Board class: Holds grid of squares, represented as an array of arrays. The logic for game completion is determined in 4 methods in this class - SameInRow, SameInColumn, SameInBend, SameInBendSinister


  • Here's what I have left to do:
  • Player class
  • Game class
  • Game controller class

  • I'd started my Player class like this:Can I continue with the above?
     
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No. A list/array/collection of players does not belong in the player class. Get a sheet of paper and work out what does belong to a player, and what belongs to the game.
    What is the difference between game and game controller?
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think the Player class should contain the play() method. The Game class should initialise the players and store them in an array. This method (createPlayers()) that I've written shouldn't be in the Player class.

    The Game controller is the starting point of the program, which creates an instance of the game and gets it started.
     
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It sounds as if you are still confused about your design, going on about which methods should be in which class. Start by working out what you want to do, then later consider how to do it.
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I worked this out on paper.

    Player:
  • Attributes: symbol
  • Methods: play()

  • Game:
  • Attributes: Players, Board, gameStatus
  • Methods: makeMove(), checkGameStatus()

  • Game Controller:
  • contains the main method.
  • starts the game by creating an instance of Game().
  • calls the play() method using the Player instances in the Game class.
  • calls the checkGameStatus() method in the Game class to see if the game has finished.



  •  
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Surely the player takes a move and the game is played?
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, I think I missed the turns part. Initially, when I wrote down how the game is played and what components are involved on paper, I wrote down "rule book" as one of the components. When I started thinking classes, I thought my Game class would be my rule book. But now, I am thinking that my Game Controller class is the rule book, which limits the player creation to 2, makes sure that the players don't play out of turn, calls the play method using the player object, and finally checks if the game has finished.

    Player:
  • Attributes: symbol
  • Methods: play()

  • Game:
  • Attributes: Players, Board, gameStatus
  • Methods: makeMove(), checkGameStatus()

  • Game Controller:
  • contains the main method.
  • starts the game by creating an instance of Game().
  • calls the play() method using the Player instances in the Game class; makes sure that the players don't play out of turns.
  • calls the checkGameStatus() method in the Game class to see if the game has finished.

  •  
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Not sure if you've been busy, but posting this in case you didn't receive notification.
     
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Prasanna Raman wrote:Not sure if you've been busy, but posting this in case you didn't receive notification.

    Please read this.

    Why should I repeat what I have already said? I think you are getting confused, and should write down the instructions for playing noughts and crosses. Does the game take a turn or the player?
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry I only posted again because I really thought that you might not have received a notification because it's happened a couple of times in the past. Sorry again, I really didn't mean to be a pest.
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • There are 2 players in the game; they take turns to play.
  • The game begins when the first players makes a move. Then the other players makes his move.
  • The players continue to make moves in turns until there is a winner or there are no more empty squares.

  • I've written these instructions down on paper. I am really getting confused between how the game is played in real life and how I should express that in code. In real life, it's a player who starts the game. In my code, I don't know how I can make a player start the game.

    The game controller class that I wrote is the one that makes sure that the game is played by the rules. So that creates the players, makes sure the players don't skip turns and checks if the game is finished.

    I've thought about this for a long time now. I really can't figure out how to make the classes talk to each other and which one starts what.

     
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You have already got the board and square classes to talk to each other. Remind everybody what the public interface of board is.
    What you tell the board is something like, “player X would like square b2,” but that is dependent on player X deciding which square he would like. You would have a printout on the command line/terminal something like this:-Then X enters b2 and we get this:-And I thought you already had a method for returning the state of the board as a Square[] or a Symbol[] or similar, and you had already worked out how to decide whether the game was ongoing, X wins, O wins, or drawn. I think you have done a lot more of the game than you think. And remember you need a public interface for the game and the classes. That way you can put a GUI atop it.
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much. My board's interface is this:
    Yes, I have 4 methods in the Board class to determine if the game is finished. And, this method returns the board as a symbol array.
    I am getting confused with the following:

  • In real world, the player would be the one to start the game. I don't know how I should achieve that in my code. Or, is it wrong to think of the game being started by the player?
  • Should Game be the class that makes the players play by the rules or should there be a separate class, a Game Controller, to do that?

  • I've thought long and hard about the above questions, but I can't convince myself one way or the other I feel that I'm really close to finishing this, but somehow I keep going back and forth without any clarity on what should trigger what.
     
    Campbell Ritchie
    Marshal
    Posts: 64471
    225
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What the interface means is the names and public details of the methods. Not private methods. Nor the contents of the methods, which is implementation. There is an example of public interfaces here. Well 4000 examples.
    You might do well to run the javadoc tool on all your classes, and see what you get. It will probably be good if you have provided good documentation comments. One of the examples in your last post looks good, the other less so.
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, here is the interface of my Board class:
     
    Prasanna Raman
    Ranch Hand
    Posts: 442
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here's what I think my Game class will look like:

  • It will contain 2 player instances.
  • The constructor will initialise the player objects.
  • It will have a public method which will accept user input (row and column) from the console.
  • It will then call the public method in the Board class to update the board.
  • Then, it checks the game status by calling the 4 public methods in the Board class and returns a Game Completion object (or an enum).
  • It repeats steps 3-5 until the game is over.

  • But, in my Player class I have a method called play() which I never use in any of the above steps. In real life, I would think that the act of playing by a player means passing the location to the game, but I am not able to show that in my design. I don't know if my thinking is wrong or my design or both


     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!