//#-hidden-code
//
//  See LICENSE folder for this template’s licensing information.
//
//  Abstract:
//  The Swift file containing the source code edited by the user of this playground book.
//
import PlaygroundSupport
import UIKit

let page = PlaygroundPage.current
page.needsIndefiniteExecution = false
var cNumber = 1
let 🔳 = 0
let 🟥 = 1
let 🟩 = 2
let 🟦 = 3

var droped = [[0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0],
              [0,0,0,0,0,0,0,0,0,0]]

var counter = [0,0,0,0,0,0,0,0,0,0]


func send(_ str: String) {
    if let proxy1 = page.liveView as? PlaygroundRemoteLiveViewProxy {
        let message: PlaygroundValue = .string(str)
        proxy1.send(message)
    }
}

func clear() {
    send("clear")
    send("color 1")
    cNumber = 1
    for i in 0 ... 9 {
        counter[i] = 0
        for j in 0 ... 9 {
            droped[i][j] = 0
        }
    }
}

func drop(_ x: Int) {
    if(x > 0 && x < 11) {
        Thread.sleep(forTimeInterval: 0.4)
        send("drop "+x.description)
        if(counter[x-1] < 10) {
           droped[9-counter[x-1]][x-1] = cNumber
           counter[x-1] = counter[x-1] + 1
        }
    } else {
        PlaygroundPage.current.assessmentStatus = .fail(hints: ["drop()の引数に範囲外の値が指定されています。drop()の括弧内には1〜10の整数型(Int)の数値を入れてください。"], solution: nil)
    }
}

func color(_ t: Int) {
    if(t >= 0 && t < 4) {
        send("color "+t.description)
        cNumber = t
    } else {
        PlaygroundPage.current.assessmentStatus = .fail(hints: ["color()の引数に範囲外の値が指定されています。color()の括弧内には0〜3の整数型(Int)の数値を入れてください。"], solution: nil)
    }
}

func sendMessage(_ message: String) {
    send("message "+message)
}

func check() {
    var clear = true
    for i in 0 ... 9 {
        for j in 0 ... 9 {
            if(droped[i][j] != answer[i][j]) {
                clear = false
            }
        }
    }
    if (clear == true) {
        sendMessage("8,1,⑧-😄できました！")
        PlaygroundPage.current.assessmentStatus = .pass(message: "😃おみごと！クリアーです!🎉")
    } else {
        sendMessage("8,2,⑧-🥶できていません")
        PlaygroundPage.current.assessmentStatus = .fail(hints: ["🤔ざんねん！宝石の「いち」と「いろ」をかくにんしてみましょう。"], solution: nil)
    }
}

let answer = [[🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🟩],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🟩,🟩],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🟩,🟩,🟩],
              [🔳,🔳,🔳,🔳,🔳,🔳,🟩,🟩,🟩,🟩],
              [🔳,🔳,🔳,🔳,🔳,🟩,🟩,🟩,🟩,🟩],
              [🔳,🔳,🔳,🔳,🟩,🟩,🟩,🟩,🟩,🟩],
              [🔳,🔳,🔳,🟩,🟩,🟩,🟩,🟩,🟩,🟩],
              [🔳,🔳,🟩,🟩,🟩,🟩,🟩,🟩,🟩,🟩],
              [🔳,🟩,🟩,🟩,🟩,🟩,🟩,🟩,🟩,🟩],
              [🟩,🟩,🟩,🟩,🟩,🟩,🟩,🟩,🟩,🟩]]

//#-end-hidden-code
/*: some text
 ##  💎⑧チャレンジしよう！

　次のように宝石を落とすプログラムをつくろう！\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🟩\
 🔳🔳🔳🔳🔳🔳🔳🔳🟩🟩\
 🔳🔳🔳🔳🔳🔳🔳🟩🟩🟩\
 🔳🔳🔳🔳🔳🔳🟩🟩🟩🟩\
 🔳🔳🔳🔳🔳🟩🟩🟩🟩🟩\
 🔳🔳🔳🔳🟩🟩🟩🟩🟩🟩\
 🔳🔳🔳🟩🟩🟩🟩🟩🟩🟩\
 🔳🔳🟩🟩🟩🟩🟩🟩🟩🟩\
 🔳🟩🟩🟩🟩🟩🟩🟩🟩🟩\
 🟩🟩🟩🟩🟩🟩🟩🟩🟩🟩
 
 * Important:
「for」の中に「for」を使うことができます。（２重ループ）\
 「for i in」の中に「for」をいれるときは、「for j in」を使いましょう。
 ---
 */
//#-code-completion(everything, hide)
//#-code-completion(identifier, show, 🟥, 🟩, 🟦)
//#-hidden-code
clear()
//#-end-hidden-code
//#-editable-code
//ここからプログラムを入力します。

//#-end-editable-code
//#-hidden-code
check()
//#-end-hidden-code
