added TextDisplay and GraphicsDisplay
This commit is contained in:
		@@ -8,5 +8,6 @@
 | 
			
		||||
    <orderEntry type="inheritedJdk" />
 | 
			
		||||
    <orderEntry type="sourceFolder" forTests="false" />
 | 
			
		||||
    <orderEntry type="library" name="scala-sdk-2.13.12" level="project" />
 | 
			
		||||
    <orderEntry type="library" name="FunGraphics-1.5.13" level="application" />
 | 
			
		||||
  </component>
 | 
			
		||||
</module>
 | 
			
		||||
							
								
								
									
										5
									
								
								src/magic_squares/Displayable.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								src/magic_squares/Displayable.scala
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
package magic_squares
 | 
			
		||||
 | 
			
		||||
trait Displayable {
 | 
			
		||||
  def display(g: Grid): Unit
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										53
									
								
								src/magic_squares/GraphicsDisplay.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										53
									
								
								src/magic_squares/GraphicsDisplay.scala
									
									
									
									
									
										Normal 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)
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										9
									
								
								src/magic_squares/TextDisplay.scala
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								src/magic_squares/TextDisplay.scala
									
									
									
									
									
										Normal 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(","))
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user