diff --git a/src/apps/ttt.html b/src/apps/ttt.html index 6e05c56..f49d4f7 100644 --- a/src/apps/ttt.html +++ b/src/apps/ttt.html @@ -27,18 +27,7 @@

TicTacToe

- -
- -
+

+
diff --git a/src/apps/ttt.js b/src/apps/ttt.js index 1575d95..f35c03f 100644 --- a/src/apps/ttt.js +++ b/src/apps/ttt.js @@ -2,21 +2,29 @@ if (team != 0 && team != 1) { throw "team is not 0 or 1! team=" + team; } -let teamName = "blue"; -let color = "dodgerblue"; -let otherColor = "firebrick"; -let port = "8080"; -if (team === 1) { - teamName = "red"; - color = "firebrick"; - otherColor = "dodgerblue"; - port = "8081"; -} +const teams = [ + { + name: "blue", + color: "dodgerblue", + port: "8080", + }, + { + name: "red", + color: "firebrick", + port: "8081", + }, +]; document.getElementById("team").innerHTML = - 'Team : ' + teamName + ""; + 'Team : ' + + teams[team].name + + ""; -const ws = new WebSocket("ws://192.254.0.2:" + port + "/" + teamName); +const ws = new WebSocket( + "ws://192.254.0.2:" + teams[team].port + "/" + teams[team].name, +); ws.onmessage = (event) => { console.log(event.data); @@ -53,5 +61,19 @@ ws.onmessage = (event) => { cells.push(cell); } document.getElementById("grid").replaceChildren(...cells); + if (msg.turn == null) { + if (msg.winner == null) { + document.getElementById("winner").innerHTML = "Draw!"; + } else { + document.getElementById("winner").innerHTML = + 'Winner : ' + + teams[msg.winner].name + + ""; + } + } else { + document.getElementById("winner").innerHTML = ""; + } } }; diff --git a/src/apps/ttt.rs b/src/apps/ttt.rs index d88f2ab..074fd31 100644 --- a/src/apps/ttt.rs +++ b/src/apps/ttt.rs @@ -49,6 +49,11 @@ impl Game { } } } + if self.board.iter().all(|c| c.is_some()) { + self.winner = None; + self.turn = None; + return true; + } false } } @@ -169,7 +174,7 @@ impl App for TttApp { if self.end.map(|e| e.elapsed()).unwrap_or_default() > Duration::from_secs(5) { self.end = None; *game = Game { - turn: Some(!unwrap_opt(game.winner).await), + turn: Some(!game.winner.unwrap_or_default()), ..Game::default() }; } @@ -208,8 +213,9 @@ impl App for TttApp { } } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] pub enum Team { + #[default] Zero = 0, One = 1, }