Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: [Caml-list] Mandelbrot renderer
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Simon <sk75@u...>
Subject: Re: [Caml-list] Mandelbrot renderer

> And as I said in another post, I really would like an OCaml sudoko GENERATOR
(not a solver) on my portable phone, I almost finished all the grids I have in
the demo of a java commercial program I found on getjar.com, so this now is
urgent :-)


This won't work on your phone, but it will generate a wide range of sudoku
puzzles, all of them will most likely be near to impossible. It works by
shuffling a sudoku puzzle I took from Tuesday's paper and coded in after I
removed a few unneeded numbers, then applying a permutation to the numbers. Not
the most complex, but it should produce a very large range of puzzles, although
probably not every single possible least-complex puzzle. Yes, it's long for its
simplicity, all in imperative style and it won't work on your phone :(.
---

Random.self_init()

let m = [|
    [| 9; 4; 0; 0; 0; 7; 0; 0; 0 |];
    [| 0; 6; 0; 0; 0; 0; 0; 1; 0 |];
    [| 0; 0; 8; 0; 0; 1; 0; 5; 3 |];
    [| 3; 0; 7; 0; 0; 0; 0; 6; 0 |];
    [| 0; 0; 0; 0; 2; 0; 0; 0; 0 |];
    [| 0; 5; 0; 0; 0; 0; 0; 3; 7 |];
    [| 0; 9; 0; 8; 0; 0; 1; 0; 0 |];
    [| 0; 1; 0; 0; 0; 0; 0; 8; 0 |];
    [| 0; 0; 0; 4; 0; 0; 0; 0; 9 |]
|]

let swap_row board rowA rowB =
    let t = board.(rowA) in
    let _ = board.(rowA) <- board.(rowB) in
    board.(rowB) <- t

let swap_col board colA colB =
    for i = 0 to 8 do
        let t = board.(i).(colA) in
        let _ = board.(i).(colA) <- board.(i).(colB) in
        board.(i).(colB) <- t
    done
   
let shuffle_rows board =   
    for i = 0 to 2 do
        for j = 0 to 2 do
            swap_row board (i * 3 + j) (i * 3 + (Random.int 3))
        done
    done
       
let shuffle_cols board =   
    for i = 0 to 2 do
        for j = 0 to 2 do
            swap_col board (i * 3 + j) (i * 3 + (Random.int 3))
        done
    done
   
(* shuffle the 3x3 squares in their rows *)
let shuffle_square_rows board =
    let swap_square_row a b =
        for i = 0 to 2 do
            swap_row board (a + i) (b + i)
        done
    in
   
    for i = 0 to 2 do
        swap_square_row (i * 3) (Random.int 3 * 3)
    done

(* shuffle the 3x3 squares in their columns *)
let shuffle_square_cols board =
    let swap_square_col a b =
        for i = 0 to 2 do
            swap_col board (a + i) (b + i)
        done
    in
   
    for i = 0 to 2 do
        swap_square_col (i * 3) (Random.int 3 * 3)
    done

let shuffle board =
    shuffle_rows board;
    shuffle_cols board;
    shuffle_square_rows board;
    shuffle_square_cols board

(* swap the numbers around *)
let permutation board =
    let arr = Array.init 10 (fun i -> i) in
    (* really crappy way of making a permutation *)
    for i  = 0 to 999 do
        swap_row arr (i mod 9 + 1) (Random.int 9 + 1)
    done;
    for i = 0 to 8 do
        let row = board.(i) in
        for j = 0 to 8 do
            row.(j) <- arr.(row.(j))
        done
    done

let () =
    shuffle m;
    permutation m;
    Array.iter (fun r -> Array.iter (print_int)  r; print_newline()) m