diff --git a/Lab17.iml b/Lab17.iml
index 4de040d..712d48a 100644
--- a/Lab17.iml
+++ b/Lab17.iml
@@ -8,5 +8,6 @@
+
\ No newline at end of file
diff --git a/src/magic_squares/Displayable.scala b/src/magic_squares/Displayable.scala
new file mode 100644
index 0000000..c29aa23
--- /dev/null
+++ b/src/magic_squares/Displayable.scala
@@ -0,0 +1,5 @@
+package magic_squares
+
+trait Displayable {
+ def display(g: Grid): Unit
+}
diff --git a/src/magic_squares/GraphicsDisplay.scala b/src/magic_squares/GraphicsDisplay.scala
new file mode 100644
index 0000000..d981621
--- /dev/null
+++ b/src/magic_squares/GraphicsDisplay.scala
@@ -0,0 +1,53 @@
+package magic_squares
+
+import hevs.graphics.FunGraphics
+
+import java.awt.{Color, Font}
+import javax.swing.SwingConstants
+
+trait GraphicsDisplay extends Displayable {
+ private var _win: Option[FunGraphics] = None
+ private val WIDTH: Int = 400
+ private val HEIGHT: Int = 400
+ private val MARGIN: Int = 20
+ private val FONT: Font = new Font("Arial", Font.PLAIN, 24)
+ override def display(g: Grid): Unit = {
+ if (_win.isEmpty) {
+ _win = Some(new FunGraphics(WIDTH, HEIGHT))
+ }
+ val win: FunGraphics = _win.get
+ win.clear()
+ val size: Int = g.length
+
+ val w: Int = WIDTH - 2 * MARGIN
+ val h: Int = HEIGHT - 2 * MARGIN
+ val gridSize: Int = math.min(w, h)
+ val cellSize: Int = gridSize / size
+ val ox: Int = (WIDTH - gridSize) / 2
+ val oy: Int = (HEIGHT - gridSize) / 2
+
+ win.drawRect(ox, oy, gridSize, gridSize)
+ for (i: Int <- 1 until size) {
+ win.drawLine(ox + cellSize * i, oy, ox + cellSize * i, oy + gridSize)
+ win.drawLine(ox, oy + cellSize * i, ox + gridSize, oy + cellSize * i)
+ }
+
+ for (y: Int <- 0 until size) {
+ for (x: Int <- 0 until size) {
+ val v: Int = g(y)(x)
+ if (v != 0) {
+ win.drawString(
+ (ox + cellSize * (x + 0.5)).toInt,
+ (oy + cellSize * (y + 0.5)).toInt,
+ v.toString,
+ FONT,
+ Color.BLACK,
+ SwingConstants.CENTER,
+ SwingConstants.CENTER
+ )
+ }
+ }
+ }
+ win.syncGameLogic(5)
+ }
+}
diff --git a/src/magic_squares/MagicSquareSolver.scala b/src/magic_squares/MagicSquareSolver.scala
index dabc328..ae8be0a 100644
--- a/src/magic_squares/MagicSquareSolver.scala
+++ b/src/magic_squares/MagicSquareSolver.scala
@@ -1,6 +1,6 @@
package magic_squares
-class MagicSquareSolver(initialGrid: Grid) {
+abstract class MagicSquareSolver(initialGrid: Grid) extends Displayable {
protected var _initial: Grid = initialGrid
protected val _size: Int = _initial.length
protected val SUM: Int = 15
@@ -28,6 +28,7 @@ class MagicSquareSolver(initialGrid: Grid) {
private def solveFrom(grid: Grid, x: Int, y: Int): Option[Grid] = {
if (DEBUG) println(s"Solving from $x, $y")
if (DEBUG) print(grid)
+ display(grid)
if (!isValid(grid)) {
if (DEBUG) println(" Grid is invalid")
return None
@@ -111,7 +112,7 @@ object MagicSquareSolver {
Array(8, 0, 0),
Array(0, 0, 7),
Array(0, 9, 0)
- ))
+ )) with TextDisplay
solver1.solve()
println()
/*
@@ -124,7 +125,7 @@ object MagicSquareSolver {
Array(9, 0, 0),
Array(0, 0, 7),
Array(0, 8, 0)
- ))
+ )) with TextDisplay
solver2.solve()
println()
/*
@@ -135,7 +136,7 @@ object MagicSquareSolver {
Array(0, 0, 2),
Array(0, 5, 7),
Array(0, 0, 0)
- ))
+ )) with GraphicsDisplay
solver3.solve()
/*
4,9,2
diff --git a/src/magic_squares/TextDisplay.scala b/src/magic_squares/TextDisplay.scala
new file mode 100644
index 0000000..396d244
--- /dev/null
+++ b/src/magic_squares/TextDisplay.scala
@@ -0,0 +1,9 @@
+package magic_squares
+
+trait TextDisplay extends Displayable {
+ override def display(g: Grid): Unit = {
+ for (y: Int <- g.indices) {
+ println(g(y).mkString(","))
+ }
+ }
+}