{"id":304160,"date":"2020-05-24T21:00:24","date_gmt":"2020-05-24T21:00:24","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=304160"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=304160","title":{"rendered":"\u0418\u0437\u0443\u0447\u0430\u044e Scala: \u0427\u0430\u0441\u0442\u044c 1 \u2014 \u0418\u0433\u0440\u0430 \u0437\u043c\u0435\u0439\u043a\u0430"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/503560\/\">\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/la\/fy\/jy\/lafyjybej-y6p-4n1c4cdfl1pqc.jpeg\"><\/div>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440! \u041a\u043e\u0433\u0434\u0430 \u044f \u0438\u0437\u0443\u0447\u0430\u044e \u043d\u043e\u0432\u044b\u0439 \u044f\u0437\u044b\u043a \u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0434\u0435\u043b\u0430\u044e \u043d\u0430 \u043d\u0435\u043c \u0437\u043c\u0435\u0439\u043a\u0443. \u041c\u043e\u0436\u0435\u0442 \u043a\u0430\u043a\u043e\u043c\u0443 \u043d\u0438\u0431\u0443\u0434\u044c \u043d\u043e\u0432\u0438\u0447\u043a\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u043e\u0436\u0435 \u0438\u0437\u0443\u0447\u0430\u0435\u0442 Scala \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043a\u043e\u0434 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043d\u043e\u0432\u0438\u0447\u043a\u0430 \u0432 \u044d\u0442\u043e\u043c \u042f\u041f. \u0423 \u043e\u043f\u044b\u0442\u043d\u044b\u0445 \u0441\u043a\u0430\u043b\u0438\u0441\u0442\u043e\u0432 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043c\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 Scala \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0433\u0440\u0443\u0441\u0442\u044c. \u0417\u0430 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442. <br \/>  <a name=\"habracut\"><\/a>  <\/p>\n<h2>\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<ul>\n<li>\u0418\u0437\u0443\u0447\u0430\u044e Scala: \u0427\u0430\u0441\u0442\u044c 1 \u2014 \u0418\u0433\u0440\u0430 \u0437\u043c\u0435\u0439\u043a\u0430<\/li>\n<\/ul>\n<p>  <\/p>\n<h2>\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p>  <a href=\"https:\/\/gitlab.com\/VictorWinbringer\/scalasnakegame\" rel=\"nofollow\">\u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438<\/a><\/p>\n<h2>\u041e\u0431 \u0438\u0433\u0440\u0435<\/h2>\n<p>  \u0414\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0433\u0440\u0430\u0444\u0438\u043a\u043e\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f libGdx \u043d\u0430 LWJGL \u0431\u0435\u043a\u0435\u043d\u0434\u0435. \u0423\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0441\u0442\u0440\u0435\u043b\u043e\u0447\u0435\u043a \u043d\u0430 \u043a\u043b\u0430\u0432\u0438\u0430\u0442\u0443\u0440\u0435.<\/p>\n<h2>Domain<\/h2>\n<p>  \u0414\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0443\u0448\u043d\u043e\u0441\u0442\u0435\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438\u0441\u044c case class \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435, c\u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0438 \u0432 \u0446\u0435\u043b\u043e\u043c \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 record \u0438\u0437 Haskell.<\/p>\n<p>  \u0422\u043e\u0447\u043a\u0430 \u0432 2\u0434 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435: <\/p>\n<pre><code class=\"scala\">case class Point(x: Int, y: Int)<\/code><\/pre>\n<p>  \u041d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u0430\u0442\u0442\u0435\u0440\u043d \u043c\u0430\u0442\u0447\u0438\u043d\u0433\u0430 \u044d\u0442\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a enum:<\/p>\n<pre><code class=\"scala\">     \/\/\u0412 \u043a\u0430\u043a\u043e\u043c \u043d\u0438\u0431\u0443\u0434\u044c \u0421\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u043e\u043c \u044d\u0442\u043e\u043c\u0443 \u0431\u044b\u043b \u0431\u044b     \/\/enum Direction     \/\/{     \/\/    Up,     \/\/    Down,     \/\/    Right,     \/\/    Left     \/\/} sealed abstract class Direction  case object Up extends Direction  case object Down extends Direction  case object Right extends Direction  case object Left extends Direction<\/code><\/pre>\n<p>  \u0424\u0440\u0435\u0439\u043c \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0434\u0432\u0438\u0436\u0435\u0442\u0441\u044f \u0437\u043c\u0435\u0439\u043a\u0430 <\/p>\n<pre><code class=\"scala\">case class Frame(min: Point, max: Point) {   def points = {     for (i &lt;- min.x until max.x + 1;          j &lt;- min.y until max.y + 1          if i == min.x ||            i == max.x ||            j == min.y ||            j == max.y)       yield Point(i, j)   } }<\/code><\/pre>\n<p>  \u0415\u0434\u0430 \u0434\u043b\u044f \u0437\u043c\u0435\u0439\u043a\u0438: <\/p>\n<pre><code class=\"scala\">case class Food(body: Point, random: Random) {   def moveRandomIn(frame: Frame): Food = {     val x = random.between(frame.min.x + 1, frame.max.x)     val y = random.between(frame.min.y + 1, frame.max.y)     copy(body = Point(x, y))   } }<\/code><\/pre>\n<p>  \u0417\u043c\u0435\u0439\u043a\u0430: <\/p>\n<pre><code class=\"scala\">case class Snake(body: List[Point], direction: Direction) {   def move(): Snake = {     val point = direction match {       case Up() =&gt; body.head.copy(y = body.head.y + 1)       case Down() =&gt; body.head.copy(y = body.head.y - 1)       case Left() =&gt; body.head.copy(x = body.head.x - 1)       case Right() =&gt; body.head.copy(x = body.head.x + 1)     }     copy(body = point :: body.filter(p =&gt; p != body.last))   }    def turn(direction: Direction): Snake = {     copy(direction = direction)   }    def eat(food: Food): Snake = {     copy(body = food.body :: body)   }    def canEat(food: Food): Boolean = {     food.body == body.head   }    def headIsIn(frame: Frame): Boolean = {     body.head.x &lt; frame.max.x &amp;&amp;       body.head.y &lt; frame.max.y &amp;&amp;       body.head.x &gt; frame.min.x &amp;&amp;       body.head.y &gt; frame.min.y   }    def isBitTail() = {     body.tail.exists(p =&gt; p == body.head)   } }<\/code><\/pre>\n<p>  \u0418\u0433\u0440\u0430: <\/p>\n<pre><code class=\"scala\">package domain  case class Game(food: Food, snake: Snake, frame: Frame, elapsedTime: Float, start: Snake) {   val framePoints = frame.points.toList    def handle(input: List[Direction]): Game = {     if (input.isEmpty) {       this     } else {       copy(snake = input.foldLeft(snake)((s, d) =&gt; s.turn(d)))     }   }    def update(deltaTime: Float): Game = {     val elapsed = elapsedTime + deltaTime     if (elapsed &gt; 0.1) {       val game = copy(snake = snake.move(), elapsedTime = 0)       if (!game.snake.headIsIn(frame)) {         game.reset()       } else if (game.snake.isBitTail()) {         game.reset()       } else if (game.snake.canEat(food)) {         game.copy(snake = snake.eat(food), food = food.moveRandomIn(frame))       } else {         game       }     } else {       copy(elapsedTime = elapsed)     }   }    def reset() = copy(snake = start)    def points: List[Point] = {     (food.body :: snake.body) ::: framePoints   } } <\/code><\/pre>\n<p>  <\/p>\n<h2>Presentation<\/h2>\n<p>  \u041a\u043b\u0430\u0441\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u043d\u0430\u0436\u0430\u0442\u044b\u0445 \u043a\u043d\u043e\u043f\u043a\u0430\u0445<\/p>\n<pre><code class=\"scala\">import com.badlogic.gdx.{InputAdapter}  class InputCondensate extends InputAdapter {    private var keys: List[Int] = Nil    def list: List[Int] = keys.reverse    def clear(): Unit = {     keys = Nil   }    override def keyDown(keycode: Int): Boolean = {     keys = keycode :: keys     true   } }<\/code><\/pre>\n<p>  \u041a\u043b\u0430\u0441\u0441 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u0438\u0433\u0440\u044b:<\/p>\n<pre><code class=\"scala\">import com.badlogic.gdx.Input.Keys import com.badlogic.gdx.graphics.glutils.ShapeRenderer import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType import com.badlogic.gdx.graphics.{Color, GL20} import com.badlogic.gdx.{Game, Gdx}  class SnakeGame(var game: domain.Game, val sizeMultiplayer: Float) extends Game {   lazy val prs = new InputCondensate   lazy val shapeRenderer: ShapeRenderer = new ShapeRenderer()    override def create(): Unit = {     Gdx.input.setInputProcessor(prs)   }    override def render(): Unit = {     game = game       .handle(prs.list.map(i =&gt; i match {         case Keys.UP =&gt; domain.Up()         case Keys.DOWN =&gt; domain.Down()         case Keys.LEFT =&gt; domain.Left()         case Keys.RIGHT =&gt; domain.Right()       }))       .update(Gdx.graphics.getDeltaTime())      prs.clear()     Gdx.gl.glClearColor(1, 1, 1, 1)     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)     shapeRenderer.setColor(Color.BLACK)     shapeRenderer.begin(ShapeType.Filled)     for (p &lt;- game.points)       shapeRenderer.circle(p.x * sizeMultiplayer, p.y * sizeMultiplayer, sizeMultiplayer \/ 2)     shapeRenderer.end()   } }<\/code><\/pre>\n<p>  \u0413\u043b\u0430\u0432\u043d\u0430\u044f \u0442\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430 \u0438\u0433\u0440\u044b:<\/p>\n<pre><code class=\"scala\">import com.badlogic.gdx.backends.lwjgl.{LwjglApplication, LwjglApplicationConfiguration}  import scala.util.Random  object Main extends App {   val config = new LwjglApplicationConfiguration   config.title = &quot;Scala Snake Game&quot;   config.width = 300   config.height = 300   val food = domain.Food(domain.Point(4, 4), new Random())   val frame = domain.Frame(domain.Point(0, 0), domain.Point(30, 30))   val snake = domain.Snake(domain.Point(5, 5) :: domain.Point(6, 6) :: domain.Point(7, 7) :: Nil, domain.Right())   val game = domain.Game(food, snake, frame, 0, snake)   new LwjglApplication(new SnakeGame(game, 10), config) } <\/code><\/pre>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/503560\/\"> https:\/\/habr.com\/ru\/post\/503560\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/503560\/\">\n<div style=\"text-align:center;\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/la\/fy\/jy\/lafyjybej-y6p-4n1c4cdfl1pqc.jpeg\"><\/div>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0442 \u0425\u0430\u0431\u0440! \u041a\u043e\u0433\u0434\u0430 \u044f \u0438\u0437\u0443\u0447\u0430\u044e \u043d\u043e\u0432\u044b\u0439 \u044f\u0437\u044b\u043a \u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0434\u0435\u043b\u0430\u044e \u043d\u0430 \u043d\u0435\u043c \u0437\u043c\u0435\u0439\u043a\u0443. \u041c\u043e\u0436\u0435\u0442 \u043a\u0430\u043a\u043e\u043c\u0443 \u043d\u0438\u0431\u0443\u0434\u044c \u043d\u043e\u0432\u0438\u0447\u043a\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0442\u043e\u0436\u0435 \u0438\u0437\u0443\u0447\u0430\u0435\u0442 Scala \u0431\u0443\u0434\u0435\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043a\u043e\u0434 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043d\u043e\u0432\u0438\u0447\u043a\u0430 \u0432 \u044d\u0442\u043e\u043c \u042f\u041f. \u0423 \u043e\u043f\u044b\u0442\u043d\u044b\u0445 \u0441\u043a\u0430\u043b\u0438\u0441\u0442\u043e\u0432 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u043c\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 Scala \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0433\u0440\u0443\u0441\u0442\u044c. \u0417\u0430 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442.   <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-304160","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=304160"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/304160\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=304160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=304160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=304160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}