summaryrefslogtreecommitdiff
path: root/2017-battleships/src/ships.rs
blob: 422f24ec82e3259a3394e33d2182a0753a4b78a3 (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
use std::fmt;
use std::str;

use math::*;

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub enum Weapon {
    SingleShot,
    DoubleShotVertical,
    DoubleShotHorizontal,
    CornerShot,
    CrossShotDiagonal,
    CrossShotHorizontal,
    SeekerMissle
}

impl fmt::Display for Weapon {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use Weapon::*;
        
        f.write_str(
            match self {
                &SingleShot => "1",
                &DoubleShotVertical => "2",
                &DoubleShotHorizontal => "3",
                &CornerShot => "4",
                &CrossShotDiagonal => "5",
                &CrossShotHorizontal => "6",
                &SeekerMissle => "7"
            }
        )
    }
}

impl Weapon {
    pub fn energy_per_round(map_size: u16) -> u16 {
        if map_size < 10 {
            2
        }
        else if map_size < 14 {
            3
        }
        else {
            4
        }
    }
    pub fn energy_cost(&self, map_size: u16) -> u16 {
        use Weapon::*;
        let epr = Weapon::energy_per_round(map_size);
        match self {
            &SingleShot => 1,
            &DoubleShotVertical | &DoubleShotHorizontal => 8*epr,
            &CornerShot => 10*epr,
            &CrossShotDiagonal => 12*epr,
            &CrossShotHorizontal => 14*epr,
            &SeekerMissle => 10*epr
        }
    }
    pub fn single_shot_rounds_to_ready(&self, current_energy: u16, map_size: u16) -> u16 {
        let single_shot_cost = Weapon::SingleShot.energy_cost(map_size);
        let energy_per_round = Weapon::energy_per_round(map_size) - single_shot_cost;
        let required_energy = self.energy_cost(map_size) - current_energy;
        //weird plus is to make the integer rounding up instead of down
        (required_energy + energy_per_round - 1) / energy_per_round
    }

    pub fn affected_cells(&self, target: Point, map_size: u16) -> Vec<Point> {
        use Weapon::*;

        let p = target;
        match self {
            &SingleShot => {
                vec!(Some(p))
            },
            &DoubleShotVertical => {
                vec!(
                    p.move_point(Direction::North, 1, map_size),
                    p.move_point(Direction::South, 1, map_size)
                )
            },
            &DoubleShotHorizontal => {
                vec!(
                    p.move_point(Direction::East, 1, map_size),
                    p.move_point(Direction::West, 1, map_size)
                )
            },
            &CornerShot => {
                vec!(
                    p.move_point(Direction::NorthEast, 1, map_size),
                    p.move_point(Direction::SouthEast, 1, map_size),
                    p.move_point(Direction::NorthWest, 1, map_size),
                    p.move_point(Direction::SouthWest, 1, map_size),
                )
            },
            &CrossShotDiagonal => {
                vec!(
                    p.move_point(Direction::NorthEast, 1, map_size),
                    p.move_point(Direction::SouthEast, 1, map_size),
                    p.move_point(Direction::NorthWest, 1, map_size),
                    p.move_point(Direction::SouthWest, 1, map_size),
                    Some(p)
                )
            },
            &CrossShotHorizontal => {
                vec!(
                    p.move_point(Direction::North, 1, map_size),
                    p.move_point(Direction::East, 1, map_size),
                    p.move_point(Direction::South, 1, map_size),
                    p.move_point(Direction::West, 1, map_size),
                    Some(p)
                )
            },
            &SeekerMissle => {
                vec!(
                    Some(p),
                    
                    p.move_point(Direction::North, 1, map_size),
                    p.move_point(Direction::East, 1, map_size),
                    p.move_point(Direction::South, 1, map_size),
                    p.move_point(Direction::West, 1, map_size),
                    
                    p.move_point(Direction::NorthEast, 1, map_size),
                    p.move_point(Direction::SouthEast, 1, map_size),
                    p.move_point(Direction::NorthWest, 1, map_size),
                    p.move_point(Direction::SouthWest, 1, map_size),

                    p.move_point(Direction::North, 2, map_size),
                    p.move_point(Direction::East, 2, map_size),
                    p.move_point(Direction::South, 2, map_size),
                    p.move_point(Direction::West, 2, map_size)
                )
            }
        }.iter().filter_map(|&p| p).collect::<Vec<_>>()
    }
}


#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
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 str::FromStr for Ship {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        use Ship::*;
        
        match s {
            "Battleship" => Ok(Battleship),
            "Carrier" => Ok(Carrier),
            "Cruiser" => Ok(Cruiser),
            "Destroyer" => Ok(Destroyer),
            "Submarine" => Ok(Submarine),
            _ => Err(String::from("ship type is not known"))
        }
    }
}

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

    pub fn weapons(&self) -> Vec<Weapon> {
        use Ship::*;
        use Weapon::*;
        
        match self {
            &Battleship => vec!(SingleShot, CrossShotDiagonal),
            &Carrier => vec!(SingleShot, CornerShot),
            &Cruiser => vec!(SingleShot, CrossShotHorizontal),
            &Destroyer => vec!(SingleShot, DoubleShotVertical, DoubleShotHorizontal),
            &Submarine => vec!(SingleShot, SeekerMissle)
        }
    }

    pub fn all_types() -> Vec<Ship> {
        use Ship::*;
        
        vec!(
            Battleship,
            Carrier,
            Cruiser,
            Destroyer,
            Submarine
        )
    }
}