Send out game data when Stage 3 capacity is reached.

This commit is contained in:
Patrick Fairbank
2020-03-22 17:21:32 -07:00
parent de976ab59f
commit 27dc4a8773
12 changed files with 279 additions and 33 deletions

58
game/control_panel.go Normal file
View File

@@ -0,0 +1,58 @@
// Copyright 2020 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Represents the state of an alliance Control Panel in the 2020 game.
package game
import "math/rand"
type ControlPanel struct {
CurrentColor ControlPanelColor
}
type ControlPanelColor int
const (
ColorUnknown ControlPanelColor = iota
ColorRed
ColorGreen
ColorBlue
ColorYellow
)
type ControlPanelStatus int
const (
ControlPanelNone ControlPanelStatus = iota
ControlPanelRotation
ControlPanelPosition
)
// Returns a random color that does not match the current color.
func (controlPanel *ControlPanel) GetStage3TargetColor() ControlPanelColor {
if controlPanel.CurrentColor == ColorUnknown {
// If the sensor or manual scorekeeping did not detect/set the current color, pick one of the four at random.
return ControlPanelColor(rand.Intn(4) + 1)
}
newColor := int(controlPanel.CurrentColor) + rand.Intn(3) + 1
if newColor > 4 {
newColor -= 4
}
return ControlPanelColor(newColor)
}
// Returns the string that is to be sent to the driver station for the given color.
func GetGameDataForColor(color ControlPanelColor) string {
switch color {
case ColorRed:
return "R"
case ColorGreen:
return "G"
case ColorBlue:
return "B"
case ColorYellow:
return "Y"
}
return ""
}

View File

@@ -0,0 +1,44 @@
// Copyright 2020 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
package game
import (
"github.com/stretchr/testify/assert"
"math/rand"
"testing"
)
func TestControlPanelGetStage3TargetColor(t *testing.T) {
rand.Seed(0)
var controlPanel ControlPanel
controlPanel.CurrentColor = ColorUnknown
results := getStage3TargetColorNTimes(&controlPanel, 10000)
assert.Equal(t, [5]int{0, 2543, 2527, 2510, 2420}, results)
controlPanel.CurrentColor = ColorRed
results = getStage3TargetColorNTimes(&controlPanel, 10000)
assert.Equal(t, [5]int{0, 0, 3351, 3311, 3338}, results)
controlPanel.CurrentColor = ColorGreen
results = getStage3TargetColorNTimes(&controlPanel, 10000)
assert.Equal(t, [5]int{0, 3335, 0, 3320, 3345}, results)
controlPanel.CurrentColor = ColorBlue
results = getStage3TargetColorNTimes(&controlPanel, 10000)
assert.Equal(t, [5]int{0, 3328, 3296, 0, 3376}, results)
controlPanel.CurrentColor = ColorYellow
results = getStage3TargetColorNTimes(&controlPanel, 10000)
assert.Equal(t, [5]int{0, 3303, 3388, 3309, 0}, results)
}
// Invokes the method N times and returns a map of the counts for each result, for statistical testing.
func getStage3TargetColorNTimes(controlPanel *ControlPanel, n int) [5]int {
var results [5]int
for i := 0; i < n; i++ {
results[controlPanel.GetStage3TargetColor()]++
}
return results
}

View File

@@ -16,10 +16,11 @@ type Score struct {
TeleopCellsOuter [4]int
TeleopCellsInner [4]int
ControlPanelStatus
EndgameStatuses [3]EndgameStatus
RungIsLevel bool
Fouls []Foul
ElimDq bool
EndgameStatuses [3]EndgameStatus
RungIsLevel bool
Fouls []Foul
ElimDq bool
Stage3TargetColor ControlPanelColor
}
type ScoreSummary struct {
@@ -55,14 +56,6 @@ const (
StageExtra
)
type ControlPanelStatus int
const (
ControlPanelNone ControlPanelStatus = iota
ControlPanelRotation
ControlPanelPosition
)
// Represents the state of a robot at the end of the match.
type EndgameStatus int