commit 0cc22e2526f58d1399c8963f00f0a4df00171433
parent aab172c73b463fd843910fe2bc5c5044e1438659
Author: thing1 <thing1@seacrossedlovers.xyz>
Date: Wed, 21 Jan 2026 00:35:03 +0000
almost got it working
Diffstat:
2 files changed, 75 insertions(+), 18 deletions(-)
diff --git a/chess/chess.ha b/chess/chess.ha
@@ -3,6 +3,7 @@ use fmt;
use ascii;
export type invalidplace = !void;
+export type usedplace = !void;
export type invalidmove = !void;
export type empty = !void;
@@ -24,7 +25,8 @@ export type piece = struct {
ty: ptype,
team: color,
valid: movefn,
- moved: bool
+ moved: bool,
+ dead: bool
};
export type board = struct {
@@ -44,6 +46,8 @@ export fn finish(b: *board) void = {
export fn mkpiece(b: *board, x: i64, y: i64, ty: ptype, team: color) (*piece | !invalidplace) = {
if (x < 0 || x >= b.w || y < 0 || y >= b.h) return invalidplace;
+ if (getpiece(b, x, y) is *piece) return usedplace;
+
let valid = switch(ty) {
case ptype::KING => yield &kingmove;
case ptype::QUEEN => yield &queenmove;
@@ -53,14 +57,14 @@ export fn mkpiece(b: *board, x: i64, y: i64, ty: ptype, team: color) (*piece | !
case ptype::PAWN => yield &pawnmove;
};
- append(b.pieces, piece{x = x, y = y, ty = ty, team = team, valid = valid, moved = false})!;
+ append(b.pieces, piece{x = x, y = y, ty = ty, team = team, valid = valid, moved = false, dead = false})!;
return &b.pieces[len(b.pieces) - 1];
};
export fn getpiece(b: *board, x: i64, y: i64) (*piece | !empty | !invalidplace) = {
if (x < 0 || x >= b.w || y < 0 || y >= b.h) return invalidplace;
for (let p &.. b.pieces)
- if (p.x == x && p.y == y)
+ if (p.x == x && p.y == y && !p.dead)
return p;
return empty;
};
@@ -68,6 +72,12 @@ export fn getpiece(b: *board, x: i64, y: i64) (*piece | !empty | !invalidplace)
export fn movepiece(b: *board, x1: i64, y1: i64, x2: i64, y2: i64) (void | !empty | !invalidmove | !invalidplace) = {
let p = getpiece(b, x1, y1)?;
if (!p.valid(*p, *b, x2, y2)) return invalidmove;
+
+ match (getpiece(b, x2, y2)) {
+ case let p: *piece => p.dead = true;
+ case => yield;
+ };
+
p.x = x2;
p.y = y2;
p.moved = true;
diff --git a/cmd/betterchess/main.ha b/cmd/betterchess/main.ha
@@ -1,28 +1,75 @@
use fmt;
use os;
use io;
+use bufio;
+use strings;
+use strconv;
use chess;
-export fn main() void = {
+type move = struct {
+ x1: i64,
+ y1: i64,
+ x2: i64,
+ y2: i64
+};
+
+fn setup() chess::board = {
let b = chess::mkboard(8, 8);
+ chess::mkpiece(&b, 0, 0, chess::ptype::ROOK, chess::color::WHITE)!;
+ chess::mkpiece(&b, 1, 0, chess::ptype::KNIGHT, chess::color::WHITE)!;
+ chess::mkpiece(&b, 2, 0, chess::ptype::BISHOP, chess::color::WHITE)!;
+ chess::mkpiece(&b, 3, 0, chess::ptype::QUEEN, chess::color::WHITE)!;
+ chess::mkpiece(&b, 4, 0, chess::ptype::KING, chess::color::WHITE)!;
+ chess::mkpiece(&b, 5, 0, chess::ptype::BISHOP, chess::color::WHITE)!;
+ chess::mkpiece(&b, 6, 0, chess::ptype::KNIGHT, chess::color::WHITE)!;
+ chess::mkpiece(&b, 7, 0, chess::ptype::ROOK, chess::color::WHITE)!;
- let p = match (chess::mkpiece(&b, 3, 6, chess::ptype::PAWN, chess::color::BLACK)) {
- case let p: *chess::piece => yield p;
- case => fmt::fatal("invalid placement");
- };
- let p = match (chess::mkpiece(&b, 2, 5, chess::ptype::PAWN, chess::color::WHITE)) {
- case let p: *chess::piece => yield p;
- case => fmt::fatal("invalid placement");
- };
+ for (let i: i64 = 0; i < 8; i += 1)
+ chess::mkpiece(&b, i, 1, chess::ptype::PAWN, chess::color::WHITE)!;
+
+ chess::mkpiece(&b, 0, 7, chess::ptype::ROOK, chess::color::BLACK)!;
+ chess::mkpiece(&b, 1, 7, chess::ptype::KNIGHT, chess::color::BLACK)!;
+ chess::mkpiece(&b, 2, 7, chess::ptype::BISHOP, chess::color::BLACK)!;
+ chess::mkpiece(&b, 3, 7, chess::ptype::QUEEN, chess::color::BLACK)!;
+ chess::mkpiece(&b, 4, 7, chess::ptype::KING, chess::color::BLACK)!;
+ chess::mkpiece(&b, 5, 7, chess::ptype::BISHOP, chess::color::BLACK)!;
+ chess::mkpiece(&b, 6, 7, chess::ptype::KNIGHT, chess::color::BLACK)!;
+ chess::mkpiece(&b, 7, 7, chess::ptype::ROOK, chess::color::BLACK)!;
+
+ for (let i: i64 = 0; i < 8; i += 1)
+ chess::mkpiece(&b, i, 6, chess::ptype::PAWN, chess::color::BLACK)!;
+
+ return b;
+};
+fn read_num() i64 = {
+ let word = strings::fromutf8(bufio::read_tok(os::stdin, ' ', '\n') as []u8)!;
+ defer free(word);
+ return strconv::stoi64(word)!;
- match (chess::movepiece(&b, 3, 6, 3, 4)) {
- case chess::empty => fmt::println("empty space")!;
- case chess::invalidmove => fmt::println("invalid move")!;
- case chess::invalidplace => fmt::println("invalid place")!;
- case => fmt::println("valid move")!;
+};
+
+fn parseinput() move = {
+ return move{
+ x1 = read_num(),
+ y1 = read_num(),
+ x2 = read_num(),
+ y2 = read_num()
};
+};
+
+export fn main() void = {
+ let b = setup();
+ let iswhite = true;
+
+ for (true) {
+ fmt::printf("{}'s move:\n", if (iswhite) "white" else "black")!;
+ let m = parseinput();
- chess::print_board(b, os::stdout);
+ chess::movepiece(&b, m.x1, m.y1, m.x2, m.y2)!;
+
+ iswhite = !iswhite;
+ chess::print_board(b, os::stdout);
+ };
};