Code
local opt = dialog.CreateDialog("Player "..user.GetPlayerName() .." is black.")
cell={{"empty","empty"},{"empty","empty"}}
grid = {}
for i = 1, 2 do
grid[i] = {}
for j = 1, 2 do
grid[i][j] = "empty" -- Fill the values here
end
end
cell=grid
opt.labelMsg=dialog.AddLabel("Black places first. ")
repeat
isFed={{false,false},{false,false}}
print("In a real game, Player " .. user.GetPlayerName())
print (" can forfeit the game but not the match to save time.") --Jon copied from Bruno Kestomont's JET 3.6
--opt.AddAlgorithm1 = dialog.AddCheckbox("160, 70, 2, 34.555, 2.2, 1.2, 0.3 (made up band)", true)
--opt.AddAlgorithm2 = dialog.AddCheckbox("160, 70, 2, 34.555, 2.2, 1.2, 0.3 (made up band)", true)
--opt.AddAlgorithm3 = dialog.AddCheckbox("black", true)
--opt.AddAlgorithm4 = dialog.AddCheckbox("black", true)
opt.AddAlgorithm4 = dialog.AddLabel(cell[1][1].." ---- "..cell[1][2])
opt.AddAlgorithm5 = dialog.AddLabel(" | ".." ".." | ") --because of sans serif
opt.AddAlgorithm6 = dialog.AddLabel(cell[2][1].." ---- "..cell[2][2])
opt.oneOne = dialog.AddButton("Row 1 Col 1", 1)
opt.oneTwo = dialog.AddButton("Row 1 Col 2", 2)
--opt.linebreak=dialog.hr("--")
opt.label0=dialog.AddLabel("If old stones do have access to farmland or supply lines, ")
opt.label1=dialog.AddLabel(" they will starve. ") --attempt at linebreak")
opt.label2=dialog.AddLabel("Place stone at: ") --attempt at linebreak
opt.twoOne = dialog.AddButton("Row 2 Col 1", 3)
opt.twoTwo = dialog.AddButton("Row 2 Col 2", 4)
opt.pass = dialog.AddButton("Pass", 5)
opt.cancel = dialog.AddButton("Cancel", -1)
res=dialog.Show(opt)
whiteTurn=false
msg=""
function canEat(eaterColor,placementCell) --more like attack
if cell[(res+1)%4]==eaterColor and cell[(res+1)%4]~=eaterColor and cell[(res+1)%4]~="empty" then
--cell[2]~=eaterColor and cell[2]~="empty") and cell[4]==eaterColor) then
return true
end
return false
end
function checkFed()
--avoid this scenario with multiple deliveries of food:
--white white
--white empty
--starved white
--white empty
--this is taxicab geometry so it takes 2 iterations to deliver food from one corner to the opposite.
--this is why living far from farms is dangerous during pandemic. but traveling is dangerous too.
for numDeliveries=1,2 do
print("numDeliveries of food is "..numDeliveries)
for row=1, 2 do
for col=1,2 do
if cell[row][col]=="empty" then --liberty
isFed[(row)%2+1][col]=true --round world, though same as normal flat world rules of Go if 2x2
isFed[row][(col)%2+1]=true
else
if cell[(row)%2+1][col]==cell[row][col] and isFed[row][col] then --supply lines
isFed[(row)%2+1][col]=true --round world
end
if cell[row][(col)%2+1]==cell[row][col] and isFed[row][col] then
isFed[row][(col)%2+1]=true
end
end
end
end
print("fed: ")
for row=1,2 do
rowString=""
for col=1,2 do
rowString=rowString..tostring(isFed[row][col]).." "
end
print(rowString)
end
end
end
function starve()
for row=1, 2 do
for col=1,2 do
if cell[row][col]~="empty" and isFed[row][col]~=true then --no liberty
cell[row][col]="empty" --died
msg=msg.." Cell "..row..","..col.." starved. "
end
end
end
print("after famine: ")
for row=1,2 do
rowString=""
for col=1,2 do
rowString=rowString..tostring(isFed[row][col]).." "
end
print(rowString)
end
end
if(res==5) then whiteTurn=true
else
if(res>2) then row=2 else row=1 end
col=2-res%2
if cell[row][col]=="empty" then
--print("canEat is "..tostring(canEat("black",res))) --old algorithm for capturing, confusing with eat and starve
--In nixboard github code, it uses the can_eat algorithm to tell if a capture is available, which is called atari.
-- Atari in Go is like check in Chess.
--Capturing is kind of like white blood cells engulfing,
-- and eating infected cells. In Go, though, one color is not more evil than the other.
--In my own simple code, I rename things so that the stones eat vegetables from farmland, though there is war and seige.
cell[row][col]="black"
isFed={{false,false},{false,false}} --let white starve after black seiges
isFed[row][col]=true
print("Black played "..row..","..col..". ")
checkFed()
starve()
isFed={{false,false},{false,false}} --after giving a chance to capture, see if it was suicide
checkFed()
starve()
whiteTurn=true
else opt.labelMsg=dialog.AddLabel("Can't place at cell "..row..", "..col)
end
end
function ai()
isFed={{false,false},{false,false}}
for row=1, 2 do
for col=1,2 do
if(cell[row][col]=="empty") then
cell[row][col]="white"
isFed={{false,false},{false,false}} --let black starve after white seiges
isFed[row][col]=true
msg=msg.."White played "..row..","..col..". "
print("White played "..row..","..col..". ")
checkFed()
starve()
isFed={{false,false},{false,false}} --after giving a chance to capture, see if it was suicide
checkFed()
starve()
opt.labelMsg=dialog.AddLabel(msg)
return true
end
end
end
opt.labelMsg=dialog.AddLabel("White passes. ")
return false --pass
end
if whiteTurn then
ai()
end
until res==-1