A3: Anfang

This commit is contained in:
Johannes Theiner 2020-05-12 17:21:13 +02:00
parent c2493dcc4d
commit 53c9a8aba0

View File

@ -0,0 +1,89 @@
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):
}
}
}