A3: fully working version

This commit is contained in:
Johannes Theiner 2020-05-30 07:48:26 +02:00
parent e3c0d873b9
commit c261e9c5c8
1 changed files with 16 additions and 7 deletions

View File

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