90 lines
1.6 KiB
Go
90 lines
1.6 KiB
Go
package main
|
|
|
|
type CardinalDirection string
|
|
|
|
const (
|
|
north = CardinalDirection("North")
|
|
south = CardinalDirection("South")
|
|
east = CardinalDirection("East")
|
|
west = CardinalDirection("West")
|
|
)
|
|
|
|
var Directions = []CardinalDirection{
|
|
north, south, east, west,
|
|
}
|
|
|
|
func nextDirection(currentDirection CardinalDirection) CardinalDirection {
|
|
switch currentDirection {
|
|
case north:
|
|
return east
|
|
case south:
|
|
return west
|
|
case east:
|
|
return south
|
|
case west:
|
|
return north
|
|
default:
|
|
return north
|
|
//TODO: dont return this here, return nil instead
|
|
}
|
|
}
|
|
|
|
type Colour string
|
|
|
|
const (
|
|
red = Colour("Red")
|
|
green = Colour("Green")
|
|
yellow = Colour("Yellow")
|
|
)
|
|
|
|
var Colours = []Colour{
|
|
red, green, yellow,
|
|
}
|
|
|
|
func nextColour(currentColour Colour) Colour {
|
|
switch currentColour {
|
|
case red:
|
|
return green
|
|
case green:
|
|
return yellow
|
|
case yellow:
|
|
return red
|
|
default:
|
|
return red
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
var directionChannel = make(chan CardinalDirection)
|
|
var colourChannel = make(chan Colour)
|
|
var waitChannel = make(chan string)
|
|
|
|
for _, direction := range Directions {
|
|
go TrafficLight(direction, directionChannel, colourChannel, waitChannel)
|
|
}
|
|
|
|
directionChannel <- north
|
|
directionChannel <- south
|
|
|
|
}
|
|
|
|
func TrafficLight(direction CardinalDirection, directionChannel chan CardinalDirection, colourChannel chan Colour, waitChannel chan string) {
|
|
var colour Colour
|
|
for {
|
|
switch msg := <-waitChannel; {
|
|
case msg == "proceed":
|
|
directionChannel <- nextDirection(direction)
|
|
|
|
}
|
|
switch msg := <-directionChannel; {
|
|
case msg == direction:
|
|
|
|
}
|
|
colourChannel <- nextColour(colour)
|
|
switch msg := <-colourChannel; {
|
|
case msg == nextColour(colour):
|
|
|
|
}
|
|
}
|
|
}
|