//#-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 = true
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.5)
        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 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) {
        let str = """
        ### 😄すばらしいです❗️
        # 💡落とす位置が確認できましたね❗️
        """
            PlaygroundPage.current.assessmentStatus = .pass(message: str)
    } else {
        PlaygroundPage.current.assessmentStatus = .fail(hints: ["🤔ざんねん！宝石の「いち」をかくにんしてみましょう。"], solution: nil)
    }
}

let answer = [[🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳,🔳],
              [🔳,🔳,🟥,🔳,🟥,🔳,🟥,🔳,🟥,🔳]]

//#-end-hidden-code
/*: some text
 ##  💎宝石ドロップ②
 
　つぎのように宝石をおとすプログラムをつくろう！\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🔳🔳🔳🔳🔳🔳🔳🔳\
 🔳🔳🟥🔳🟥🔳🟥🔳🟥🔳
 
 * Note:
drop()に入るすうじをまちがえずにプログラミングしよう！\
はじめの１つめのプログラムは入っていますヨ。
 ---
 */
//#-hidden-code
clear()
//#-code-completion(everything, hide)
//#-code-completion(identifier, show, 🟥, 🟩, 🟦)
//#-end-hidden-code
//#-editable-code
drop(3)
//ここからプログラムをいれます。


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