diff --git a/src/main/scala/de/htwg/se/minesweeper/controller/baseController/BaseControllerState.scala b/src/main/scala/de/htwg/se/minesweeper/controller/baseController/BaseControllerState.scala index 10d0135..93e1b72 100644 --- a/src/main/scala/de/htwg/se/minesweeper/controller/baseController/BaseControllerState.scala +++ b/src/main/scala/de/htwg/se/minesweeper/controller/baseController/BaseControllerState.scala @@ -5,7 +5,9 @@ import scala.util.{Failure, Success, Try} import de.htwg.se.minesweeper.controller._ private abstract class BaseControllerState(controller: BaseController) { - def reveal(x: Int, y: Int): Try[Unit] = { + // returns the field before the reveal + def reveal(x: Int, y: Int): Try[FieldInterface] = { + val field = controller.getField controller.getField.withRevealed(x, y) match { case Success(value) => controller.field = value case Failure(exception) => return Failure(exception) @@ -19,12 +21,12 @@ private abstract class BaseControllerState(controller: BaseController) { } else if controller.field.hasWon then { controller.notifyObservers(WonEvent()) } - Success(()) + Success(field) } } private class FirstMoveBaseControllerState(controller: BaseController) extends BaseControllerState(controller) { - override def reveal(x: Int, y: Int): Try[Unit] = { + override def reveal(x: Int, y: Int): Try[FieldInterface] = { while controller.field.getCell(x, y) match { case Success(cell) => cell.nearbyBombs != 0 || cell.isBomb case Failure(exception) => return Failure(exception) @@ -32,10 +34,14 @@ private class FirstMoveBaseControllerState(controller: BaseController) extends B controller.field = controller.factory.createField(controller.width, controller.height, controller.bomb_chance) } - controller.undoStack = controller.undoStack.prepended(new RevealCommand(controller, x, y)) controller.changeState(AnyMoveBaseControllerState(controller)) - - super.reveal(x, y) + val field = controller.getField + super.reveal(x, y) match { + case Success(_) => Success(field) + case Failure(exception) => { + Failure(exception) + } + } } } diff --git a/src/main/scala/de/htwg/se/minesweeper/controller/baseController/Commands.scala b/src/main/scala/de/htwg/se/minesweeper/controller/baseController/Commands.scala index 8ad755a..e2eb3bc 100644 --- a/src/main/scala/de/htwg/se/minesweeper/controller/baseController/Commands.scala +++ b/src/main/scala/de/htwg/se/minesweeper/controller/baseController/Commands.scala @@ -10,13 +10,24 @@ private trait Command { } private class RevealCommand(controller: BaseController, x: Int, y: Int) extends Command { - private val field = controller.getField - override def execute(): Try[Unit] = controller.state.reveal(x, y) + private var field = controller.getField + override def execute(): Try[Unit] = { + controller.state.reveal(x, y) match { + case scala.util.Success(field) => { + this.field = field + scala.util.Success(()) + } + case scala.util.Failure(exception) => scala.util.Failure(exception) + } + } override def undo(): Unit = { controller.field = field controller.notifyObservers(FieldUpdatedEvent(field)) } - override def redo(): Try[Unit] = controller.state.reveal(x, y) + override def redo(): Try[Unit] = controller.state.reveal(x, y) match { + case scala.util.Success(_) => scala.util.Success(()) + case scala.util.Failure(exception) => scala.util.Failure(exception) + } } private class FlagCommand(controller: BaseController, x: Int, y: Int) extends Command {