主要程式

struct ContentView: View {

@State var input = “”

@State var result = “?”

let password = “1234”

var body: some View {

VStack {

Text(result)

Text(“輸入密碼”)

HStack {

ForEach(0 ..< 4) { item in

Image(systemName: self.input.count <= item ? “circle” : “circle.fill”)

}

}

HStack {

ForEach(1 ..< 4) { item in

Button(action: {

self.input.append(“\(item)”)

if self.input.count == 4 {

if self.input == self.password {

self.result = “O”

} else {

self.result = “X”

self.input = “”

}

}

}) {

Text(“\(item)”)

}

}

}

HStack {

ForEach(4 ..< 7) { item in

Button(action: {

self.input.append(“\(item)”)

if self.input == self.password {

self.result = “O”

} else {

self.result = “X”

self.input = “”

}

}) {

Text(“\(item)”)

}

}

}

HStack {

ForEach(7 ..< 10) { item in

Button(action: {

self.input.append(“\(item)”)

if self.input == self.password {

self.result = “O”

} else {

self.result = “X”

self.input = “”

}

}) {

Text(“\(item)”)

}

}

}

HStack {

Button(action: {

self.input.append(“0”)

if self.input == self.password {

self.result = “O”

} else {

self.result = “X”

self.input = “”

}

}) {

Text(“0”)

}

Button(action: {

self.input.removeLast()

}) {

Image(systemName: “delete.left”)

}

}

}

}

}

struct ContentView_Previews: PreviewProvider {

static var previews: some View {

ContentView()

}

}