[SWIFT] Making a minute repeater part.1

What is a minute repeater?

It is a function installed in ultra-super high-priced wristwatches (originally it has a history of being installed in pocket watches). It tells you the current time by the number of gong sounds that are charged inside.

(1) Number of hour units (hours): Bass single note, 1 time = 1 o'clock to 12 times = 12 o'clock.

(2) Number of 60 minutes divided into 15-minute units (quarter): High-pitched and low-pitched sounds are combined alternately, and 1 set of high-pitched sound → low-pitched sound = 15 minutes, 2 sets repeated = 30 minutes, 3 sets repeated = 45 Minutes.

(3) Fractions less than 15 minutes (minutes): High-pitched single notes, from 1 time = 1 minute to 14 times = 14 minutes.

This is the case at 12:59, where the number of strike sounds is the highest and is often used for demonstrations. ● First, the bass sounds 12 times and it is 12 o'clock. ● Next, if you divide 59 minutes by 15 minutes (15 minutes x 3 times) + 14 minutes, you can hit 3 sets of quarters with a combination of treble and bass for 45 minutes. ● Hit the last remaining 14 minutes 14 times with high notes.

What is a minute repeater? Its history and representative model

We will implement this.

Build an algorithm

It is supposed to be activated when the button is pressed. I wrote the method to get the time numerically in Last article.

  @IBAction func actionButton(_ sender: UIButton) {
    
    //Get the time numerically
    let now = Date()
    let time = Calendar.current.dateComponents([.hour, .minute], from: now)

    //Number of hours
    var hour = time.hour!

    //Since it will be acquired in 24 hours, if it is after 13:00-12
    if hour > 12 {
      hour -= 12
    }
    
    //Number of 60 minutes divided into 15 minutes
    let quarter = time.minute! / 15

    //Fractions less than 15 minutes
    let minute = time.minute! % 15
    
    //Variables for use in while
    var hourCount = 0
    var quarterCount = 0
    var minuteCount = 0

    //First gong
    while hourCount < hour {
      print("dong")
      hourCount += 1
    }
    
    //Second gong
    while quarterCount < quarter {
      print("ding-dong")
      quarterCount += 1
    }
    
    //Third gong
    while minuteCount < minute {
      print("ding")
      minuteCount += 1
    }
  }

It may be a crappy algorithm. Next time, we will replace print with sound playback.

Recommended Posts

Making a minute repeater part.2
Making a minute repeater part.1
Making Frontale a microservice
Precautions when writing a program: Part 3
A little regular expression story Part 1
Make a language! (Making a simple calculator ①)
A little regular expression story Part 2