Advertisement
Celestial_Dodo

Lights out

Oct 16th, 2023 (edited)
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.74 KB | None | 0 0
  1. import SwiftUI
  2.  
  3. struct LightsOutApp: App {
  4.     var body: some Scene {
  5.         WindowGroup {
  6.             ContentView()
  7.         }
  8.     }
  9. }
  10.  
  11. var SingleMode = false
  12.  
  13. struct ContentView: View {
  14.     @State var buttons: [[ButtonState]] = Array(repeating: Array(repeating: ButtonState.off, count: columns), count: rows)
  15.    
  16.     var body: some View {
  17.         HStack {
  18.             VStack {
  19.                 ForEach(0..<rows, id: \.self) { row in
  20.                     HStack {
  21.                         ForEach(0..<columns, id: \.self) { column in
  22.                             Button(action: {
  23.                                 buttonClick(row: row, column: column)
  24.                             }) {
  25.                                 Text(buttons[row][column].text)
  26.                                     .frame(width: 50, height: 50)
  27.                                     .background(buttons[row][column].color)
  28.                                     .border(Color.clear, width: 0)
  29.                             }
  30.                         }
  31.                     }
  32.                 }
  33.             }
  34.            
  35.             // Button on the left side
  36.             Button(action: {
  37.                 if SingleMode == false {
  38.                     SingleMode = true
  39.                 } else {
  40.                     SingleMode = false
  41.                 }
  42.             }) {
  43.                 Text("Single Toggle Mode")
  44.                     .padding(10)
  45.                     .background(Color.blue)
  46.                     .foregroundColor(Color.white)
  47.             }
  48.         }
  49.     }
  50.    
  51.     func buttonClick(row: Int, column: Int) {
  52.         buttons[row][column].toggle()
  53.        
  54.         let neighbors = [(row - 1, column), (row + 1, column), (row, column - 1), (row, column + 1)]
  55.        
  56.         if SingleMode == false {
  57.             for (neighborRow, neighborColumn) in neighbors {
  58.                 if (0..<rows).contains(neighborRow) && (0..<columns).contains(neighborColumn) {
  59.                     buttons[neighborRow][neighborColumn].toggle()
  60.                 }
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66. enum ButtonState: Identifiable {
  67.     case on, off
  68.    
  69.     var text: String {
  70.         switch self {
  71.         case .on:
  72.             return ""
  73.         case .off:
  74.             return ""
  75.         }
  76.     }
  77.    
  78.     var color: Color {
  79.         switch self {
  80.         case .on:
  81.             return Color.red
  82.         case .off:
  83.             return Color.green
  84.         }
  85.     }
  86.    
  87.     mutating func toggle() {
  88.         self = (self == .on) ? .off : .on
  89.     }
  90.    
  91.     var id: Int {
  92.         text.hashValue
  93.     }
  94. }
  95.  
  96. let rows = 5
  97. let columns = 5
  98.  
  99. @available(iOS 15.0, *)
  100. struct ContentView_Previews: PreviewProvider {
  101.     static var previews: some View {
  102.         ContentView()
  103.     }
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement