Only one runner can be safe on one base at a time.

Test this new change.
Add custom printTable() for use in test code.
This commit is contained in:
Sage Vaillancourt 2025-02-23 18:10:56 -05:00
parent aceefeb25c
commit f42ef06ff6
5 changed files with 60 additions and 11 deletions

View File

@ -80,8 +80,17 @@ function Baserunning:outEligibleRunners(fielder)
local touchedBase = utils.isTouchingBase(fielder.x, fielder.y)
local didOutRunner = false
local runnerBaseBiMap = {}
for _, runner in pairs(self.runners) do
local theTouchedBase = utils.isTouchingBase(runner.x, runner.y)
if theTouchedBase ~= nil and runnerBaseBiMap[theTouchedBase] == nil then
runnerBaseBiMap[runner] = theTouchedBase
runnerBaseBiMap[theTouchedBase] = runner
end
end
for i, runner in pairs(self.runners) do
local runnerOnBase = utils.isTouchingBase(runner.x, runner.y)
local runnerOnBase = runnerBaseBiMap[runner]
if -- Force out
touchedBase
and runner.prevBase -- Make sure the runner is not standing at home

View File

@ -2,9 +2,11 @@ local gfx <const> = playdate.graphics
local ButtonFont <const> = gfx.font.new("fonts/font-full-circle.pft")
--- Assumes that background image is of size
--- Assumes that background image is of size:
---
--- XXX
--- XOX
---
--- Where each character is the size of the screen, and 'O' is the default view.
function getDrawOffset(ballX, ballY)
local offsetX, offsetY

View File

@ -11,3 +11,28 @@ local _f = require("fielding")
Fielding, newFielder = _f[1], _f[2]
HomeTeamSpriteGroup = {}
-- Print contents of `tbl`, with indentation.
-- `indent` sets the initial level of indentation.
function str(tbl, indent, nl)
if not indent then
indent = 1
end
nl = nl or "\n"
if type(tbl) == "table" then
local indentStr = string.rep(" ", indent)
local ret = "{" .. nl
for k, v in pairs(tbl) do
--ret = ret .. indentStr .. "[" .. str(k, -9999, "") .. "]" .. ": " .. str(v, indent + 1, nl) .. "," .. nl
ret = ret .. indentStr .. "[" .. tostring(k) .. "]" .. ": " .. tostring(v) .. "," .. nl
end
return ret .. indentStr .. nl .. "}"
else
return tostring(tbl)
end
end
function printTable(tbl)
print(str(tbl))
end

View File

@ -42,22 +42,21 @@ end
---@alias Condition { fielderWithBallAt: XyPair, outWhen: BaseIndexOrXyPair[][], safeWhen: BaseIndexOrXyPair[][] }
---@param expected boolean
---@param expectedOuts number
---@param fielderWithBallAt XyPair
---@param when number[][]
function assertRunnerOutCondition(expected, when, fielderWithBallAt)
local msg = expected and "out" or "safe"
function assertRunnerOutCondition(expectedOuts, when, fielderWithBallAt)
for _, runnersOn in ipairs(when) do
local baserunning = buildRunnersOn(runnersOn)
local outedSomeRunner = baserunning:outEligibleRunners(fielderWithBallAt)
luaunit.failIf(outedSomeRunner ~= expected, "Runner should have been " .. msg .. ", but was not!")
baserunning:outEligibleRunners(fielderWithBallAt)
luaunit.assertEquals(expectedOuts, baserunning.outs, "Incorrect number of outs.")
end
end
---@param condition Condition
function assertRunnerStatuses(condition)
assertRunnerOutCondition(true, condition.outWhen, condition.fielderWithBallAt)
assertRunnerOutCondition(false, condition.safeWhen, condition.fielderWithBallAt)
assertRunnerOutCondition(1, condition.outWhen, condition.fielderWithBallAt)
assertRunnerOutCondition(0, condition.safeWhen, condition.fielderWithBallAt)
end
function testForceOutsAtFirst()
@ -157,4 +156,18 @@ function testTagOutsShouldNotHappenOnBase()
})
end
function testTagOutsWithMultipleRunnersOnOneBase()
assertRunnerStatuses({
fielderWithBallAt = C.Bases[C.Third],
outWhen = {
{ 3, 3 },
},
safeWhen = {
{ 1, 1 },
{ 2, 2 },
{ 4, 4 },
},
})
end
os.exit(luaunit.LuaUnit.run())

View File

@ -3,9 +3,9 @@ require("graphics")
local function assertSmallDifference(previous, current, ballValue, ballLabel)
local difference = math.abs(previous - current)
local baseError = "Expected a small difference, but received a difference of "
local baseError = "Expected a small difference, but received a difference of " .. difference
local fullDetails = luaunit.prettystr({ previous = previous, current = current, [ballLabel] = ballValue })
luaunit.assertIsTrue(difference < 2, baseError .. difference .. ":\n " .. luaunit.prettystr(fullDetails))
luaunit.assertIsTrue(difference < 2, baseError .. ":\n " .. luaunit.prettystr(fullDetails))
end
function testNoJumpsInYOffset()