summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordemo <demo@antix1>2026-05-24 22:30:23 -0400
committerdemo <demo@antix1>2026-05-24 22:30:23 -0400
commit1c528db2e28b1392c7001e46f148e283f585a00f (patch)
tree379b8ac1ccc932d226106d31c63cb242a0ad212f
parent5d62bf1bbd63017a28cbd489a7e12571bac65f85 (diff)
feat: get something that looks like it works!
-rw-r--r--main.go63
1 files changed, 42 insertions, 21 deletions
diff --git a/main.go b/main.go
index 4431dd0..a4670c0 100644
--- a/main.go
+++ b/main.go
@@ -3,7 +3,7 @@ package main
import (
"flag"
"fmt"
- "log"
+ "sync"
"time"
)
@@ -17,33 +17,54 @@ func main() {
}
func game(numSecs int) {
- table := make(chan Ball)
- go player("ping", table)
- go player("pong", table)
+ var wg sync.WaitGroup
+ done := make(chan struct{})
+ p0 := make(chan Ball)
- table <- Ball{}
+ p1 := player(1, &wg, done, p0)
+ p2 := player(2, &wg, done, p1)
+ t := time.Tick(time.Duration(numSecs) * time.Second)
- // Make the game last this long.
- time.Sleep(time.Duration(numSecs) * time.Second)
+ p0 <- Ball{}
- // Game over: grab the ball.
- <-table
+loop:
+ for b := range p2 {
+ select {
+ case p0 <- b:
+ case <-t:
+ break loop
+ }
+ }
+
+ close(p0)
+
+ wg.Wait()
+ fmt.Println("Done for now")
}
-func player(name string, table chan Ball) {
- log.Printf("started player %s", name)
+func player(id int, wg *sync.WaitGroup, done <-chan struct{}, input <-chan Ball) <-chan Ball {
+ out := make(chan Ball)
- for {
- ball := <-table
- ball.hits++
- fmt.Println(name, ball.hits)
+ wg.Go(func() {
+ defer func() {
+ close(out)
+ fmt.Printf("(%d) finished\n", id)
+ }()
- // Simulate some contact with the paddle.
- time.Sleep(100 * time.Millisecond)
+ fmt.Printf("(%d) started\n", id)
+ loop:
+ for b := range input {
+ b.hits++
+ fmt.Printf("(%d) %d\n", id, b.hits)
+ time.Sleep(100 * time.Millisecond)
- table <- ball
- }
+ select {
+ case out <- b:
+ case <-done:
+ break loop
+ }
+ }
+ })
- // FIXME: this is currently unreachable.
- log.Printf("finished player %s", name)
+ return out
}