summaryrefslogtreecommitdiff
path: root/2017-battleships/src/placement.rs
blob: 4740d7610a497fe1cea5a1d8491e4176d011a3a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use actions::*;
use math::*;
use ships::*;

pub fn place_ships_randomly(map_size: u16) -> Action {
    let mut current_placement: Vec<ShipPlacement>;
    
    while {
        current_placement = create_random_placement(map_size);
        !ShipPlacement::valid_placements(&current_placement, map_size)
    } {}
    Action::PlaceShips(current_placement)
}

fn create_random_placement(map_size: u16) -> Vec<ShipPlacement> {
    vec!(
        ShipPlacement::new(Ship::Battleship, Point::random(map_size), Direction::random()),
        ShipPlacement::new(Ship::Carrier, Point::random(map_size), Direction::random()),
        ShipPlacement::new(Ship::Cruiser, Point::random(map_size), Direction::random()),
        ShipPlacement::new(Ship::Destroyer, Point::random(map_size), Direction::random()),
        ShipPlacement::new(Ship::Submarine, Point::random(map_size), Direction::random())
    )
}