Skip to content

Commit 0d3b240

Browse files
committed
Closes #6; use unit pointer in field struct
1 parent b952dd9 commit 0d3b240

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/client.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ pub const Client = struct {
124124
}
125125
}
126126
self.game.?.printPlayerNames();
127-
self.game.?.board.printUnits(&self.game.?);
127+
self.game.?.board.printUnits();
128128
},
129129
@intFromEnum(Message.Type.resources) => {
130130
var it0 = std.mem.tokenizeAny(u8, buff[1..], ";");
@@ -193,10 +193,10 @@ pub const Client = struct {
193193
const y = try std.fmt.parseUnsigned(u32, it.next().?, 10);
194194

195195
const unit = self.game.?.units.get(id).?;
196-
self.game.?.board.getField(unit.x, unit.y).unit_id = null;
196+
self.game.?.board.getField(unit.x, unit.y).unit = null;
197197
unit.x = x;
198198
unit.y = y;
199-
self.game.?.board.getField(x, y).unit_id = id;
199+
self.game.?.board.getField(x, y).unit = unit;
200200
},
201201
@intFromEnum(Message.Type.attack) => {
202202
var it = std.mem.tokenizeAny(u8, buff[1..], " \n");

src/game.zig

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const coords = @import("coordinateOps.zig").coords;
66

77
pub const Unit = struct { id: u32 = undefined, hp: u32 = undefined, x: u32 = undefined, y: u32 = undefined, owner: ?*Player = undefined };
88

9-
pub const Field = struct { unit_id: ?u32, res_hp: ?u32 };
9+
pub const Field = struct { unit: ?*Unit, res_hp: ?u32 };
1010

1111
pub const Board = struct {
1212
fields: []Field,
@@ -18,7 +18,7 @@ pub const Board = struct {
1818
const b = Board{ .fields = try allocator.alloc(Field, x * y), .x = x, .y = y, .allocator = allocator };
1919
errdefer allocator.free(b.fields);
2020
for (b.fields) |*f| {
21-
f.* = Field{ .unit_id = null, .res_hp = null };
21+
f.* = Field{ .unit = null, .res_hp = null };
2222
}
2323
return b;
2424
}
@@ -41,7 +41,7 @@ pub const Board = struct {
4141
for (0..self.y) |yi| {
4242
for (0..self.x) |xi| {
4343
const f = self.getField(@intCast(xi), @intCast(yi));
44-
if (f.res_hp != null and f.unit_id == null) {
44+
if (f.res_hp != null and f.unit == null) {
4545
const dist = distance(x, y, @intCast(xi), @intCast(yi));
4646
if (dist < minDist) {
4747
minDist = dist;
@@ -57,18 +57,18 @@ pub const Board = struct {
5757
for (0..self.y) |yi| {
5858
for (0..self.x) |xi| {
5959
self.fields[xi * self.y + yi].res_hp = null;
60-
self.fields[xi * self.y + yi].unit_id = null;
60+
self.fields[xi * self.y + yi].unit = null;
6161
}
6262
}
6363
}
6464

65-
pub fn printUnits(self: *Board, game: *Game) void {
65+
pub fn printUnits(self: *Board) void {
6666
print("units:\n", .{});
6767
for (0..self.y) |yi| {
6868
for (0..self.x) |xi| {
6969
const f = self.getField(@intCast(xi), @intCast(yi));
70-
if (f.unit_id) |id| {
71-
print("{c}", .{game.units.get(id).?.owner.?.name.items[0]});
70+
if (f.unit) |u| {
71+
print("{c}", .{u.owner.?.name.items[0]});
7272
} else print("-", .{});
7373
}
7474
print("\n", .{});
@@ -183,14 +183,14 @@ pub const Game = struct {
183183
unitPtr.owner = player;
184184
try player.units.put(unitPtr.id, unitPtr);
185185
try self.units.put(unitPtr.id, unitPtr);
186-
self.board.getField(unitPtr.x, unitPtr.y).unit_id = unitPtr.id;
186+
self.board.getField(unitPtr.x, unitPtr.y).unit = unitPtr;
187187
}
188188

189189
pub fn deleteUnit(self: *Game, id: u32) void {
190190
const unit = self.units.get(id).?;
191191
const player = unit.owner.?;
192192

193-
self.board.getField(unit.x, unit.y).unit_id = null;
193+
self.board.getField(unit.x, unit.y).unit = null;
194194
_ = player.units.remove(unit.id);
195195
_ = self.units.remove(id);
196196

@@ -269,5 +269,5 @@ test "clear" {
269269
game.clear();
270270
try std.testing.expectEqual(0, game.players.items.len);
271271
try std.testing.expectEqual(false, game.units.contains(0));
272-
try std.testing.expectEqual(null, game.board.getField(0, 0).unit_id);
272+
try std.testing.expectEqual(null, game.board.getField(0, 0).unit);
273273
}

0 commit comments

Comments
 (0)