A3: fully working version

This commit is contained in:
Johannes Theiner 2020-05-30 07:48:26 +02:00
parent e3c0d873b9
commit c261e9c5c8

View File

@ -2,7 +2,6 @@ package main
import (
"fmt"
"runtime"
)
//Definition CardinalDirection als enum
@ -46,13 +45,22 @@ func (colour Colour) String() string {
var northSouthChannel = make(chan string)
var eastWestChannel = make(chan string)
var northEastChannel = make(chan string)
var southWestChannel = make(chan string)
//Channel für die Achse erhalten, zu der direction gehört
func getAxisChannel(direction CardinalDirection) chan string {
if direction == north || direction == south {
return northSouthChannel
} else {
return eastWestChannel
}
return eastWestChannel
}
func getNextChannel(direction CardinalDirection) chan string {
if direction == north || direction == east {
return northEastChannel
}
return southWestChannel
}
func main() {
@ -70,7 +78,9 @@ func TrafficLight(direction CardinalDirection) {
var colour = red
if direction == east || direction == west {
handover("changeDirection", direction)
select {
case <-getNextChannel(direction):
}
}
for {
@ -79,7 +89,6 @@ func TrafficLight(direction CardinalDirection) {
if colour == red {
sync("wait", direction)
handover("changeDirection", direction)
runtime.Gosched()
}
sync("changeLight"+nextColour(colour).String(), direction)
colour = nextColour(colour)
@ -87,10 +96,10 @@ func TrafficLight(direction CardinalDirection) {
}
func handover(message string, direction CardinalDirection) {
getAxisChannel(nextDirection(direction)) <- message
getNextChannel(direction) <- message
select {
case <-getAxisChannel(direction):
case <-getNextChannel(direction):
}
}