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
54
55
56
57
58
59
//! [`Sim`]: the rules of a game as a deterministic machine.
use ;
use crateRng;
/// The rules of a game, apart from how it looks. Same state, same inputs and
/// same random numbers give the same result on every machine; that is what
/// lets several machines play one game by sharing only their inputs, and lets
/// a test or a bot play with no terminal.
///
/// Keep everything that affects the rules in the `Sim`, and everything that
/// only affects the picture (sparks, screen shake) outside it. Inside `step`:
///
/// - take random numbers only from the `rng` you are given;
/// - read no clock and no window size (put the size in the state);
/// - use `game::math::{sin, cos, exp}` instead of the `f32` methods, which can
/// differ between machines in the last bit;
/// - iterate `Vec`s and arrays, not `HashMap`s.
///
/// ```
/// use terminal_graphics_interactive::game::*;
///
/// /// Two runners; each input is -1, 0 or 1.
/// struct Race { at: Vec<i32> }
///
/// impl Sim for Race {
/// type Input = i32;
/// fn step(&mut self, inputs: &[i32], _rng: &mut Rng) {
/// for (at, input) in self.at.iter_mut().zip(inputs) {
/// *at += input.clamp(&-1, &1);
/// }
/// }
/// fn hash(&self, h: &mut Hasher) {
/// for at in &self.at {
/// h.i32(*at);
/// }
/// }
/// }
///
/// let mut race = Race { at: vec![0, 0] };
/// let mut rng = Rng::new(1);
/// race.step(&[1, -1], &mut rng);
/// race.step(&[1, 0], &mut rng);
/// assert_eq!(race.at, [2, -1]);
/// ```