Random colours in button Array

//Need to create a random Array of 18 x 8 buttons with max 8 buttons with the same color of
//the 18 and no redundant color in a row… Stuck on getting the random string values into the
// Array back - Any Ideas ? :slight_smile: I’m on day 2 of CWC+

import SwiftUI

enum ChromaButton:String {
case a1 = β€œ1”
case b1 = β€œ2”
case c1 = β€œ3”
case d1 = β€œ4”
case e1 = β€œ5”
case f1 = β€œ6”
case g1 = β€œ7”
case h1 = β€œ8”
case i1 = β€œ9”
case j1 = β€œ10”
case k1 = β€œ11”
case l1 = β€œ12”
case m1 = β€œ13”
case n1 = β€œ14”
case o1 = β€œ15”
case p1 = β€œ16”
case q1 = β€œ17”
case r1 = β€œ18”
var buttonColor: Color {
switch self {
case .a1:
return Color(UIColor(red: 255/255, green: 0/255, blue: 255/255, alpha: 1))
case .b1:
return Color(UIColor(red: 190/255, green: 2/255, blue: 255/255, alpha: 1))
case .c1:
return Color(UIColor(red: 125/255, green: 1/255, blue: 255/255, alpha: 1))
case .d1:
return Color(UIColor(red: 22/255, green: 0/255, blue: 255/255, alpha: 1))
case .e1:
return Color(UIColor(red: 0/255, green: 125/255, blue: 255/255, alpha: 1))
case .f1:
return Color(UIColor(red: 0/255, green: 190/255, blue: 255/255, alpha: 1))
case .g1:
return Color(UIColor(red: 0/255, green: 255/255, blue: 255/255, alpha: 1))
case .h1:
return Color(UIColor(red: 0/255, green: 252/255, blue: 187/255, alpha: 1))
case .i1:
return Color(UIColor(red: 2/255, green: 255/255, blue: 125/255, alpha: 1))
case .j1:
return Color(UIColor(red: 1/255, green: 255/255, blue: 0/255, alpha: 1))
case .k1:
return Color(UIColor(red: 125/255, green: 255/255, blue: 1/255, alpha: 1))
case .l1:
return Color(UIColor(red: 190/255, green: 255/255, blue: 4/255, alpha: 1))
case .m1:
return Color(UIColor(red: 255/255, green: 255/255, blue: 0/255, alpha: 1))
case .n1:
return Color(UIColor(red: 255/255, green: 190/255, blue: 0/255, alpha: 1))
case .o1:
return Color(UIColor(red: 255/255, green: 125/255, blue: 0/255, alpha: 1))
case .p1:
return Color(UIColor(red: 255/255, green: 2/255, blue: 0/255, alpha: 1))
case .q1:
return Color(UIColor(red: 255/255, green: 0/255, blue: 125/255, alpha: 1))
case .r1:
return Color(UIColor(red: 255/255, green: 0/255, blue: 190/255, alpha: 1))
}
}
}
struct ContentView: View {
let buttons: [[ChromaButton]] = [
[.a1, .a1, .a1, .a1, .a1, .a1, .a1, .a1],
[.b1, .b1, .b1, .b1, .b1, .b1, .b1, .b1],
[.c1, .c1, .c1, .c1, .c1, .c1, .c1, .c1],
[.d1, .d1, .d1, .d1, .d1, .d1, .d1, .d1],
[.e1, .e1, .e1, .e1, .e1, .e1, .e1, .e1],
[.f1, .f1, .f1, .f1, .f1, .f1, .f1, .f1],
[.g1, .g1, .g1, .g1, .g1, .g1, .g1, .g1],
[.h1, .h1, .h1, .h1, .h1, .h1, .h1, .h1],
[.i1, .i1, .i1, .i1, .i1, .i1, .i1, .i1],
[.j1, .j1, .j1, .j1, .j1, .j1, .j1, .j1],
[.k1, .k1, .k1, .k1, .k1, .k1, .k1, .k1],
[.l1, .l1, .l1, .l1, .l1, .l1, .l1, .l1],
[.m1, .m1, .m1, .m1, .m1, .m1, .m1, .m1],
[.n1, .n1, .n1, .n1, .n1, .n1, .n1, .n1],
[.o1, .o1, .o1, .o1, .o1, .o1, .o1, .o1],
[.p1, .p1, .p1, .p1, .p1, .p1, .p1, .p1],
[.q1, .q1, .q1, .q1, .q1, .q1, .q1, .q1],
[.r1, .r1, .r1, .r1, .r1, .r1, .r1, .r1]
]
func rdmColor() β†’ String {
return β€œ.a1”
}

var body: some View {
    ZStack{
        Color.white.edgesIgnoringSafeArea(.all)
        VStack{
            Spacer()
           
                ForEach(buttons, id: \.self) { row in
                    HStack(spacing: 35){
                        ForEach(row, id: \.self) { item in
                            Button(action:{
                                
                            }, label: {
                                Text(item.rawValue)
                                    .font(.system(size:32))
                                    .frame(
                                        width: self.buttonWidth(item: item),
                                        height: self.buttonHeight())
                                    .background(item.buttonColor)
                                    .foregroundColor(.white)
                                    .cornerRadius(self.buttonWidth(item: item)/2)
                            })
                        }
                    }
                }
                .padding(.bottom, 5)
           
        }
    }
}
//
func buttonWidth(item: ChromaButton) -> CGFloat {
    return (UIScreen.main.bounds.width - (9*12)) / 14
}
//
func buttonHeight() -> CGFloat {
    return (UIScreen.main.bounds.width - (9*12)) / 14
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

@netventure

Hi Yan,

Welcome to the community.

You say you are on day 2 of CWC+ but which course and lesson are you following?

I did the 14 days beginner courses in a row and now continuing the others :slight_smile: long way though … My wife asked me to help her with a small app to sort colours (chromo bio therapy) … So I started how I could :slight_smile:
Cheers,
Yan

@netventure

So in each row, do you want the same color for each of the 8 buttons or do you want a random color for each of the 8 button where the color selected is from the 18 colors that you have but none are repeated in that group of 8?

There are 18 rows and 18 colours … Each colour should appear randomly max 8 times in total and not more than 1 time in a row. The user should choose by clicking on each row max 4 colours row by row. The clicked ones should turn invisible. After the completion of all choices, the system should count the amount of chosen colours by colour … (that was the request of my wife :slight_smile: ) … I managed to build of the grid (chose buttons to be able the click them… But struggled on the syntax of the random part to give back the strings to the array (as you can see in the code)
Thanks, Yan

@netventure

OK that explains what you are trying to do with the button 2 dimensional array.

My gut feeling is that I think each color appearance in the grid should be a struct which stores the color (Color) and whether or not it is visible or invisible (Boolean). Scanning through the grid and determining which colors are visible or invisible and the number of instances of that color should give you the count for each color. Does that seem to be a reasonable approach?