App15Window.scala
/*
* Copyright (c) 2010-2013, KOTSUBU-chan and/or its affiliates.
* Copyright (c) 1998, Atelier-AYA.
* All rights reserved.
*/
package cherry.pie
// ----------------------------------------
import swing._
object App15Window extends MainFrame {
val version = App15Window
.getClass.getName+": #1.0.02 J"
// ----------------------------------------
title = "15 puzzle"
contents = new App15View(new Dimension(200,200))
peer.setLocationRelativeTo(null)
}
// ----------------------------------------
class App15View(size: Dimension) extends FlowPanel {
preferredSize = size
new Publisher {
import swing.event.MouseClicked
reactions += {
case MouseClicked(_,point,_,_,_) => update(point)
}
}.listenTo(mouse.clicks)
import scala.collection.mutable.ArrayBuffer
val tiles = ArrayBuffer.empty[Tile]
0 to 14 foreach { i => tiles += Tile(i%4, i/4, i+1) }
val nullTile = NullTile(3,3)
tiles += nullTile
shuffle(100)
// ----------------------------------------
import java.awt.Point
def update(point: Point) =
detect(point) match {
case Some(tile: Tile) => move(tile); repaint
case None =>
}
def detect(point: Point) = {
tiles find { _.contains(point) } match {
case Some(tile) =>
;print(tile)
if (tile.isInContactWith(nullTile)) Some(tile) else None
case None =>
}
}
override def paintComponent(g: Graphics2D) =
tiles foreach { _.paint(g) }
def shuffle(count: Int) {
val random = new scala.util.Random()
0 to count foreach { e => move(tiles(random.nextInt(15))) }
}
def move(tile: Tile) = tile.swap(nullTile)
}
/*
* Copyright (c) 2010-2013, KOTSUBU-chan and/or its affiliates.
* Copyright (c) 1998, Atelier-AYA.
* All rights reserved.
* ----------------------------------------
$ scalac -encoding "SJIS" -d bin src/{Apple,AppWindow,App15Window,Shape,Game}.scala
$ date; scala -cp bin cherry.pie.application
* ----------------------------------------
2013年 11月13日 水曜日 17時58分00秒 JST
----------------------------------------
cherry.pie.application$: #1.0.03
cherry.pie.appWindow$: #1.0.03
cherry.pie.Shape$: #1.0.03
cherry.pie.Game$: #1.0.03
----------------------------------------
(16,16) -> (0,0)
(45,16) -> (2,0)
(74,17) -> (4,0)
(31,41) -> (1,3)
(57,40)
(45,68) -> (2,6)
$
*/
package cherry.pie
// ----------------------------------------
import swing._
object <span style="color: #ff5252">application</span> extends SimpleSwingApplication {
val version = application
.getClass.getName+": #1.0.03"
// ----------------------------------------
println("-"*40)
println(application.version)
println(appWindow.version)
println(Shape.version)
println(Game.version)
println("-"*40)
def top = appWindow
// ----------------------------------------
App15Window.visible = true
}
/*
* Copyright (c) 2010-2013, KOTSUBU-chan and/or its affiliates.
* Copyright (c) 1998, Atelier-AYA.
* All rights reserved.
*/
package cherry.pie
// ----------------------------------------
import swing._
object appWindow extends MainFrame {
val version = appWindow
.getClass.getName+": #1.0.03"
// ----------------------------------------
title = "Honeycomb"
contents = new View(new Dimension(200,200))
peer.setLocationRelativeTo(null)
}
// ----------------------------------------
class View(size: Dimension) extends FlowPanel {
preferredSize = size
new Publisher {
import swing.event.MouseClicked
reactions += {
case MouseClicked(_,p,_,_,_) => update(p)
}
}.listenTo(mouse.clicks)
// ----------------------------------------
import scala.collection.mutable.ArrayBuffer
val tiles = ArrayBuffer.empty[Honeycomb]
tiles += Honeycomb(0,0)
tiles += Honeycomb(2,0)
tiles += Honeycomb(4,0)
tiles += Honeycomb(1,3)
tiles += Honeycomb(2,6)
// ----------------------------------------
import java.awt.Point
def update(point: Point) {
;print("(%d,%d)" format (point.x, point.y))
tiles find { _.contains(point) } match {
case Some(tile) =>
;print(" -> "+tile)
case None =>
}
;println
}
// ----------------------------------------
override def paintComponent(g: Graphics2D) =
tiles foreach { _.paint(g) }
}
/*
* Copyright (c) 2010-2013, KOTSUBU-chan and/or its affiliates.
* Copyright (c) 1998, Atelier-AYA.
* All rights reserved.
*/
package cherry.pie
// ----------------------------------------
object Game {
val version = Game
.getClass.getName+": #1.0.03"
}
// ----------------------------------------
import java.awt._
object NullTile {
def apply(x0: Int, y0: Int) = new Tile(x0, y0, 0) {
override def paint(g: Graphics2D) =
Rectangle(x*width, y*height, width, height)
.paint(g, Color.lightGray)
}
}
object Tile {
def apply(x: Int, y: Int, n: Int) = new Tile(x, y, n)
}
class Tile(var x0: Int, var y0: Int, n: Int) {
def x = x0
def x_=(x: Int) { x0 = x }
def y = y0
def y_=(y: Int) { y0 = y }
override def toString = "(%d,%d)%d" format (x,y,n)
val (width, height) = (50, 50)
val (px, py) = (x*width, y*height)
val rect = Rectangle(px, py, width, height)
val (dx, dy) = (4, 15)
val symb = Symbol(n+"", px+dx, py+dy)
def paint(g: Graphics2D) {
rect.paint(g)
symb.paint(g)
}
def contains(p: Point) = rect.contains(p)
def isInContactWith(that: Tile) = {
val a = x - that.x
val b = y - that.y
a*a + b*b == 1
}
def swap(that: Tile) = {
val (this_x, this_y) = (x, y)
this.move(that.x, that.y)
that.move(this_x, this_y)
}
def move(x: Int, y: Int) {
this.x = x; this.y = y
val (px, py) = (x*width, y*height)
rect.move(px, py)
symb.move(px+dx, py+dy)
}
}
// ----------------------------------------
object Honeycomb {
def apply(x: Int, y: Int) = new Honeycomb(x, y)
}
class Honeycomb(x0: Int, y0: Int) {
override def toString = "(%d,%d)" format (x0,y0)
val peer = Hexagon(x0, y0)
def contains(p: Point) = peer.contains(p)
def paint(g: Graphics2D) = peer.paint(g, Color.green)
}
/*
* Copyright (c) 2010-2013, KOTSUBU-chan and/or its affiliates.
* Copyright (c) 1998, Atelier-AYA.
* All rights reserved.
*/
package cherry.pie
// ----------------------------------------
object Shape {
val version = Shape
.getClass.getName+": #1.0.03"
}
// ----------------------------------------
import java.awt._
trait Shape {
def paint(g: Graphics2D)
def contains(p: Point): Boolean
def move(x: Int, y: Int) {}
}
// ----------------------------------------
abstract class Geometry extends Shape {
def paint(g: Graphics2D, fillColor: Color)
def paint(g: Graphics2D): Unit = paint(g, defaultFillColor)
def defaultFillColor = Color.white
def defaultDrawColor = Color.black
}
// ----------------------------------------
object Hexagon {
def apply(x: Int, y: Int) = new Hexagon(x, y)
}
class Hexagon(var x0: Int, var y0: Int) extends Geometry {
val (dx,dy,scale) = (7,4,2)
import scala.collection.immutable.List
val vertices = List*1
def nPoints = vertices.length
val bounds = points(scale)
def xPoints = bounds map { case (x,_) => x }
def yPoints = bounds map { case (_,y) => y }
def points(scale: Int) = {
import scala.collection.mutable.ListBuffer
val buf = ListBuffer.empty[(Int,Int)]
vertices foreach { case (x,y) =>
val px = (x0+x)*dx*scale + x0
val py = (y0+y)*dy*scale + y0 - y0/3
buf += *2
}
buf.toArray
}
// ----------------------------------------
def paint(g: Graphics2D, fillColor: Color) {
g.setColor(fillColor)
g.fillPolygon(xPoints, yPoints, nPoints)
g.setColor(defaultDrawColor)
g.drawPolygon(xPoints, yPoints, nPoints)
}
def contains(p: Point) = {
val (x,y) = (1,2)
val px = (x0+x)*dx*scale + x0
val py = (y0+y)*dy*scale + y0 - y0/3
val x1 = px - p.x
val y1 = py - p.y
val xs = dx*scale
x1*x1 + y1*y1 <= xs*xs
}
}
// ----------------------------------------
object Rectangle {
def apply(x: Int, y: Int, width: Int, height: Int) =
(new Rectangle)(x, y, width, height)
}
class Rectangle extends Geometry {
var peer: java.awt.Rectangle = _
def apply(x: Int, y: Int, width: Int, height: Int) = {
peer = new java.awt.Rectangle(x, y, width, height); this
}
def paint(g: Graphics2D, fillColor: Color) {
val (x, y) = (peer.x, peer.y)
val (width, height) = (peer.width, peer.height)
g.setColor(fillColor)
g.fillRect(x, y, width, height)
g.setColor(defaultDrawColor)
g.drawRect(x, y, width, height)
}
def contains(p: Point) = peer.contains(p)
override def move(x: Int, y: Int) = peer.setLocation(x,y)
}
// ----------------------------------------
object Symbol {
def apply(s: String, x: Int, y: Int) = new Symbol(s, x, y)
}
class Symbol(s: String, var x: Int, var y: Int)
extends Geometry {
def paint(g: Graphics2D, fillColor: Color) {
g.setColor(defaultDrawColor)
g.drawString(s, x, y)
}
def contains(p: Point) = false
override def move(x: Int, y: Int) = { this.x = x; this.y = y }
}
/*
src/Shape.scala:31: error: object List is not a value
val vertices = List(3,4)
^
/