ttt/
board.rs

1// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2// * Copyright (c) 2022
3// *
4// * This project is dual-licensed under the MIT and Apache licenses.
5// *
6// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
7// ** APACHE 2.0 LICENSE
8// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
9// *
10// * Licensed under the Apache License, Version 2.0 (the "License");
11// * you may not use this file except in compliance with the License.
12// * You may obtain a copy of the License at
13// *
14// *     http://www.apache.org/licenses/LICENSE-2.0
15// *
16// * Unless required by applicable law or agreed to in writing, software
17// * distributed under the License is distributed on an "AS IS" BASIS,
18// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// * See the License for the specific language governing permissions and
20// * limitations under the License.
21// *
22// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
23// ** MIT LICENSE
24// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
25// *
26// * Permission is hereby granted, free of charge, to any person obtaining a copy
27// * of this software and associated documentation files (the "Software"), to deal
28// * in the Software without restriction, including without limitation the rights
29// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30// * copies of the Software, and to permit persons to whom the Software is
31// * furnished to do so, subject to the following conditions:
32// *
33// * The above copyright notice and this permission notice shall be included in all
34// * copies or substantial portions of the Software.
35// *
36// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
42// * SOFTWARE.
43// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
44
45use super::square::Square;
46
47#[derive(Debug, PartialEq, Eq)]
48
49pub struct Board {
50    squares: [[Square; 3]; 3],
51}
52
53#[allow(dead_code)]
54impl Board {
55    pub fn new() -> Board {
56        Board {
57            squares: [[Square::new(); 3]; 3],
58        }
59    }
60
61    fn get_row(&self, row: usize) -> [Square; 3] {
62        [
63            self.get_square(row, 0),
64            self.get_square(row, 1),
65            self.get_square(row, 2),
66        ]
67    }
68
69    fn get_column(&self, column: usize) -> [Square; 3] {
70        [
71            self.get_square(0, column),
72            self.get_square(1, column),
73            self.get_square(2, column),
74        ]
75    }
76
77    fn get_diagonal(&self, diagonal: usize) -> [Square; 3] {
78        match diagonal {
79            0 => [
80                self.get_square(0, 0),
81                self.get_square(1, 1),
82                self.get_square(2, 2),
83            ],
84            1 => [
85                self.get_square(0, 2),
86                self.get_square(1, 1),
87                self.get_square(2, 0),
88            ],
89            _ => panic!("Invalid diagonal"),
90        }
91    }
92
93    pub fn get_square(&self, row: usize, column: usize) -> Square {
94        self.squares[row][column]
95    }
96
97    pub fn print(&self) {
98        for row in 0..3 {
99            println!(
100                "{}|{}|{}",
101                self.get_square(row, 0),
102                self.get_square(row, 1),
103                self.get_square(row, 2)
104            );
105            if row < 2 {
106                println!("-----------");
107            }
108        }
109    }
110}
111
112impl Default for Board {
113    fn default() -> Self {
114        Self::new()
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn test_get_row() {
124        let board = Board::new();
125        assert_eq!(
126            board.get_row(0),
127            [Square::new(), Square::new(), Square::new()]
128        );
129        assert_eq!(
130            board.get_row(1),
131            [Square::new(), Square::new(), Square::new()]
132        );
133        assert_eq!(
134            board.get_row(2),
135            [Square::new(), Square::new(), Square::new()]
136        );
137    }
138
139    #[test]
140    fn test_get_column() {
141        let board = Board::new();
142        assert_eq!(
143            board.get_column(0),
144            [Square::new(), Square::new(), Square::new()]
145        );
146        assert_eq!(
147            board.get_column(1),
148            [Square::new(), Square::new(), Square::new()]
149        );
150        assert_eq!(
151            board.get_column(2),
152            [Square::new(), Square::new(), Square::new()]
153        );
154    }
155
156    #[test]
157    fn test_get_diagonal() {
158        let board = Board::new();
159        assert_eq!(
160            board.get_diagonal(0),
161            [Square::new(), Square::new(), Square::new()]
162        );
163        assert_eq!(
164            board.get_diagonal(1),
165            [Square::new(), Square::new(), Square::new()]
166        );
167    }
168}