File: hearts.lua

package info (click to toggle)
gnome-hearts 0.1.3-2etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 2,040 kB
  • ctags: 286
  • sloc: sh: 3,514; ansic: 2,511; xml: 2,030; makefile: 100
file content (215 lines) | stat: -rw-r--r-- 6,580 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
--[[
	Hearts - hearts.lua
	Copyright 2006 Sander Marechal
	
	This program 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.
	
	This program 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 this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
]]

-- define the globals
hand, trick, score, rules = nil, nil, nil, nil
suit, rank = 1, 2

-- define the suits, the card values and the players
clubs, diamonds, hearts, spades = 0, 1, 2, 3
ace, two, three, four, five, six, seven, eight, nine, ten, jack, queen, king, ace_high = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
north, east, south, west = 1, 2, 3, 4 -- one off from the C code because lua arrays start at 1

--[[ Some usefull functions for selecting subsets of card lists ]]

-- return true if card is in list
function have_card(card, list)
	if type(list) ~= "table" then return false end
	if type(card) ~= "table" then return false end
	
	for list_index, list_card in ipairs(list) do
		if card[suit] == list_card[suit] and card[rank] == list_card[rank] then
			return list_index
		end
	end
	
	return false
end

-- returns true if a card of suit is in list
function have_suit(card_suit, list)
	local curr_card = nil
	for _, curr_card in ipairs(list) do
		if curr_card[suit] == card_suit then
			return true
		end
	end
	return false
end

-- return a list of all cards that are valid to play
function get_valid_cards(list)
	local card, result = nil, {}
	if type(list) ~= "table" then return {} end
	for _, card in ipairs(list) do
		if is_valid_card(card) then
			table.insert(result, {card[suit], card[rank]})
		end
	end
	return result
end

-- return all cards from a given suit
function get_cards_of_suit(list, card_suit)
	local card, result = nil, {}
	if type(list) ~= "table" then return {} end
	for _, card in ipairs(list) do
		if card[suit] == card_suit then
			table.insert(result, {card[suit], card[rank]})
		end
	end
	return result
end

-- return all valid cards from a given suit
function get_valid_cards_of_suit(list, card_suit)
	local card, result = nil, {}
	if type(list) ~= "table" then return {} end
	for _, card in ipairs(list) do
		if card[suit] == card_suit and is_valid_card(card) then
			table.insert(result, {card[suit], card[rank]})
		end
	end
	return result
end

-- return all cards that are not points
function get_pointless_cards(list, include_bonus)
	local card, result = nil, {}
	if type(list) ~= "table" then return {} end
	for _, card in ipairs(list) do
		if card[suit] ~= hearts and not (card[suit] == spades and card[rank] == queen) then
			table.insert(result, {card[suit], card[rank]})
		end
	end
	return result
end

-- return all cards that are not points
function get_point_cards(list, include_bonus)
	local card, result = nil, {}
	if type(list) ~= "table" then return {} end
	for _, card in ipairs(list) do
		if card[suit] == hearts or (card[suit] == spades and card[rank] == queen) then
			table.insert(result, {card[suit], card[rank]})
		end
	end
	return result
end

-- return the highest card on the trick
function get_highest_card()
	local trump = trick_get_trump()
	local high_card = {trump, 1}
	if trump == nil then return nil end
	
	for index = 1, 4 do
		local card = trick[index]
		if card ~= nil and card[suit] == trump and card[rank] > high_card[rank] then
			high_card = card
		end
	end
	return high_card
end

--[[ Various callbacks for sorting lists of cards ]]

-- sort ascending, regardless of suit
function ascending(one, two)
	assert(type(one) == "table", "sort ascending: one is not a card")
	assert(type(two) == "table", "sort ascending: two is not a card")
	
	if one[rank] < two[rank] then
		return true
	end
	
	return false
end

-- sort descending, regardless of suit
function descending(one, two)
	assert(type(one) == "table", "sort descending: one is not a card")
	assert(type(two) == "table", "sort descending: two is not a card")
	
	if one[rank] > two[rank] then
		return true
	end
	
	return false
end

-- sort clubs, diamonds, spades, hearts ascending
function suit_ascending(one, two)
	assert(type(one) == "table", "sort suit_ascending: one is not a card")
	assert(type(two) == "table", "sort suit_ascending: two is not a card")

	local one_id = one[suit] * 13 + one[rank]	
	local two_id = two[suit] * 13 + two[rank]	
	
	if one[suit] == hearts then one_id = one_id + 26 end
	if two[suit] == hearts then two_id = two_id + 26 end
	
	return (one_id < suit_id)
end

-- sort hearts, spades, diamonds, clubs descending
function suit_descending(one, two)
	assert(type(one) == "table", "sort suit_descending: one is not a card")
	assert(type(two) == "table", "sort suit_descending: two is not a card")

	local one_id = one[suit] * 13 + one[rank]	
	local two_id = two[suit] * 13 + two[rank]	
	
	if one[suit] == hearts then one_id = one_id + 26 end
	if two[suit] == hearts then two_id = two_id + 26 end
	
	return (one_id > suit_id)
end

-- sort cards ascending by point value
function points_ascending(one, two)
	assert(type(one) == "table", "sort points_ascending: one is not a card")
	assert(type(two) == "table", "sort points_ascending: two is not a card")
	
	if one[suit] == spades and one[rank] == queen then return false end
	if two[suit] == spades and two[rank] == queen then return true end
	
	if one[suit] == hearts and two[suit] ~= hearts then return false end
	if one[suit] ~= hearts and two[suit] == hearts then return true end
	
	if one[suit] == hearts and two[suit] == hearts then return (one[rank] < two[rank]) end
	
	return false
end

-- sort cards descending by point value
function points_descending(one, two)
	assert(type(one) == "table", "sort points_descending: one is not a card")
	assert(type(two) == "table", "sort points_descending: two is not a card")
	
	if one[suit] == spades and one[rank] == queen then return true end
	if two[suit] == spades and two[rank] == queen then return false end
	
	if one[suit] == hearts and two[suit] ~= hearts then return true end
	if one[suit] ~= hearts and two[suit] == hearts then return false end
	
	if one[suit] == hearts and two[suit] == hearts then return (one[rank] > two[rank]) end
	
	return false
end