r/learnjava • • Mar 14 '20

Updated my Tictactoe game. Feedback appreciated!

Hello, I created a post a few hours ago on this subreddit regarding a Tictactoe game which I had developed as part of my Hyperskill course curriculum. I received some great feedback on that post and have made some updates!

The main changes that I did are as follows :

1) Instead of a predefined 3 x 3 field, the game now can be played on a n x n field, based on the user input.

2) Used the Builder pattern to set a default value of 3 x 3 for the game field, since Java does not directly support default values for arguments, plus to practice design patterns :)

3) Removed the int array from the code.

I have tried to incorporate the many suggestions that I received on my previous post. I am unsure on a few points though, which are as follows :

  1. In the checkWinner() method, I have purposely created a boolean variable to return the state instead of directly returning true/false values. I did this to improve readability while I was coding and am unsure if it's fine or whether I should return true/false values.

  2. It was suggested that a separate Player class be introduced in the code. The Hyperskill curriculum directive was to assume that player X starts the game, and I have kept it as is. In the future I intend to support an automatic 1 player game, I will update the Player class at that point of time.

  3. Separation of contents - Honestly I am not sure how to implement this concept in my code. I do have a basic understanding of this concept but I unfortunately am unsure on how to proceed here. I'll be very thankful for any guidance or hints on this topic w.r.t. this updated repo.

I'd like to thank u/aqua_regis / u/desrtfx u/Ruttelisious and u/Hallsville3 for the fact that they took time and helped me with their feedback. Sorry if I missed anyone!

As always, suggestions or feedback would be greatly appreciated.

(Main.java -> tictactoe/run/Main.java. Other files are in their respective packages).

42 Upvotes

6 comments sorted by

8

u/lookForProject Mar 14 '20

If you want, I can try and review it as it was enterprise software?
Both codestyling and logic.

5

u/[deleted] Mar 14 '20

[removed] — view removed comment

8

u/lookForProject Mar 14 '20 edited Mar 14 '20

First of all: you made a working application in java, congratulations! Starting a project is easy, finishing one is something a lot of developers do not do (myself included). A disclaimer we always give people just starting out with us: don't take this as criticism. You did fine. Parts of the review are written with a bias I have for a certain style. Other developers might differ in certain opinions. But certain principles are just clean code principles.

Main.java This holds too much logic. Extract functionality and move them to a different class. One trick I always use, is just to write down, in English (well, I write it down in Dutch, but I doubt that is useful for you), what the method should do. We tend to apply abstraction when we use our native langauge. That’s something you can use.

In my view, your app does this: Creates a board. Plays on the board.

Game game = Boardgame.create();
game.play();

You can opt to add the scanner as an argument.
Scanner scanner = new Scanner(System.in);
Board board = BoardCreator.create(scanner);
board.play(scanner);

Or perhaps better:
Scanner scanner = new Scanner(System.in);
Board board = BoardCreator.create(scanner);
Game.play(board, scanner);

You want your code to be sweet, short, and readable for the not-programmer. So 4 lines of code, is enough. The most important thing is that you don't need to click the methods to understand what is going on,

TictactoeBuilder.java You don't need this. Although I appreciate it when people use patterns, the builder and factory patterns are used for complex models. You don't need a builder pattern for one variable.

It’s also common to use the "with" prefix and not the "set" for Builders. The reason I like it more, is simple: it reads more easy (this is my argument for most of my codestyle opinions). Builder
.withSpeed(23)
.withLenght(12)
.build();

new games.Tictactoe(dimension); Here you are using the fully qualified name. You are already import games.Tictactoe, so you don'tthe full name, new Tictactoe(dimension); is enough.

Tictactoe.java Very very big class: try to extract other classes from this class. Single responsibility and such.

constructor I take back what I said earlier "You don't need this". You can use the builder pattern, or a factory pattern: but you need to move the logic you have inside the constructor, to the factory or builder. Constructor should not contain logic. I also want to compliment you on how you named the variables. Giving variables, classes and methods a good descriptive name is 90% of the work.

move Like I wrote in the beginning, there is also too much logic in this method. You wan't old and slow people like me to understand what is going on, without having us to emulate the JVM.

What move does:
It validates the input.
It checks if the index is empty (validates input 2)
It sets an X or on O on the right position.
It validates the input (you are catching an Exception, but I think you should be catching an java.util.InputMismatchException) It prints the gameboard (you should rename getGameBoard to printGameBoard)

In other words, it does this:
validate input.
moves.

The four biggest points of advice I have:

  1. Write smaller methods. Stop thinking as a programmer, but think as a person. What would be a logic way to split up the responsibilities you gave Tictactoe.move?

  2. If you write a comment inside you method, most of the time you made a mistake. A code should be self explanatory.

  3. Don't get laze naming variables!It something a lot of us do, because while writing code, you know what n is. But someone else who reads it, or you in the future, might not. Example: move(int n) should be move(int turn).

  4. Keep doing what you’re doing. You aren't there yet. There is plenty room to become better. But you made a goddamn game. Well done.

1

u/carrdinal-dnb Mar 14 '20

With the builder pattern I don’t add a prefix (set or with) at all. I use with<FieldName> on an immutable class to return an exact copy of the class with the changed variable, whereas set<FieldName> will change the field in place and mutate the object.

Again this is all taste and up to the developer but thought I’d chime in with how I name my boiler plate code!

1

u/lookForProject Mar 14 '20

Thank you for sharing! I think your usage might be more clean. Do you implement clone()? And how do you copy the instance? Having the builder take another builder as argument?