<commit_before>:-module(splendor, [runGame/1, cardDataRaw/13, addGems/3, removeGems/3, subtractGems/3, gemCount/2, show/3, stateProxy/3, card/2, setVerbose/1, canBuyCard/3, randomGems/4, randomGetGems/4, isGetGemValid/4]). 
%, ,minusGem/2

:-dynamic closeCards/3.
:-dynamic openCards/3.
:-dynamic tokens/1.
:-dynamic player/3.
:-dynamic turn/1.
:-dynamic verbose/1.
:-dynamic nobles/1.
:-dynamic selectableNobles/1.

:-  use_module(cardData). 

selectableNobles([]).   %This is used for the case when there are few nobles visit the player at the same turn.

openCards([],[],[]).    %Open visible cards on the board.
availableGems([]).

unshift(X, L, [X|L]).

%close_cards([[],[],[]]).
%card data - A->ID of the card , N1 to N5 - white,blue,green,red,black gems, X->color of the card
%P-> point of the card, L-> deck of the card.
cardData(A, [N1,N2,N3,N4,N5,0]-X-P, L) :- 
	cardDataRaw(L,A,N1,N2,N3,N4,N5,P,_,_,_,_,_),
	(
		cardDataRaw(_,A,_,_,_,_,_,_,1,_,_,_,_),X=1;
		cardDataRaw(_,A,_,_,_,_,_,_,_,1,_,_,_),X=2;
		cardDataRaw(_,A,_,_,_,_,_,_,_,_,1,_,_),X=3;
		cardDataRaw(_,A,_,_,_,_,_,_,_,_,_,1,_),X=4;
		cardDataRaw(_,A,_,_,_,_,_,_,_,_,_,_,1),X=5
	).

% given the card id, finds RequiredGems-BonusColor-Points info of the card
card(A, C) :-
	cardData(A, C, _). 

% finds id, point and colors_needed_to_be_visited_by_this_noble info of all nobles
allNobles(Z) :-
	findall(Id-[N1, N2, N3, N4, N5, 0]-Point, cardDataRawNoble(Id, N1, N2, N3, N4, N5, Point), Z).

% finds the ids of cards in each deck
% initialCards(IdsOfCardsInTheFirstDeck,IdsOfCardsInTheSecondDeck,IdsOfCardsInTheThirdDeck)
initialCards(L1,L2,L3) :-
	findall(X, cardData(X,_-_-_,1), L1),
	findall(X, cardData(X,_-_-_,2), L2),
	findall(X, cardData(X,_-_-_,3), L3).

closeCards(L1,L2,L3) :-
	initialCards(L1,L2,L3).

shuffle(A,B):-
	random_permutation(A,B).

shuffleCards :-
	closeCards(L1, L2, L3),
	shuffle(L1, SL1),
	shuffle(L2, SL2),
	shuffle(L3, SL3),
	retractall(closeCards(_, _, _)),
	assert(closeCards(SL1, SL2, SL3)).

% finds the first N elements of a list
% firstN (N, GivenList, SubList)
firstN(_,[], []).
firstN(N, [H|T], [H|R]) :-
	N1 is N-1,
	(
		N1=0,R=[];
		N1>0,firstN(N1, T, R)
	),!.

%shuffle nobles and select playerCount+1 from the front of the deck as nobles of the game.
shuffleNobles(N) :-
	allNobles(AllNobles),
	shuffle(AllNobles, ShuffledNobles),
	N1 is N+1,
	firstN(N1, ShuffledNobles, SelectedNobles),
	retractall(nobles(_)),
	forall(member(X, SelectedNobles), assert(nobles(X))).

gemCountForPlayerCount(2, 4).
gemCountForPlayerCount(3, 5).
gemCountForPlayerCount(4, 7).

initialize(N) :-
	(retractall(selectableNobles(_))),
	(assert(selectableNobles([]))),
	(retractall(closeCards(_,_,_));true),
	assert(closeCards(L1,L2,L3) :- initialCards(L1,L2,L3)),
	shuffleCards,
	shuffleNobles(N),
	retractall(openCards(_,_,_)),
	assert(openCards([],[],[])),
	dealCards,dealCards,dealCards,dealCards,
	(retractall(tokens(_));true),
	gemCountForPlayerCount(N, C),
	assert(tokens([C,C,C,C,C,5])),
	(retractall(turn(_));true),
	assert(turn(1)).

