fix syntax

This commit is contained in:
2026-02-17 09:15:15 -06:00
parent f310c18b84
commit 4e1b63fd0e
52 changed files with 2169 additions and 1754 deletions

View File

@@ -28,26 +28,21 @@ var isMyTurn = false;
function updateTitle() {
var title = "Misty Chess - ";
switch(gameState) {
case 'waiting':
title += "Press S to start server or J to join";
break;
case 'searching':
title += "Searching for server...";
break;
case 'server_waiting':
title += "Waiting for player to join...";
break;
case 'connected':
if (myColor) {
title += (mover.turn == myColor ? "Your turn (" + myColor + ")" : "Opponent's turn (" + mover.turn + ")");
} else {
title += mover.turn + " turn";
}
break;
if (gameState == 'waiting') {
title += "Press S to start server or J to join";
} else if (gameState == 'searching') {
title += "Searching for server...";
} else if (gameState == 'server_waiting') {
title += "Waiting for player to join...";
} else if (gameState == 'connected') {
if (myColor) {
title += (mover.turn == myColor ? "Your turn (" + myColor + ")" : "Opponent's turn (" + mover.turn + ")");
} else {
title += mover.turn + " turn";
}
}
log.console(title)
}
@@ -179,13 +174,19 @@ var opponentMouseColor = [1.0, 0.0, 0.0, 1.0]; // Red for opponent mouse
/* ── draw one 8×8 chess board ──────────────────────────────────── */
function drawBoard() {
for (var y = 0; y < 8; ++y)
for (var x = 0; x < 8; ++x) {
var isMyHover = hoverPos && hoverPos[0] == x && hoverPos[1] == y;
var isOpponentHover = opponentMousePos && opponentMousePos[0] == x && opponentMousePos[1] == y;
var isValidMove = selectPos && holdingPiece && isValidMoveForTurn(selectPos, [x, y]);
var color = ((x+y)&1) ? dark : light;
var y = 0;
var x = 0;
var isMyHover = null;
var isOpponentHover = null;
var isValidMove = null;
var color = null;
for (y = 0; y < 8; ++y)
for (x = 0; x < 8; ++x) {
isMyHover = hoverPos && hoverPos[0] == x && hoverPos[1] == y;
isOpponentHover = opponentMousePos && opponentMousePos[0] == x && opponentMousePos[1] == y;
isValidMove = selectPos && holdingPiece && isValidMoveForTurn(selectPos, [x, y]);
color = ((x+y)&1) ? dark : light;
if (isValidMove) {
color = allowedColor; // Gold for allowed moves
@@ -220,47 +221,51 @@ function isValidMoveForTurn(from, to) {
/* ── draw every live piece ─────────────────────────────────────── */
function drawPieces() {
grid.each(function (piece) {
if (piece.captured) return;
var piece = null;
var r = null;
var opponentPiece = null;
grid.each(function (p) {
if (p.captured) return;
// Skip drawing the piece being held (by me or opponent)
if (holdingPiece && selectPos &&
piece.coord[0] == selectPos[0] &&
piece.coord[1] == selectPos[1]) {
return;
}
// Skip drawing the piece being held by opponent
if (opponentHoldingPiece && opponentSelectPos &&
piece.coord[0] == opponentSelectPos[0] &&
piece.coord[1] == opponentSelectPos[1]) {
if (holdingPiece && selectPos &&
p.coord[0] == selectPos[0] &&
p.coord[1] == selectPos[1]) {
return;
}
var r = { x: piece.coord[0]*S, y: piece.coord[1]*S,
// Skip drawing the piece being held by opponent
if (opponentHoldingPiece && opponentSelectPos &&
p.coord[0] == opponentSelectPos[0] &&
p.coord[1] == opponentSelectPos[1]) {
return;
}
var pr = { x: p.coord[0]*S, y: p.coord[1]*S,
width:S, height:S };
draw2d.image(piece.sprite, r);
draw2d.image(p.sprite, pr);
});
// Draw the held piece at the mouse position if we're holding one
if (holdingPiece && selectPos && hoverPos) {
var piece = grid.at(selectPos)[0];
piece = grid.at(selectPos)[0];
if (piece) {
var r = { x: hoverPos[0]*S, y: hoverPos[1]*S,
r = { x: hoverPos[0]*S, y: hoverPos[1]*S,
width:S, height:S };
draw2d.image(piece.sprite, r);
}
}
// Draw opponent's held piece if they're dragging one
if (opponentHoldingPiece && opponentSelectPos && opponentMousePos) {
var opponentPiece = grid.at(opponentSelectPos)[0];
opponentPiece = grid.at(opponentSelectPos)[0];
if (opponentPiece) {
var r = { x: opponentMousePos[0]*S, y: opponentMousePos[1]*S,
r = { x: opponentMousePos[0]*S, y: opponentMousePos[1]*S,
width:S, height:S };
// Draw with slight transparency to show it's the opponent's piece
draw2d.image(opponentPiece.sprite, r);
}
@@ -325,13 +330,16 @@ function joinServer() {
}
$receiver(e => {
var fromCell = null;
var piece = null;
if (e.kind == 'update')
send(e, update(e.dt))
else if (e.kind == 'draw')
send(e, draw())
else if (e.type == 'game_start' || e.type == 'move' || e.type == 'greet')
log.console("Receiver got message:", e.type, e);
if (e.type == 'greet') {
log.console("Server received greet from client");
// Store the client's actor object for ongoing communication
@@ -339,7 +347,7 @@ $receiver(e => {
log.console("Stored client actor:", opponent);
gameState = 'connected';
updateTitle();
// Send game_start to the client
log.console("Sending game_start to client");
send(opponent, {
@@ -357,9 +365,9 @@ $receiver(e => {
} else if (e.type == 'move') {
log.console("Received move from opponent:", e.from, "to", e.to);
// Apply opponent's move
var fromCell = grid.at(e.from);
fromCell = grid.at(e.from);
if (length(fromCell)) {
var piece = fromCell[0];
piece = fromCell[0];
if (mover.tryMove(piece, e.to)) {
isMyTurn = true; // It's now our turn
updateTitle();

View File

@@ -2,11 +2,13 @@ function grid(w, h) {
var newgrid = meme(grid_prototype)
newgrid.width = w;
newgrid.height = h;
// create a height×width array of empty lists
// create a height*width array of empty lists
newgrid.cells = array(h);
for (var y = 0; y < h; y++) {
var y = 0;
var x = 0;
for (y = 0; y < h; y++) {
newgrid.cells[y] = array(w);
for (var x = 0; x < w; x++) {
for (x = 0; x < w; x++) {
newgrid.cells[y][x] = []; // each cell holds its own list
}
}
@@ -45,9 +47,12 @@ var grid_prototype = {
// call fn(entity, coord) for every entity in every cell
each(fn) {
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
def list = this.cells[y][x]
var list = null;
var y = 0;
var x = 0;
for (y = 0; y < this.height; y++) {
for (x = 0; x < this.width; x++) {
list = this.cells[y][x]
arrfor(list, function(entity) {
fn(entity, entity.coord);
})
@@ -57,9 +62,11 @@ var grid_prototype = {
// printable representation
toString() {
var out = `grid [${this.width}×${this.height}]\n`;
for (var y = 0; y < this.height; y++) {
for (var x = 0; x < this.width; x++) {
var out = `grid [${this.width}x${this.height}]\n`;
var y = 0;
var x = 0;
for (y = 0; y < this.height; y++) {
for (x = 0; x < this.width; x++) {
out += length(this.cells[y][x]);
}
if (y != this.height - 1) out += "\n";

View File

@@ -10,9 +10,9 @@ var MovementSystem_prototype = {
tryMove: function (piece, to) {
if (piece.colour != this.turn) return false;
// normalise to into our hybrid coord
var dest = [to.x ?? t[0],
to.y ?? to[1]];
// normalise 'to' into our hybrid coord
var dest = [!is_null(to.x) ? to.x : to[0],
!is_null(to.y) ? to.y : to[1]];
if (!this.grid.inBounds(dest)) return false;
if (!this.rules.canMove(piece, piece.coord, dest, this.grid)) return false;

View File

@@ -10,7 +10,7 @@ function Piece(kind, colour) {
}
function startingPosition(grid) {
var W = 'white', B = 'black', x;
var W = 'white', B = 'black', x = 0;
// pawns
for (x = 0; x < 8; x++) {

View File

@@ -1,30 +1,30 @@
/* helper robust coord access */
function cx(c) { return c.x ?? c[0] }
function cy(c) { return c.y ?? c[1] }
/* helper -- robust coord access */
function cx(c) { return !is_null(c.x) ? c.x : c[0] }
function cy(c) { return !is_null(c.y) ? c.y : c[1] }
/* simple move-shape checks */
var deltas = {
pawn: function (pc, dx, dy, grid, to) {
pawn: function (pc, dx, dy, ctx) {
var dir = (pc.colour == 'white') ? -1 : 1;
var base = (pc.colour == 'white') ? 6 : 1;
var one = (dy == dir && dx == 0 && length(grid.at(to)) == 0);
var one = (dy == dir && dx == 0 && length(ctx.grid.at(ctx.to)) == 0);
var two = (dy == 2 * dir && dx == 0 && cy(pc.coord) == base &&
length(grid.at({ x: cx(pc.coord), y: cy(pc.coord)+dir })) == 0 &&
length(grid.at(to)) == 0);
var cap = (dy == dir && Math.abs(dx) == 1 && length(grid.at(to)));
length(ctx.grid.at({ x: cx(pc.coord), y: cy(pc.coord)+dir })) == 0 &&
length(ctx.grid.at(ctx.to)) == 0);
var cap = (dy == dir && abs(dx) == 1 && length(ctx.grid.at(ctx.to)));
return one || two || cap;
},
rook : function (pc, dx, dy) { return (dx == 0 || dy == 0); },
bishop: function (pc, dx, dy) { return Math.abs(dx) == Math.abs(dy); },
queen : function (pc, dx, dy) { return (dx == 0 || dy == 0 || Math.abs(dx) == Math.abs(dy)); },
knight: function (pc, dx, dy) { return (Math.abs(dx) == 1 && Math.abs(dy) == 2) ||
(Math.abs(dx) == 2 && Math.abs(dy) == 1); },
king : function (pc, dx, dy) { return Math.max(Math.abs(dx), Math.abs(dy)) == 1; }
bishop: function (pc, dx, dy) { return abs(dx) == abs(dy); },
queen : function (pc, dx, dy) { return (dx == 0 || dy == 0 || abs(dx) == abs(dy)); },
knight: function (pc, dx, dy) { return (abs(dx) == 1 && abs(dy) == 2) ||
(abs(dx) == 2 && abs(dy) == 1); },
king : function (pc, dx, dy) { return max(abs(dx), abs(dy)) == 1; }
};
function clearLine(from, to, grid) {
var dx = Math.sign(cx(to) - cx(from));
var dy = Math.sign(cy(to) - cy(from));
var dx = sign(cx(to) - cx(from));
var dy = sign(cy(to) - cy(from));
var x = cx(from) + dx, y = cy(from) + dy;
while (x != cx(to) || y != cy(to)) {
if (length(grid.at({ x: x, y: y }))) return false;
@@ -37,7 +37,7 @@ function canMove(piece, from, to, grid) {
var dx = cx(to) - cx(from);
var dy = cy(to) - cy(from);
var f = deltas[piece.kind];
if (!f || !f(piece, dx, dy, grid, to)) return false;
if (!f || !f(piece, dx, dy, {grid: grid, to: to})) return false;
if (piece.kind == 'knight') return true;
return clearLine(from, to, grid);
}