-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow1.cdc
74 lines (65 loc) · 1.78 KB
/
flow1.cdc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// After encountering some difficulty, looked up few persons code, tried to study it and produced this. I'm not good!
pub struct Canvas{
pub let width: UInt8
pub let height: UInt8
pub let pixels: String
init(width: UInt8, height: UInt8, pixels: String){
self.width = width
self.height = height
self.pixels = pixels
}
}
pub fun printView(canvas: Canvas){
let borderLine = "+-----+"
let sideLine = "|"
let countHeight = Int(canvas.height)
let countWidth = Int(canvas.width)
var count = 0
log(borderLine)//top frame
while count <= countHeight-1{
log(sideLine.concat(canvas.pixels.slice(from: (count * countHeight), upTo: (count * countHeight + countWidth)).concat(sideLine)))
count = count + 1
}
log(borderLine) //footer
}
pub resource Printer {
pub fun print(canvas: Canvas): @Picture? {
printView(canvas: canvas)// print canvas with frame
let mPicture <- create Picture(canvas: canvas)
return <- mPicture
}
}
pub fun serializeStringArray(_ lines: [String]): String{
var buffer = ""
for line in lines{
buffer = buffer.concat(line)
}
return buffer
}
pub resource Picture{
pub let canvas: Canvas
init(canvas: Canvas){
self.canvas = canvas
}
}
pub fun main(){
let pixelsX = [
"* *",
" * * ",
" * ",
" * * ",
"* *"
]
let canvasX = Canvas(
width: 5,
height: 5,
pixels: serializeStringArray(pixelsX)
)
let letterX <- create Picture(canvas: canvasX)
log(letterX.canvas.pixels)
let printX <- create Printer()
let printO <- printX.print(canvas: canvasX)
destroy letterX
destroy printX
destroy printO
}