Refactorings and linter fixes

This commit is contained in:
Stephen Seo 2022-03-09 18:14:01 +09:00
parent 76e6d3be52
commit 7e9718a743
3 changed files with 21 additions and 22 deletions

View file

@ -43,8 +43,8 @@ pub fn check_win_draw(board: &BoardType) -> Option<(BoardState, WinType)> {
for x in 0..((COLS - 3) as usize) { for x in 0..((COLS - 3) as usize) {
let idx = x + y * (COLS as usize); let idx = x + y * (COLS as usize);
let result = check_result(has_right_horizontal_at_idx(idx, board)); let result = check_result(has_right_horizontal_at_idx(idx, board));
if result.is_some() { if let Some(result) = result {
return Some((result.unwrap(), WinType::Horizontal(idx))); return Some((result, WinType::Horizontal(idx)));
} }
} }
} }
@ -54,8 +54,8 @@ pub fn check_win_draw(board: &BoardType) -> Option<(BoardState, WinType)> {
for x in 0..(COLS as usize) { for x in 0..(COLS as usize) {
let idx = x + y * (COLS as usize); let idx = x + y * (COLS as usize);
let result = check_result(has_down_vertical_at_idx(idx, board)); let result = check_result(has_down_vertical_at_idx(idx, board));
if result.is_some() { if let Some(result) = result {
return Some((result.unwrap(), WinType::Vertical(idx))); return Some((result, WinType::Vertical(idx)));
} }
} }
} }
@ -65,8 +65,8 @@ pub fn check_win_draw(board: &BoardType) -> Option<(BoardState, WinType)> {
for x in 0..((COLS - 3) as usize) { for x in 0..((COLS - 3) as usize) {
let idx = x + y * (COLS as usize); let idx = x + y * (COLS as usize);
let result = check_result(has_right_up_diagonal_at_idx(idx, board)); let result = check_result(has_right_up_diagonal_at_idx(idx, board));
if result.is_some() { if let Some(result) = result {
return Some((result.unwrap(), WinType::DiagonalUp(idx))); return Some((result, WinType::DiagonalUp(idx)));
} }
} }
} }
@ -76,8 +76,8 @@ pub fn check_win_draw(board: &BoardType) -> Option<(BoardState, WinType)> {
for x in 0..((COLS - 3) as usize) { for x in 0..((COLS - 3) as usize) {
let idx = x + y * (COLS as usize); let idx = x + y * (COLS as usize);
let result = check_result(has_right_down_diagonal_at_idx(idx, board)); let result = check_result(has_right_down_diagonal_at_idx(idx, board));
if result.is_some() { if let Some(result) = result {
return Some((result.unwrap(), WinType::DiagonalDown(idx))); return Some((result, WinType::DiagonalDown(idx)));
} }
} }
} }

View file

@ -69,16 +69,16 @@ impl BoardState {
*self == BoardState::Empty *self == BoardState::Empty
} }
pub fn into_win(&self) -> Self { pub fn into_win(self) -> Self {
match *self { match self {
BoardState::Empty => BoardState::Empty, BoardState::Empty => BoardState::Empty,
BoardState::Cyan | BoardState::CyanWin => BoardState::CyanWin, BoardState::Cyan | BoardState::CyanWin => BoardState::CyanWin,
BoardState::Magenta | BoardState::MagentaWin => BoardState::MagentaWin, BoardState::Magenta | BoardState::MagentaWin => BoardState::MagentaWin,
} }
} }
pub fn from_win(&self) -> Self { pub fn from_win(self) -> Self {
match *self { match self {
BoardState::Empty => BoardState::Empty, BoardState::Empty => BoardState::Empty,
BoardState::Cyan | BoardState::CyanWin => BoardState::Cyan, BoardState::Cyan | BoardState::CyanWin => BoardState::Cyan,
BoardState::Magenta | BoardState::MagentaWin => BoardState::MagentaWin, BoardState::Magenta | BoardState::MagentaWin => BoardState::MagentaWin,

View file

@ -356,7 +356,7 @@ impl Component for Wrapper {
} }
let append_result = element_append_class( let append_result = element_append_class(
&document, &document,
&format!("slot{}", idx + 1 * (COLS as usize)), &format!("slot{}", idx + (COLS as usize)),
"win", "win",
); );
if let Err(e) = append_result { if let Err(e) = append_result {
@ -380,9 +380,8 @@ impl Component for Wrapper {
} }
shared.board[idx].replace(shared.board[idx].get().into_win()); shared.board[idx].replace(shared.board[idx].get().into_win());
shared.board[idx + 1 * (COLS as usize)].replace( shared.board[idx + (COLS as usize)]
shared.board[idx + 1 * (COLS as usize)].get().into_win(), .replace(shared.board[idx + (COLS as usize)].get().into_win());
);
shared.board[idx + 2 * (COLS as usize)].replace( shared.board[idx + 2 * (COLS as usize)].replace(
shared.board[idx + 2 * (COLS as usize)].get().into_win(), shared.board[idx + 2 * (COLS as usize)].get().into_win(),
); );
@ -398,7 +397,7 @@ impl Component for Wrapper {
} }
let append_result = element_append_class( let append_result = element_append_class(
&document, &document,
&format!("slot{}", idx + 1 - 1 * (COLS as usize)), &format!("slot{}", idx + 1 - (COLS as usize)),
"win", "win",
); );
if let Err(e) = append_result { if let Err(e) = append_result {
@ -422,8 +421,8 @@ impl Component for Wrapper {
} }
shared.board[idx].replace(shared.board[idx].get().into_win()); shared.board[idx].replace(shared.board[idx].get().into_win());
shared.board[idx + 1 - 1 * (COLS as usize)].replace( shared.board[idx + 1 - (COLS as usize)].replace(
shared.board[idx + 1 - 1 * (COLS as usize)].get().into_win(), shared.board[idx + 1 - (COLS as usize)].get().into_win(),
); );
shared.board[idx + 2 - 2 * (COLS as usize)].replace( shared.board[idx + 2 - 2 * (COLS as usize)].replace(
shared.board[idx + 2 - 2 * (COLS as usize)].get().into_win(), shared.board[idx + 2 - 2 * (COLS as usize)].get().into_win(),
@ -440,7 +439,7 @@ impl Component for Wrapper {
} }
let append_result = element_append_class( let append_result = element_append_class(
&document, &document,
&format!("slot{}", idx + 1 + 1 * (COLS as usize)), &format!("slot{}", idx + 1 + (COLS as usize)),
"win", "win",
); );
if let Err(e) = append_result { if let Err(e) = append_result {
@ -464,8 +463,8 @@ impl Component for Wrapper {
} }
shared.board[idx].replace(shared.board[idx].get().into_win()); shared.board[idx].replace(shared.board[idx].get().into_win());
shared.board[idx + 1 + 1 * (COLS as usize)].replace( shared.board[idx + 1 + (COLS as usize)].replace(
shared.board[idx + 1 + 1 * (COLS as usize)].get().into_win(), shared.board[idx + 1 + (COLS as usize)].get().into_win(),
); );
shared.board[idx + 2 + 2 * (COLS as usize)].replace( shared.board[idx + 2 + 2 * (COLS as usize)].replace(
shared.board[idx + 2 + 2 * (COLS as usize)].get().into_win(), shared.board[idx + 2 + 2 * (COLS as usize)].get().into_win(),