Skip to content

Instantly share code, notes, and snippets.

@rurumimic
Created September 14, 2018 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rurumimic/6736eb2655af85ddc3fdb7acaa16d2e6 to your computer and use it in GitHub Desktop.
Save rurumimic/6736eb2655af85ddc3fdb7acaa16d2e6 to your computer and use it in GitHub Desktop.
보드 만지기
import Foundation
struct board<T> {
let w: Int
let h: Int
var board: [T]
init(w: Int, h: Int, v: T) {
self.w = w
self.h = h
self.board = [T].init(repeating: v, count: w*h)
}
subscript(w: Int, h: Int) -> T {
get {
return board[h * self.w + w]
}
set {
board[h * self.w + w] = newValue
}
}
}
var b = board(w: 3, h: 4, v: 0)
print(b.board)
b[0, 0] = 1
b[1, 2] = 2
b[2, 3] = 3
print(b.board)
for y in 0..<b.h {
for x in 0..<b.w {
print(b[x, y], terminator: " ")
}
print()
}
///////////////////////////////////////////////////////////////
let s = ["CCBDE", "AAADE", "AAABF", "CCBBF"]
// copy
var myboard = board(w: s[0].count, h: s.count, v: " ")
var check = board(w: s[0].count, h: s.count, v: false)
for (y, sr) in s.enumerated() {
for (x, sc) in sr.enumerated() {
myboard[x, y] = String(sc)
}
}
// print
print(myboard.board)
print(check.board)
for y in 0..<myboard.h {
for x in 0..<myboard.w {
print(myboard[x, y], terminator: " ")
}
print()
}
// check block
for y in 0..<myboard.h-1 {
for x in 0..<myboard.w-1 {
let char = myboard[x, y]
if char == " " {
continue
}
if char == myboard[x+1, y]
&& char == myboard[x+1, y+1]
&& char == myboard[x, y+1] {
check[x, y] = true
check[x+1, y] = true
check[x, y+1] = true
check[x+1, y+1] = true
}
}
}
// print check
for y in 0..<check.h {
for x in 0..<check.w {
print(check[x, y], terminator: " ")
}
print()
}
// change block
// remove dupli
for y in 0..<check.h {
for x in 0..<check.w {
if check[x, y] == true {
myboard[x, y] = " "
}
}
}
// print board
for y in 0..<myboard.h {
for x in 0..<myboard.w {
print(myboard[x, y], terminator: " ")
}
print()
}
// down blocks
// fill buffer with " "
for x in 0..<myboard.w {
var buffer: [String] = [String].init(repeatElement(" ", count: myboard.h))
for y in 0..<myboard.h {
if myboard[x, y] != " " {
buffer.insert(myboard[x, y], at: 0) // insert front, count = height+1
_ = buffer.removeLast() // count = height
}
}
for y in 0..<myboard.h {
myboard[x, y] = buffer.popLast()!
}
}
// print board
for y in 0..<myboard.h {
for x in 0..<myboard.w {
print(myboard[x, y], terminator: " ")
}
print()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment