How to return a custom type <Hero> from View (GUI) to controller?

86 Views Asked by At

I'm making an MVC game called Swingy for a 42 project, it requires me to have a console and a GUI view implemented in the MVC pattern. I figured that I would have an instance of my view (either GUI or console within the controller). So I made an interface called Viewable that both views would implement. The issue is that my controller calls this.view.newHero() which should return a new Hero() object, but I can't figure out how to get the return value from the GUI.

I've tried using a ArrayBlockingQueue but the issue seems to stem from the return value in my interface.

Controller

public GameController() {
    this.view = new GUI(); // Just for testing, will either be new Console or new GUI
    this.hero = this.view.newHero(); // The problematic function
    this.makeNewBoard();
    this.gameBoard.printBoard();
}

Interface

public interface Viewable {
  public Hero newHero();
}

Console View newHero method (shortened)

public Hero newHero() {
  name = getInput();
  heroClass = getInput();
  return new Hero(name, heroClass);
}

GUI

public Hero newHero() {
  //Here I've tried making a blockingQueue and waiting for input but the issue is that my function must return a Hero type. If I return null then it causes an issue in the controller.
}

In the GUI view the user clicks a button to start a new game, then a JOptionPane input Dialog should open and ask for the hero name. It should then return a new Hero with this name.

0

There are 0 best solutions below