moveCard(L, [H|T], [H|L], T).

%takes 4 lists ; L-> cards open on the board initially, CL->cards closed on the board initially
%PL-> cards open on the board after operation, PCL->cards closed on the board after operation
%If the L already has 4 cards open or CL has no more cards to give, then these will stay as the open and closed cards.
%Otherwise take a "Card" from CL and put it to PL. PCL is the list which is CL-"Card". New open and closed cards are PL and PCL.
dealCardsSingle(L, CL, PL, PCL) :-
	length(L, Len),
	length(CL, CLen),
	(
		(Len=4;CLen=0),
		PL=L, 
		PCL=CL
		;
		moveCard(L,CL,PL,PCL)
	).

dealCards :- 
	openCards(L1, L2, L3),
	closeCards(CL1, CL2, CL3),
	dealCardsSingle(L1, CL1, PL1, PCL1),
	dealCardsSingle(L2, CL2, PL2, PCL2),
	dealCardsSingle(L3, CL3, PL3, PCL3),
	retractall(openCards(_,_,_)),
	retractall(closeCards(_,_,_)),
	assert(openCards(PL1,PL2,PL3)),
	assert(closeCards(PCL1,PCL2,PCL3)).

initializePlayers(PlayerModules) :-
	length(PlayerModules, PlayerCount),
	PlayerCount > 1, PlayerCount =< 4,
	retractall(player(_,_,_)),
	forall(between(1, PlayerCount, PlayerNo),
		(
			nth1(PlayerNo, PlayerModules, PlayerModule),
			atom_concat(player, PlayerNo, Player),
			assert(player(Player, score, 0)),
			/* % noble tile selection test
				(
				PlayerNo=1,assert(player(Player, bonuses, [3,3,3,3,3,0]));
				PlayerNo>1,assert(player(Player, bonuses, [0,0,0,0,0,0]))
				),
			*/
			assert(player(Player, bonuses, [0,0,0,0,0,0])),
<commit_msg>Update splendor.pl<commit_after>:-module(splendor, [runGame/1, cardDataRaw/13, addGems/3, removeGems/3, subtractGems/3, gemCount/2, show/3, stateProxy/3, card/2, setVerbose/1, canBuyCard/3, randomGems/4, randomGetGems/4, isGetGemValid/4]). 
%, ,minusGem/2

:-dynamic closeCards/3.
:-dynamic openCards/3.
:-dynamic tokens/1.
:-dynamic player/3.
:-dynamic turn/1.
:-dynamic verbose/1.
:-dynamic nobles/1.
:-dynamic selectableNobles/1.

:-  use_module(cardData). 

selectableNobles([]).   %This is used for the case when there are few nobles visit the player at the same turn.

openCards([],[],[]).    %Open visible cards on the board.
availableGems([]).

unshift(X, L, [X|L]).

%close_cards([[],[],[]]).
%card data - A->ID of the card , N1 to N5 - white,blue,green,red,black gems, X->color of the card
%P-> point of the card, L-> deck of the card.
cardData(A, [N1,N2,N3,N4,N5,0]-X-P, L) :- 
	cardDataRaw(L,A,N1,N2,N3,N4,N5,P,_,_,_,_,_),
	(
		cardDataRaw(_,A,_,_,_,_,_,_,1,_,_,_,_),X=1;
		cardDataRaw(_,A,_,_,_,_,_,_,_,1,_,_,_),X=2;
		cardDataRaw(_,A,_,_,_,_,_,_,_,_,1,_,_),X=3;
		cardDataRaw(_,A,_,_,_,_,_,_,_,_,_,1,_),X=4;
		cardDataRaw(_,A,_,_,_,_,_,_,_,_,_,_,1),X=5
	).

% given the card id, finds RequiredGems-BonusColor-Points info of the card
card(A, C) :-
	cardData(A, C, _). 

% finds id, point and colors_needed_to_be_visited_by_this_noble info of all nobles
allNobles(Z) :-
	findall(Id-[N1, N2, N3, N4, N5, 0]-Point, cardDataRawNoble(Id, N1, N2, N3, N4, N5, Point), Z).

% finds the ids of cards in each deck
% initialCards(IdsOfCardsInTheFirstDeck,IdsOfCardsInTheSecondDeck,IdsOfCardsInTheThirdDeck)
initialCards(L1,L2,L3) :-
	findall(X, cardData(X,_-_-_,1), L1),
	findall(X, cardData(X,_-_-_,2), L2),
	findall(X, cardData(X,_-_-_,3), L3).

closeCards(L1,L2,L3) :-
	initialCards(L1,L2,L3).

shuffle(A,B):-
	random_permutation(A,B).

shuffleCards :-
	closeCards(L1, L2, L3),
	shuffle(L1, SL1),
	shuffle(L2, SL2),
	shuffle(L3, SL3),
	retractall(closeCards(_, _, _)),
	assert(closeCards(SL1, SL2, SL3)).

% finds the first N elements of a list
% firstN (N, GivenList, SubList)
firstN(_,[], []).
firstN(N, [H|T], [H|R]) :-
	N1 is N-1,
	(
		N1=0,R=[];
		N1>0,firstN(N1, T, R)
	),!.

%shuffle nobles and select playerCount+1 from the front of the deck as nobles of the game.
shuffleNobles(N) :-
	allNobles(AllNobles),
	shuffle(AllNobles, ShuffledNobles),
	N1 is N+1,
	firstN(N1, ShuffledNobles, SelectedNobles),
	retractall(nobles(_)),
	forall(member(X, SelectedNobles), assert(nobles(X))).

gemCountForPlayerCount(2, 4).
gemCountForPlayerCount(3, 5).
gemCountForPlayerCount(4, 7).

initialize(N) :-
	(retractall(selectableNobles(_))),
	(assert(selectableNobles([]))),
	(retractall(closeCards(_,_,_));true),
	assert(closeCards(L1,L2,L3) :- initialCards(L1,L2,L3)),
	shuffleCards,
	shuffleNobles(N),
	retractall(openCards(_,_,_)),
	assert(openCards([],[],[])),
	dealCards,dealCards,dealCards,dealCards,
	(retractall(tokens(_));true),
	gemCountForPlayerCount(N, C),
	assert(tokens([C,C,C,C,C,5])),
	(retractall(turn(_));true),
	assert(turn(1)).

moveCard(L, [H|T], [H|L], T).

%takes 4 lists ; L-> cards open on the board initially, CL->cards closed on the board initially
%PL-> cards open on the board after operation, PCL->cards closed on the board after operation
%If the L already has 4 cards open or CL has no more cards to give, then these will stay as the open and closed cards.
%Otherwise take a "Card" from CL and put it to PL. PCL is the list which is CL-"Card". New open and closed cards are PL and PCL.
dealCardsSingle(L, CL, PL, PCL) :-
	length(L, Len),
	length(CL, CLen),
	(
		(Len=4;CLen=0),
		PL=L, 
		PCL=CL
		;
		moveCard(L,CL,PL,PCL)
	).

dealCards :- 
	openCards(L1, L2, L3),
	closeCards(CL1, CL2, CL3),
	dealCardsSingle(L1, CL1, PL1, PCL1),
	dealCardsSingle(L2, CL2, PL2, PCL2),
	dealCardsSingle(L3, CL3, PL3, PCL3),
	retractall(openCards(_,_,_)),
	retractall(closeCards(_,_,_)),
	assert(openCards(PL1,PL2,PL3)),
	assert(closeCards(PCL1,PCL2,PCL3)).

% adds player's initial score, bonuses, gems, reserve cards, noble tiles
% additionally uses player's module and its initialize predicate
% for each player name in PlayerModules
initializePlayers(PlayerModules) :-
	length(PlayerModules, PlayerCount),
	PlayerCount > 1, PlayerCount =< 4,
	retractall(player(_,_,_)),
	forall(between(1, PlayerCount, PlayerNo),
		(
			nth1(PlayerNo, PlayerModules, PlayerModule),
			atom_concat(player, PlayerNo, Player),
			assert(player(Player, score, 0)),
			/* % noble tile selection test
				(
				PlayerNo=1,assert(player(Player, bonuses, [3,3,3,3,3,0]));
				PlayerNo>1,assert(player(Player, bonuses, [0,0,0,0,0,0]))
				),
			*/
			assert(player(Player, bonuses, [0,0,0,0,0,0])),

{'fraction_non_alphanumeric': 0.1817588868355256, 'fraction_numerical': 0.03543863463419937, 'mean_word_length': 5.344827586206897, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3974372', 'n_tokens_mistral': 4085, 'n_tokens_neox': 3890, 'n_words': 835}