Skip to content

Commit

Permalink
Enhance closing behavior
Browse files Browse the repository at this point in the history
Co-authored-by: DonatJR <[email protected]>
  • Loading branch information
SailReal and DonatJR committed Nov 16, 2018
1 parent 0ccda39 commit a417fb5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
12 changes: 2 additions & 10 deletions src/main/scala/de/htwg/se/sudoku/Sudoku.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.htwg.se.sudoku

import java.awt.GraphicsEnvironment
import java.io.BufferedReader

import akka.actor.{ActorSystem, Props}
import akka.pattern.ask
Expand Down Expand Up @@ -38,18 +39,9 @@ object Sudoku {
controller.createNewGrid

def main(args: Array[String]): Unit = {
var input: String = ""
tui.processInput(new BufferedReader(Console.in))

do {
Thread.sleep(200)

if (Console.in.ready()) {
input = readLine()
tui.processInputLine(input)
}
} while (input != "q")
webserver.unbind()
fileIoHttpServer.unbind()
controller.finish()
}
}
27 changes: 19 additions & 8 deletions src/main/scala/de/htwg/se/sudoku/aview/Tui.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package de.htwg.se.sudoku.aview

import java.io.BufferedReader

import com.typesafe.scalalogging.{LazyLogging, Logger}
import de.htwg.se.sudoku.controller.controllerComponent.ControllerInterface
import de.htwg.se.sudoku.controller.controllerComponent.GameStatus
import de.htwg.se.sudoku.controller.controllerComponent.{
CandidatesChanged,
CellChanged,
GridSizeChanged
}
import de.htwg.se.sudoku.controller.controllerComponent._

import scala.swing.Reactor
import scala.swing.event.Event

class Tui(controller: ControllerInterface) extends Reactor with LazyLogging {

var stopProcessingInput = false

def processInput(input: BufferedReader) = {
while (!stopProcessingInput) {
if (input.ready()) {
val line = input.readLine()
processInputLine(line)
} else {
Thread.sleep(200) // don't waste cpu cycles if no input is given
}
}
}


listenTo(controller)
def size = controller.gridSize
def randomCells: Int = size * size / 8

def processInputLine(input: String): Unit = {
input match {
case "q" =>
case "q" => controller.finish()
case "e" => controller.createEmptyGrid
case "n" => controller.createNewGrid
case "z" => controller.undo
Expand All @@ -46,6 +56,7 @@ class Tui(controller: ControllerInterface) extends Reactor with LazyLogging {
case event: GridSizeChanged => printTui
case event: CellChanged => printTui
case event: CandidatesChanged => printCandidates
case event: SudokuShutdown => stopProcessingInput = true
}

def printTui: Unit = {
Expand Down
12 changes: 9 additions & 3 deletions src/main/scala/de/htwg/se/sudoku/aview/gui/SwingGui.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.htwg.se.sudoku.aview.gui

import de.htwg.se.sudoku.controller.controllerComponent.{CandidatesChanged, CellChanged, ControllerInterface, GridSizeChanged}
import java.awt.event.WindowEvent

import de.htwg.se.sudoku.controller.controllerComponent._
import de.htwg.se.sudoku.util.Observer
import javax.swing.WindowConstants

import scala.swing.Swing.LineBorder
import scala.swing._
Expand All @@ -16,7 +19,9 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer {
title = "HTWG Sudoku"
var cells = Array.ofDim[CellPanel](controller.gridSize, controller.gridSize)

override def closeOperation(): Unit = System.exit(0)
override def closeOperation(): Unit = controller.finish()

peer.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)

def highlightpanel = new FlowPanel {
contents += new Label("Highlight:")
Expand Down Expand Up @@ -82,7 +87,7 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer {
controller.load
})
contents += new MenuItem(Action("Quit") {
System.exit(0)
controller.finish()
})
}
contents += new Menu("Edit") {
Expand Down Expand Up @@ -133,6 +138,7 @@ class SwingGui(controller: ControllerInterface) extends Frame with Observer {
case event: GridSizeChanged => resize(event.newSize)
case event: CellChanged => redraw
case event: CandidatesChanged => redraw
case event: SudokuShutdown => peer.dispatchEvent(new WindowEvent(peer, WindowEvent.WINDOW_CLOSING))
}

def resize(gridSize: Int) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,5 @@ class CellChanged extends Event
case class GridSizeChanged(newSize: Int) extends Event

class CandidatesChanged extends Event

class SudokuShutdown extends Event
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,8 @@ class Controller @Inject()(var grid: GridInterface)
grid = grid.setShowCandidates(row, col)
}

override def finish(): Unit = fileIo.unbind()
override def finish(): Unit = {
publish(new SudokuShutdown)
fileIo.unbind()
}
}

0 comments on commit a417fb5

Please sign in to comment.