From bb208dfdebb7dead0e7d68e837c37972498c22d5 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 2 Jun 2018 13:09:13 +0200 Subject: Moved replay-based test to have convenience import from game engine replay --- tests/live-comparison.rs | 85 ++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 42 deletions(-) (limited to 'tests/live-comparison.rs') diff --git a/tests/live-comparison.rs b/tests/live-comparison.rs index e090907..e8f2b3a 100644 --- a/tests/live-comparison.rs +++ b/tests/live-comparison.rs @@ -3,52 +3,53 @@ extern crate zombot; use zombot::json; use zombot::engine::command::{Command, BuildingType}; use zombot::engine::geometry::Point; +use zombot::engine::settings::GameSettings; -#[test] -fn it_successfully_simulates_moves() { - let (settings, mut state) = json::read_state_from_file("tests/state0.json").expect("Failed to read state0.json"); +use std::fs::File; +use std::io::prelude::*; - let all_commands = [ - (Command::Build(Point::new(3,2),BuildingType::Energy), Command::Nothing), - (Command::Nothing, Command::Nothing), - (Command::Nothing, Command::Build(Point::new(4,3),BuildingType::Energy)), - (Command::Build(Point::new(3,1),BuildingType::Energy), Command::Nothing), - (Command::Nothing, Command::Nothing), - (Command::Build(Point::new(3,0),BuildingType::Energy),Command::Build(Point::new(6,0),BuildingType::Energy)), - (Command::Nothing,Command::Nothing), - (Command::Build(Point::new(3,3),BuildingType::Energy),Command::Build(Point::new(7,1),BuildingType::Attack)), - (Command::Nothing,Command::Nothing), - (Command::Build(Point::new(2,3),BuildingType::Attack),Command::Nothing), - - (Command::Build(Point::new(2,1),BuildingType::Energy),Command::Build(Point::new(5,3),BuildingType::Defence)), - (Command::Nothing,Command::Nothing), - (Command::Build(Point::new(1,0),BuildingType::Attack),Command::Nothing), - (Command::Nothing,Command::Build(Point::new(5,0),BuildingType::Defence)), - (Command::Build(Point::new(0,2),BuildingType::Attack),Command::Nothing), - (Command::Build(Point::new(3,1),BuildingType::Energy),Command::Nothing), - (Command::Nothing,Command::Nothing), - (Command::Build(Point::new(0,1),BuildingType::Attack),Command::Build(Point::new(7,2),BuildingType::Defence)), - (Command::Build(Point::new(2,1),BuildingType::Energy),Command::Nothing), - (Command::Nothing,Command::Nothing), +#[test] +fn it_successfully_simulates_replay() { + let replay_folder = "tests/after_112"; + let (settings, mut state) = json::read_state_from_file(&format!("{}/Round 000/state.json", replay_folder)).unwrap(); + + for i in 0..54 { + let player = read_player_command(&format!("{}/Round {:03}/PlayerCommand.txt", replay_folder, i)); + let opponent = read_opponent_command(&format!("{}/Round {:03}/OpponentCommand.txt", replay_folder, i), &settings); + let (_, mut expected_state) = json::read_state_from_file(&format!("{}/Round {:03}/state.json", replay_folder, i+1)).unwrap(); - (Command::Build(Point::new(0,0),BuildingType::Attack),Command::Nothing), - (Command::Build(Point::new(0,3),BuildingType::Attack),Command::Build(Point::new(4,1),BuildingType::Defence)), - (Command::Nothing,Command::Nothing), - (Command::Build(Point::new(1,3),BuildingType::Attack),Command::Nothing), - (Command::Build(Point::new(3,1),BuildingType::Energy),Command::Nothing), - (Command::Nothing,Command::Build(Point::new(6,1),BuildingType::Defence)), - (Command::Build(Point::new(2,2),BuildingType::Energy),Command::Nothing), - (Command::Build(Point::new(1,2),BuildingType::Energy),Command::Nothing), - (Command::Build(Point::new(3,1),BuildingType::Energy),Command::Build(Point::new(7,0),BuildingType::Defence)), - (Command::Build(Point::new(2,1),BuildingType::Energy),Command::Nothing) - ]; - - for (i, &(player, opponent)) in all_commands.iter().enumerate() { - let file = format!("tests/state{}.json", i+1); state.simulate_mut(&settings, player, opponent); - let (_, mut actual_state) = json::read_state_from_file(&file).unwrap(); state.sort(); - actual_state.sort(); - assert_eq!(state, actual_state, "\nFailed on state {}\n", i+1); + expected_state.sort(); + assert_eq!(state, expected_state, "\nFailed on state {}\n", i+1); + } +} + +fn read_player_command(filename: &str) -> Command { + let mut file = File::open(filename).unwrap(); + let mut content = String::new(); + file.read_to_string(&mut content).unwrap(); + if content.trim() == "No Command" { + Command::Nothing + } + else { + let mut components = content.split(','); + Command::Build( + Point::new(components.next().unwrap().trim().parse().unwrap(), + components.next().unwrap().trim().parse().unwrap() + ), + BuildingType::from_u8(components.next().unwrap().trim().parse().unwrap()).unwrap() + ) + } +} + +fn read_opponent_command(filename: &str, settings: &GameSettings) -> Command { + match read_player_command(filename) { + Command::Nothing => Command::Nothing, + Command::Build(p, b) => Command::Build(Point::new( + settings.size.x - p.x - 1, + p.y + ), b) } + } -- cgit v1.2.3