restructure: arrays and slices
This commit is contained in:
87
15-project-retro-led-clock/exercises/02-alarm/main.go
Normal file
87
15-project-retro-led-clock/exercises/02-alarm/main.go
Normal file
@ -0,0 +1,87 @@
|
||||
// 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: Set an Alarm
|
||||
//
|
||||
// Goal is printing " ALARM! " every 10 seconds.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
//
|
||||
// ██ ███ ███ ██ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ ███ ███ █ ███ ███
|
||||
// █ █ ░ █ █ ░ █ █
|
||||
// ███ ███ ███ ███ ███ ███
|
||||
//
|
||||
// ███ █ ███ ██ █ █ █
|
||||
// █ █ █ █ █ █ █ ███ █
|
||||
// ███ █ ███ ██ █ █ █
|
||||
// █ █ █ █ █ █ █ █ █
|
||||
// █ █ ███ █ █ █ █ █ █ █
|
||||
//
|
||||
// ██ ███ ███ ██ █ █ ██
|
||||
// █ █ ░ █ █ ░ █ █ █
|
||||
// █ ███ ███ █ ███ █
|
||||
// █ █ ░ █ █ ░ █ █
|
||||
// ███ ███ ███ ███ █ ███
|
||||
//
|
||||
// HINTS
|
||||
//
|
||||
// <<< BEWARE THE SPOILER! >>>
|
||||
//
|
||||
// I recommend you to first solve the exercise yourself before reading the
|
||||
// following hint.
|
||||
//
|
||||
//
|
||||
// + You can create a new array named alarm with the same length of the
|
||||
// clocks array, so you can copy the alarm array to the clocks array
|
||||
// every 10 seconds.
|
||||
//
|
||||
// ---------------------------------------------------------
|
||||
|
||||
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,53 @@
|
||||
// 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()
|
||||
|
||||
clock := [...]placeholder{
|
||||
digits[hour/10], digits[hour%10],
|
||||
colon,
|
||||
digits[min/10], digits[min%10],
|
||||
colon,
|
||||
digits[sec/10], digits[sec%10],
|
||||
}
|
||||
|
||||
alarmed := sec%10 == 0
|
||||
|
||||
for line := range clock[0] {
|
||||
if alarmed {
|
||||
clock = alarm
|
||||
}
|
||||
|
||||
for index, digit := range clock {
|
||||
next := clock[index][line]
|
||||
if digit == colon && sec%2 == 0 {
|
||||
next = " "
|
||||
}
|
||||
fmt.Print(next, " ")
|
||||
}
|
||||
fmt.Println()
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
@ -0,0 +1,154 @@
|
||||
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 alarm = [...]placeholder{
|
||||
{
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
},
|
||||
{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
"█ █",
|
||||
"█ █",
|
||||
},
|
||||
{
|
||||
"█ ",
|
||||
"█ ",
|
||||
"█ ",
|
||||
"█ ",
|
||||
"███",
|
||||
},
|
||||
{
|
||||
"███",
|
||||
"█ █",
|
||||
"███",
|
||||
"█ █",
|
||||
"█ █",
|
||||
},
|
||||
{
|
||||
"██ ",
|
||||
"█ █",
|
||||
"██ ",
|
||||
"█ █",
|
||||
"█ █",
|
||||
},
|
||||
{
|
||||
"█ █",
|
||||
"███",
|
||||
"█ █",
|
||||
"█ █",
|
||||
"█ █",
|
||||
},
|
||||
{
|
||||
" █ ",
|
||||
" █ ",
|
||||
" █ ",
|
||||
" ",
|
||||
" █ ",
|
||||
},
|
||||
{
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
" ",
|
||||
},
|
||||
}
|
||||
|
||||
var digits = [...]placeholder{
|
||||
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||
}
|
Reference in New Issue
Block a user