battleship_bot 1.1.5

The game of battleship and a few bot implementations
Documentation
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//! All the shoot functions
//! 
//! This module contains all the functions to shoot at the boats.
//! All functions take a 2d vector to see what shots have been taken and return a new position to fire.
//! If you want to implement your own place function, it has to take a [`ShotMap`] and a [`Pos`] and return a [`Pos`].
//! 
//! # Example
//! ```rust
//! use battleship_bot::*;
//! use rand::random;
//! 
//! fn shoot(_last_pos: Pos, shots: ShotMap) -> (Pos, bool) {
//!     let mut shot = random();
//!     while !valid_shot(shots, shot) {
//!         shot = random();
//!     }
//! 
//!     (shot, false)
//! }
//! 
//! let mut game = Battleship::new(
//!     place::random,
//!     place::random,
//! 
//!     shoot,
//!     shoot::random
//! );
//! 
//! println!("{} won", game.play_and_record_game().winner);
//! ```

use rand::seq::SliceRandom;

use crate::battleship::position::Pos;
use crate::player::destroy::{random_destroy, destroy};
use crate::player::utils::get_hits;
use crate::pos;
use crate::battleship::boat::{BOATS, Boat};
use crate::battleship::constants::{NUM_COLS, NUM_ROWS, ShotMap};

/// Check if pos is a valid position for a shot in shots
/// 
/// Checks if pos is in range of the board and the position isn't shot yet.
/// 
/// # Example
/// ```rust
/// use battleship_bot::*;
/// 
/// let mut shots = [[None; 10]; 10];
/// 
/// assert!(valid_shot(shots, pos!(0, 0)));
/// assert!(!valid_shot(shots, pos!(10, 0)));
/// assert!(!valid_shot(shots, pos!(10, 10)));
/// 
/// shots[3][5] = Some(Shot::Miss);
/// assert!(!valid_shot(shots, pos!(3, 5)));
/// ```
pub fn valid_shot(shots: ShotMap, pos: Pos) -> bool  {
    pos.x < NUM_COLS &&
    pos.y < NUM_ROWS &&
    shots[pos.x][pos.y].is_none()
}

fn random_find(shots: ShotMap) -> Pos {
    let mut shot = rand::random();

    while !valid_shot(shots, shot) {
        shot = rand::random();
    }

    shot
}

/// Shoots completely randomly
/// 
/// Shoot spaces that aren't shot randomly
/// 
/// # Example
/// 
/// ```rust
/// use battleship_bot::Battleship;
/// use battleship_bot::{shoot, place};
/// 
/// let mut battleship = Battleship::new(
///     place::random,
///     place::random,
/// 
///     shoot::random,
///     shoot::random
/// );
/// 
/// let (p1_wins, p2_wins) = battleship.play_games(1_000);
/// 
/// // This is true because the starting player has a small advantage
/// assert!(p1_wins > p2_wins);
/// ```
pub fn random(_: Pos, shots: ShotMap) -> (Pos, bool) {
    (
        random_find(shots),
        false
    )
}

/// Shoots completely randomly until a ship is hit, then focuses on it
/// 
/// Shoot spaces that aren't shot randomly.
/// Until it hits a ship, then shoot around it to find the rest.
/// 
/// # Example
/// 
/// ```rust
/// use battleship_bot::Battleship;
/// use battleship_bot::{shoot, place};
/// 
/// let mut battleship = Battleship::new(
///     place::random,
///     place::random,
/// 
///     shoot::random,
///     shoot::random_and_random_destroy
/// );
/// 
/// let (p1_wins, p2_wins) = battleship.play_games(1_000);
/// 
/// assert!(p2_wins > p1_wins);
/// ```
pub fn random_and_random_destroy(_: Pos, shots: ShotMap) -> (Pos, bool) {
    if let Some(pos) = random_destroy(shots) {
        (pos, false)
    } else {
        (random_find(shots), true)
    }
}

/// Shoots completely randomly until a ship is hit, then destroys it smarter than random_and_random_destroy
/// 
/// Shoot spaces that aren't shot randomly.
/// Until it hits a ship, then shoot around it to find the rest.
/// But also account for the fact that ships are a line, so don't shoot spaces to the sides.
/// 
/// # Example
/// 
/// ```rust
/// use battleship_bot::Battleship;
/// use battleship_bot::{shoot, place};
/// 
/// let mut battleship = Battleship::new(
///     place::random,
///     place::random,
/// 
///     shoot::random_and_random_destroy,
///     shoot::random_and_destroy
/// );
/// 
/// let (p1_wins, p2_wins) = battleship.play_games(1_000);
/// 
/// assert!(p2_wins > p1_wins);
/// ```
pub fn random_and_destroy(_: Pos, shots: ShotMap) -> (Pos, bool) {
    if let Some(pos) = destroy(shots) {
        (pos, false)
    } else {
        (random_find(shots), true)
    }
}

