From 9d6b1f767ee62802245f1eff5c31d7b4caaa6602 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Tue, 23 Apr 2024 14:59:32 +0200 Subject: [PATCH] implemented grid of size n --- src/magic_squares/GraphicsDisplay.scala | 2 +- src/magic_squares/MagicSquareSolver.scala | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/magic_squares/GraphicsDisplay.scala b/src/magic_squares/GraphicsDisplay.scala index d981621..e9d9148 100644 --- a/src/magic_squares/GraphicsDisplay.scala +++ b/src/magic_squares/GraphicsDisplay.scala @@ -48,6 +48,6 @@ trait GraphicsDisplay extends Displayable { } } } - win.syncGameLogic(5) + win.syncGameLogic(60) } } diff --git a/src/magic_squares/MagicSquareSolver.scala b/src/magic_squares/MagicSquareSolver.scala index ae8be0a..31cb763 100644 --- a/src/magic_squares/MagicSquareSolver.scala +++ b/src/magic_squares/MagicSquareSolver.scala @@ -3,7 +3,7 @@ package magic_squares abstract class MagicSquareSolver(initialGrid: Grid) extends Displayable { protected var _initial: Grid = initialGrid protected val _size: Int = _initial.length - protected val SUM: Int = 15 + protected val SUM: Int = _size * (_size * _size + 1) / 2 protected val DEBUG: Boolean = false protected def print(grid: Grid): Unit = { @@ -38,7 +38,7 @@ abstract class MagicSquareSolver(initialGrid: Grid) extends Displayable { return Some(grid) } var values: Array[Int] = Array(_initial(y)(x)) - if (values(0) == 0) values = (1 to 9).toArray + if (values(0) == 0) values = (1 to _size * _size).toArray if (DEBUG) println(s" Values to test: " + values.mkString("[", ", ", "]")) val newGrid: Grid = copy(grid) @@ -143,5 +143,14 @@ object MagicSquareSolver { 3,5,7 8,1,6 */ + + val solver4: MagicSquareSolver = new MagicSquareSolver(Array( + Array(11, 0, 0, 20, 3), + Array(4, 12, 0, 0, 16), + Array(0, 5, 13, 21, 9), + Array(10, 0, 0, 14, 22), + Array(23, 6, 19, 2, 0), + )) with GraphicsDisplay + solver4.solve() } } \ No newline at end of file