(*
*  LikeEternalAndRealNeurons (L.E.A.R.N)                                       
*
*  Copyright (c) 2006 Gilles Bizet
*
*  This file is part of LikeEternalAndRealNeurons (L.E.A.R.N)                
*
*  LikeEternalAndRealNeurons (L.E.A.R.N)                                      
*  is free software; you can redistribute it and/or modify                    
*  it under the terms of the GNU General Public License as published by      
*  the Free Software Foundation; either version 2 of the License, or          
*  (at your option) any later version.                                        
*
*  LikeEternalAndRealNeurons (L.E.A.R.N)                                      
*  is distributed in the hope that it will be useful,                         
*  but WITHOUT ANY WARRANTY; without even the implied warranty of             
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
*  GNU General Public License for more details.                              
*  You should have received a copy of the GNU General Public License         
*  along with Foobar; if not, write to the Free Software                       
*  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA   
*)

(**************************************************************************)



open Array
open List
open Str
(*exemple de position notation FEN = fen par defaut pour tests*)
let fen = "rnbq1rk1/ppp1n1pp/3b1p2/3p2B1/3P4/3B1N2/PPP2PPP/RN1Q1RK1 w - - 0 8";;
(*NB: il faudra tenir compte de " pour ValidMove
   *ex(fr): http://www.apprendre-en-ligne.net/crypto/passecret/fen.html OU 
   http://www.iechecs.com/notation.htm#format_fen
   *ex(en): http://en.wikipedia.org/wiki/Forsyth-Edwards_Notation#Definition OR
   http://www.very-best.de/pgn-spec.htm#16.
*)
   

(*string -> list of substring - fonction d'analyse de script du module Str*)
let symbols1 = regexp "[/ ]" 
let lfen fen = split symbols1 fen
let afen fen = of_list (lfen fen)                             
let apos fen = sub (afen fen) 0 8  (*substring, position only*)

let empty x = match x with
  | '1'->['-'| '2'->['-';'-']| '3'->['-';'-';'-']| '4'->['-';'-';'-';'-']
  | '5'->['-';'-';'-';'-';'-']|'6'-> ['-';'-';'-';'-';'-';'-']|'7'->['-';'-';'-';'-';'-';'-';'-'
  |'8'-> ['-';'-';'-';'-';'-';'-';'-';'-'
  |'r'->['r']|'b'->['b']|'n'->['n']|'q'->['q']|'k'->['k']|'p'->['p']
  |'R'->['R']|'B'->['B']|'N'->['N']|'Q'->['Q']|'K'->['K']|'P'->['P']
  |_->[]
  
  (*http://pleac.sourceforge.net/pleac_ocaml/strings.html *********************
* let array_of_chars = Array.init (String.length s) (fun i -> s.[i]);;
* let array_of_codes s = Array.init (String.length s) (fun i -> Char.code s.[i];;
* let chars = List.map (fun x -> x.[0]);;
************************************************************************************)


let array_of_chars s =  init (String.length s) (fun i -> s.[i])        (*-->interface definitions.mli*)
let list_of_chars s = to_list (array_of_chars s) ;;                            (*methode directe ?*)            

let boardVL fen =
let board = Array.make 8 [] in
for k=0 to 7 do
board.(k) <-   concat (map empty  (list_of_chars (apos fen).(k)))         
                   (***different data structure***)

 done;board;;                                                         (*8-Vector of 8-Char-List*)
(*boardVL fen;;*)

 (*data structure --> 8x8-array=matrix of Char *)            (*pas du tout optimise !*)
let boardM fen =
let bVL = boardVL fen in  
let board = create_matrix 8 8 '-' in
for i=0 to 7 do 
  for j=0 to 7 do
    board.(i).(j) <- nth bVL.(i) j 
                                 done
                                 done;board;;
(*boardM fen;;*)
                                                                                                          
 (*data structure --> 8x8-array=matrix of Char-pair , like f functions *)
let boardFM fen =
 let bM =  boardM fen in
   let board = create_matrix 8 8 ('-','-'in
  for i=0 to 7 do 
  for j=0 to 7 do
    board.(i).(j) <- (Char.lowercase bM.(i).(j) ,match bM.(i).(j) with
                        |'r'|'n'|'b'|'q'|'k'|'p'->'b'
                        |'-' ->'-'
                        |_->'w')
done
                      done;board;;
 (* boardFM fen;;*)                   

(*f functions*)
let f  x = (boardFM fen).(fst x).(snd x) ;;
(*f (0,0);;*)



















  
  (************************************************************************************************************* * Programme PASCAL (Delphi) * Conversion notation EPD -> Ei tableau * * procedure ConversionEPD(s:string); * var i,j,c:Integer; * * begin * i:=1;c:=0; * while si<> ' ' do * begin * If (si<= '8') and (si>= '1') then * begin * for j:=0 to StrToInt(si)-1 do * begin * Ej+c:=0; * end; * c:=c+ StrToInt (si); * end * else * begin * * If si='r'then Ec:= 10; * If si='R'then Ec:= 4; * If si='n'then Ec:= 8; * If si='N'then Ec:= 2; * If si='b'then Ec:= 9; * If si='B'then Ec:= 3; * If si='q'then Ec:= 11; * If si='Q'then Ec:= 5; * If si='k'then Ec:= 12; * If si='K'then Ec:= 6; * If si='p'then Ec:= 7; * If si='P'then Ec:= 1; * If si<>'/' then c:=c+1; * end; * i:=i+1; * end; * *end;********************************************)