A3: nearly there
This commit is contained in:
parent
dbe8fcbf68
commit
c90d077551
|
@ -31,6 +31,22 @@ var Colours = []Colour{
|
||||||
red, green, yellow,
|
red, green, yellow,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func nextDirection(currentDirection CardinalDirection) CardinalDirection {
|
||||||
|
switch currentDirection {
|
||||||
|
case north:
|
||||||
|
return east
|
||||||
|
case south:
|
||||||
|
return west
|
||||||
|
case east:
|
||||||
|
return south
|
||||||
|
case west:
|
||||||
|
return north
|
||||||
|
default:
|
||||||
|
return north
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func nextColour(currentColour Colour) Colour {
|
func nextColour(currentColour Colour) Colour {
|
||||||
switch currentColour {
|
switch currentColour {
|
||||||
case red:
|
case red:
|
||||||
|
@ -52,10 +68,10 @@ func main() {
|
||||||
var eastColour = make(chan Colour, 2)
|
var eastColour = make(chan Colour, 2)
|
||||||
var westColour = make(chan Colour, 2)
|
var westColour = make(chan Colour, 2)
|
||||||
|
|
||||||
var northDirection = make(chan string, 4)
|
var northDirection = make(chan string, 2)
|
||||||
var southDirection = make(chan string, 4)
|
var southDirection = make(chan string, 2)
|
||||||
var eastDirection = make(chan string, 4)
|
var eastDirection = make(chan string, 2)
|
||||||
var westDirection = make(chan string, 4)
|
var westDirection = make(chan string, 2)
|
||||||
|
|
||||||
go TrafficLight(north, northColour, southColour, northDirection, eastDirection, southDirection)
|
go TrafficLight(north, northColour, southColour, northDirection, eastDirection, southDirection)
|
||||||
go TrafficLight(south, southColour, northColour, southDirection, westDirection, northDirection)
|
go TrafficLight(south, southColour, northColour, southDirection, westDirection, northDirection)
|
||||||
|
@ -65,27 +81,59 @@ func main() {
|
||||||
<-quitChannel
|
<-quitChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func TrafficLight(direction CardinalDirection, currentColour <-chan Colour, oppositeColour chan<- Colour, currentDirection <-chan string, nextDirection chan<- string, oppositeDirection chan<- string) {
|
func TrafficLight(direction CardinalDirection, currentColour <-chan Colour, oppositeColour chan<- Colour, currentDirection <-chan string, nextDirectionChan chan<- string, oppositeDirection chan<- string) {
|
||||||
var colour = red
|
var colour = red
|
||||||
|
|
||||||
if direction != north && direction != south {
|
if direction != north && direction != south {
|
||||||
nextDirection <- "go"
|
nextDirectionChan <- string(nextDirection(direction))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
show(direction, colour)
|
show(direction, colour)
|
||||||
|
|
||||||
oppositeColour <- nextColour(colour)
|
if colour == red {
|
||||||
select {
|
syncLight(currentDirection, oppositeDirection)
|
||||||
case msg := <-currentColour:
|
changeDirection(direction, currentDirection, nextDirectionChan)
|
||||||
if msg == nextColour(colour) {
|
|
||||||
colour = msg
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
colour = changeLight(colour, currentColour, oppositeColour)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case msg := <-currentColour:
|
||||||
|
if msg == nextColour(colour) {
|
||||||
|
return nextColour(colour)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return colour
|
||||||
|
}
|
||||||
|
|
||||||
func show(direction CardinalDirection, colour Colour) {
|
func show(direction CardinalDirection, colour Colour) {
|
||||||
fmt.Println(string(direction), " : ", string(colour))
|
fmt.Println(string(direction), " : ", string(colour))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue