Skip to content

Commit e040281

Browse files
committed
snake_case and dedents
1 parent c752a6a commit e040281

4 files changed

Lines changed: 151 additions & 56 deletions

File tree

exercises/practice/camicia/.meta/example.lua

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
local function getCardValue(card)
1+
local function get_value(card)
22
if card == 'J' then
33
return 1
44
elseif card == 'Q' then
@@ -12,15 +12,15 @@ local function getCardValue(card)
1212
end
1313
end
1414

15-
local function simulateGame(playerA, playerB)
15+
local function simulate_game(playerA, playerB)
1616
local handA = {}
1717
for _, card in ipairs(playerA) do
18-
table.insert(handA, getCardValue(card))
18+
table.insert(handA, get_value(card))
1919
end
2020

2121
local handB = {}
2222
for _, card in ipairs(playerB) do
23-
table.insert(handB, getCardValue(card))
23+
table.insert(handB, get_value(card))
2424
end
2525

2626
local turn = 'A'
@@ -58,36 +58,34 @@ local function simulateGame(playerA, playerB)
5858
else
5959
turn = 'A'
6060
end
61-
else
62-
if currentDebt > 0 then
63-
currentDebt = currentDebt - 1
64-
if currentDebt == 0 then
65-
for _, p in ipairs(pile) do
66-
table.insert(otherHand, p)
67-
end
68-
pile = {}
69-
totalTricks = totalTricks + 1
70-
currentDebt = 0
71-
72-
if #handA == 0 or #handB == 0 then
73-
return { status = 'finished', tricks = totalTricks, cards = cardsPlayed }
74-
end
61+
elseif currentDebt > 0 then
62+
currentDebt = currentDebt - 1
63+
if currentDebt == 0 then
64+
for _, p in ipairs(pile) do
65+
table.insert(otherHand, p)
66+
end
67+
pile = {}
68+
totalTricks = totalTricks + 1
69+
currentDebt = 0
7570

76-
if turn == 'A' then
77-
turn = 'B'
78-
else
79-
turn = 'A'
80-
end
71+
if #handA == 0 or #handB == 0 then
72+
return { status = 'finished', tricks = totalTricks, cards = cardsPlayed }
8173
end
82-
else
74+
8375
if turn == 'A' then
8476
turn = 'B'
8577
else
8678
turn = 'A'
8779
end
8880
end
81+
else
82+
if turn == 'A' then
83+
turn = 'B'
84+
else
85+
turn = 'A'
86+
end
8987
end
9088
end
9189
end
9290

93-
return { simulateGame = simulateGame }
91+
return { simulate_game = simulate_game }

exercises/practice/camicia/.meta/spec_generator.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ return {
33

44
generate_test = function(case)
55
local lines = {}
6+
local function snake_case(str)
7+
local s = str:gsub('%u', function(c)
8+
return '_' .. c:lower()
9+
end)
10+
if s:sub(1, 1) == '_' then
11+
s = s:sub(2)
12+
end
13+
return s
14+
end
615
local function string_array(arr)
716
if #arr == 0 then
817
return "{}"
@@ -21,7 +30,8 @@ return {
2130
case.expected.tricks, case.expected.cards)
2231

2332
table.insert(lines, string.format("local expected = %s", expected))
24-
table.insert(lines, string.format("local result = camicia.%s(playerA, playerB)", case.property))
33+
34+
table.insert(lines, string.format("local result = camicia.%s(playerA, playerB)", snake_case(case.property)))
2535
table.insert(lines, "assert.are.same(expected, result)")
2636

2737
return table.concat(lines, "\n")
Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,91 @@
1-
local function simulateGame(playerA, playerB)
1+
local function get_value(card)
2+
if card == 'J' then
3+
return 1
4+
elseif card == 'Q' then
5+
return 2
6+
elseif card == 'K' then
7+
return 3
8+
elseif card == 'A' then
9+
return 4
10+
else
11+
return 0
12+
end
213
end
314

4-
return { simulateGame = simulateGame }
15+
local function simulate_game(playerA, playerB)
16+
local handA = {}
17+
for _, card in ipairs(playerA) do
18+
table.insert(handA, get_value(card))
19+
end
20+
21+
local handB = {}
22+
for _, card in ipairs(playerB) do
23+
table.insert(handB, get_value(card))
24+
end
25+
26+
local turn = 'A'
27+
local pile = {}
28+
local seen = {}
29+
local totalTricks = 0
30+
local cardsPlayed = 0
31+
local currentDebt = 0
32+
33+
while true do
34+
if #pile == 0 then
35+
local round = table.concat(handA, ",") .. "|" .. table.concat(handB, ",") .. "|" .. turn
36+
if seen[round] then
37+
return { status = 'loop', tricks = totalTricks, cards = cardsPlayed }
38+
end
39+
seen[round] = true
40+
end
41+
42+
local activeHand = turn == 'A' and handA or handB
43+
local otherHand = turn == 'A' and handB or handA
44+
45+
if #activeHand == 0 then
46+
local extraTrick = #pile == 0 and 0 or 1
47+
return { status = 'finished', tricks = totalTricks + extraTrick, cards = cardsPlayed }
48+
end
49+
50+
local cardVal = table.remove(activeHand, 1)
51+
table.insert(pile, cardVal)
52+
cardsPlayed = cardsPlayed + 1
53+
54+
if cardVal > 0 then
55+
currentDebt = cardVal
56+
if turn == 'A' then
57+
turn = 'B'
58+
else
59+
turn = 'A'
60+
end
61+
elseif currentDebt > 0 then
62+
currentDebt = currentDebt - 1
63+
if currentDebt == 0 then
64+
for _, p in ipairs(pile) do
65+
table.insert(otherHand, p)
66+
end
67+
pile = {}
68+
totalTricks = totalTricks + 1
69+
currentDebt = 0
70+
71+
if #handA == 0 or #handB == 0 then
72+
return { status = 'finished', tricks = totalTricks, cards = cardsPlayed }
73+
end
74+
75+
if turn == 'A' then
76+
turn = 'B'
77+
else
78+
turn = 'A'
79+
end
80+
end
81+
else
82+
if turn == 'A' then
83+
turn = 'B'
84+
else
85+
turn = 'A'
86+
end
87+
end
88+
end
89+
end
90+
91+
return { simulate_game = simulate_game }

0 commit comments

Comments
 (0)