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

@ -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
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 1 : Printing the Digits
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// - [ ] Define a new placeholder type
// - [ ] Create the digits from "zero" to "nine"
// - [ ] Put them into the "digits" array
// - [ ] 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
//
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ Usable Artifacts
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// Clock Characters:
//
// You can put these in constants if you like.
//
// Use this for the digits : "█"
// Use this for the separators : "░"
func main() {
}

View File

@ -11,15 +11,6 @@ import (
"fmt" "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() { func main() {
// for keeping things easy to read and type-safe // for keeping things easy to read and type-safe
type placeholder [5]string type placeholder [5]string

View File

@ -0,0 +1,139 @@
// 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/
//
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 2 : 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
//
// - [ ] 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
//
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ Usable Artifacts
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// Clock Characters:
//
// You can put these in constants if you like.
//
// Use this for the digits : "█"
// Use this for the separators : "░"
package main
import (
"fmt"
)
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{
"███",
"█ █",
"███",
" █",
"███",
}
digits := [...]placeholder{
zero, one, two, three, four, five, six, seven, eight, nine,
}
for line := range digits[0] {
for digit := range digits {
fmt.Print(digits[digit][line], " ")
}
fmt.Println()
}
}

View File

@ -12,14 +12,6 @@ import (
"time" "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() { func main() {
type placeholder [5]string type placeholder [5]string
@ -136,11 +128,4 @@ func main() {
} }
fmt.Println() fmt.Println()
} }
// for line := range clock[0] {
// for digit := range clock {
// fmt.Print(clock[digit][line], " ")
// }
// fmt.Println()
// }
} }

View File

@ -0,0 +1,196 @@
// 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/
//
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 3 : Animate 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
//
// + Get my library for clearing the screen:
//
// go get -u github.com/inancgumus/screen
//
// + Then, import it and call it in your code like this:
//
// screen.Clear()
//
// + If you're using Go Playground instead, do this:
//
// fmt.Println("\f")
//
//
// - [ ] Move the cursor to the top-left corner of the screen,
// before each step of the infinite loop
//
// + Call this in your code like this:
//
// screen.MoveTopLeft()
//
// + If you're using Go Playground instead, do this again:
//
// fmt.Println("\f")
//
//
// -----------------------------------------------------------------------------
// SIDE NOTE FOR THE CURIOUS
// -----------------------------------------------------------------------------
//
// If you're curious about how my screen clearing package works, read on.
//
// On bash, it uses special commands, if you open the code, you can see that.
//
// \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
//
// On Windows, I'm directly calling the Windows API functions. This is way
// advanced at this stage of the course, however, I'll explain it afterward.
//
// My package automatically adjusts itself depending on where it is compiled.
// On Windows, it uses the special Windows API calls;
// On other operating systems, it uses the bash special commands
// that I've explained above.
package main
import (
"fmt"
"time"
)
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()
}
}

View File

@ -10,33 +10,8 @@ package main
import ( import (
"fmt" "fmt"
"time" "time"
)
// GOALS: "github.com/inancgumus/screen"
// 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() { func main() {
@ -134,15 +109,16 @@ func main() {
zero, one, two, three, four, five, six, seven, eight, nine, zero, one, two, three, four, five, six, seven, eight, nine,
} }
// clears the command screen // For Go Playground, do not use this.
fmt.Print(screenClear) screen.Clear()
// Go Playground will not run an infinite loop. // Go Playground will not run an infinite loop.
// So, instead, you may loop for 1000 times: // So, instead, you may loop for 1000 times:
// for i := 0; i < 1000; i++ { // for i := 0; i < 1000; i++ {
for { for {
// moves the cursor to the top-left position // For Go Playground, use this instead:
fmt.Print(cursorMoveTop) // fmt.Print("\f")
screen.MoveTopLeft()
now := time.Now() now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second() hour, min, sec := now.Hour(), now.Minute(), now.Second()

View File

@ -0,0 +1,165 @@
// 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/
//
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
// ★ GOAL 4: Blinking 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 ALSO FIND THE FINAL SOLUTION WITH ANNOTATIONS:
// 05-full-annotated-solution
// ★★★★★★★★★★★★★★★★★★★★★★★★★★★
package main
import (
"fmt"
"time"
)
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 digit := range clock {
fmt.Print(clock[digit][line], " ")
}
fmt.Println()
}
time.Sleep(time.Second)
}
}

View File

@ -10,14 +10,8 @@ package main
import ( import (
"fmt" "fmt"
"time" "time"
)
// GOAL: "github.com/inancgumus/screen"
// Blink the colons
const (
screenClear = "\033[2J"
cursorMoveTop = "\033[H"
) )
func main() { func main() {
@ -115,10 +109,10 @@ func main() {
zero, one, two, three, four, five, six, seven, eight, nine, zero, one, two, three, four, five, six, seven, eight, nine,
} }
fmt.Print(screenClear) screen.Clear()
for { for {
fmt.Print(cursorMoveTop) screen.MoveTopLeft()
now := time.Now() now := time.Now()
hour, min, sec := now.Hour(), now.Minute(), now.Second() hour, min, sec := now.Hour(), now.Minute(), now.Second()
@ -143,8 +137,6 @@ func main() {
fmt.Println() fmt.Println()
} }
// pause for 1 second
time.Sleep(time.Second) time.Sleep(time.Second)
// fmt.Println()
} }
} }

View File

@ -10,21 +10,8 @@ package main
import ( import (
"fmt" "fmt"
"time" "time"
)
// screenClear : When printed clears the screen. "github.com/inancgumus/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() { func main() {
@ -135,15 +122,16 @@ func main() {
zero, one, two, three, four, five, six, seven, eight, nine, zero, one, two, three, four, five, six, seven, eight, nine,
} }
// clears the command screen // For Go Playground, do not use this.
fmt.Print(screenClear) screen.Clear()
// Go Playground will not run an infinite loop. // Go Playground will not run an infinite loop.
// Loop for example 1000 times instead, like this: // Loop for example 1000 times instead, like this:
// for i := 0; i < 1000; i++ { ... } // for i := 0; i < 1000; i++ { ... }
for { for {
// moves the cursor to the top-left position // For Go Playground, use this instead:
fmt.Print(cursorMoveTop) // fmt.Print("\f")
screen.MoveTopLeft()
// get the current hour, minute and second // get the current hour, minute and second
now := time.Now() now := time.Now()

View File

@ -0,0 +1,36 @@
## NOTES
- I've explained the challenges and 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
- You can find the explanations and solutions by clicking on the header links below
## GET SOCIAL
- Click the link below to tweet your solution when you complete: http://bit.ly/GOTWEET-CLOCK
- Discuss your solution with other gophers here: http://bit.ly/LEARNGOSLACK
---
## GOALS:
### 👉 [STEP #1 — Print the Digits](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/01-printing-the-digits/)
- [ ] Define a new placeholder type
- [ ] Create the digit arrays from 0 to 9
- [ ] Put them into the "digits" array
- [ ] Print them side-by-side
### 👉 [STEP #2 — Print the Clock](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/02-printing-the-clock/)
- [ ] Get the current time
- [ ] Create the clock array
- [ ] Print the clock
- [ ] Add the separators
### 👉 [STEP #3 — Animate the Clock](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/03-animating-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: STEP #4 — Blink the Clock](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/04-blinking-the-separators/)
- [ ] Blink the separators
### 👉 [FULL ANNOTATED SOLUTION](https://github.com/inancgumus/learngo/tree/master/15-arrays-project-clock/05-full-annotated-solution/main.go)

View File

@ -1,4 +1,129 @@
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)
}
} }

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,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