# GetDiagonals function (chess)

I just scripted a GetDiagonals function for my chess game (for the bishop), is there anything I should change?

``````function RoundModule.GetDiagonal(posX, posY, which)
local squares = {}

if which == "rightTop" or which == "all" then
--right top
local rightX = posX
for y = posY, 8 do
rightX = RoundModule.GetNextLetter(rightX)
if rightX == nil then
break
end
table.insert(squares, {X = rightX, Y = y})
end
end

if which == "leftTop" or which == "all" then
--left top
local leftX = posX
for y = posY, 8 do
leftX = RoundModule.GetPreviousLetter(leftX)
if leftX == nil then
break
end
table.insert(squares, {X = leftX, Y = y})
end
end

if which == "rightBottom" or which == "all" then
--right bottom
local rightBottomX = posX
for y = posY, 1 do
rightBottomX = RoundModule.GetNextLetter(rightBottomX)
if rightBottomX == nil then
break
end
table.insert(squares, {X = rightBottomX, Y = y})
end
end

if which == "leftBottom" or which == "all" then
--left bottom
local leftBottomX = posX
for y = posY, 1 do
leftBottomX = RoundModule.GetPreviousLetter(leftBottomX)
if leftBottomX == nil then
break
end
table.insert(squares, {X = leftBottomX, Y = y})
end
end

return squares
end
``````

Thanks!

2 Likes

I canâ€™t find any problems!

Showing the entire â€śRoundModuleâ€ť whould be helpfull to see if there are any problems in it!

1 Like

Please correct me if Iâ€™m wrong, but is the goal of you script to get all the legal squares you can move to? It wasnâ€™t really made clear what your intention is

1 Like

Yep, not really all the legal squares though (I didnâ€™t implement any piece-checks, there could be pieces on those squares), just all legal squares if there werenâ€™t any other pieces.

1 Like

Thanks!
Sure

``````local RoundModule = {}

local letters = {
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H"
}

function RoundModule.GetPreviousLetter(current)
return letters[table.find(letters, current)-1]
end

function RoundModule.GetNextLetter(current)
return letters[table.find(letters, current)+1]
end

function RoundModule.IncrementY(posY, increment)
return ((posY + increment <= 8 and posY + increment >= 1) and posY + increment) or posY
end

function RoundModule.GetSurroundingSquares(posX, posY)
local top = {X = posX, Y = RoundModule.IncrementY(posY, 1)}
local bottom = {X = posX, Y = RoundModule.IncrementY(posY, -1)}
local right = {X = RoundModule.GetNextLetter(posX) or posX, Y = posY}
local left = {X = RoundModule.GetPreviousLetter(posX) or posX, Y = posY}
local rightTop = {X = RoundModule.GetNextLetter(posX) or posX, Y = RoundModule.IncrementY(posY, 1)}
local leftTop = {X = RoundModule.GetPreviousLetter(posX) or posX, Y = RoundModule.IncrementY(posY, 1)}
local rightBottom = {X = RoundModule.GetNextLetter(posX) or posX, Y = RoundModule.IncrementY(posY, -1)}
local leftBottom = {X = RoundModule.GetPreviousLetter(posX) or posX, Y = RoundModule.IncrementY(posY, -1)}

return {top, bottom, right, left, rightTop, leftTop, rightBottom, leftBottom}
end

function RoundModule.GetLane(posX, posY, which) --posX is the letter, posY is the number
local squares = {}

if which == "top" or which == "all" then
for y = posY, 8 do
table.insert(squares, {X = posX, Y = y})
end
end

if which == "bottom" or which == "all" then
for y = posY, 1 do
table.insert(squares, {X = posX, Y = y})
end
end

if which == "left" or which == "all" then
local leftLetter = posX
while true do
leftLetter = RoundModule.GetPreviousLetter(leftLetter)
if leftLetter == nil then
break
end
table.insert(squares, {X = leftLetter, Y = posY})
end
end

if which == "right" or which == "all" then
local rightLetter = posX
while true do
rightLetter = RoundModule.GetNextLetter(rightLetter)
if rightLetter == nil then
break
end
table.insert(squares, {X = rightLetter, Y = posY})
end
end

return squares
end

function RoundModule.GetDiagonal(posX, posY, which)
local squares = {}

if which == "rightTop" or which == "all" then
--right top
local rightX = posX
for y = posY, 8 do
rightX = RoundModule.GetNextLetter(rightX)
if rightX == nil then
break
end
table.insert(squares, {X = rightX, Y = y})
end
end

if which == "leftTop" or which == "all" then
--left top
local leftX = posX
for y = posY, 8 do
leftX = RoundModule.GetPreviousLetter(leftX)
if leftX == nil then
break
end
table.insert(squares, {X = leftX, Y = y})
end
end

if which == "rightBottom" or which == "all" then
--right bottom
local rightBottomX = posX
for y = posY, 1 do
rightBottomX = RoundModule.GetNextLetter(rightBottomX)
if rightBottomX == nil then
break
end
table.insert(squares, {X = rightBottomX, Y = y})
end
end

if which == "leftBottom" or which == "all" then
--left bottom
local leftBottomX = posX
for y = posY, 1 do
leftBottomX = RoundModule.GetPreviousLetter(leftBottomX)
if leftBottomX == nil then
break
end
table.insert(squares, {X = leftBottomX, Y = y})
end
end

return squares
end

return RoundModule
``````