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:
parent
aceefeb25c
commit
f42ef06ff6
|
@ -80,8 +80,17 @@ function Baserunning:outEligibleRunners(fielder)
|
||||||
local touchedBase = utils.isTouchingBase(fielder.x, fielder.y)
|
local touchedBase = utils.isTouchingBase(fielder.x, fielder.y)
|
||||||
local didOutRunner = false
|
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
|
for i, runner in pairs(self.runners) do
|
||||||
local runnerOnBase = utils.isTouchingBase(runner.x, runner.y)
|
local runnerOnBase = runnerBaseBiMap[runner]
|
||||||
if -- Force out
|
if -- Force out
|
||||||
touchedBase
|
touchedBase
|
||||||
and runner.prevBase -- Make sure the runner is not standing at home
|
and runner.prevBase -- Make sure the runner is not standing at home
|
||||||
|
|
|
@ -2,9 +2,11 @@ local gfx <const> = playdate.graphics
|
||||||
|
|
||||||
local ButtonFont <const> = gfx.font.new("fonts/font-full-circle.pft")
|
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
|
--- XXX
|
||||||
--- XOX
|
--- XOX
|
||||||
|
---
|
||||||
--- Where each character is the size of the screen, and 'O' is the default view.
|
--- Where each character is the size of the screen, and 'O' is the default view.
|
||||||
function getDrawOffset(ballX, ballY)
|
function getDrawOffset(ballX, ballY)
|
||||||
local offsetX, offsetY
|
local offsetX, offsetY
|
||||||
|
|
|
@ -11,3 +11,28 @@ local _f = require("fielding")
|
||||||
Fielding, newFielder = _f[1], _f[2]
|
Fielding, newFielder = _f[1], _f[2]
|
||||||
|
|
||||||
HomeTeamSpriteGroup = {}
|
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
|
||||||
|
|
|
@ -42,22 +42,21 @@ end
|
||||||
|
|
||||||
---@alias Condition { fielderWithBallAt: XyPair, outWhen: BaseIndexOrXyPair[][], safeWhen: BaseIndexOrXyPair[][] }
|
---@alias Condition { fielderWithBallAt: XyPair, outWhen: BaseIndexOrXyPair[][], safeWhen: BaseIndexOrXyPair[][] }
|
||||||
|
|
||||||
---@param expected boolean
|
---@param expectedOuts number
|
||||||
---@param fielderWithBallAt XyPair
|
---@param fielderWithBallAt XyPair
|
||||||
---@param when number[][]
|
---@param when number[][]
|
||||||
function assertRunnerOutCondition(expected, when, fielderWithBallAt)
|
function assertRunnerOutCondition(expectedOuts, when, fielderWithBallAt)
|
||||||
local msg = expected and "out" or "safe"
|
|
||||||
for _, runnersOn in ipairs(when) do
|
for _, runnersOn in ipairs(when) do
|
||||||
local baserunning = buildRunnersOn(runnersOn)
|
local baserunning = buildRunnersOn(runnersOn)
|
||||||
local outedSomeRunner = baserunning:outEligibleRunners(fielderWithBallAt)
|
baserunning:outEligibleRunners(fielderWithBallAt)
|
||||||
luaunit.failIf(outedSomeRunner ~= expected, "Runner should have been " .. msg .. ", but was not!")
|
luaunit.assertEquals(expectedOuts, baserunning.outs, "Incorrect number of outs.")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param condition Condition
|
---@param condition Condition
|
||||||
function assertRunnerStatuses(condition)
|
function assertRunnerStatuses(condition)
|
||||||
assertRunnerOutCondition(true, condition.outWhen, condition.fielderWithBallAt)
|
assertRunnerOutCondition(1, condition.outWhen, condition.fielderWithBallAt)
|
||||||
assertRunnerOutCondition(false, condition.safeWhen, condition.fielderWithBallAt)
|
assertRunnerOutCondition(0, condition.safeWhen, condition.fielderWithBallAt)
|
||||||
end
|
end
|
||||||
|
|
||||||
function testForceOutsAtFirst()
|
function testForceOutsAtFirst()
|
||||||
|
@ -157,4 +156,18 @@ function testTagOutsShouldNotHappenOnBase()
|
||||||
})
|
})
|
||||||
end
|
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())
|
os.exit(luaunit.LuaUnit.run())
|
||||||
|
|
|
@ -3,9 +3,9 @@ require("graphics")
|
||||||
|
|
||||||
local function assertSmallDifference(previous, current, ballValue, ballLabel)
|
local function assertSmallDifference(previous, current, ballValue, ballLabel)
|
||||||
local difference = math.abs(previous - current)
|
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 })
|
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
|
end
|
||||||
|
|
||||||
function testNoJumpsInYOffset()
|
function testNoJumpsInYOffset()
|
||||||
|
|
Loading…
Reference in New Issue