A3: realy close to the finish line

This commit is contained in:
Johannes Theiner 2020-05-29 17:24:03 +02:00
parent e605657395
commit e3c0d873b9
1 changed files with 35 additions and 77 deletions

View File

@ -2,47 +2,29 @@ package main
import (
"fmt"
"runtime"
)
var printDebugMessages = false
type CardinalDirection string
//Definition CardinalDirection als enum
type CardinalDirection int
const (
north = CardinalDirection("North")
south = CardinalDirection("South")
east = CardinalDirection("East")
west = CardinalDirection("West")
north = iota
east
south
west
)
func nextDirection(currentDirection CardinalDirection) CardinalDirection {
switch currentDirection {
case north:
return east
case east:
return south
case south:
return west
case west:
return north
}
return north
//definiert die nächste Richtung in der Reihenfolge north -> east -> south -> west -> north ...
func nextDirection(direction CardinalDirection) CardinalDirection {
return (direction + 1) % (west + 1)
}
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 (direction CardinalDirection) String() string {
return [...]string{"North", "East", "South", "West"}[direction]
}
//Definition Colour als enum
type Colour int
const (
@ -51,40 +33,26 @@ const (
yellow
)
func (colour Colour) String() string {
switch colour {
case red:
return "Red"
case yellow:
return "Yellow"
case green:
return "Green"
default:
return "null"
}
}
//definiert nächste Farbe in der Reihenfolge red -> green -> yellow -> red ...
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
func (colour Colour) String() string {
return [...]string{"Red", "Green", "Yellow"}[colour]
}
//Erstellung der Channel
var northSouthChannel = make(chan string)
var eastWestChannel = 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 nil
}
func main() {
@ -111,41 +79,31 @@ func TrafficLight(direction CardinalDirection) {
if colour == red {
sync("wait", direction)
handover("changeDirection", direction)
runtime.Gosched()
}
sync("changeLight"+string(nextColour(colour)), direction)
sync("changeLight"+nextColour(colour).String(), direction)
colour = nextColour(colour)
}
}
func handover(message string, direction CardinalDirection) {
getChannel(nextDirection(direction)) <- message
getAxisChannel(nextDirection(direction)) <- message
select {
case msg := <-getChannel(direction):
debug(msg)
case <-getAxisChannel(direction):
}
}
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)
case getAxisChannel(direction) <- message:
case <-getAxisChannel(direction):
}
}
func show(direction CardinalDirection, colour Colour) {
fmt.Println(string(direction), " : ", colour.String())
}
func debug(message string) {
if printDebugMessages {
fmt.Println("Debug: " + message)
}
fmt.Println(direction.String(), " : ", colour.String())
}