Skip to content

Instantly share code, notes, and snippets.

@mmcdaris
Last active May 12, 2016 03:35
Show Gist options
  • Save mmcdaris/2ad259c3f595734a481c39a3e45fca38 to your computer and use it in GitHub Desktop.
Save mmcdaris/2ad259c3f595734a481c39a3e45fca38 to your computer and use it in GitHub Desktop.
kitty ping pong!

example game 🐱 🎾 🐯 thanks @thompiler

package main
import (
"fmt"
"math/rand"
"time"
)
type Ball struct {
color string
hits int
}
func init() {
rand.Seed(time.Now().UTC().UnixNano())
}
func main() {
// Vincent Batts: Golang: good/bad/ugly
// https://youtu.be/cMYhGNofHA4?t=765
// ping pong!
table := make(chan *Ball) // table is a channel for balls
go player("bello", table)
go player("lola", table)
table <- &Ball{"red", 0}
duration := time.Duration(rand.Intn(2)+1) * time.Second
time.Sleep(duration)
ball := <-table // game over: grab the ball
fmt.Printf("------\nWell Played! %s ball hit %d times.\n", ball.color, ball.hits)
}
func player(name string, table chan *Ball) {
cat1 := "🐱"
cat2 := "🐯"
ballmoji := "🎾"
for {
ball := <-table
ball.hits++
if ball.hits%2 == 0 {
fmt.Printf("%s %s\t\t\t%s\n", cat1, ballmoji, " ")
fmt.Printf("%s \t%s\t\t%s\n", " ", ballmoji, " ")
fmt.Printf("%s \t\t%s\t%s\n", " ", ballmoji, " ")
fmt.Printf("%s \t\t\t%s%s\n", " ", ballmoji, " ")
} else {
fmt.Printf("%s \t\t\t%s %s\n", " ", ballmoji, cat2)
fmt.Printf("%s \t\t%s\t %s\n", " ", ballmoji, "")
fmt.Printf("%s \t%s\t\t %s\n", " ", ballmoji, "")
fmt.Printf("%s %s\t\t\t %s\n", " ", ballmoji, "")
}
time.Sleep(100 * time.Millisecond)
table <- ball
}
}
@thompiler
Copy link

Haha this is so awesome! Great job @mmcdaris! 🐱 X 💯 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment