-
Notifications
You must be signed in to change notification settings - Fork 0
/
SquareLib.sol
54 lines (47 loc) · 917 Bytes
/
SquareLib.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
pragma solidity >=0.4.18;
library SquareLib {
struct MagicSquare {
uint256[][] rows;
uint256 n;
}
function initialize(uint256 n)
internal
pure
returns (MagicSquare memory square)
{
uint256 i;
square = MagicSquare({
rows: new uint256[][](n),
n: n
});
for (i = 0; i < n; i++) {
square.rows[i] = new uint256[](n);
}
}
function step(
MagicSquare memory square,
uint256 x,
uint256 y,
uint256 i
)
internal
pure
returns (
uint256 newX,
uint256 newY,
uint256 lastI
)
{
if (square.rows[x][y] != 0) {
newX = (x + 2) % square.n;
newY = (square.n + y - 1) % square.n;
lastI = i - 1;
return (newX, newY, lastI);
}
square.rows[x][y] = i;
newX = (square.n + x - 1) % square.n;
newY = (y + 1) % square.n;
lastI = i;
return (newX, newY, lastI);
}
}