Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.69 KB | None | 0 0
  1. class ChessyRootComponent private constructor(
  2.  
  3.         val rootElement: HTMLDivElement,
  4.         val squares: Array<Array<SquareComponent>>
  5. ) {
  6.  
  7.     companion object {
  8.  
  9.         fun create(onSquareClick: (x: Int, y: Int) -> Unit): ChessyRootComponent {
  10.  
  11.             val rootEl = document.create.div("chessy-root")
  12.             val boardEl = rootEl.append.div("chessy-board")
  13.             val squares =
  14.                     Array(8) { x ->
  15.                         Array(8) { y ->
  16.                             SquareComponent.create((x + y) % 2 == 1) { onSquareClick(x, y) }
  17.                         }
  18.                     }
  19.  
  20.             for (l in arrayOf("", "a", "b", "c", "d", "e", "f", "g", "h", "")) {
  21.                 boardEl.append.div("chessy-square chessy-square-out") {
  22.                     text(l)
  23.                 }
  24.             }
  25.             for (y in 7 downTo 0) {
  26.                 boardEl.append.div("chessy-square chessy-square-out") {
  27.                     text(y + 1)
  28.                 }
  29.                 for (x in 0..7) {
  30.                     boardEl.append(squares[x][y].div)
  31.                 }
  32.                 boardEl.append.div("chessy-square chessy-square-out") {
  33.                     text(y + 1)
  34.                 }
  35.             }
  36.             for (l in arrayOf("", "a", "b", "c", "d", "e", "f", "g", "h", "")) {
  37.                 boardEl.append.div("chessy-square chessy-square-out") {
  38.                     text(l)
  39.                 }
  40.             }
  41.  
  42.             return ChessyRootComponent(rootEl, squares)
  43.         }
  44.     }
  45. }
  46.  
  47.  
  48. class SquareComponent private constructor(
  49.  
  50.         val div: HTMLElement
  51. ) {
  52.  
  53.     fun update(square: Square) {
  54.         div.removeClass("white-player")
  55.  
  56.         val piece = square.piece
  57.         if (piece == null) {
  58.             div.innerText = ""
  59.         } else {
  60.             div.innerText = charForPiece(piece.type).toString()
  61.             if (piece.player == Player.WHITE) {
  62.                 div.addClass("white-player")
  63.             }
  64.         }
  65.     }
  66.  
  67.     private fun charForPiece(type: PieceType): Char {
  68.         return when (type) {
  69.  
  70.             PieceType.PAWN -> '♟'
  71.             PieceType.KNIGHT -> '♞'
  72.             PieceType.BISHOP -> '♝'
  73.             PieceType.ROOK -> '♜'
  74.             PieceType.QUEEN -> '♛'
  75.             PieceType.KING -> '♚'
  76.         }
  77.     }
  78.  
  79.     companion object {
  80.  
  81.         fun create(isWhite: Boolean, onClick: () -> Unit): SquareComponent {
  82.             val div = document.create.div("chessy-square") {
  83.                 if (isWhite) {
  84.                     classes += "white"
  85.                 }
  86.                 onClickFunction = { onClick() }
  87.             }
  88.             return SquareComponent(div)
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement