-1

I'm very new to Java and i'm struggling a little with a project we've been set. I have to make a TicTacToe game, and it must contain 5 classes only : a driver class, GUI class, an abstract class for a computer player (randomly chooses between two strategies) which is extended to two classes of a Strategic computer class and a dumb computer class.

Everyone has different views on what should be on each class so I wanted a clarification.

My "driver" class includes the main method calling on the GUI class. And my GUI class consists of all GUI components, and the computer classes containing all the computer moves. I wanted to know where I would put all codes regarding to checking for a winning combination and invoking the computer moves?

Some say in the GUI class but some argue that this will be going against the SOLID principles. Then some say it should be in the driver class? But I am little unsure as to how I would make my driver class interact to check winning moves?

  • I would advise to use try and fail approach to learn – MaxZoom Jan 21 '18 at 02:16
  • I have a fully functional code already if it were to be done all in one class already. I am just little unsure about dividing the codes and what information should go with what class as its such different views from everybody else – Nicole Velos Jan 21 '18 at 02:19
  • Your question is not a good fit for this site. Please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections to see how this site works and to better understand what questions should and should not be asked here. – Hovercraft Full Of Eels Jan 21 '18 at 02:28
  • You have the class list already predefined in first paragraph. Try to put the code into the classes based on its responsibility. – MaxZoom Jan 21 '18 at 02:30

1 Answers1

0

Based on the S in SOLID, a class should have only a single responsibility.

In this case, the GUI class should only have the responsibility of handling the display of what's going on and the mouse or keyboard input (if there is supposed to be any). That's what a (G)UI is, after all.

The driver has the responsibility of running the game, which involves keeping track of its state and asking the players what their next moves are based on the game state. Naturally, this is the place to check for a win, because this is part of running the game and keeping track of its state. You'd expect the driver to know when to stop asking the players for new moves.

Also, you shouldn't have to duplicate the checking of whether the game has been won in each and every computer-player class. That can add up to a lot of duplicated work if you add newer classes for different play styles, and what happens if you forget to add the check in one of those classes? So, the computer-player classes aren't the proper place to add it.


As far as interaction, one simple way is to have the computer-player classes have a method that goes something like:

// Returns the board state after making a move
public static Board makeMove(Board oldBoard, boolean playerIsXs) {

You can make a Player interface that provides that method and then implement it in each computer-player class, which makes it fairly easy to make new computer players. The driver calls that method, then checks the returned Board for a win or a draw, which can be a separate boolean isGameEnded(Board board) method or even three separate methods to check for X winning, O winning, and a draw.

There are other possibilities of organizing things as well, of course, so you can experiment.

Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33