summaryrefslogtreecommitdiff
path: root/src/ships.rs
blob: e21b20e485e4cc67b96e98b9b4ddd71b19ec016b (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
use std::fmt;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Ship {
    Battleship,
    Carrier,
    Cruiser,
    Destroyer,
    Submarine
}

impl fmt::Display for Ship {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Ship::*;
        
        f.write_str(
            match self {
                &Battleship => "Battleship",
                &Carrier => "Carrier",
                &Cruiser => "Cruiser",
                &Destroyer => "Destroyer",
                &Submarine => "Submarine"
            }
        )
    }
}

impl Ship {
    pub fn length(&self) -> u16 {
        use Ship::*;
        
        match self {
            &Battleship => 4,
            &Carrier => 5,
            &Cruiser => 3,
            &Destroyer => 2,
            &Submarine => 3
        }
    }
}