Skip to content

Commit 963ac5e

Browse files
committed
Flexible grid dimensions
1 parent 54992ac commit 963ac5e

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

script/DeployEthPixelWar.s.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import {console} from "forge-std-1.9.6/src/console.sol";
66
import {EthPixelWar} from "../src/EthPixelWar.sol";
77

88
contract DeployEthPixelWar is Script {
9-
uint16 public gridSize = 10;
9+
uint16 public dimX = 10;
10+
uint16 public dimY = 10;
1011
bool public liteMode = false;
1112

1213
function run() external returns (EthPixelWar) {
1314
vm.startBroadcast();
14-
EthPixelWar ethPixelWar = new EthPixelWar(gridSize, liteMode, msg.sender);
15+
EthPixelWar ethPixelWar = new EthPixelWar(dimX, dimY, liteMode, msg.sender);
1516
vm.stopBroadcast();
1617
console.log("Deployed EthPixelWar contract at:", address(ethPixelWar));
1718
return ethPixelWar;

script/DeployEthPixelWarLite.s.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import {console} from "forge-std-1.9.6/src/console.sol";
66
import {EthPixelWar} from "../src/EthPixelWar.sol";
77

88
contract DeployEthPixelWarLite is Script {
9-
uint16 public gridSize = 10;
9+
uint16 public dimX = 10;
10+
uint16 public dimY = 10;
1011
bool public liteMode = true;
1112

1213
function run() external returns (EthPixelWar) {
1314
vm.startBroadcast();
14-
EthPixelWar ethPixelWar = new EthPixelWar(gridSize, liteMode, msg.sender);
15+
EthPixelWar ethPixelWar = new EthPixelWar(dimX, dimY, liteMode, msg.sender);
1516
vm.stopBroadcast();
1617
console.log("Deployed EthPixelWar (lite) contract at:", address(ethPixelWar));
1718
return ethPixelWar;

src/EthPixelWar.sol

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ struct Pixel {
1313

1414
contract EthPixelWar is Ownable {
1515
bool public immutable liteMode;
16-
uint16 public immutable nbPixels;
16+
uint16 public immutable dimX;
17+
uint16 public immutable dimY;
1718
bool public pixelWarIsActive;
1819
mapping(uint16 => Pixel) public grid;
1920
mapping(address => uint256) public pendingWithdrawals;
@@ -22,15 +23,16 @@ contract EthPixelWar is Ownable {
2223
event ColorUpdated(uint16 pixelId, uint8 r, uint8 g, uint8 b);
2324
event PixelWarEnded();
2425

25-
constructor(uint16 _gridSize, bool _liteMode, address _initialOwner) Ownable(_initialOwner) {
26-
require(_gridSize > 0 && _gridSize <= 255, "Grid size must be between 1 and 255");
27-
nbPixels = _gridSize * _gridSize;
26+
constructor(uint16 _dimX, uint16 _dimY, bool _liteMode, address _initialOwner) Ownable(_initialOwner) {
27+
require(_dimX > 0 && _dimX <= 255 && _dimY > 0 && _dimY <= 255, "Grid dimensions must be between 1 and 255");
2828
liteMode = _liteMode;
29+
dimX = _dimX;
30+
dimY = _dimY;
2931
pixelWarIsActive = true;
3032
}
3133

3234
modifier validPixelId(uint16 pixelId) {
33-
require(pixelId < nbPixels, "Invalid pixel id");
35+
require(pixelId < dimX * dimY, "Invalid pixel id");
3436
_;
3537
}
3638

@@ -84,7 +86,7 @@ contract EthPixelWar is Ownable {
8486
function endPixelWar() public onlyOwner onlyActivePixelWar {
8587
pixelWarIsActive = false;
8688

87-
for (uint16 pixelId = 0; pixelId < nbPixels; pixelId++) {
89+
for (uint16 pixelId = 0; pixelId < dimX * dimY; pixelId++) {
8890
Pixel storage pixel = grid[pixelId];
8991
if (pixel.owner != address(0) && pixel.highestBid > 0) {
9092
pendingWithdrawals[pixel.owner] += pixel.highestBid;
@@ -95,8 +97,8 @@ contract EthPixelWar is Ownable {
9597
}
9698

9799
function getGrid() public view returns (Pixel[] memory) {
98-
Pixel[] memory pixels = new Pixel[](nbPixels);
99-
for (uint16 pixelId = 0; pixelId < nbPixels; pixelId++) {
100+
Pixel[] memory pixels = new Pixel[](dimX * dimY);
101+
for (uint16 pixelId = 0; pixelId < dimX * dimY; pixelId++) {
100102
pixels[pixelId] = grid[pixelId];
101103
}
102104
return pixels;

test/EthPixelWar.t.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ contract EthPixelWarTest is Test {
1313
function setUp() public {
1414
alice = address(0x123);
1515
bob = address(0x456);
16-
epw = new EthPixelWar(3, false, alice);
16+
epw = new EthPixelWar(3, 4, false, alice);
1717

1818
// Label the addresses for easier readability in logs
1919
vm.label(alice, "Alice");
@@ -171,7 +171,7 @@ contract EthPixelWarTest is Test {
171171
vm.stopPrank();
172172

173173
Pixel[] memory pixels = epw.getGrid();
174-
assertEq(pixels.length, 9);
174+
assertEq(pixels.length, 12);
175175
assertEq(pixels[5].owner, alice);
176176
assertEq(pixels[5].highestBid, 3 ether);
177177
assertEq(pixels[5].r, 255);

0 commit comments

Comments
 (0)