• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dividing Code into classes  RSS feed

 
Arran Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a program to play Tic-Tac-Toe and the command line and it works fine but its all one java file and I want to divide it into classes to make it more reliable.

Ive tried but have no luck so I'm wondering if anyone on here could show me what to do to sort it out .

Heres the code :




 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I'd start wil getting your code out of main() and changing the static methods and variables to instance.
 
Arran Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there a lot of work involved in dividing this code up into classes ?
 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can be a lot of work. This kind of work is called "refactoring" in general, and it is a very useful skill for a programmer to have. IDEs can help, but at first it's a good idea to do it by hand.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid there is. I can probably write all the code in an hour, but I have done it before so I might be quicker than you. You need to spend much longer thinking about the code than writing it. Start by writing down a description of the game. Then highlight all the verbs and all the nouns and show us what you highlighted.

The verbs and nouns technique is crude and simple but works well for a many smaller applications.
 
Arran Farrell
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I am afraid there is. I can probably write all the code in an hour, but I have done it before so I might be quicker than you. You need to spend much longer thinking about the code than writing it. Start by writing down a description of the game. Then highlight all the verbs and all the nouns and show us what you highlighted.

The verbs and nouns technique is crude and simple but works well for a many smaller applications.



If it takes you an hour it'll take me a year haha
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:. . . This kind of work is called "refactoring" . . .
Would you refactor such monolithic code at all, or would you start from scratch?
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good way to start is to look at your code and see if you can spot "things" that you work with a lot. I can already spot a few things:

  • A board.
  • Squares.
  • Rows.
  • Players.
  • Symbols.
  • Moves.
  • A game.

  • Not all of these have enough responsibilities to warrant a whole new class, but you can start with moving out those that do.

    Remember that whenever you need a static variable or method, you probably didn't divide up your responsibilities enough.

    For instance, the static isGameDecided() could be a non-static Game.isDecided() instead.
     
    Knute Snortum
    Sheriff
    Posts: 4087
    112
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    Knute Snortum wrote:. . . This kind of work is called "refactoring" . . .
    Would you refactor such monolithic code at all, or would you start from scratch?

    You might be right. Time for a rewrite. (Because I tend to like rewrites I have taught myself to lean towards refactoring, but I may have leaned too far!)
     
    Arran Farrell
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Im working on other projects so haven't got the time for a rewrite. Was hoping for a simple solution to what I've got but it seems thats not possible :L
     
    Campbell Ritchie
    Marshal
    Posts: 55772
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Stephan van Hulst wrote:. . .
  • A board.
  • Squares.
  • Rows.
  • Players.
  • Symbols.
  • Moves.
  • A game.
  • . . .

    For instance, the static isGameDecided() could be a non-static Game.isDecided() instead.
    I would have thought the only element in your list maybe not worth creating a class for is move.

    The difference between static gameDecided and non‑static gameDecided is that (static) gameDecided = true; means every game is now marked as “decided” whereas the non‑static version means different games can be completed or still in play.
    I would think it is easier to create a game for two human players, because then you do not have to program the rules into the game.
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 7817
    142
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I would think it is easier to create a game for two human players, because then you do not have to program the rules into the game.

    Or indeed, a Player has-a Strategy, where one of the strategies is to use the console to ask the user for a move. I might be moving ahead of myself though.
     
    Junilu Lacar
    Sheriff
    Posts: 11165
    160
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Likes 2 Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Arran Farrell wrote:Im working on other projects so haven't got the time for a rewrite. Was hoping for a simple solution to what I've got but it seems thats not possible :L

    Famous last words.

    If you don't learn how to organize your code properly, you'll keep making the same kind of mess you have made here. It's best to slow down now and not rush headlong into your next project before you have some idea of what kind of bad things you should avoid in the future.

    If I were to point out the bad things you did there, it definitely would be a pretty long list. You've already been told that you should not cram everything into main. Another thing is that you shouldn't have all static methods. There are many more things but it would take a while to list them so if you really have no time, by all means go ahead with your other project. I'd encourage you to revisit this program when you do have some time in the future so you can learn and get better as a programmer.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!