···
2
+
fmt::{Display, Formatter},
3
+
ops::{Index, IndexMut},
6
+
#[derive(Debug, PartialEq, Eq)]
7
+
pub struct Vector2u16 {
13
+
pub fn new(x: u16, y: u16) -> Self {
18
+
#[derive(Debug, PartialEq, Eq)]
20
+
pub first_pos: Vector2u16,
21
+
pub last_pos: Vector2u16,
25
+
pub fn new(first_pos: Vector2u16, last_pos: Vector2u16) -> Self {
33
+
#[derive(Debug, PartialEq, Eq)]
35
+
pub cells: [BoardCell; 64],
38
+
impl Display for Board {
39
+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
40
+
writeln!(f, " | A B C D E F G H ")?;
41
+
writeln!(f, "---+-----------------")?;
46
+
" {row} | {} {} {} {} {} {} {} {}",
47
+
self.cells[8 * row - 1],
48
+
self.cells[8 * row],
49
+
self.cells[8 * row + 1],
50
+
self.cells[8 * row + 2],
51
+
self.cells[8 * row + 3],
52
+
self.cells[8 * row + 4],
53
+
self.cells[8 * row + 5],
54
+
self.cells[8 * row + 6],
62
+
impl Default for Board {
63
+
fn default() -> Self {
65
+
cells: [Default::default(); 64],
70
+
impl Index<Vector2u16> for Board {
71
+
type Output = BoardCell;
73
+
fn index(&self, index: Vector2u16) -> &Self::Output {
74
+
&self.cells[index.y as usize * 8 + index.x as usize]
78
+
impl IndexMut<Vector2u16> for Board {
79
+
fn index_mut(&mut self, index: Vector2u16) -> &mut Self::Output {
80
+
&mut self.cells[index.y as usize * 8 + index.x as usize]
85
+
fn new(boats: impl IntoIterator<Item = Boat>) -> Self {
86
+
let mut board = Self::default();
88
+
for (i, boat) in boats.into_iter().enumerate() {
89
+
let start_x = boat.first_pos.x.min(boat.last_pos.x);
90
+
let start_y = boat.first_pos.y.min(boat.last_pos.y);
91
+
let end_x = boat.first_pos.x.max(boat.last_pos.x);
92
+
let end_y = boat.first_pos.y.max(boat.last_pos.y);
94
+
for x in start_x..=end_x {
95
+
for y in start_y..=end_y {
96
+
board[Vector2u16 { x, y }] = BoardCell::Boat(i as u8);
105
+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
106
+
pub enum BoardCell {
114
+
impl Display for BoardCell {
115
+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
117
+
BoardCell::Nothing => write!(f, "."),
118
+
BoardCell::Missed => write!(f, "o"),
119
+
BoardCell::Hit => write!(f, "x"),
120
+
BoardCell::Boat(n) => write!(f, "{n}"),
134
+
cells: [Default::default(); 64],
140
+
fn init_boards_with_boats() {
141
+
let board = Board::new([
142
+
Boat::new(Vector2u16 { x: 2, y: 0 }, Vector2u16 { x: 2, y: 1 }),
143
+
Boat::new(Vector2u16 { x: 3, y: 3 }, Vector2u16 { x: 5, y: 3 }),
144
+
Boat::new(Vector2u16 { x: 1, y: 4 }, Vector2u16 { x: 1, y: 7 }),
145
+
Boat::new(Vector2u16 { x: 3, y: 6 }, Vector2u16 { x: 7, y: 6 }),
148
+
let n = BoardCell::Nothing;
149
+
let a = BoardCell::Boat(0);
150
+
let b = BoardCell::Boat(1);
151
+
let c = BoardCell::Boat(2);
152
+
let d = BoardCell::Boat(3);
158
+
n, n, a, n, n, n, n, n, //
159
+
n, n, a, n, n, n, n, n, //
160
+
n, n, n, n, n, n, n, n, //
161
+
n, n, n, b, b, b, n, n, //
162
+
n, c, n, n, n, n, n, n, //
163
+
n, c, n, n, n, n, n, n, //
164
+
n, c, n, d, d, d, d, d, //
165
+
n, c, n, n, n, n, n, n,