使用kotlin开发javafx程序,拼图游戏


import javafx.application.Application
import javafx.event.EventHandler
import javafx.geometry.Rectangle2D
import javafx.scene.Scene
import javafx.scene.control.Alert
import javafx.scene.control.Alert.AlertType
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.scene.input.MouseEvent
import javafx.scene.layout.BorderPane
import javafx.scene.layout.GridPane
import javafx.scene.layout.VBox
import javafx.stage.Stage
import java.util.Random

fun main(args: Array) = Application.launch(PingTu::class.java, *args)

class PingTu : Application() {
    val N = 9
    val n = random()              //自定义的函数,产生逆序数为偶数的不重复数组
    val m = findnum(n)  //找出那个不在随机数组里面的数字
    val imageViews = (1..N).map { ImageView() }.toTypedArray()

    @Throws(Exception::class)
    override fun start(arg0: Stage) {
        init(arg0)
    }

    fun init(stage: Stage) {
        val image = Image("pingtu/1.png")
        val gridPane = GridPane()

        var k = 0
        (0.. 2).forEach {i->
            (0.. 2).forEach {j->
                imageViews[k] = ImageView(image)        //初始化数组
                imageViews[k].onMouseClicked = Myevent()      //设置点击事件
                imageViews[k].viewport = Rectangle2D(100.0 * j, 100.0 * i, 100.0, 100.0)             //切割图片
                ++k
            }
        }

        k = 0
        (0.. 2).forEach {i->
            (0.. 2).forEach {j->
                if (k<8){//按照产生的随机数将imageView数组加入面板
                    gridPane.add(imageViews[n[k]], j, i)
                }
                ++k
            }
        }
        val incomp = ImageView(imageViews[m].image)              //用于显示空格子的图片
        incomp.viewport = imageViews[m].viewport
        val image2 = Image("pingtu/2.png")                                 //2.png为一个透明图,放在空格子中
        imageViews[m].image = image2
        gridPane.add(imageViews[m], 2, 2)
        gridPane.isGridLinesVisible = true
        val borderPane = BorderPane(gridPane)

        val comp = ImageView(image)                                     //用于显示完整的大图

        borderPane.right = VBox(20.0, incomp, comp)
        stage.scene = Scene(borderPane, 820.0, 420.0)
        stage.isResizable = false
        stage.title = "拼图"
        stage.show()
    }

    inner class Myevent : EventHandler {               //点击事件的实现
        override fun handle(arg0: MouseEvent) {
            // TODO Auto-generated method stub
            val img = arg0.source as ImageView
            val sx = img.layoutX
            val sy = img.layoutY
            val dispx = sx - imageViews[m].layoutX
            val dispy = sy - imageViews[m].layoutY
            if (dispx == -100.0 && dispy == 0.0) {               //点击的空格左边的格子
                swapimg(img, imageViews[m])             //交换imageView
                if (issucc(imageViews)) {                          //判断是否拼成功
                    val alert = Alert(AlertType.WARNING, "成功!")
                    alert.show()
                }
            } else if (dispx == 0.0 && dispy == -100.0) {        //上面的格子
                swapimg(img, imageViews[m])
                if (issucc(imageViews)) {
                    val alert = Alert(AlertType.WARNING, "成功!")
                    alert.show()
                }
            } else if (dispx == 100.0 && dispy == 0.0) {               //右边的格子
                swapimg(img, imageViews[m])
                if (issucc(imageViews)) {
                    val alert = Alert(AlertType.WARNING, "成功!")
                    alert.show()
                }
            } else if (dispx == 0.0 && dispy == 100.0) {                //下面的格子
                swapimg(img, imageViews[m])
                if (issucc(imageViews)) {
                    val alert = Alert(AlertType.WARNING, "成功!")
                    alert.show()
                }
            }
        }

        fun swapimg(i1: ImageView, i2: ImageView) {              //交换两个imageView的实现
            val row1 = GridPane.getRowIndex(i1)
            val colu1 = GridPane.getColumnIndex(i1)
            val row2 = GridPane.getRowIndex(i2)
            val colu2 = GridPane.getColumnIndex(i2)

            GridPane.setRowIndex(i1, row2)
            GridPane.setColumnIndex(i1, colu2)
            GridPane.setRowIndex(i2, row1)
            GridPane.setColumnIndex(i2, colu1)
        }
    }


    fun issucc(imageViews: Array): Boolean {                                //判断是否拼成功
        (0..8).forEach { i ->
            if (i != 3 * GridPane.getRowIndex(imageViews[i]) + GridPane.getColumnIndex(imageViews[i])) {
                return false
            }
        }
        return true
    }

    fun findnum(n: IntArray): Int {                                             //找出m
        (0..8).forEach { j ->
            if (j in n) {
            } else {
                return j
            }
        }
        return -1
    }

    fun random(): IntArray {             //生成8个不重复的逆序数为偶数的数字
        var ran = IntArray(8)
        while (!iso(ran)) {
            ran = random_num()
        }
        return ran
    }

    fun random_num(): IntArray {      //生成8个不重复数
        val r = IntArray(8)
        val random = Random()
        var i = 0
        while (i < 8) {
            r[i] = random.nextInt(9)
            for (j in 0 until i) {
                while (r[i] == r[j]) {
                    i--
                    break
                }
            }
            ++i
        }
        return r
    }

    fun iso(num: IntArray): Boolean {          //判断逆序数是否为偶数
        var sum = 0
        for (i in 0..6) {
            for (j in i..7) {
                if (num[i] > num[j]) {
                    sum++
                }
            }
        }
        return sum % 2 == 0 && sum != 0
    }
}

 

转载于:https://my.oschina.net/u/3820046/blog/3100822

你可能感兴趣的:(使用kotlin开发javafx程序,拼图游戏)