Last active
May 13, 2022 17:50
-
-
Save VizGhar/d8c284032bb205ee451d45eb14af12ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color | |
import java.awt.Dimension | |
import java.awt.Graphics | |
import java.io.File | |
import javax.imageio.ImageIO | |
import javax.swing.BorderFactory | |
import javax.swing.JFrame | |
import javax.swing.JPanel | |
import kotlin.math.* | |
const val startX = 250.0 | |
const val startY = 250.0 | |
const val riesenieX = 60 | |
const val riesenieY = 60 | |
data class Riesenie(val sila: Int, val smer: Int) { | |
fun cost() : Double { | |
var rychlost = sila.toDouble() | |
var vysledneX = startX | |
var vysledneY = startY | |
while (rychlost != 0.0) { | |
vysledneX += rychlost * cos(Math.toRadians(smer.toDouble())) | |
vysledneY += rychlost * sin(Math.toRadians(smer.toDouble())) | |
rychlost = max(rychlost - 20, 0.0) | |
} | |
val dx = vysledneX - riesenieX | |
val dy = vysledneY - riesenieY | |
return sqrt(dx * dx + dy * dy) | |
} | |
fun animate() { | |
var rychlost = sila.toDouble() | |
var vysledneX = startX | |
var vysledneY = startY | |
while (rychlost != 0.0) { | |
vysledneX += rychlost * cos(Math.toRadians(smer.toDouble())) | |
vysledneY += rychlost * sin(Math.toRadians(smer.toDouble())) | |
setBallPosition(vysledneX, vysledneY) | |
rychlost = max(rychlost - 20, 0.0) | |
} | |
} | |
} | |
fun main() { | |
createAndShowGUI() | |
// vytvorenie nahodneho riesenia | |
var riesenie = Riesenie(50, 100) | |
readLine() | |
while(true) { | |
// ziskanie susednych rieseni | |
val noveRiesenia = arrayOf( | |
Riesenie(riesenie.sila, riesenie.smer - 1), | |
Riesenie(riesenie.sila, riesenie.smer + 1), | |
Riesenie(max(riesenie.sila - 1, 0), riesenie.smer), | |
Riesenie(min(riesenie.sila + 1, 100), riesenie.smer) | |
) | |
val najlepsieNoveRiesenie = noveRiesenia.minByOrNull { it.cost() }!! | |
// vyhodnotenie | |
if (najlepsieNoveRiesenie.cost() >= riesenie.cost()) { break } | |
else { riesenie = najlepsieNoveRiesenie } | |
} | |
riesenie.animate() | |
println("najlepsie riesenie - ${riesenie.sila} / ${riesenie.smer}") | |
} | |
private val panel by lazy { MyPanel() } | |
fun createAndShowGUI() { | |
val f = JFrame("HillClimb Minigolf") | |
f.defaultCloseOperation = JFrame.EXIT_ON_CLOSE | |
f.setSize(500, 500) | |
f.add(panel) | |
f.pack() | |
f.isResizable = false | |
f.isVisible = true | |
} | |
fun setBallPosition(x: Double, y: Double) { panel.setPosition(x, y) } | |
internal class MyPanel : JPanel() { | |
private var x = startX | |
private var y = startY | |
override fun getPreferredSize(): Dimension { | |
return Dimension(500, 500) | |
} | |
public override fun paintComponent(g: Graphics) { | |
super.paintComponent(g) | |
g.color = Color(0xCFE838) | |
g.fillRect(0, 0, 500, 500) | |
g.color = Color.BLACK | |
g.fillOval(riesenieX - 10, riesenieY - 10, 20, 20) | |
try { | |
g.drawImage(ImageIO.read(File("minigolf.png")), x.roundToInt() - 7, y.roundToInt() - 7, 20, 20) { _, _, _, _, _, _ -> true } | |
} catch (ignore: Exception) { | |
g.color = Color.WHITE | |
g.fillOval(x.roundToInt() - 10, y.roundToInt() - 10, 20, 20) | |
} | |
} | |
fun setPosition(x: Double, y: Double) { | |
val stepx = (x - this.x) / 100 | |
val stepy = (y - this.y) / 100 | |
for (i in 0 .. 100) { | |
this.x += stepx | |
this.y += stepy | |
repaint() | |
Thread.sleep(10) | |
} | |
} | |
init { | |
border = BorderFactory.createLineBorder(Color.black) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment