From 2b632ebf09d75936ede824941bf0cdf7c58db3b9 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Fri, 14 Dec 2018 15:14:34 +0300 Subject: [PATCH] add: array led clock exercises --- .../exercises/00-text/main.go | 23 +++ .../exercises/00-text/solution/main.go | 11 ++ .../exercises/01-refactor/main.go | 173 ++++++++++++++++++ .../exercises/01-refactor/solution/main.go | 48 +++++ .../01-refactor/solution/placeholders.go | 95 ++++++++++ .../exercises/02-alarm/main.go | 87 +++++++++ .../exercises/02-alarm/placeholders.go | 95 ++++++++++ .../exercises/02-alarm/solution/main.go | 53 ++++++ .../02-alarm/solution/placeholders.go | 154 ++++++++++++++++ .../exercises/03-split-second/main.go | 147 +++++++++++++++ .../exercises/03-split-second/placeholders.go | 95 ++++++++++ .../03-split-second/solution/main.go | 51 ++++++ .../03-split-second/solution/placeholders.go | 103 +++++++++++ .../exercises/04-ticker/main.go | 113 ++++++++++++ .../exercises/04-ticker/placeholders.go | 95 ++++++++++ .../exercises/04-ticker/solution/main.go | 78 ++++++++ .../04-ticker/solution/placeholders.go | 95 ++++++++++ 15-arrays-project-clock/exercises/README.md | 21 +++ etc/stratch/main.go | 125 ------------- 19 files changed, 1537 insertions(+), 125 deletions(-) create mode 100644 15-arrays-project-clock/exercises/00-text/main.go create mode 100644 15-arrays-project-clock/exercises/00-text/solution/main.go create mode 100644 15-arrays-project-clock/exercises/01-refactor/main.go create mode 100644 15-arrays-project-clock/exercises/01-refactor/solution/main.go create mode 100644 15-arrays-project-clock/exercises/01-refactor/solution/placeholders.go create mode 100644 15-arrays-project-clock/exercises/02-alarm/main.go create mode 100644 15-arrays-project-clock/exercises/02-alarm/placeholders.go create mode 100644 15-arrays-project-clock/exercises/02-alarm/solution/main.go create mode 100644 15-arrays-project-clock/exercises/02-alarm/solution/placeholders.go create mode 100644 15-arrays-project-clock/exercises/03-split-second/main.go create mode 100644 15-arrays-project-clock/exercises/03-split-second/placeholders.go create mode 100644 15-arrays-project-clock/exercises/03-split-second/solution/main.go create mode 100644 15-arrays-project-clock/exercises/03-split-second/solution/placeholders.go create mode 100644 15-arrays-project-clock/exercises/04-ticker/main.go create mode 100644 15-arrays-project-clock/exercises/04-ticker/placeholders.go create mode 100644 15-arrays-project-clock/exercises/04-ticker/solution/main.go create mode 100644 15-arrays-project-clock/exercises/04-ticker/solution/placeholders.go create mode 100644 15-arrays-project-clock/exercises/README.md diff --git a/15-arrays-project-clock/exercises/00-text/main.go b/15-arrays-project-clock/exercises/00-text/main.go new file mode 100644 index 0000000..dc09d53 --- /dev/null +++ b/15-arrays-project-clock/exercises/00-text/main.go @@ -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() { +} diff --git a/15-arrays-project-clock/exercises/00-text/solution/main.go b/15-arrays-project-clock/exercises/00-text/solution/main.go new file mode 100644 index 0000000..b5e729e --- /dev/null +++ b/15-arrays-project-clock/exercises/00-text/solution/main.go @@ -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() { +} diff --git a/15-arrays-project-clock/exercises/01-refactor/main.go b/15-arrays-project-clock/exercises/01-refactor/main.go new file mode 100644 index 0000000..fb910eb --- /dev/null +++ b/15-arrays-project-clock/exercises/01-refactor/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/01-refactor/solution/main.go b/15-arrays-project-clock/exercises/01-refactor/solution/main.go new file mode 100644 index 0000000..185b1ed --- /dev/null +++ b/15-arrays-project-clock/exercises/01-refactor/solution/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/01-refactor/solution/placeholders.go b/15-arrays-project-clock/exercises/01-refactor/solution/placeholders.go new file mode 100644 index 0000000..d30cfba --- /dev/null +++ b/15-arrays-project-clock/exercises/01-refactor/solution/placeholders.go @@ -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, +} diff --git a/15-arrays-project-clock/exercises/02-alarm/main.go b/15-arrays-project-clock/exercises/02-alarm/main.go new file mode 100644 index 0000000..5bf2d78 --- /dev/null +++ b/15-arrays-project-clock/exercises/02-alarm/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/02-alarm/placeholders.go b/15-arrays-project-clock/exercises/02-alarm/placeholders.go new file mode 100644 index 0000000..d30cfba --- /dev/null +++ b/15-arrays-project-clock/exercises/02-alarm/placeholders.go @@ -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, +} diff --git a/15-arrays-project-clock/exercises/02-alarm/solution/main.go b/15-arrays-project-clock/exercises/02-alarm/solution/main.go new file mode 100644 index 0000000..805c920 --- /dev/null +++ b/15-arrays-project-clock/exercises/02-alarm/solution/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/02-alarm/solution/placeholders.go b/15-arrays-project-clock/exercises/02-alarm/solution/placeholders.go new file mode 100644 index 0000000..4e9003e --- /dev/null +++ b/15-arrays-project-clock/exercises/02-alarm/solution/placeholders.go @@ -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, +} diff --git a/15-arrays-project-clock/exercises/03-split-second/main.go b/15-arrays-project-clock/exercises/03-split-second/main.go new file mode 100644 index 0000000..37e2de2 --- /dev/null +++ b/15-arrays-project-clock/exercises/03-split-second/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/03-split-second/placeholders.go b/15-arrays-project-clock/exercises/03-split-second/placeholders.go new file mode 100644 index 0000000..d30cfba --- /dev/null +++ b/15-arrays-project-clock/exercises/03-split-second/placeholders.go @@ -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, +} diff --git a/15-arrays-project-clock/exercises/03-split-second/solution/main.go b/15-arrays-project-clock/exercises/03-split-second/solution/main.go new file mode 100644 index 0000000..adb1b01 --- /dev/null +++ b/15-arrays-project-clock/exercises/03-split-second/solution/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/03-split-second/solution/placeholders.go b/15-arrays-project-clock/exercises/03-split-second/solution/placeholders.go new file mode 100644 index 0000000..9697ab9 --- /dev/null +++ b/15-arrays-project-clock/exercises/03-split-second/solution/placeholders.go @@ -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, +} diff --git a/15-arrays-project-clock/exercises/04-ticker/main.go b/15-arrays-project-clock/exercises/04-ticker/main.go new file mode 100644 index 0000000..bc5fe78 --- /dev/null +++ b/15-arrays-project-clock/exercises/04-ticker/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/04-ticker/placeholders.go b/15-arrays-project-clock/exercises/04-ticker/placeholders.go new file mode 100644 index 0000000..d30cfba --- /dev/null +++ b/15-arrays-project-clock/exercises/04-ticker/placeholders.go @@ -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, +} diff --git a/15-arrays-project-clock/exercises/04-ticker/solution/main.go b/15-arrays-project-clock/exercises/04-ticker/solution/main.go new file mode 100644 index 0000000..9e81aea --- /dev/null +++ b/15-arrays-project-clock/exercises/04-ticker/solution/main.go @@ -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) + } +} diff --git a/15-arrays-project-clock/exercises/04-ticker/solution/placeholders.go b/15-arrays-project-clock/exercises/04-ticker/solution/placeholders.go new file mode 100644 index 0000000..d30cfba --- /dev/null +++ b/15-arrays-project-clock/exercises/04-ticker/solution/placeholders.go @@ -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, +} diff --git a/15-arrays-project-clock/exercises/README.md b/15-arrays-project-clock/exercises/README.md new file mode 100644 index 0000000..6181515 --- /dev/null +++ b/15-arrays-project-clock/exercises/README.md @@ -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. \ No newline at end of file diff --git a/etc/stratch/main.go b/etc/stratch/main.go index 800d78b..da29a2c 100644 --- a/etc/stratch/main.go +++ b/etc/stratch/main.go @@ -1,129 +1,4 @@ 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() - - // [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) - } }