This commit is contained in:
Arkitu 2025-08-09 15:09:49 +02:00
parent 1d7eaa028b
commit 23d03920ae
3 changed files with 44 additions and 27 deletions

View File

@ -27,18 +27,7 @@
<body> <body>
<h1>TicTacToe</h1> <h1>TicTacToe</h1>
<h3 id="team"></h3> <h3 id="team"></h3>
<!-- <div class="cell" style="background-color:"></div> --> <h3 id="winner"></h3>
<div id="grid"> <div id="grid"></div>
<!-- <div class="cell" id="cell0"></div>
<div class="cell" id="cell1"></div>
<div class="cell" id="cell2"></div>
<div class="cell" id="cell3"></div>
<div class="cell" id="cell4"></div>
<div class="cell" id="cell5"></div>
<div class="cell" id="cell6"></div>
<div class="cell" id="cell7"></div>
<div class="cell" id="cell8"></div>
<div class="cell" id="cell9"></div> -->
</div>
</body> </body>
</html> </html>

View File

@ -2,21 +2,29 @@
if (team != 0 && team != 1) { if (team != 0 && team != 1) {
throw "team is not 0 or 1! team=" + team; throw "team is not 0 or 1! team=" + team;
} }
let teamName = "blue"; const teams = [
let color = "dodgerblue"; {
let otherColor = "firebrick"; name: "blue",
let port = "8080"; color: "dodgerblue",
if (team === 1) { port: "8080",
teamName = "red"; },
color = "firebrick"; {
otherColor = "dodgerblue"; name: "red",
port = "8081"; color: "firebrick",
} port: "8081",
},
];
document.getElementById("team").innerHTML = document.getElementById("team").innerHTML =
'Team : <span style="color:' + color + '">' + teamName + "</span>"; 'Team : <span style="color:' +
teams[team].color +
'">' +
teams[team].name +
"</span>";
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) => { ws.onmessage = (event) => {
console.log(event.data); console.log(event.data);
@ -53,5 +61,19 @@ ws.onmessage = (event) => {
cells.push(cell); cells.push(cell);
} }
document.getElementById("grid").replaceChildren(...cells); document.getElementById("grid").replaceChildren(...cells);
if (msg.turn == null) {
if (msg.winner == null) {
document.getElementById("winner").innerHTML = "Draw!";
} else {
document.getElementById("winner").innerHTML =
'Winner : <span style="color:' +
teams[msg.winner].color +
'">' +
teams[msg.winner].name +
"</span>";
}
} else {
document.getElementById("winner").innerHTML = "";
}
} }
}; };

View File

@ -49,6 +49,11 @@ impl Game {
} }
} }
} }
if self.board.iter().all(|c| c.is_some()) {
self.winner = None;
self.turn = None;
return true;
}
false false
} }
} }
@ -169,7 +174,7 @@ impl App for TttApp {
if self.end.map(|e| e.elapsed()).unwrap_or_default() > Duration::from_secs(5) { if self.end.map(|e| e.elapsed()).unwrap_or_default() > Duration::from_secs(5) {
self.end = None; self.end = None;
*game = Game { *game = Game {
turn: Some(!unwrap_opt(game.winner).await), turn: Some(!game.winner.unwrap_or_default()),
..Game::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 { pub enum Team {
#[default]
Zero = 0, Zero = 0,
One = 1, One = 1,
} }