diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 95a8bbe7..62d779e8 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -58,6 +58,7 @@ "safe_div.ml": 20, "shuffle.ml": 55, "subset_sum.ml": 8, + "tic_tac_toe.ml": 1, "word_freq.ml": 8, "zip_unzip.ml": 1000, "sieve.ml": 15, diff --git a/lib/ocaml/baseline/tic_tac_toe.ml b/lib/ocaml/baseline/tic_tac_toe.ml new file mode 100644 index 00000000..7eadef1f --- /dev/null +++ b/lib/ocaml/baseline/tic_tac_toe.ml @@ -0,0 +1,34 @@ +let check_row b r = + let a = b.(r * 3) in + if a <> 0 && a = b.(r * 3 + 1) && a = b.(r * 3 + 2) then a + else 0 + +let check_col b c = + let a = b.(c) in + if a <> 0 && a = b.(c + 3) && a = b.(c + 6) then a + else 0 + +let check_diag b = + let a = b.(0) in + if a <> 0 && a = b.(4) && a = b.(8) then a + else + let b' = b.(2) in + if b' <> 0 && b' = b.(4) && b' = b.(6) then b' + else 0 + +let winner b = + let r = ref 0 in + for i = 0 to 2 do + let cr = check_row b i in + if cr <> 0 then r := cr; + let cc = check_col b i in + if cc <> 0 then r := cc + done; + let cd = check_diag b in + if cd <> 0 then r := cd; + !r + +;; + +let b = Array.of_list [1;1;1; 0;2;0; 0;0;2] in +winner b diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 880975f1..cd5033fb 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,13 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-09 Phase 5.1 — tic_tac_toe.ml baseline (3x3 winner check, + X wins top row → 1). Board encoded as 9-element flat int array + with 0=empty, 1=X, 2=O. Three predicate functions check row, + column, and either diagonal; main `winner` loops over the 8 + winning lines. Tests Array.of_list with row-major indexing, + multi-fn collaboration, and structural equality on int values. + 66 baseline programs total. - 2026-05-09 Phase 5.1 — subset_sum.ml baseline (count subsets of [1..8] summing to 10 = 8). Pure recursion: at each element, take it or don't. Base case: target=0 → 1, target≠0 → 0. 2^8 = 256