caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Simon <sk75@uow.edu.au>
To: christophe.raffalli@univ-savoie.fr
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Mandelbrot renderer
Date: Thu, 24 Nov 2005 13:38:41 +1100 (EST)	[thread overview]
Message-ID: <20051124133841.AUT44679@minerva.its.uow.edu.au> (raw)


> 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
   


             reply	other threads:[~2005-11-24  2:38 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-24  2:38 Simon [this message]
  -- strict thread matches above, loose matches on Subject: below --
2005-11-23  1:50 Jon Harrop
2005-11-23  8:00 ` [Caml-list] " David Baelde
2005-11-23 10:10   ` Yaron Minsky
2005-11-23 14:58     ` Christophe Raffalli
2005-11-24 14:43   ` Chris Campbell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051124133841.AUT44679@minerva.its.uow.edu.au \
    --to=sk75@uow.edu.au \
    --cc=caml-list@inria.fr \
    --cc=christophe.raffalli@univ-savoie.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).