From 53c9a8aba0b5b6e52a76d2c383dad9f0edf497f7 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 12 May 2020 17:21:13 +0200 Subject: [PATCH] A3: Anfang --- go/TrafficLights/TrafficLight.go | 89 ++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 go/TrafficLights/TrafficLight.go diff --git a/go/TrafficLights/TrafficLight.go b/go/TrafficLights/TrafficLight.go new file mode 100644 index 0000000..cd7cf5e --- /dev/null +++ b/go/TrafficLights/TrafficLight.go @@ -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): + + } + } +}