fn grid_find(shots: ShotMap, last_pos: Pos) -> Pos {
    if shots[0][0].is_none() {
        return pos!(0, 0)
    }

    let mut min_len = 6;

    for boat in BOATS {
        let hits = get_hits(shots);
        let hits = hits
            .iter()
            .filter(|shot| shot.0 == boat);
        let hits_len = hits.clone().count();

        let boat_len = boat.length();

        if hits_len < boat_len && boat_len < min_len {
            min_len = boat_len;
        }
    }

    let mut position = last_pos;

    let mut has_reset = false;
    while !valid_shot(shots, position) {
        position.x += min_len;
        
        if position.x >= NUM_COLS {
            position.y += 1;

            position.x %= NUM_COLS;
            if position.y % 2 == 1 && position.x == 0 {
                position.x += 1;
            } else if position.y % 2 == 0 && position.x == 1 {
                position.x -= 1;
            }
        }

        if position.y >= NUM_ROWS {
            if has_reset {
                position = random_find(shots);
            } else {
                position = pos!(0, 0);
                has_reset = true;
            }
        }
    }

    position
}

/// Shoots in a grid until it finds a ship and destroy it
/// 
/// Shoot in a grid pattern until it finds a ship, then destroy it.
/// 
/// # Example
/// 
/// ```rust
/// use battleship_bot::Battleship;
/// use battleship_bot::{shoot, place};
/// 
/// let mut battleship = Battleship::new(
///     place::random,
///     place::random,
/// 
///     shoot::random_and_destroy,
///     shoot::grid_and_destroy
/// );
/// 
/// let (p1_wins, p2_wins) = battleship.play_games(1_000);
/// 
/// assert!(p2_wins > p1_wins);
/// ```
pub fn grid_and_destroy(last_pos: Pos, shots: ShotMap) -> (Pos, bool) {
    if let Some(pos) = destroy(shots) {
        (pos, false)
    } else {
        (grid_find(shots, last_pos), true)
    }
}

fn update_heatmap(heatmap: &mut [[usize; NUM_ROWS]; NUM_COLS], shots: ShotMap, boat: Boat, horizontal: bool, pos: Pos) {
    let mut overlaps = false;

    if horizontal {
        for x_off in 0..boat.length() {
            if shots[pos.x + x_off][pos.y].is_some() {
                overlaps = true;
                break;
            }
        }
    } else {
        for y_off in 0..boat.length() {
            if shots[pos.x][pos.y + y_off].is_some() {
                overlaps = true;
                break;
            }
        }
    }

    if overlaps {
        return
    }

    if horizontal {
        for x_off in 0..boat.length() {
            heatmap[pos.x + x_off][pos.y] += 1;
        }
    } else {
        for y_off in 0..boat.length() {
            heatmap[pos.x][pos.y + y_off] += 1;
        }
    }
}

fn create_heatmap(shots: ShotMap) -> [[usize; NUM_ROWS]; NUM_COLS] {
    let mut heatmap = [[0; NUM_ROWS]; NUM_COLS];

    for boat in BOATS {
        for x in 0..=NUM_COLS - boat.length() {
            for y in 0..NUM_ROWS {
                update_heatmap(&mut heatmap, shots, boat, true, pos!(x, y));
            }
        }

        for x in 0..NUM_COLS {
            for y in 0..=NUM_ROWS - boat.length() {
                update_heatmap(&mut heatmap, shots, boat, false, pos!(x, y));
            }
        }
    }

    heatmap
}

fn heatmap_find(shots: ShotMap) -> Pos {
    let heatmap = create_heatmap(shots);

    let max = heatmap
        .iter().map(
            |row| row.iter().max().expect("No items in row")

        ).max().expect("No items in heatmap");

    let mut possible_positions = vec![];

    for (x, row) in heatmap.iter().enumerate() {
        for (y, heat) in row.iter().enumerate() {
            if heat == max {
                possible_positions.push(pos!(x, y));
            }
        }
    }

    let mut pos = *possible_positions
        .choose(&mut rand::thread_rng())
        .expect("Failed to choose random position");

    while !valid_shot(shots, pos) {
        pos = *possible_positions
            .choose(&mut rand::thread_rng())
            .expect("Failed to choose random position");
    }

    pos
}

/// Creates a heatmap for the boats and shoots the highest heat
/// 
/// Creates a heatmap using all possible positions the boats can be in, then shoots one of the highest ones.
/// 
/// # Example
/// 
/// ```rust
/// use battleship_bot::Battleship;
/// use battleship_bot::{shoot, place};
/// 
/// let mut battleship = Battleship::new(
///     place::random,
///     place::random,
/// 
///     shoot::grid_and_destroy,
///     shoot::heatmap_and_destroy
/// );
/// 
/// let (p1_wins, p2_wins) = battleship.play_games(1_000);
/// 
/// assert!(p2_wins > p1_wins);
/// ```
pub fn heatmap_and_destroy(_: Pos, shots: ShotMap) -> (Pos, bool) {
    if let Some(pos) = destroy(shots) {
        (pos, false)
    } else {
        (heatmap_find(shots), false)
    }
}

