Last active
May 12, 2016 03:35
-
-
Save mmcdaris/2ad259c3f595734a481c39a3e45fca38 to your computer and use it in GitHub Desktop.
kitty ping pong!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@thompiler Thanks for the help!!!