I've got this code which checks nearby cells for mines, and increases the number by one for every mine it finds. The problem is, its REALLY long, and I feel like it could be shortened, I just don't know how. Is there any way to shorten this?
Cell_Array stuff
Turing: | type Cells : %Record to store data for each cell
record
x1, y1 : int
clicked, mine : boolean
number : int
end record
var cell_Array : array 1 .. 20, 1 .. 20 of Cells %Array of records, 1 per cell
|
Long code
Turing: | procedure GenerateNumber
var num : int := 0
for i : 1 .. 20
for ii : 1 .. 20
if cell_Array (i, ii).mine = false then
if i not= 20 then
if cell_Array (i + 1, ii).mine = true then
num := num + 1
end if
end if
if i not= 20 and ii not= 20 then
if cell_Array (i + 1, ii + 1).mine = true then
num := num + 1
end if
end if
if ii not= 20 then
if cell_Array (i, ii + 1).mine = true then
num := num + 1
end if
end if
if i not= 1 and ii not= 20 then
if cell_Array (i - 1, ii + 1).mine = true then
num := num + 1
end if
end if
if i not= 1 then
if cell_Array (i - 1, ii).mine = true then
num := num + 1
end if
end if
if i not= 1 and ii not= 1 then
if cell_Array (i - 1, ii - 1).mine = true then
num := num + 1
end if
end if
if ii not= 1 then
if cell_Array (i, ii - 1).mine = true then
num := num + 1
end if
end if
if i not= 20 and ii not= 1 then
if cell_Array (i + 1, ii - 1).mine = true then
num := num + 1
end if
end if
end if
cell_Array (i, ii).number := num
num := 0
end for
end for
end GenerateNumber |
|