#[cfg(test)]
mod tests {
    use rand::Rng;

    use crate::Shot;

    use super::*;

    #[test]
    fn test_valid_shot() {
        let mut shots = [[None; 10]; 10];

        for x in 0..10 {
            for y in 0..10 {
                assert!(valid_shot(shots, pos!(x, y)));
            }
        }

        assert!(!valid_shot(shots, pos!(10, 0)));
        assert!(!valid_shot(shots, pos!(0, 10)));
        assert!(!valid_shot(shots, pos!(10, 10)));

        let mut rng = rand::thread_rng();

        for _ in 0..100 {
            let x = rng.gen_range(0..10);
            let y = rng.gen_range(0..10);

            if rand::random() {
                shots[x][y] = Some(Shot::Miss);
            } else {
                shots[x][y] = Some(Shot::Hit(Boat::Destroyer));
            }

            assert!(!valid_shot(shots, pos!(x, y)));
        }
    }

    #[test]
    fn test_random_and_random_destroy() {
        let mut shots = [[None; NUM_ROWS]; NUM_COLS];

        shots[1][1] = Some(Shot::Hit(Boat::Cruiser));
        shots[1][2] = Some(Shot::Hit(Boat::Cruiser));
        
        let possible = [
            pos!(1, 0),
            pos!(1, 3),
            pos!(0, 1),
            pos!(0, 2),
            pos!(2, 1),
            pos!(2, 2)
        ];

        for _ in 0..100 {
            let shot = random_and_random_destroy(pos!(0, 0), shots).0;

            assert!(possible.contains(&shot));
        }
    }

    #[test]
    fn test_random_and_destroy() {
        let mut shots = [[None; NUM_ROWS]; NUM_COLS];

        shots[1][1] = Some(Shot::Hit(Boat::Cruiser));
        shots[1][2] = Some(Shot::Hit(Boat::Cruiser));
        
        let possible = [
            pos!(1, 0),
            pos!(1, 3),
        ];

        for _ in 0..100 {
            let shot = random_and_destroy(pos!(0, 0), shots).0;

            assert!(possible.contains(&shot));
        }
    }

    #[test]
    fn test_grid_and_destroy() {
        let mut shots = [[None; NUM_ROWS]; NUM_COLS];

        assert!(grid_and_destroy(pos!(0, 0), shots).0 == pos!(0, 0));

        shots[0][0]= Some(Shot::Hit(Boat::Destroyer));

        let (shot, _) = grid_and_destroy(pos!(0, 0), shots);
        shots[shot.x][shot.y] = Some(Shot::Hit(Boat::Destroyer));

        assert!(grid_and_destroy(pos!(0, 0), shots).0 == pos!(3, 0));
    }

    #[test]
    fn test_grid_and_destroy2() {
        let mut shots = [[None; NUM_ROWS]; NUM_COLS];

        assert!(grid_and_destroy(pos!(0, 0), shots).0 == pos!(0, 0));
        shots[0][0] = Some(Shot::Miss);

        assert!(grid_and_destroy(pos!(0, 0), shots).0 == pos!(2, 0));
        shots[2][0] = Some(Shot::Miss);

        assert!(grid_and_destroy(pos!(0, 0), shots).0 == pos!(4, 0));
    }

    #[test]
    fn test_create_heatmap() {
        let mut shots = [[None; NUM_ROWS]; NUM_COLS];

        let heatmap = create_heatmap(shots);

        assert!(heatmap[0][0] == 10);
        assert!(heatmap[0][NUM_ROWS - 1] == 10);
        assert!(heatmap[NUM_COLS - 1][0] == 10);
        assert!(heatmap[NUM_COLS - 1][NUM_ROWS - 1] == 10);

        for col in heatmap.iter().take(NUM_COLS - 1).skip(1) {
            for heat in col.iter() {
                assert!(*heat > 10);
            }
        }

        assert!(heatmap[0][1] == heatmap[1][0]);

        shots[0][0] = Some(Shot::Miss);
        let heatmap = create_heatmap(shots);
        assert!(heatmap[0][1] == 10);

        shots[0][2] = Some(Shot::Miss);
        let heatmap = create_heatmap(shots);
        assert!(heatmap[0][1] == 5);
    }

    #[test]
    fn test_heatmap_and_destroy() {
        let mut shots = [[None; NUM_ROWS]; NUM_COLS];

        let possible = [
            pos!(4, 4),
            pos!(4, 5),
            pos!(5, 4),
            pos!(5, 5),
        ];

        assert!(possible.contains(&heatmap_and_destroy(pos!(0, 0), shots).0));

        shots[4][4] = Some(Shot::Miss);
        assert!(heatmap_and_destroy(pos!(0, 0), shots).0 == pos!(5, 5));
    }
}