summaryrefslogtreecommitdiff
path: root/2019-worms/tests/official-runner-matching.rs
blob: 1b62088c3a7357dd528e3db848eca72b32eb2ad3 (plain)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use steam_powered_wyrm::command::{Action, Command};
use steam_powered_wyrm::constants::*;
use steam_powered_wyrm::game::*;
use steam_powered_wyrm::geometry::*;
use steam_powered_wyrm::json;

use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

#[test]
fn simulates_the_same_match() {
    let replays = Path::new("tests/replays/");
    for replay in replays.read_dir().expect("read_dir failed") {
        let replay = replay.expect("error on replay").path();

        let mut game_board = GameBoard::new(
            json::read_state_from_json_file(&replay.join(Path::new("A-init.json")))
                .expect("Failed to read initial state"),
        );
        let player_csv = read_file_lines(&replay.join(Path::new("A-log.csv")), 1);
        let opponent_csv = read_file_lines(&replay.join(Path::new("B-log.csv")), 1);

        for round in 0..player_csv.len() {
            println!("Testing round {}", round);

            let player = split_csv(&player_csv[round]);
            let opponent = split_csv(&opponent_csv[round]);

            assert_eq!(
                round + 1,
                player[0]
                    .parse::<usize>()
                    .expect(&format!("Invalid player input on round {}", round))
            );
            assert_eq!(
                round + 1,
                opponent[0]
                    .parse::<usize>()
                    .expect(&format!("Invalid opponent input on round {}", round))
            );

            if round != 0 {
                let player_move = read_move(
                    &player,
                    game_board.players[0].worms[game_board.players[0].active_worm].id,
                );
                let opponent_move = read_move(
                    &opponent,
                    game_board.players[1].worms[game_board.players[1].active_worm].id,
                );
                let _ = game_board.simulate([player_move, opponent_move]);
                if player[1] == "invalid" {
                    game_board.players[0].moves_score -= INVALID_COMMAND_SCORE_PENALTY;
                }
                if opponent[1] == "invalid" {
                    game_board.players[1].moves_score -= INVALID_COMMAND_SCORE_PENALTY;
                }
            }

            for player_index in 0..2 {
                let csv_row = match player_index {
                    0 => &player,
                    _ => &opponent,
                };
                assert_eq!(
                    csv_row[4].parse::<i32>().unwrap(),
                    game_board.players[player_index].score(),
                    "Score is incorrect for player {}, Row: {:?}",
                    player_index,
                    csv_row
                );
                for worm_index in 0..3 {
                    let worm_id = worm_index as i32 + 1;

                    match game_board.players[player_index].find_worm(worm_id) {
                        Some(worm) => {
                            assert_eq!(
                                csv_row[6 + worm_index * 3].parse::<i32>().unwrap(),
                                worm.health,
                                "Worm health is incorrect for worm {} on player {}, Row: {:?}",
                                worm_id,
                                player_index,
                                csv_row
                            );
                            assert_eq!(
                                csv_row[7 + worm_index * 3].parse::<i8>().unwrap(),
                                worm.position.x,
                                "Worm x is incorrect for worm {} on player {}, Row: {:?}",
                                worm_id,
                                player_index,
                                csv_row
                            );
                            assert_eq!(
                                csv_row[8 + worm_index * 3].parse::<i8>().unwrap(),
                                worm.position.y,
                                "Worm y is incorrect for worm {} on player {}, Row: {:?}",
                                worm_id,
                                player_index,
                                csv_row
                            );
                        }
                        None => {
                            // If the worms don't appear in my state, they should be dead
                            assert!(
                                csv_row[6 + worm_index * 3].parse::<i32>().unwrap() <= 0,
                                "Worm is not actually dead"
                            );
                        }
                    }
                }
            }
        }
    }
}

fn read_file_lines(path: &Path, skip: usize) -> Vec<String> {
    let mut file = File::open(path).unwrap();
    let mut contents = String::new();
    file.read_to_string(&mut contents).unwrap();
    contents
        .split("\n")
        .skip(skip)
        .map(String::from)
        .filter(|s| !s.is_empty())
        .collect()
}

fn split_csv(input: &str) -> Vec<String> {
    let mut result = Vec::new();
    let mut next = Vec::new();
    let mut quoted = false;

    for c in input.chars() {
        match c {
            '"' => {
                quoted = !quoted;
            }
            ',' if !quoted => {
                result.push(next.iter().collect());
                next = Vec::new();
            }
            c => {
                next.push(c);
            }
        }
    }
    result.push(next.iter().collect());
    result
}

fn read_move(csv_line: &[String], expected_worm_id: i32) -> Command {
    let worm_id = csv_line[3].parse::<i32>().unwrap();
    let select = if worm_id == expected_worm_id {
        None
    } else {
        Some(worm_id)
    };
    match csv_line[1].as_ref() {
        "move" => {
            let (x, y) = read_xy_pair(&csv_line[2]);
            Command {
                worm: select,
                action: Action::Move(Point2d::new(x, y)),
            }
        }
        "dig" => {
            let (x, y) = read_xy_pair(&csv_line[2]);
            Command {
                worm: select,
                action: Action::Dig(Point2d::new(x, y)),
            }
        }
        "banana" => {
            let (x, y) = read_xy_pair(&csv_line[2]);
            Command {
                worm: select,
                action: Action::Bomb(Point2d::new(x, y)),
            }
        }
        "snowball" => {
            let (x, y) = read_xy_pair(&csv_line[2]);
            Command {
                worm: select,
                action: Action::Snowball(Point2d::new(x, y)),
            }
        }
        "nothing" | "invalid" => Command {
            worm: select,
            action: Action::DoNothing,
        },
        "shoot" => {
            use steam_powered_wyrm::geometry::Direction::*;

            let dir = match csv_line[2].as_ref() {
                "shoot N" => North,
                "shoot NE" => NorthEast,
                "shoot E" => East,
                "shoot SE" => SouthEast,
                "shoot S" => South,
                "shoot SW" => SouthWest,
                "shoot W" => West,
                "shoot NW" => NorthWest,
                _ => panic!("Unknown shoot direction: {}", csv_line[2]),
            };
            Command {
                worm: select,
                action: Action::Shoot(dir),
            }
        }
        x => {
            panic!("Unknown command {}", x);
        }
    }
}

fn read_xy_pair(input: &str) -> (i8, i8) {
    let mut char_iter = input.chars();
    let _ = char_iter
        .by_ref()
        .take_while(|c| *c != ' ')
        .collect::<String>();
    let x = char_iter
        .by_ref()
        .take_while(|c| *c != ' ')
        .collect::<String>()
        .trim()
        .parse::<i8>()
        .unwrap();
    let y = char_iter
        .by_ref()
        .take_while(|c| *c != ' ')
        .collect::<String>()
        .trim()
        .parse::<i8>()
        .unwrap();
    (x, y)
}