Skip to content

Commit

Permalink
fixed undo bug
Browse files Browse the repository at this point in the history
  • Loading branch information
bafto committed Dec 20, 2023
1 parent 25d46e4 commit 02b41b9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,23 +21,27 @@ 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)
} do {
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)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 02b41b9

Please # to comment.