add: array led clock exercises
This commit is contained in:
23
15-arrays-project-clock/exercises/00-text/main.go
Normal file
23
15-arrays-project-clock/exercises/00-text/main.go
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
// EXERCISE: Text
|
||||||
|
//
|
||||||
|
// 1. Text
|
||||||
|
//
|
||||||
|
// HINT
|
||||||
|
// Text
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// Text
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
11
15-arrays-project-clock/exercises/00-text/solution/main.go
Normal file
11
15-arrays-project-clock/exercises/00-text/solution/main.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// 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
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
}
|
173
15-arrays-project-clock/exercises/01-refactor/main.go
Normal file
173
15-arrays-project-clock/exercises/01-refactor/main.go
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
// 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: Refactor
|
||||||
|
//
|
||||||
|
// Goal is refactoring the clock project by moving some of its parts to
|
||||||
|
// a new file in the same package.
|
||||||
|
//
|
||||||
|
// 1. Create a new file: placeholders.go
|
||||||
|
// 2. Move the placeholder type to placeholder.go
|
||||||
|
// 3. Move all the placeholders (zero to nine and the colon) to placeholder.go
|
||||||
|
// 4. Move the digits array to placeholders.go
|
||||||
|
//
|
||||||
|
// HINT
|
||||||
|
// + placeholders.go file should belong to main package as well
|
||||||
|
//
|
||||||
|
// To remember how to do so: Rewatch the "PART I — Packages, Scopes and Importing"
|
||||||
|
// section.
|
||||||
|
//
|
||||||
|
// + Short declaration won't work in the package scope.
|
||||||
|
// Remember why by watching: "Short Declaration: Package Scope" lecture
|
||||||
|
//
|
||||||
|
// + If you receive the following error and you don't know what to do watch:
|
||||||
|
// "Packages - Learn how to run multiple files" lecture.
|
||||||
|
//
|
||||||
|
// # command-line-arguments
|
||||||
|
// undefined: placeholder
|
||||||
|
// undefined: colon
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// The same output before the refactoring.
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/inancgumus/screen"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
type placeholder [5]string
|
||||||
|
|
||||||
|
zero := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
one := placeholder{
|
||||||
|
"██ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
" █ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
two := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
three := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
four := placeholder{
|
||||||
|
"█ █",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
five := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
six := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ ",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
seven := placeholder{
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
" █",
|
||||||
|
}
|
||||||
|
|
||||||
|
eight := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
nine := placeholder{
|
||||||
|
"███",
|
||||||
|
"█ █",
|
||||||
|
"███",
|
||||||
|
" █",
|
||||||
|
"███",
|
||||||
|
}
|
||||||
|
|
||||||
|
colon := placeholder{
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
" ░ ",
|
||||||
|
" ",
|
||||||
|
}
|
||||||
|
|
||||||
|
digits := [...]placeholder{
|
||||||
|
zero, one, two, three, four, five, six, seven, eight, nine,
|
||||||
|
}
|
||||||
|
|
||||||
|
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,48 @@
|
|||||||
|
// 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],
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
}
|
87
15-arrays-project-clock/exercises/02-alarm/main.go
Normal file
87
15-arrays-project-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)
|
||||||
|
}
|
||||||
|
}
|
95
15-arrays-project-clock/exercises/02-alarm/placeholders.go
Normal file
95
15-arrays-project-clock/exercises/02-alarm/placeholders.go
Normal file
@ -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,
|
||||||
|
}
|
53
15-arrays-project-clock/exercises/02-alarm/solution/main.go
Normal file
53
15-arrays-project-clock/exercises/02-alarm/solution/main.go
Normal file
@ -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,
|
||||||
|
}
|
147
15-arrays-project-clock/exercises/03-split-second/main.go
Normal file
147
15-arrays-project-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,
|
||||||
|
}
|
113
15-arrays-project-clock/exercises/04-ticker/main.go
Normal file
113
15-arrays-project-clock/exercises/04-ticker/main.go
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
// 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: Ticker: Slide the Clock
|
||||||
|
//
|
||||||
|
// Your goal is slide the placeholders every second.
|
||||||
|
// Please run the solution to see it in action.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// THIS IS A HARD EXERCISE:
|
||||||
|
// + It will take you days but it will worth it.
|
||||||
|
// + For experienced developers, this can take an hour or so.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 1. You need to determine the starting and the ending digits to create
|
||||||
|
// the sliding effect.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 2. Each second, start from the next placeholder, skip the previous one.
|
||||||
|
// This means: Only draw the next placeholders.
|
||||||
|
//
|
||||||
|
// Like this:
|
||||||
|
//
|
||||||
|
// 12:40:31
|
||||||
|
// 2:40:31
|
||||||
|
// 40:31
|
||||||
|
// 0:31
|
||||||
|
// :31
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 3. After the last placeholder is displayed, fill the lines for the missing
|
||||||
|
// placeholders, and then start from the first placeholder. Draw it to the
|
||||||
|
// right part of the screen.
|
||||||
|
//
|
||||||
|
// Like this:
|
||||||
|
//
|
||||||
|
// 12:40:31
|
||||||
|
// 2:40:31
|
||||||
|
// 40:31
|
||||||
|
// 0:31
|
||||||
|
// :31
|
||||||
|
// 31
|
||||||
|
// 1
|
||||||
|
// 1
|
||||||
|
// 31
|
||||||
|
// :31
|
||||||
|
// 0:31
|
||||||
|
// 40:31
|
||||||
|
// :40:31
|
||||||
|
// 2:40:31
|
||||||
|
// 12:40:31
|
||||||
|
//
|
||||||
|
// As you can see, you need to draw the clock from the right part of the
|
||||||
|
// screen, beginning from the first placeholder.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// HINTS
|
||||||
|
// + You would need to clear the screen inside the loop instead of once.
|
||||||
|
// Otherwise the previous placeholders will be left on the screen.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// EXPECTED OUTPUT
|
||||||
|
// Please run the solution to see it in action. Do not look at the source-code
|
||||||
|
// though.
|
||||||
|
// ---------------------------------------------------------
|
||||||
|
|
||||||
|
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 {
|
||||||
|
next := clock[index][line]
|
||||||
|
if digit == colon && sec%2 == 0 {
|
||||||
|
next = " "
|
||||||
|
}
|
||||||
|
fmt.Print(next, " ")
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
}
|
||||||
|
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
}
|
||||||
|
}
|
95
15-arrays-project-clock/exercises/04-ticker/placeholders.go
Normal file
95
15-arrays-project-clock/exercises/04-ticker/placeholders.go
Normal file
@ -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,
|
||||||
|
}
|
78
15-arrays-project-clock/exercises/04-ticker/solution/main.go
Normal file
78
15-arrays-project-clock/exercises/04-ticker/solution/main.go
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
// 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() {
|
||||||
|
// by using 'shift' we can create the slide effect for placeholders.
|
||||||
|
for shift := 0; ; shift++ {
|
||||||
|
// we need to clear the screen here.
|
||||||
|
// or the previous character will be left on the screen
|
||||||
|
//
|
||||||
|
// alternative: you can fill the rest of the missing placeholders
|
||||||
|
// with empty lines
|
||||||
|
screen.Clear()
|
||||||
|
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] {
|
||||||
|
l := len(clock)
|
||||||
|
|
||||||
|
// this sets the beginning and the ending placeholders.
|
||||||
|
// to prevent the indexing error: we use the remainder operator.
|
||||||
|
s, e := shift%l, l
|
||||||
|
|
||||||
|
// to slide placeholders from the right part of the screen.
|
||||||
|
//
|
||||||
|
// here, we assume that as if the clock's length is double of its length.
|
||||||
|
// this makes things easy to manage: that's why: l*2 is there.
|
||||||
|
//
|
||||||
|
// whenever, the current shift factor's double remainder is greater than
|
||||||
|
// the length of the clock - 1, it changes the starting and ending positions.
|
||||||
|
if shift%(l*2) > l-1 {
|
||||||
|
s, e = 0, shift%l+1
|
||||||
|
}
|
||||||
|
|
||||||
|
// print empty lines for the right-to-left slide effect.
|
||||||
|
//
|
||||||
|
// this creates the effect of moving placeholders from right to left.
|
||||||
|
for j := 0; j < l-e; j++ {
|
||||||
|
fmt.Print(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// draw the digits starting from 's' to 'e'
|
||||||
|
for i := s; i < e; i++ {
|
||||||
|
next := clock[i][line]
|
||||||
|
if clock[i] == 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,
|
||||||
|
}
|
21
15-arrays-project-clock/exercises/README.md
Normal file
21
15-arrays-project-clock/exercises/README.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# Exercises
|
||||||
|
|
||||||
|
These exercises will reinforce your knowledge of manipulating arrays. You will prove yourself that you can write easy to maintain Go programs.
|
||||||
|
|
||||||
|
1. **[Refactor](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/exercises/01-refactor)**
|
||||||
|
|
||||||
|
In this exercise, you will refactor the project to multiple files by moving
|
||||||
|
all the placeholders. This will make the project easy to maintain down the road.
|
||||||
|
|
||||||
|
2. **[Set an Alarm](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/exercises/02-alarm)**
|
||||||
|
|
||||||
|
You will print " ALARM! " every 10 seconds.
|
||||||
|
|
||||||
|
3. **[Split Second](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/exercises/03-split-second)**
|
||||||
|
|
||||||
|
You will display the split second in the clock, you will need to update the
|
||||||
|
clock every 1/10th of a second instead of every second.
|
||||||
|
|
||||||
|
4. **[Ticker](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/exercises/04-ticker)**
|
||||||
|
|
||||||
|
This is an HARD EXERCISE. You will slide the clock animation from right-to-left. After this exercise, you will truly feel that you've mastered everything you've learned so far.
|
@ -1,129 +1,4 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/inancgumus/screen"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type placeholder [5]string
|
|
||||||
|
|
||||||
zero := placeholder{"███", "█ █", "█ █", "█ █", "███"}
|
|
||||||
one := placeholder{"██ ",
|
|
||||||
" █ ",
|
|
||||||
" █ ",
|
|
||||||
" █ ",
|
|
||||||
"███",
|
|
||||||
}
|
|
||||||
|
|
||||||
two := placeholder{
|
|
||||||
"███",
|
|
||||||
" █",
|
|
||||||
"███",
|
|
||||||
"█ ",
|
|
||||||
"███",
|
|
||||||
}
|
|
||||||
|
|
||||||
three := placeholder{
|
|
||||||
"███",
|
|
||||||
" █",
|
|
||||||
"███",
|
|
||||||
" █",
|
|
||||||
"███",
|
|
||||||
}
|
|
||||||
|
|
||||||
four := placeholder{
|
|
||||||
"█ █",
|
|
||||||
"█ █",
|
|
||||||
"███",
|
|
||||||
" █",
|
|
||||||
" █",
|
|
||||||
}
|
|
||||||
|
|
||||||
five := placeholder{
|
|
||||||
"███",
|
|
||||||
"█ ",
|
|
||||||
"███",
|
|
||||||
" █",
|
|
||||||
"███",
|
|
||||||
}
|
|
||||||
|
|
||||||
six := placeholder{
|
|
||||||
"███",
|
|
||||||
"█ ",
|
|
||||||
"███",
|
|
||||||
"█ █",
|
|
||||||
"███",
|
|
||||||
}
|
|
||||||
|
|
||||||
seven := placeholder{
|
|
||||||
"███",
|
|
||||||
" █",
|
|
||||||
" █",
|
|
||||||
" █",
|
|
||||||
" █",
|
|
||||||
}
|
|
||||||
|
|
||||||
eight := placeholder{
|
|
||||||
"███",
|
|
||||||
"█ █",
|
|
||||||
"███",
|
|
||||||
"█ █",
|
|
||||||
"███",
|
|
||||||
}
|
|
||||||
|
|
||||||
nine := placeholder{
|
|
||||||
"███",
|
|
||||||
"█ █",
|
|
||||||
"███",
|
|
||||||
" █",
|
|
||||||
"███",
|
|
||||||
}
|
|
||||||
|
|
||||||
colon := placeholder{
|
|
||||||
" ",
|
|
||||||
" ░ ",
|
|
||||||
" ",
|
|
||||||
" ░ ",
|
|
||||||
" ",
|
|
||||||
}
|
|
||||||
|
|
||||||
digits := [...]placeholder{
|
|
||||||
zero, one, two, three, four, five, six, seven, eight, nine,
|
|
||||||
}
|
|
||||||
|
|
||||||
screen.Clear()
|
|
||||||
|
|
||||||
for {
|
|
||||||
screen.MoveTopLeft()
|
|
||||||
|
|
||||||
now := time.Now()
|
|
||||||
hour, min, sec := now.Hour(), now.Minute(), now.Second()
|
|
||||||
|
|
||||||
// [8][5]string -> [8]placeholder
|
|
||||||
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 {
|
|
||||||
next := clock[index][line]
|
|
||||||
if digit == colon && sec%2 == 0 {
|
|
||||||
next = " "
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(next, " ")
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user