• Post Reply Bookmark Topic Watch Topic
  • New Topic

Retrieving data from an interface that returns enums from two different classes  RSS feed

 
Veronika Abrudbanyay
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

I have received a project to solve that uses Enums and I am a bit stuck. It's a tic tac toe game that has enums that store the locations of cells in a 3x3 grid table, and enums that store the state of the cells (empty, X, or O). Plus I have an interface for the GameBoard that returns CellState getCellState (CellLocation cellLocation); My task is to write the Consultant interface only and I cannot change anything in the code that was provided to me. I am struggling to check the status of the Board before each steps.

My idea was that X starts the game, I use a for loop for steps and at each step I check if nrOfSteps%2==0. If Yes, I set the cellState to player O. Check if we are at winning poistion, if we have won or if there's a draw. If not, I will suggest a move. E.g.




Now, my problem is that I have tried the following for the isWon method (partial code validating only the first row for now):



But I have realized that the current Status of the board could not be equal to only one cell as I am checking in the above code. And it obviously cannot be equal to 3 different "only one cell"-s. And I am not even sure if I could be checking if the board had only one cell occupied by player X by this " board.equals (CellLocation.TOP_LEFT) && cState==player".

Could someone please give a tip on how I can incorporate both the CellState and CellLocation into one query? Should I use arrays? e.g. "CellState[][]"?

The problem is that the interface I have to write will have a "public CellLocation suggest (GameBoard gameBoard)" method that returns null. It shouldn't have a main method and the JUnit test that was pre-written for me looks at this suggest method. I will have to delegate the tasks from here.
But I cannot delegate any tasks if I don't know how to check the current status of the board.
 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Before I can help you specifically, I need to clear up some confusion.  But doing this will probably help your programming too.

I have an interface for the GameBoard that returns CellState getCellState (CellLocation cellLocation);


I'm assuming you mean that you have an Interface with the abstract method above that when implemented returns the CellState from the CellLocation.  So you have a GameBoard that had nine CellLocations, not nine Cells?  It seems to me that a CEll would have a state and a location.

My task is to write the Consultant interface only and I cannot change anything in the code that was provided to me.


This seems backwards.  You usually write an Interface and then implement it in other classes.  Are you saying that there is code provided in the Consultant Interface that you can't change?  Or are you implementing an Interface that's already been written?

The first code block you provide is poorly formatted and leaves out optional braces.  This makes it very hard to understand.  Here is one way it could be formatted:

Throwing an IllegalStateException is a terrible way to indicate when the game is won or drawn.  First, it's not an illegal state.  Second, an exception should only be thrown when the is something exceptional.  Winning or drawing a game is very expected.


 
Knute Snortum
Sheriff
Posts: 4270
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More...

Could someone please give a tip on how I can incorporate both the CellState and CellLocation into one query?


This is where having a Cell with a state and location is going to help, I think.  A GameBoard would have an array of Cells in this scenario.

The problem is that the interface I have to write will have a "public CellLocation suggest (GameBoard gameBoard)" method that returns null.


It looks like it returns a CellLocation, and that can be null?  Returning null is usually a bad practice.  What does returning null indicate?

It shouldn't have a main method...


Interfaces never do.  Are you talking about a class that implements this interface?

...and the JUnit test that was pre-written for me looks at this suggest method.


So you have an interface with the abstract method public CellLocation suggest (GameBoard gameBoard); and a JUnit test that expects a class that implements that interface?  Okay, then changing to Cells isn't going to work.

I will have to delegate the tasks from here.  But I cannot delegate any tasks if I don't know how to check the current status of the board.


So let's start with a GameBoard that can determine its status.  First, what are the statuses that a GameBoard can have?  Next, how do you determine them?  Instead of trying to "fix" your current code, I'd start by writing down how you would determine a status, step by step as if you were a child or six or seven.  Start at a high level, then break the task down into smaller tasks.  You could write a bunch of monstrous if statements, but think of a better way.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!