ocaml: phase 5.1 tic_tac_toe.ml baseline (3x3 winner check, X wins top row = 1)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 47s
Board as 9-element flat int array, 0=empty, 1=X, 2=O. Three predicate functions: check_row b r check_col b c check_diag b each return the winning player's mark or 0. Main 'winner' loops i = 0..2 calling row(i)/col(i) then check_diag, threading via a result ref. Test board: X X X . O . . . O X wins on row 0 -> winner returns 1. Tests Array.of_list with row-major 'b.(r * 3 + c)' indexing, multi-fn collaboration, and structural equality on int values. 66 baseline programs total.
This commit is contained in:
@@ -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,
|
||||
|
||||
34
lib/ocaml/baseline/tic_tac_toe.ml
Normal file
34
lib/ocaml/baseline/tic_tac_toe.ml
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user