add: arrays digital clock project

This commit is contained in:
Inanc Gumus
2018-12-06 23:20:51 +03:00
parent 8171a0193f
commit 571337643f
13 changed files with 729 additions and 266 deletions

View File

@@ -1,160 +0,0 @@
package main
//
// ~GET SOCIAL~
//
// Tweet when you complete:
// http://bit.ly/GOTWEET-CLOCK
//
// Discuss it with other gophers:
// http://bit.ly/LEARNGOSLACK
//
func main() {
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// Notes
// + I've created the solutions step by step
// + So, if you get stuck, you can check out the next step
// without having to look at the entire final solution
// + For drawing the clock, you may use the artifacts below
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ Usable Artifacts
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// Clock Characters:
//
// You can put these in constants if you like.
//
// Use this for the digits : "█"
// Use this for the separators : "░"
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 1 : Printing the Digits
// ★ Solution: 02-printing-the-digits
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// - [ ] Define a new placeholder type
// ...
// - [ ] Create the digits
// zero := ...
// one := ...
// ...
// - [ ] Put them into the "digits" array
// digits := ...
// - [ ] Print them side-by-side
// - [ ] Loop for all the lines in a digit
// - [ ] Print each digit line by line
//
// - [ ] Don't forget printing a space after each digit
// - [ ] Don't forget printing a newline after each line
//
// EXAMPLE: Let's say you want to print 10.
//
// ██ ███<--- Print a new line after printing a single line from
// █ █ █ all the digits.
// █ █ █
// █ █ █
// ███ ███
// ^^
// ||
// ++----> Add space between the digits
//
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 2 : Printing the Clock
// ★ Solution: 03-printing-the-clock
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// - [ ] Get the current time
// - [ ] Get the current hour, minute and second from the current time
// - [ ] Create the clock array
//
// - [ ] Get the individual digits from the digits array
//
// clock := ...
// - [ ] Print the clock
//
// - [ ] In the loops, use the clocks array instead
// - [ ] Create a separator array (it's also a placeholder)
// - [ ] Add the separators into the correct positions of
// the clock array
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 3 : Updating the Clock
// ★ Solution: 04-updating-the-clock
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// - [ ] Create an infinite loop to update the clock
// - [ ] Update the clock every second
//
// time.Sleep(time.Second) will stop the world for 1 second
//
// See this for more info:
// https://golang.org/pkg/time/#Sleep
// - [ ] Clear the screen before the infinite loop
//
// This string value clears the screen when printed:
//
// "\033[2J"
//
// + For Go Playground, use this instead: "\f"
//
// + This only works on bash command prompts.
// - [ ] Move the cursor to the top-left corner of the screen before each step
// of the infinite loop
//
// This string value moves the cursor like so when printed:
//
// "\033[H"
//
// + For Go Playground, use this instead: "\f"
//
// + This only works on bash command prompts.
// + If you're curious:
// \033 is a special control code:
// [2J clears the screen and the cursor
// [H moves the cursor to 0, 0 screen position
//
// See for more info:
// https://bluesock.org/~willkg/dev/ansi.html
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 4: Blinking the Separators
// ★ Solution: 05-blink-the-separators
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// - [ ] Blink the separators
//
// They should be visible per two seconds.
//
// Example: 1st second invisible
// 2nd second visible
// 3rd second invisible
// 4th second visible
//
// HINT: There are two ways to do this.
//
// 1- Manipulating the clock array directly
// (by adding/removing the separators)
//
// 2- Deciding what to print when printing the clock
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// YOU CAN FIND THE FINAL SOLUTION WITH ANNOTATIONS:
// 06-full-annotated-code
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
}

View File

@@ -1,135 +0,0 @@
// 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"
)
// GOALS:
// 1. Define a new placeholder type
// 2. Create the digits
// 3. Put all of them in "digits" array
// 4. Print them side-by-side
// Use this for the digits : "█"
// Use this for the separators : "░"
func main() {
// for keeping things easy to read and type-safe
type placeholder [5]string
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
// This array's type is "like": [10][5]string
//
// However:
// + "placeholder" is not equal to [5]string in type-wise.
// + Because: "placeholder" is a defined type, which is different
// from [5]string type.
// + [5]string is an unnamed type.
// + placeholder is a named type.
// + The underlying type of [5]string and placeholder is the same:
// [5]string
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
// Explanation: digits[0]
// + Each element of clock has the same length.
// + So: Getting the length of only one element is OK.
// + This could be: "zero" or "one" and so on... Instead of: digits[0]
//
// The range clause below is ~equal to the following code:
// line := 0; line < 5; line++
for line := range digits[0] {
// Print a line for each placeholder in digits
for digit := range digits {
fmt.Print(digits[digit][line], " ")
}
fmt.Println()
}
}

View File

@@ -1,146 +0,0 @@
// 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"
)
// GOALS:
// 1. Get time
// 2. Create the clock array
// Use the digits array
// 3. Print the digits
// Use the clock array instead of the digits array
// 4. Add the colons
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,
}
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
fmt.Printf("hour: %d, min: %d, sec: %d\n", hour, min, sec)
// [8][5]string
clock := [...]placeholder{
// extract the digits: 17 becomes, 1 and 7 respectively
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 digit := range clock {
fmt.Print(clock[digit][line], " ")
}
fmt.Println()
}
// for line := range clock[0] {
// for digit := range clock {
// fmt.Print(clock[digit][line], " ")
// }
// fmt.Println()
// }
}

View File

