/**
@descr 
@form takeCard(the card you take).
*/
takeCard(Card):-
       	cards([Card|R]),
	retractall(cards(_)),
	assert(cards(R)).
	
% This file contains some vectorfunctions for the dices and some random permutation functions
:- dynamic(cards/1).

%Determs how many elements of a list the console writes
%portray(L):-write_term(L,[maxdepth=30]).


/**
     @descr Selects N Elements from a list in ordered random and return them a a new list.
     @form rnd_select(Original list, Number of cards, New List).
*/

rnd_select(_,0,[]).
rnd_select(Xs,N,[X|Zs]) :- N > 0,
    length(Xs,L),
    I is random(L) + 1,
    remove_at(X,Xs,I,Ys),
    N1 is N - 1,
    rnd_select(Ys,N1,Zs).

/**
    @descr Returns a list of Numbers between two limits.
    @form  range(Bottom limit, Top limit, List)
*/
    
range(I,I,[I]).
range(I,K,[I|L]) :- I < K, I1 is I + 1, range(I1,K,L).

/**
    @descr Removes the Nth element from a list.
    @form  remove_at(Nth Element, The List, N, Restlist
*/
remove_at(X,[X|Xs],1,Xs).
remove_at(X,[Y|Xs],K,[Y|Ys]) :- K > 1, 
   K1 is K - 1, remove_at(X,Xs,K1,Ys).

/**
    @descr List of N random ordered numbers from 1 to K
    @form  noverk(N,K,List)
*/
noverk(N,K,List):-
       range(1,K,R), 
       rnd_select(R,N,List).

/**
       @descr Splits a list into two parts seperated at the Nth last element.
       @form  splitlist(List, Position to cut, Part1, Part2)
*/
splitlist(Alist,0,Alist,[]).
splitlist(Alist,N,Eliste1,[H|Eliste2]):-
	Alist=[H|R],
	N2 is N-1,
	splitlist(R,N2,Eliste1,Eliste2).

shufflecards :- 
	shufflecards(56).

/**
	@descr Takes N random orderes Cards (incl. Wu Feng) and inserts Wu-Feng at the 10th last position.
        @form shufflecards(N, List)
*/
	shufflecards(Count):-
	Count>10,
	WF is 56+random(10),
	noverk(Count-1,55,L),
	splitlist(L,10,A,B),
	append(A,[WF|B],List),
	assert(cards(List)).

/**
	@descr Orders the villages as entered as a list with the new positions.
	@form  helpshufflevillages(List of new positions, Enter 9 here please)
*/
helpshufflevillages([],_).
helpshufflevillages([H|T],N):-
	village(N,A,B,C,_),
	retract(village(N,_,_,_,_)),
	assertz(village(N,A,B,C,H)),
	N2 is N+1,
	helpshufflevillages(T,N2).

/**
	@descr Shuffles the Villages
*/
shufflevillages:-
	range(1,9,A),
	rnd_select(A,9,Liste),
	helpshufflevillages(Liste,1),
	!,
	% Place Buddha at the correct field
	village(6,_,_,_,Pos),
	placeBuddha(1,Pos),
	placeBuddha(2,Pos).

	


	

		
