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>
<h1>TicTacToe</h1>
<h3 id="team"></h3>
<!-- <div class="cell" style="background-color:"></div> -->
<div id="grid">
<!-- <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>
<h3 id="winner"></h3>
<div id="grid"></div>
</body>
</html>

View File

@ -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 : <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) => {
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 : <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
}
}
@ -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,
}