ttt/
square.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_value::SquareValue;
46use std::fmt::{self, Display, Formatter};
47
48#[derive(Debug, PartialEq, Eq, Clone, Copy)]
49pub struct Square {
50    value: SquareValue,
51}
52
53impl Display for Square {
54    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
55        write!(f, "{}", self.value)
56    }
57}
58
59#[allow(dead_code)]
60impl Square {
61    pub fn new() -> Square {
62        Square {
63            value: SquareValue::Empty,
64        }
65    }
66
67    fn is_empty(&self) -> bool {
68        self.value == SquareValue::Empty
69    }
70
71    fn is_x(&self) -> bool {
72        self.value == SquareValue::X
73    }
74
75    fn is_o(&self) -> bool {
76        self.value == SquareValue::O
77    }
78
79    fn get_value(&self) -> SquareValue {
80        self.value
81    }
82
83    pub fn set_value(&mut self, value: &str) {
84        match value {
85            "X" => self.value = SquareValue::X,
86            "O" => self.value = SquareValue::O,
87            _ => self.value = SquareValue::Empty,
88        }
89    }
90
91    fn set_x(&mut self) {
92        self.value = SquareValue::X;
93    }
94
95    fn set_o(&mut self) {
96        self.value = SquareValue::O;
97    }
98
99    fn set_empty(&mut self) {
100        self.value = SquareValue::Empty;
101    }
102}
103
104impl Default for Square {
105    fn default() -> Self {
106        Square::new()
107    }
108}
109
110#[cfg(test)]
111mod tests {
112    use super::*;
113
114    #[test]
115    fn test_new() {
116        let square = Square::new();
117        assert_eq!(square.value, SquareValue::Empty);
118    }
119
120    #[test]
121    fn test_is_empty() {
122        let square = Square::new();
123        assert!(square.is_empty());
124    }
125
126    #[test]
127    fn test_is_x() {
128        let mut square = Square::new();
129        square.set_x();
130        assert!(square.is_x());
131    }
132
133    #[test]
134    fn test_is_o() {
135        let mut square = Square::new();
136        square.set_o();
137        assert!(square.is_o());
138    }
139
140    #[test]
141    fn test_get_value() {
142        let mut square = Square::new();
143        square.set_x();
144        assert_eq!(square.get_value(), SquareValue::X);
145    }
146
147    #[test]
148    fn test_set_value() {
149        let mut square = Square::new();
150        square.set_value("X");
151        assert_eq!(square.get_value(), SquareValue::X);
152    }
153
154    #[test]
155    fn test_set_x() {
156        let mut square = Square::new();
157        square.set_x();
158        assert_eq!(square.get_value(), SquareValue::X);
159    }
160
161    #[test]
162    fn test_set_o() {
163        let mut square = Square::new();
164        square.set_o();
165        assert_eq!(square.get_value(), SquareValue::O);
166    }
167
168    #[test]
169    fn test_set_empty() {
170        let mut square = Square::new();
171        square.set_x();
172        square.set_empty();
173        assert_eq!(square.get_value(), SquareValue::Empty);
174    }
175
176    #[test]
177    fn test_display() {
178        let mut square = Square::new();
179        square.set_x();
180        assert_eq!(format!("{}", square), " X ");
181    }
182
183    #[test]
184    fn test_default() {
185        let square = Square::default();
186        assert_eq!(square.value, SquareValue::Empty);
187    }
188
189    #[test]
190    fn test_eq() {
191        let square1 = Square::new();
192        let square2 = Square::new();
193        assert_eq!(square1, square2);
194    }
195}