added TextDisplay and GraphicsDisplay

This commit is contained in:
Louis Heredero 2024-04-23 14:45:07 +02:00
parent 8e9567e1a2
commit 7141534e9f
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 73 additions and 4 deletions

View File

@ -8,5 +8,6 @@
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="scala-sdk-2.13.12" level="project" /> <orderEntry type="library" name="scala-sdk-2.13.12" level="project" />
<orderEntry type="library" name="FunGraphics-1.5.13" level="application" />
</component> </component>
</module> </module>

View File

@ -0,0 +1,5 @@
package magic_squares
trait Displayable {
def display(g: Grid): Unit
}

View File

@ -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)
}
}

View File

@ -1,6 +1,6 @@
package magic_squares package magic_squares
class MagicSquareSolver(initialGrid: Grid) { abstract class MagicSquareSolver(initialGrid: Grid) extends Displayable {
protected var _initial: Grid = initialGrid protected var _initial: Grid = initialGrid
protected val _size: Int = _initial.length protected val _size: Int = _initial.length
protected val SUM: Int = 15 protected val SUM: Int = 15
@ -28,6 +28,7 @@ class MagicSquareSolver(initialGrid: Grid) {
private def solveFrom(grid: Grid, x: Int, y: Int): Option[Grid] = { private def solveFrom(grid: Grid, x: Int, y: Int): Option[Grid] = {
if (DEBUG) println(s"Solving from $x, $y") if (DEBUG) println(s"Solving from $x, $y")
if (DEBUG) print(grid) if (DEBUG) print(grid)
display(grid)
if (!isValid(grid)) { if (!isValid(grid)) {
if (DEBUG) println(" Grid is invalid") if (DEBUG) println(" Grid is invalid")
return None return None
@ -111,7 +112,7 @@ object MagicSquareSolver {
Array(8, 0, 0), Array(8, 0, 0),
Array(0, 0, 7), Array(0, 0, 7),
Array(0, 9, 0) Array(0, 9, 0)
)) )) with TextDisplay
solver1.solve() solver1.solve()
println() println()
/* /*
@ -124,7 +125,7 @@ object MagicSquareSolver {
Array(9, 0, 0), Array(9, 0, 0),
Array(0, 0, 7), Array(0, 0, 7),
Array(0, 8, 0) Array(0, 8, 0)
)) )) with TextDisplay
solver2.solve() solver2.solve()
println() println()
/* /*
@ -135,7 +136,7 @@ object MagicSquareSolver {
Array(0, 0, 2), Array(0, 0, 2),
Array(0, 5, 7), Array(0, 5, 7),
Array(0, 0, 0) Array(0, 0, 0)
)) )) with GraphicsDisplay
solver3.solve() solver3.solve()
/* /*
4,9,2 4,9,2

View File

@ -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(","))
}
}
}