A3: fully working version
This commit is contained in:
parent
e3c0d873b9
commit
c261e9c5c8
@ -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):
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user