(*
*  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   
*)

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



(*Interface rudimentaire, sans GTK, pour experimenter*)
(* TODO: tcl/tk (labltk) OU/ET ncurses*)

open Definitions
open Convertfen
open Chessrules
open Database2;;
(*traitements imperatifs*)

print_newline ();;
print_endline "--------------------------------------";;
print_endline "Welcome/Bienvenue";;
print_endline "LikeEternalAndRealNeurons (L.E.A.R.N)";;
print_endline "Copyright (c) 2006 Gilles Bizet";;
print_endline "--------------------------------------";;
print_endline "---------Just for tests-----------";;
print_endline "--------------------------------------";;



(*les commandes*)

print_endline "Entrer une position (FEN)/Enter position (FEN) (ex. fen- ...)";;
print_endline "Retirer une piece/Remove a piece (ex. -a4 ou/or -e2)";;
print_endline "Ajouter une piece/Add a piece (ex. +Pa4 ou/or +re2)";;
print_endline "Coup/move: TODO ";;

(*affichage de la position de base - de la fonction par cas si besoin ************)
let fen0 = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq";;

for i=7 downto 0 do
 for j=0 to 7 do
 if snd (f0 (i,j))= 'b' then print_char (fst (f0 (i,j)))
                        else print_char (Char.uppercase(fst (f0 (i,j))))                                 
 done;
 print_newline ()
done;;
(**)

affDiagDepl (data.(2));;
(**********************************************************************************)

(*affichage de BoardFM*************************************************************)
(*let posf =";;*)
let affBoardFM posf =
 for i=0 to 7 do
  for j=0 to 7 do
   if snd (boardFM posf).(i).(j)='b' then print_char (fst (boardFM posf).(i).(j))
                 else print_char (Char.uppercase(fst (boardFM posf).(i).(j)))
  done;
print_newline ()
 done;; 

let cmd = read_line ();;
print_string "commandes/commands ?:";;
print_endline cmd;;

(* let cmd = ";;
* let delimit = regexp ";;
* split delimit cmd;;
*)

(*if e = '' then wait e;;*)


let conv x = match x with
  |'a'->0|'b'->1|'c'->2|'d'->3|'e'->4|'f'->5|'g'->6|'h'->7|_->99;;
let symV x = match x with
  |'1'->7|'2'->6|'3'->5|'4'->4|'5'->3|'6'->2|'7'->1|'8'->0|_->99;;


(*let addP piece = let board = (boardFM fen)and i = conv (String.get cmd 2)and j = (int_of_char (String.get cmd 3))-48 in board.(i).(j)<- piece;;*) 

if (Str.first_chars cmd 4 = "fen-"then affBoardFM (Str.string_after cmd 4) else affBoardFM fen0;;
 
(*if cmd.[0]='+' then addP (String.get cmd 1);;*)  
(*let f x = match x with 
 * |(i,j) when (x <> (i,j))->f0 (i,j)
 * |(i,j) -> ('-','-');;*)
    (*? modifier une definition de fonction?*)

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

(*test des règles / Rules test*)
let fentest = (Str.string_after cmd 4);;

print_newline ();;
print_endline "Test: (ex. Pe2e4 <enter>) true or false ?:";;
let cmd = read_line ();;
let x = (symV cmd.[2],conv cmd.[1]);;
let y = (symV cmd.[4],conv cmd.[3]);;

let f = fun x -> (boardFM fentest).(fst x).(snd x);;
let piece = Char.lowercase (cmd.[0]);;


 let valid  piece =

  if piece = 'r' then r f x y else
  if piece = 'b' then b f x y else false ;;

let affBoardFM2 pos  =
 for i=0 to 7 do
  for j=0 to 7 do
if (i,j)= x then Printf.printf "(%c,%c)* " (fst (pos (i,j))) (snd (pos (i,j))) else 
if (i,j)= y then Printf.printf "(%c,%c)* " (fst (pos (i,j))) (snd (pos (i,j))) else
 Printf.printf "(%c,%c) " (fst (pos (i,j))) (snd (pos (i,j)))
  done;
print_newline ()
 done;; 

affBoardFM2 f;;



print_endline (string_of_bool (valid piece));;