UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 45: Understanding Chessboard logic

Forums > 100 Days of SwiftUI

  • During the day nr. 45 topic nr. 3 I'm struggling for the first time with understanding logic behind the code. I can't contect the "dots" between the:
  1. "for...in..." if
  2. (row + column).isMultiple(of: 2)
  3. let rect = CGRect(x: startX, y: startY, width: columnsSize, height: rowSize)

(black and white chessboard with first top left rectangle being white)

My wrong understanding(example):

  • For row = 0 and column = 0 ............if (0 + 0).isMultiple(of: 2) .........is not multiple / 2

  • For row = 0 and column = 1 .............if (0 + 1).isMultiple(of: 2) .........is not multiple / 2

  • For row = 1 and column = 0.............. if (1 + 0).isMultiple(of: 2) ..........is not multiple / 2

  • Following this logic there should be 3 rectangles with the same color next to eachother

  • I assume that if (0 + 0).isMultiple(of: 2) is in fact multiple of 2 because in reality it's row: 1 and column 1 but writen down as a 0 in both cases (1 + 1 multiple of 2). Is that correct?

If it's correct then startX = 0, startY = 0 (that's the very corner of the view). How by applying startX = 0, startY = 0 into the :

  • let rect = CGRect(x: startX, y: startY, width: columnsSize, height: rowSize)
  • do we get black rectangles but not in the area attached with the 0,0 coordinations?

This is my first post with asking for help in any topic related with programming so forgive me mess if there is some. I hope it's readable. Thank you in advance.

struct Checkerboard: Shape {
    var rows: Int
    var columns: Int

    func path(in rect: CGRect) -> Path {
        var path = Path()

        let rowSize = rect.height / Double(rows)
        let columnsSize = rect.width / Double(columns)

        for row in 0..<rows {
            for column in 0..<columns {
                if (row + column).isMultiple(of: 2) {
                    let startX = columnsSize * Double(column)
                    let startY = rowSize * Double(row)

                    let rect = CGRect(x: startX, y: startY, width: columnsSize, height: rowSize)
                    path.addRect(rect)
                }
            }
        }
        return path
    }
}

3      

0 is a multiple of 2

  • 2 x 0 = 0
  • 2 x 1 = 2
  • 2 x 2 = 4
  • 2 x 3 = 6
  • etc...

so row, column

  • 0, 0 = 0 : black
  • 0, 1 = 1 : white
  • 0, 2 = 2 : black
  • 1, 0 = 1 : white
  • 1, 1 = 2 : black
  • 1, 2 = 3 : white
  • 2, 0 = 2 : black
  • 2, 1 = 3 : white
  • 2, 2 = 4 : black

put this is into a playground:

if 0.isMultiple(of: 2) {
    print("It's a multiple of 2")
} else {
    print("It's not a multiple of 2")
}

4      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.