Hands-on Rust: can_place logic is "inside out"

Title: Hands-On Rust (Chapter 11: prefab)
Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_place
logic is inside-out. Current code starts with assumption that you can’t place and then set’s it to true if any of the positions work:
let mut can_place = false;// (4)
dimensions.for_each(|pt| {// (5)
let idx = mb.map.point2d_to_index(pt);
let distance = dijkstra_map.map[idx];
if distance < 2000.0 && distance > 20.0 && mb.amulet_start != pt {// (6)
can_place = true;
}
});
I think you need to start with a more positive attitude (you
can_place
unless you find something invalid):
let mut can_place = true;
dimensions.for_each(|pt| {
let idx = mb.map.point2d_to_index(pt);
let distance = dijkstra_map.map[idx];
if distance >= 2000.0 || distance < 20.0 || pt == mb.amulet_start {
can_place = false;
}
});
I’m also seeing some other weirdness:
- player stepping off into darkness,
- inaccessible floor tiles (I can see them diagonally)
- monsters on top of one another (pretty sure I’ve seen that)
I’m assuming the others are typos on my part and I have a good bit of debugging ahead of me. Will let you know what I find.
BTW, normally (i.e. in other environments I’ve coded in), every time something “weird” happened, I’d set up a unit test to catch it and then fix it (ensuring it’s never happen again). Would be interesting how unit testing i handled where there’s so much randomness involved.
Popular Pragprog topics









Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)

Other popular topics










Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /flutter
- /preact
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm