A3: working version, i hope

This commit is contained in:
Johannes Theiner 2020-05-27 12:06:09 +02:00
parent c90d077551
commit e605657395
2 changed files with 85 additions and 80 deletions

View File

@ -4,7 +4,7 @@ import (
"fmt"
)
var debuggingAllowed = true
var printDebugMessages = false
type CardinalDirection string
@ -15,131 +15,137 @@ const (
west = CardinalDirection("West")
)
var Directions = []CardinalDirection{
north, south, east, west,
}
type Colour string
const (
red = Colour("\u001B[31m Red \u001B[0m")
green = Colour("\u001B[33m Green \u001B[0m")
yellow = Colour("\u001B[32m Yellow \u001B[0m")
)
var Colours = []Colour{
red, green, yellow,
}
func nextDirection(currentDirection CardinalDirection) CardinalDirection {
switch currentDirection {
case north:
return east
case south:
return west
case east:
return south
case south:
return west
case west:
return north
default:
}
return north
}
func oppositeDirection(currentDirection CardinalDirection) CardinalDirection {
switch currentDirection {
case north:
return south
case east:
return west
case south:
return north
case west:
return east
}
return north
}
func nextColour(currentColour Colour) Colour {
switch currentColour {
type Colour int
const (
red Colour = iota
green
yellow
)
func (colour Colour) String() string {
switch colour {
case red:
return green
case green:
return yellow
return "Red"
case yellow:
return red
return "Yellow"
case green:
return "Green"
default:
return red
return "null"
}
}
func nextColour(colour Colour) Colour {
return (colour + 1) % (yellow + 1)
}
var northChannel = make(chan string)
var southChannel = make(chan string)
var eastChannel = make(chan string)
var westChannel = make(chan string)
func getChannel(direction CardinalDirection) chan string {
switch direction {
case north:
return northChannel
case south:
return northChannel
case east:
return eastChannel
case west:
return eastChannel
}
return nil
}
func main() {
var quitChannel = make(chan string)
var northColour = make(chan Colour, 2)
var southColour = make(chan Colour, 2)
var eastColour = make(chan Colour, 2)
var westColour = make(chan Colour, 2)
var northDirection = make(chan string, 2)
var southDirection = make(chan string, 2)
var eastDirection = make(chan string, 2)
var westDirection = make(chan string, 2)
go TrafficLight(north, northColour, southColour, northDirection, eastDirection, southDirection)
go TrafficLight(south, southColour, northColour, southDirection, westDirection, northDirection)
go TrafficLight(east, eastColour, westColour, eastDirection, southDirection, westDirection)
go TrafficLight(west, westColour, eastColour, westDirection, northDirection, eastDirection)
go TrafficLight(north)
go TrafficLight(south)
go TrafficLight(east)
go TrafficLight(west)
<-quitChannel
}
func TrafficLight(direction CardinalDirection, currentColour <-chan Colour, oppositeColour chan<- Colour, currentDirection <-chan string, nextDirectionChan chan<- string, oppositeDirection chan<- string) {
func TrafficLight(direction CardinalDirection) {
var colour = red
if direction != north && direction != south {
nextDirectionChan <- string(nextDirection(direction))
if direction == east || direction == west {
handover("changeDirection", direction)
}
for {
show(direction, colour)
if colour == red {
syncLight(currentDirection, oppositeDirection)
changeDirection(direction, currentDirection, nextDirectionChan)
sync("wait", direction)
handover("changeDirection", direction)
}
colour = changeLight(colour, currentColour, oppositeColour)
sync("changeLight"+string(nextColour(colour)), direction)
colour = nextColour(colour)
}
}
func syncLight(currentDirection <-chan string, oppositeDirection chan<- string) {
oppositeDirection <- "sync"
select {
case msg := <-currentDirection:
if msg == "sync" {
return
}
}
}
func changeDirection(direction CardinalDirection, currentDirection <-chan string, nextDirectionChan chan<- string) {
nextDirectionChan <- string(nextDirection(direction))
select {
case msg := <-currentDirection:
if msg == string(direction) {
return
}
}
}
func changeLight(colour Colour, currentColour <-chan Colour, oppositeColour chan<- Colour) Colour {
oppositeColour <- nextColour(colour)
func handover(message string, direction CardinalDirection) {
getChannel(nextDirection(direction)) <- message
select {
case msg := <-currentColour:
if msg == nextColour(colour) {
return nextColour(colour)
case msg := <-getChannel(direction):
debug(msg)
}
}
return colour
func sync(message string, direction CardinalDirection) {
defer debug(string(direction) + " done with message " + message)
debug(string(direction) + " sync with message " + message)
select {
case getChannel(direction) <- message:
case msg := <-getChannel(direction):
debug(msg)
}
}
func show(direction CardinalDirection, colour Colour) {
fmt.Println(string(direction), " : ", string(colour))
fmt.Println(string(direction), " : ", colour.String())
}
func debug(message string) {
if debuggingAllowed {
fmt.Println("Debug:", message)
if printDebugMessages {
fmt.Println("Debug: " + message)
}
}

View File

@ -52,7 +52,6 @@ proc
-> wait . ChangeDirection(direction) %Richtungswechsel, wenn wieder zurück
. ChangeLight(direction, colour) % -> zur nächsten Farbe wechseln.
<> ChangeLight(direction, colour) %ansonsten zum nächsten Zustand wechseln.
;
ChangeDirection(direction : CardinalDirection) =