La Primitiva

Two handlers to aid in the creation and checking of winner combinations for the spanish game “La Primitiva”.

OS version: OS X

(*

combinaciones(integer)
Returns the specified number of random combinations for the Spanish game "La Primitiva" (simple groups of 6 digits from 1 to 49).
It will also save a list of such combinations for further comprobation.

Example: combinaciones(200) --> a list of 200 combinations




comprobar(apuestas, winner, comp, reintegro)
Checks for winner combinations of "La Primitiva". You supply:
apuestas: a list of lists of 6 digits (as the one returned by "combinaciones")
winner: the winner combination, a list of six digits
comp: the complementary number
reintegro: just that, "el reintegro"

It returns a list of 7 items:
1: number of combinations winning the "reintegro"
2: number of combinations with 3 matches
3: number of combinations with 4 matches
4: number of combinations with 5 matches
5: number of combinations with 5 matches plus the complementary number
6: number of combinations with 6 matches
7: list of offsets of such winner combinations

Example:

set apuestas to read alias "path:to:apuestas.txt" as list --> list coming from "combinaciones"

set winner to {17, 18, 21, 27, 34, 42}
set comp to 23
set reintegro to 3

comprobar(apuestas, winner, comp, reintegro)

*)
to combinaciones(maxguests)
	script blank
		property combs : {}
		property klausis : {}
		
		to savelist()
			set f to (choose file name default name "apuestas.txt")
			set f to (open for access f with write permission)
			set eof of f to 0
			write combs to f as list
			close access f
		end savelist
		
		to sortRecords from rList --> Kai Edwards
			if rList's length < 2 then return rList
			set inicio to {}
			set fin to {}
			set current_item to rList's item 1
			set el_resto to rList's rest
			
			repeat with r in el_resto
				if current_item > r then
					set inicio's end to r's contents
				else
					set fin's end to r's contents
				end if
			end repeat
			
			if inicio's length > 1 then set inicio to sortRecords from inicio
			if fin's length > 1 then set fin to sortRecords from fin
			
			inicio & {current_item} & fin
		end sortRecords
	end script
	repeat
		set blank's klausis to {}
		repeat
			set x to random number from 1 to 49
			if x is not in blank's klausis then set end of blank's klausis to x
			if (count blank's klausis) is 6 then exit repeat
		end repeat
		if {blank's klausis} is not in blank's combs then set end of blank's combs to blank's klausis
		if (count blank's combs) is maxguests then exit repeat
	end repeat
	
	repeat with i from 1 to count blank's combs
		set blank's combs's item i to sortRecords of blank from blank's combs's item i
	end repeat
	
	blank's savelist()
	
	blank's combs
end combinaciones

to comprobar(apuestas, winner, comp, reintegro)
	set reint to 0
	set de3 to 0
	set de4 to 0
	set de5 to 0
	set de5c to 0
	set de6 to 0
	
	set o1 to {} --> reintegro offsets
	set o2 to {} --> de3 offsets
	set o3 to {} --> de4 offsets
	set o4 to {} --> de5 offsets
	set o5 to {} --> de5c offsets
	set o6 to {} --> de6 offsets
	
	repeat with w from 1 to count apuestas
		set i to apuestas's item w
		set aciertos to 0
		if winner's item 1 is in i then set aciertos to aciertos + 1
		if winner's item 2 is in i then set aciertos to aciertos + 1
		if winner's item 3 is in i then set aciertos to aciertos + 1
		if winner's item 4 is in i then set aciertos to aciertos + 1
		if winner's item 5 is in i then set aciertos to aciertos + 1
		if winner's item 6 is in i then set aciertos to aciertos + 1
		if aciertos is 3 then
			set de3 to de3 + 1
			set o2's end to w
		else if aciertos is 4 then
			set de4 to de4 + 1
			set o3's end to w
		else if aciertos is 5 then
			if comp is in i then
				set de5c to de5c + 1
				set o5's end to w
			else
				set de5 to de5 + 1
				set o4's end to w
			end if
		else if aciertos is 6 then
			set de6 to de6 + 1
			set o6's end to w
		else --> reintegro?
			if reintegro is in i then set reint to reint + 1
			set o1's end to w
		end if
	end repeat
	
	{reint, de3, de4, de5, de5c, de6, {o1, o2, o3, o4, o5, o6}}
end comprobar