use math::random; use vt; use io; use os; type player = struct { x: int, y: int, }; type wall = void; type floor = void; type tile = (wall | floor); type room = struct { w: size, h: size, tiles: []tile, }; fn tile_at(r: room, x: size, y: size) tile = { const ix = x+(r.w*y); return r.tiles[ix]; }; fn set_tile_at(r: room, x: size, y: size, t: tile) void = { const ix = x+(r.w*y); r.tiles[ix] = t; }; fn get_key() u8 = { let b = [0u8]; io::read(os::stdin_file, b)!; return b[0]; }; def W = 50; def H = 90; export fn main() void = { const term = vt::open(); defer vt::close(term); vt::enablealt(term)!; vt::disablecur(term)!; const tiles: [W*H]tile = [floor...]; const room = room { w=W, h=H, tiles=tiles }; for (let i = 0z; i < W; i+=1) { set_tile_at(room, i, 0, wall); set_tile_at(room, i, H-1, wall); }; for (let i = 0z; i < H; i+=1) { set_tile_at(room, 0, i, wall); set_tile_at(room, W-1, i, wall); }; let rand = random::init(12345); for (let i = 0; i < 6; i+=1) { const x = (random::u64n(&rand, W-2)+1): size; const y = (random::u64n(&rand, H-2)+1): size; set_tile_at(room, x,y, wall); }; let player = player { x=1, y=1 }; const playerpen = vt::newpen(vt::color::BLACK, vt::color::RED); const playerstr = vt::tag(playerpen, "@"); for (true) { vt::clear(term)!; draw_room(room); vt::move(term, player.y:uint+1, player.x:uint+1)!; vt::print(term, playerstr)!; const c = get_key(): rune; let nx = player.x; let ny = player.y; switch (c) { case 'h' => nx -= 1; case 'l' => nx += 1; case 'j' => ny += 1; case 'k' => ny -= 1; case 'q' => break; case => yield; }; if (tile_at(room, nx:size, ny:size) is floor) { player.x = nx; player.y = ny; }; }; };