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 ( import (
"fmt" "fmt"
"runtime"
) )
var printDebugMessages = false //Definition CardinalDirection als enum
type CardinalDirection int
type CardinalDirection string
const ( const (
north = CardinalDirection("North") north = iota
south = CardinalDirection("South") east
east = CardinalDirection("East") south
west = CardinalDirection("West") west
) )
func nextDirection(currentDirection CardinalDirection) CardinalDirection { //definiert die nächste Richtung in der Reihenfolge north -> east -> south -> west -> north ...
switch currentDirection { func nextDirection(direction CardinalDirection) CardinalDirection {
case north: return (direction + 1) % (west + 1)
return east
case east:
return south
case south:
return west
case west:
return north
}
return north
} }
func oppositeDirection(currentDirection CardinalDirection) CardinalDirection { func (direction CardinalDirection) String() string {
switch currentDirection { return [...]string{"North", "East", "South", "West"}[direction]
case north:
return south
case east:
return west
case south:
return north
case west:
return east
}
return north
} }
//Definition Colour als enum
type Colour int type Colour int
const ( const (
@ -51,40 +33,26 @@ const (
yellow yellow
) )
func (colour Colour) String() string { //definiert nächste Farbe in der Reihenfolge red -> green -> yellow -> red ...
switch colour {
case red:
return "Red"
case yellow:
return "Yellow"
case green:
return "Green"
default:
return "null"
}
}
func nextColour(colour Colour) Colour { func nextColour(colour Colour) Colour {
return (colour + 1) % (yellow + 1) return (colour + 1) % (yellow + 1)
} }
var northChannel = make(chan string) func (colour Colour) String() string {
var southChannel = make(chan string) return [...]string{"Red", "Green", "Yellow"}[colour]
var eastChannel = make(chan string) }
var westChannel = make(chan string)
//Erstellung der Channel
func getChannel(direction CardinalDirection) chan string { var northSouthChannel = make(chan string)
switch direction { var eastWestChannel = make(chan string)
case north:
return northChannel //Channel für die Achse erhalten, zu der direction gehört
case south: func getAxisChannel(direction CardinalDirection) chan string {
return northChannel if direction == north || direction == south {
case east: return northSouthChannel
return eastChannel } else {
case west: return eastWestChannel
return eastChannel
} }
return nil
} }
func main() { func main() {
@ -111,41 +79,31 @@ 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"+string(nextColour(colour)), direction)
colour = nextColour(colour) colour = nextColour(colour)
} }
} }
func handover(message string, direction CardinalDirection) { func handover(message string, direction CardinalDirection) {
getChannel(nextDirection(direction)) <- message getAxisChannel(nextDirection(direction)) <- message
select { select {
case msg := <-getChannel(direction): case <-getAxisChannel(direction):
debug(msg)
} }
} }
func sync(message string, direction CardinalDirection) { func sync(message string, direction CardinalDirection) {
defer debug(string(direction) + " done with message " + message)
debug(string(direction) + " sync with message " + message)
select { select {
case getChannel(direction) <- message: case getAxisChannel(direction) <- message:
case msg := <-getChannel(direction): case <-getAxisChannel(direction):
debug(msg)
} }
} }
func show(direction CardinalDirection, colour Colour) { func show(direction CardinalDirection, colour Colour) {
fmt.Println(string(direction), " : ", colour.String()) fmt.Println(direction.String(), " : ", colour.String())
}
func debug(message string) {
if printDebugMessages {
fmt.Println("Debug: " + message)
}
} }