@@ -1,168 +0,0 @@
// 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"
)
// GOALS:
// 1. Update the clock in a loop
//
// 2. Before the whole loop:
// Clear the screen once
//
// 3. Before each step of the loop:
// Move the cursor to top-left position
// So, how to clear the screen and move the cursor?
// Check out the following constants.
//
// screenClear : When printed clears the screen.
// cursorMoveTop: Moves the cursor to top-left screen position
//
// + Note: This only works on bash command prompts.
//
// + For Go Playground, use these instead:
// screenClear = "\f"
// cursorMoveTop = "\f"
//
// See for more info: https://bluesock.org/~willkg/dev/ansi.html
const (
screenClear = "\033[2J"
cursorMoveTop = "\033[H"
)
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,
}
// clears the command screen
fmt.Print(screenClear)
// Go Playground will not run an infinite loop.
// So, instead, you may loop for 1000 times:
// for i := 0; i < 1000; i++ {
for {
// moves the cursor to the top-left position
fmt.Print(cursorMoveTop)
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 digit := range clock {
fmt.Print(clock[digit][line], " ")
}
fmt.Println()
}
// pause for 1 second
time.Sleep(time.Second)
}
}

View File

@@ -1,150 +0,0 @@
// 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"
)
// GOAL:
// Blink the colons
const (
screenClear = "\033[2J"
cursorMoveTop = "\033[H"
)
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,
}
fmt.Print(screenClear)
for {
fmt.Print(cursorMoveTop)
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()
}
// pause for 1 second
time.Sleep(time.Second)
// fmt.Println()
}
}

View File

@@ -1,190 +0,0 @@
// 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"
)
// screenClear : When printed clears the screen.
// cursorMoveTop: Moves the cursor to top-left screen position
//
// + Note: This only works on bash command prompts.
//
// + For Go Playground, use these instead:
// screenClear = "\f"
// cursorMoveTop = "\f"
//
// See for more info: https://bluesock.org/~willkg/dev/ansi.html
const (
screenClear = "\033[2J"
cursorMoveTop = "\033[H"
)
func main() {
// for keeping things easy to read and type-safe
type placeholder [5]string
// put the digits (placeholders) into variables
// using the placeholder array type above
zero := placeholder{
"███",
"█ █",
"█ █",
"█ █",
"███",
}
one := placeholder{
"██ ",
" █ ",
" █ ",
" █ ",
"███",
}
two := placeholder{
"███",
" █",
"███",
"█ ",
"███",
}
three := placeholder{
"███",
" █",
"███",
" █",
"███",
}
four := placeholder{
"█ █",
"█ █",
"███",
" █",
" █",
}
five := placeholder{
"███",
"█ ",
"███",
" █",
"███",
}
six := placeholder{
"███",
"█ ",
"███",
"█ █",
"███",
}
seven := placeholder{
"███",
" █",
" █",
" █",
" █",
}
eight := placeholder{
"███",
"█ █",
"███",
"█ █",
"███",
}
nine := placeholder{
"███",
"█ █",
"███",
" █",
"███",
}
colon := placeholder{
" ",
" ░ ",
" ",
" ░ ",
" ",
}
// This array's type is "like": [10][5]string
//
// However:
// + "placeholder" is not equal to [5]string in type-wise.
// + Because: "placeholder" is a defined type, which is different
// from [5]string type.
// + [5]string is an unnamed type.
// + placeholder is a named type.
// + The underlying type of [5]string and placeholder is the same:
// [5]string
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
// clears the command screen
fmt.Print(screenClear)
// Go Playground will not run an infinite loop.
// Loop for example 1000 times instead, like this:
// for i := 0; i < 1000; i++ { ... }
for {
// moves the cursor to the top-left position
fmt.Print(cursorMoveTop)
// get the current hour, minute and second
now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second()
// extract the digits: 17 becomes, 1 and 7 respectively
clock := [...]placeholder{
digits[hour/10], digits[hour%10],
colon,
digits[min/10], digits[min%10],
colon,
digits[sec/10], digits[sec%10],
}
// Explanation: clock[0]
// + Each element of clock has the same length.
// + So: Getting the length of only one element is OK.
// + This could be: "zero" or "one" and so on... Instead of: digits[0]
//
// The range clause below is ~equal to the following code:
// line := 0; line < len(clock[0]); line++
for line := range clock[0] {
// Print a line for each placeholder in clock
for index, digit := range clock {
// Colon blink on every two seconds.
// + On each sec divisible by two, prints an empty line
// + Otherwise: prints the current pixel
next := clock[index][line]
if digit == colon && sec%2 == 0 {
next = " "
}
// Print the next line and,
// give it enough space for the next placeholder
fmt.Print(next, " ")
}
// After each line of a placeholder, print a newline
fmt.Println()
}
// pause for 1 second
time.Sleep(time.Second)
}
}

View File

@@ -1,23 +0,0 @@
## GOALS:
### STEP #1 — Create Digits
- [ ] Define a new placeholder type
- [ ] Create the digits
- [ ] Put them into the "digits" array
- [ ] Print them side-by-side
### STEP #2 — Print the Clock
- [ ] Get the current time
- [ ] Create the clock array
- [ ] Print the clock
- [ ] Add the separators
### STEP #3 — Animate the Clock
- [ ] Create an infinite loop to update the clock
- [ ] Update the clock every second
- [ ] Clear the screen before the infinite loop
- [ ] Move the cursor to the top-left corner of the screen before each
step of the infinite loop
### BONUS
- [ ] Blink the separators