restructure: arrays and slices
This commit is contained in:
147
15-project-retro-led-clock/exercises/03-split-second/main.go
Normal file
147
15-project-retro-led-clock/exercises/03-split-second/main.go
Normal file
@ -0,0 +1,147 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Add Split Seconds
|
||||
//
|
||||
// Your goal is adding the split second to the clock. A split second is
|
||||
// 1/10th of a second.
|
||||
//
|
||||
// 1. Find the current split second
|
||||
// 2. Add dot character to the clock (as in the expected output)
|
||||
// 3. Add the split second digit to the clock
|
||||
// 4. Blink the dot every two seconds (just like the separators)
|
||||
// 5. Update the clock every 1/10th of a second, instead of every second.
|
||||
// (Update the clock every 100 millliseconds)
|
||||
//
|
||||
// HINTS
|
||||
// + You can find the split second using Nanosecond method of the Time type.
|
||||
// https://golang.org/pkg/time/#Time.Nanosecond
|
||||
//
|
||||
// + A split second is the first digit of the Nanosecond.
|
||||
//
|
||||
// + Remember: time.Second is an integer constant, so it can be divided
|
||||
// with a number.
|
||||
//
|
||||
// https://golang.org/pkg/time/#Time.Second
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// Note that, clock is updated every split second instead of a second.
|
||||
//
|
||||
// Separators are displayed (second is an odd number):
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █ █
|
||||
// █ █ ███ █ █ █ █ █
|
||||
// █ █ ░ █ █ ░ █ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ██
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ █ ███ █ █ █ █
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ █ ███ █ █ █ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ █ ███ █ █ █ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ █ █
|
||||
// █ █ ░ █ █ ░ █ █ █ █
|
||||
// █ █ ███ █ █ █ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ █
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ █ ███ █ █ █ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ █ ███ █ █ █ ███
|
||||
// █ █ ░ █ █ ░ █ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ █ ███ █ █ █ █
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ █
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █ █
|
||||
// █ █ ███ █ █ █ ███
|
||||
// █ █ ░ █ █ ░ █ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █ █
|
||||
// █ █ ███ █ █ █ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// ███ ███ ███ ███ ███ █ ░ ███
|
||||
//
|
||||
// Separators are not displayed (second is an even number):
|
||||
//
|
||||
// ██ ██ ███ ██ ██ ███ ███
|
||||
// █ █ █ █ █ █ █ █ █
|
||||
// █ █ ███ █ █ ███ █ █
|
||||
// █ █ █ █ █ █ █ █ █
|
||||
// ███ ███ ███ ███ ███ ███ ███
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/inancgumus/screen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
screen.Clear()
|
||||
|
||||
for {
|
||||
screen.MoveTopLeft()
|
||||
|
||||
now := time.Now()
|
||||
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||
|
||||
clock := [...]placeholder{
|
||||
digits[hour/10], digits[hour%10],
|
||||
colon,
|
||||
digits[min/10], digits[min%10],
|
||||
colon,
|
||||
digits[sec/10], digits[sec%10],
|
||||
}
|
||||
|
||||
for line := range clock[0] {
|
||||
for index, digit := range clock {
|
||||
// colon blink
|
||||
next := clock[index][line]
|
||||
if digit == colon && sec%2 == 0 {
|
||||
next = " "
|
||||
}
|
||||
fmt.Print(next, " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package main
|
||||
|
||||
type placeholder [5]string
|
||||
|
||||
var zero = placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var one = placeholder{
|
||||
"██ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
var two = placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
var three = placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var four = placeholder{
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
var five = placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var six = placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var seven = placeholder{
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
var eight = placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var nine = placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var colon = placeholder{
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
}
|
||||
|
||||
var digits = [...]placeholder{
|
||||
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/inancgumus/screen"
|
||||
)
|
||||
|
||||
func main() {
|
||||
screen.Clear()
|
||||
|
||||
for {
|
||||
screen.MoveTopLeft()
|
||||
|
||||
now := time.Now()
|
||||
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
||||
ssec := now.Nanosecond() / 1e8
|
||||
|
||||
clock := [...]placeholder{
|
||||
digits[hour/10], digits[hour%10],
|
||||
colon,
|
||||
digits[min/10], digits[min%10],
|
||||
colon,
|
||||
digits[sec/10], digits[sec%10],
|
||||
dot,
|
||||
digits[ssec],
|
||||
}
|
||||
|
||||
for line := range clock[0] {
|
||||
for index, digit := range clock {
|
||||
next := clock[index][line]
|
||||
if (digit == colon || digit == dot) && sec%2 == 0 {
|
||||
next = " "
|
||||
}
|
||||
fmt.Print(next, " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
const splitSecond = time.Second / 10
|
||||
time.Sleep(splitSecond)
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
package main
|
||||
|
||||
type placeholder [5]string
|
||||
|
||||
var zero = placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var one = placeholder{
|
||||
"██ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
var two = placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
}
|
||||
|
||||
var three = placeholder{
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var four = placeholder{
|
||||
"█ █",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
var five = placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var six = placeholder{
|
||||
"███",
|
||||
"█ ",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var seven = placeholder{
|
||||
"███",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
" █",
|
||||
}
|
||||
|
||||
var eight = placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var nine = placeholder{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
" █",
|
||||
"███",
|
||||
}
|
||||
|
||||
var colon = placeholder{
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
" ░ ",
|
||||
" ",
|
||||
}
|
||||
|
||||
var dot = placeholder{
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ░ ",
|
||||
}
|
||||
|
||||
var digits = [...]placeholder{
|
||||
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||
}
|
Reference in New Issue
Block a user