local GRID_STATE = {
NONE = 0,
MARK = 1,
}
local PROP_TYPE = {
NONE = 0,
CHOCOLATE = 1,
BALLOON = 2,
PLANE = 3,
}
local DefaultClickCellIndex = nil
local COLLECT_TARGET = 4
local PROP_COUNT_LIST = {
[PROP_TYPE.CHOCOLATE] = 6,
[PROP_TYPE.BALLOON] = 2,
[PROP_TYPE.PLANE] = 2,
}
local LoopCount = 10000
local DebugOut = false
if DebugOut then
LoopCount = 1
end
local INVALID_NUM = 0
local MAX_ROW = 5
local MAX_COL = 5
local CELL_COUNT = MAX_ROW * MAX_COL
local TableUtil = {}
TableUtil.shuffle = function(t, startIndex, endIndex)
if startIndex == nil then
startIndex = 1
end
if endIndex == nil then
endIndex = #t
end
for i = startIndex, endIndex do
local randomIndex = math.random(startIndex, endIndex)
local tempValue = t[i]
t[i] = t[randomIndex]
t[randomIndex] = tempValue
end
end
local function index2Coord(index)
return math.ceil(index / MAX_COL), (index - 1) % MAX_COL + 1
end
local function coord2Index(row, col)
return (row - 1) * MAX_COL + col
end
local function dump_value_(v)
if type(v) == "string" then
v = "\"" .. v .. "\""
end
return tostring(v)
end
local function dump(value, description, nesting)
if type(nesting) ~= "number" then nesting = 3 end
local lookupTable = {}
local result = {}
local function dump_(value, description, indent, nest, keylen)
description = description or "<var>"
local spc = ""
if type(keylen) == "number" then
spc = string.rep(" ", keylen - string.len(dump_value_(description)))
end
if type(value) ~= "table" then
result[#result +1 ] = string.format("%s%s%s = %s", indent, dump_value_(description), spc, dump_value_(value))
elseif lookupTable[tostring(value)] then
result[#result +1 ] = string.format("%s%s%s = *REF*", indent, dump_value_(description), spc)
else
lookupTable[tostring(value)] = true
if nest > nesting then
result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, dump_value_(description))
else
result[#result +1 ] = string.format("%s%s = {", indent, dump_value_(description))
local indent2 = indent.." "
local keys = {}
local keylen = 0
local values = {}
for k, v in pairs(value) do
keys[#keys + 1] = k
local vk = dump_value_(k)
local vkl = string.len(vk)
if vkl > keylen then keylen = vkl end
values[k] = v
end
table.sort(keys, function(a, b)
if type(a) == "number" and type(b) == "number" then
return a < b
else
return tostring(a) < tostring(b)
end
end)
for i, k in ipairs(keys) do
dump_(values[k], k, indent2, nest + 1, keylen)
end
result[#result +1] = string.format("%s}", indent)
end
end
end
dump_(value, description, "- ", 1)
for i, line in ipairs(result) do
print(line)
end
end
local function dump_board(value, description)
print(description or "")
local str = "\r\n"
for row = 1, MAX_ROW, 1 do
for col = 1, MAX_COL, 1 do
local cellIndex = coord2Index(row, col)
local cell = value[cellIndex]
str = str .. " " .. cell
end
if row ~= MAX_ROW then
str = str .. "\r\n"
end
end
print(str)
end
function table.nums(t)
local count = 0
for k, v in pairs(t) do
count = count + 1
end
return count
end
function table.clone(t, nometa)
local u = {}
for i, v in pairs(t) do
if type(v) == "table" then
u[i] = table.clone(v)
else
u[i] = v
end
end
return u
end
local function getOneCell(cellNum, cellType)
local cell = { }
cell.mNum_ = cellNum
cell.mType_ = cellType
function cell:getDescription()
local cellNumStr = " "
if self.mNum_ ~= INVALID_NUM then
cellNumStr = tostring(self.mNum_)
if string.len(cellNumStr) == 1 then
cellNumStr = " " .. cellNumStr
end
end
return string.format("{%s, %s}", cellNumStr, self.mType_)
end
function cell:beenTagged()
assert(self.mNum_ ~= INVALID_NUM, "Cell has been tagged.")
self.mNum_ = INVALID_NUM
return self.mType_
end
function cell:setType(type)
self.mType_ = type
end
function cell:getType()
return self.mType_
end
return cell
end
local function PrintCurrentState(board, allScore, ballStr, getPropMsg)
local str = "\r\n"
for i = 1, MAX_ROW, 1 do
for j = 1, MAX_COL, 1 do
local cellIndex = (i - 1) * MAX_COL + j
local cell = board[cellIndex]
str = str .. string.format("%s ", cell:getDescription())
end
if i ~= MAX_ROW then
str = str .. "\r\n"
end
end
if ballStr ~= INVALID_NUM then
print("Read ball: ", ballStr)
end
print(str)
if getPropMsg and getPropMsg ~= "" then
print(getPropMsg)
end
if allScore then
print("Current total score: ", allScore)
end
print()
end
local function getIndexRange(index)
local row, col = index2Coord(index)
local range = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}
local indexList = {}
for dir, offset in ipairs(range) do
local newRow, newCol = row + offset[1], col + offset[2]
if newRow >= 1 and newRow <= MAX_ROW and newCol >= 1 and newCol <= MAX_COL then
local dirIndex = coord2Index(newRow, newCol)
table.insert(indexList, dirIndex)
end
end
return indexList
end
local function getOneBoardConfig()
local propConfig = {}
local chocolateConfig = {}
local gridList = {}
for i = 1, CELL_COUNT do
gridList[i] = i
propConfig[i] = PROP_TYPE.NONE
end
TableUtil.shuffle(gridList)
local putCount = 0
for i = 1, CELL_COUNT, 1 do
if putCount >= PROP_COUNT_LIST[PROP_TYPE.CHOCOLATE] then
break
end
local startIndex = gridList[i]
if propConfig[startIndex] == PROP_TYPE.NONE then
local indexList = getIndexRange(startIndex)
TableUtil.shuffle(indexList)
for dir, index in ipairs(indexList) do
if propConfig[index] == PROP_TYPE.NONE then
propConfig[index] = PROP_TYPE.CHOCOLATE
propConfig[startIndex] = PROP_TYPE.CHOCOLATE
putCount = putCount + 1
chocolateConfig[index] = {startIndex, index}
chocolateConfig[startIndex] = {startIndex, index}
break
end
end
end
end
local emptyList = {}
for i = 1, CELL_COUNT do
if propConfig[i] == PROP_TYPE.NONE then
table.insert(emptyList, i)
end
end
TableUtil.shuffle(emptyList)
local allProp = {}
for pType, pCount in pairs(PROP_COUNT_LIST) do
if pType ~= PROP_TYPE.CHOCOLATE then
for idx = 1, pCount, 1 do
table.insert(allProp, pType)
end
end
end
for _, pType in ipairs(allProp) do
local emptyIdx = 1
for id, idx in ipairs(emptyList) do
local indexList = getIndexRange(idx)
local isEmpty = true
for _, dirIndex in ipairs(indexList) do
if propConfig[dirIndex] == PROP_TYPE.BALLOON or propConfig[dirIndex] == PROP_TYPE.PLANE then
isEmpty = false
break
end
end
if isEmpty then
emptyIdx = id
break
end
end
local idx = table.remove(emptyList, emptyIdx)
propConfig[idx] = pType
end
return propConfig, chocolateConfig
end
math.randomseed(os.time())
local function simulateReadBall()
local balls = { }
for i = 1, CELL_COUNT do
table.insert(balls, i)
end
local _propConfig, _chocolatConfig = getOneBoardConfig()
local board = { }
for i = 1, CELL_COUNT do
table.insert(board, getOneCell(i, _propConfig[i]))
end
local _markList = {}
local _isBingo = 0
local _totaleScore = 0
local _scoreWhen15 = 0
local _readBallCount = 0
local _collections = 0
for i = 1, MAX_ROW * MAX_COL do
_markList[i] = GRID_STATE.NONE
end
local _waitPropList = {}
local numToBoardIndex = { }
for cellIndex, cell in ipairs(board) do
if cell.mNum_ ~= INVALID_NUM then
numToBoardIndex[cell.mNum_] = cellIndex
end
end
TableUtil.shuffle(balls)
local removeOnlyGrid
local function checkCollectChocolate(gridIndex)
if _chocolatConfig[gridIndex] then
local isCollect = true
local collectList = {}
for _, index in ipairs(_chocolatConfig[gridIndex]) do
table.insert(collectList, index)
if _markList[index] ~= GRID_STATE.MARK then
isCollect = false
end
end
if isCollect then
for _, index in ipairs(collectList) do
_chocolatConfig[index] = nil
end
_collections = _collections + 1
return _chocolatConfig
end
end
return nil
end
local function checkUseBalloon(gridIndex)
local indexList = getIndexRange(gridIndex)
for _, index in ipairs(indexList) do
removeOnlyGrid(index)
end
end
local function checkUsePlan()
if #_waitPropList <= 0 then
return
end
local leftList = {}
for index, list in pairs(_chocolatConfig) do
if _markList[list[1]] == GRID_STATE.MARK and _markList[list[2]] ~= GRID_STATE.MARK then
table.insert(leftList, list[2])
elseif _markList[list[1]] ~= GRID_STATE.MARK and _markList[list[2]] == GRID_STATE.MARK then
table.insert(leftList, list[1])
end
end
if table.nums(leftList) > 0 then
local gridIndex = table.remove(_waitPropList, 1)
removeOnlyGrid(leftList[1])
end
end
function removeOnlyGrid(gridIndex)
if _markList[gridIndex] == GRID_STATE.MARK then
return
end
_markList[gridIndex] = GRID_STATE.MARK
local propType = _propConfig[gridIndex]
if propType == PROP_TYPE.CHOCOLATE then
checkCollectChocolate(gridIndex)
elseif propType == PROP_TYPE.BALLOON then
checkUseBalloon(gridIndex)
elseif propType == PROP_TYPE.PLANE then
table.insert(_waitPropList, {gridIndex = gridIndex, propType = propType})
end
checkUsePlan()
if DebugOut then
dump_board(_markList, gridIndex .. " " .. propType)
end
end
local function updateLogic(gridIndex)
removeOnlyGrid(gridIndex)
end
local function checkBingo()
if _totaleScore >= COLLECT_TARGET then
_isBingo = 1
return true
end
return false
end
while #balls > 0 do
_readBallCount = _readBallCount + 1
local ballNum = balls[1]
table.remove(balls, 1)
local ballIndex = numToBoardIndex[ballNum]
assert(ballIndex ~= nil, string.format("Cant find ball num index: %d", ballNum))
assert(board[ballIndex].mNum_ == ballNum, "NumToBoardIndex Map Error!")
board[ballIndex]:beenTagged()
if ballIndex ~= DefaultClickCellIndex then
updateLogic(ballIndex)
end
_totaleScore = _collections
if _totaleScore > COLLECT_TARGET then
_totaleScore = COLLECT_TARGET
end
if DebugOut then
PrintCurrentState(board, _totaleScore, ballNum)
end
if _readBallCount <= 15 then
_scoreWhen15 = _totaleScore
end
if checkBingo() then
break
end
end
return _readBallCount, _scoreWhen15
end
local bingoNeedReadBall = { }
for i = 1, CELL_COUNT do
bingoNeedReadBall[i] = 0
end
local allTotalScoreWhen15 = 0
for i = 1, LoopCount do
local ballCountCompleteBingo, snailNodePositionWhen15 = simulateReadBall()
bingoNeedReadBall[ballCountCompleteBingo] = bingoNeedReadBall[ballCountCompleteBingo] + 1
allTotalScoreWhen15 = allTotalScoreWhen15 + snailNodePositionWhen15
end
local bingoTotal = { }
bingoTotal[1] = 0
for i = 2, CELL_COUNT do
bingoTotal[i] = bingoNeedReadBall[i] + bingoTotal[i - 1]
end
print("标记格子数量发生bingo:", table.concat(bingoNeedReadBall, ", "))
print("标记累计发生bingo数量:", table.concat(bingoTotal, ", "))
print("第15次读球分:", allTotalScoreWhen15 / LoopCount)
print("第15次读球累计bingo数:", bingoTotal[15])