implement basic hotseat multiplayer

dev
LeRoyce Pearson 2024-02-19 23:30:37 -07:00
parent 51e6b293e9
commit 41ede6d1cb
1 changed files with 71 additions and 18 deletions

View File

@ -9,11 +9,12 @@
-- stack of cards that cards are drawn from -- stack of cards that cards are drawn from
draw_pile=nil draw_pile=nil
discard_pile=nil discard_pile=nil
cards_in_hand=nil players_hands=nil
current_player=nil
-- List of cards that have been added to a meld, but are not yet placed on the tabletop. May be too -- List of cards that have been added to a meld, but are not yet placed on the tabletop. May be too
-- few cards for a meld. Should be verified before adding it to the list of melds on the tabletop. -- few cards for a meld. Should be verified before adding it to the list of melds on the tabletop.
cards_in_meld_draft=nil cards_in_meld_draft=nil
melds_on_tabletop=nil melds_on_tabletop={}
-- `points_of_interest` is an array of things that can be hovered/selected by the controller -- `points_of_interest` is an array of things that can be hovered/selected by the controller
current_page=nil current_page=nil
@ -21,19 +22,7 @@ hovered=nil
current_handler=nil current_handler=nil
function BOOT() function BOOT()
draw_pile = create_deck()
draw_pile:shuffle()
cards_in_hand = draw_pile:draw_stack(10)
cards_in_hand:sort(rummy_hand_sort_comparison)
cards_in_meld_draft=CardStack:new()
discard_pile = draw_pile:draw_stack(1)
current_handler=handler_main_menu current_handler=handler_main_menu
melds_on_tabletop={}
end end
function TIC() function TIC()
@ -50,6 +39,7 @@ function TIC()
if response.redirect then if response.redirect then
current_handler=response.redirect current_handler=response.redirect
request=nil request=nil
hovered=nil
elseif response.page then elseif response.page then
current_page=response.page current_page=response.page
else else
@ -145,11 +135,40 @@ function TIC()
end end
function handler_main_menu(action) function handler_main_menu(action)
local number_of_players=nil
if action=="start-1-player" then if action=="start-1-player" then
number_of_players=1
return { redirect=handler_draw_card } return { redirect=handler_draw_card }
elseif action=="start-2-player" then elseif action=="start-2-player" then
number_of_players=2
elseif action=="start-3-player" then elseif action=="start-3-player" then
number_of_players=3
elseif action=="start-4-player" then elseif action=="start-4-player" then
number_of_players=4
end
local num_cards_for_num_players={ 10, 10, 7, 7, 6, 6 }
if number_of_players then
draw_pile=create_deck()
draw_pile:shuffle()
local cards_per_player=num_cards_for_num_players[number_of_players]
players_hands={}
for i=1,number_of_players do
local player_hand=draw_pile:draw_stack(cards_per_player)
player_hand:sort(rummy_hand_sort_comparison)
table.insert(players_hands, player_hand)
end
discard_pile = draw_pile:draw_stack(1)
current_player=1
cards_in_meld_draft=CardStack:new()
return { redirect=handler_start_of_turn }
end end
local player_count_cards={ local player_count_cards={
@ -185,6 +204,31 @@ function handler_main_menu(action)
return { page=elements } return { page=elements }
end end
function handler_start_of_turn(action)
if action=="start-turn" then
hovered=nil
return { redirect=handler_draw_card }
end
local elements={}
table.insert(elements, {
visual="Player "..current_player.."'s turn",
textx=60, texty=40,
})
local space_around=((240-24)/(2))
local x=space_around
table.insert(elements, {
visual="OK",
textx=space_around+8, texty=68+10,
x=space_around, y=68,
action="start-turn",
})
return { page=elements }
end
function get_actions_from_page(page) function get_actions_from_page(page)
local actions={} local actions={}
for i,element in ipairs(page) do for i,element in ipairs(page) do
@ -285,7 +329,7 @@ end
function build_hand_ui(elements, hand, draft, can_select) function build_hand_ui(elements, hand, draft, can_select)
local hand_start_x=((240 - #cards_in_hand * 12 - 24) / 2) local hand_start_x=((240 - #hand * 12 - 24) / 2)
local hand_y=110 local hand_y=110
for i,card in ipairs(hand) do for i,card in ipairs(hand) do
if can_select then if can_select then
@ -313,6 +357,8 @@ function build_hand_ui(elements, hand, draft, can_select)
end end
function handler_draw_card(action) function handler_draw_card(action)
local cards_in_hand=players_hands[current_player]
if draw_pile:contains(action) then if draw_pile:contains(action) then
table.insert(cards_in_hand, draw_pile:draw()) table.insert(cards_in_hand, draw_pile:draw())
cards_in_hand:sort(rummy_hand_sort_comparison) cards_in_hand:sort(rummy_hand_sort_comparison)
@ -335,6 +381,8 @@ function handler_draw_card(action)
end end
function handler_player_action(action) function handler_player_action(action)
local cards_in_hand=players_hands[current_player]
if cards_in_hand:contains(action) then if cards_in_hand:contains(action) then
if cards_in_meld_draft:contains(action) then if cards_in_meld_draft:contains(action) then
table.remove(cards_in_meld_draft, cards_in_meld_draft:index_of(action)) table.remove(cards_in_meld_draft, cards_in_meld_draft:index_of(action))
@ -361,7 +409,7 @@ function handler_player_action(action)
return { redirect=handler_discard_confirm } return { redirect=handler_discard_confirm }
end end
elseif table_index_of(melds_on_tabletop, action) then elseif table_index_of(melds_on_tabletop, action) then
local meld=point_of_interest.interest local meld=action
local meld_with_draft=meld:with(cards_in_meld_draft) local meld_with_draft=meld:with(cards_in_meld_draft)
if rummy_is_valid_meld(meld_with_draft) then if rummy_is_valid_meld(meld_with_draft) then
for i,card in ipairs(cards_in_meld_draft) do for i,card in ipairs(cards_in_meld_draft) do
@ -385,6 +433,8 @@ function handler_player_action(action)
end end
function handler_player_secondary_action(action) function handler_player_secondary_action(action)
local cards_in_hand=players_hands[current_player]
if cards_in_hand:contains(action) then if cards_in_hand:contains(action) then
if cards_in_meld_draft:contains(action) then if cards_in_meld_draft:contains(action) then
table.remove(cards_in_meld_draft, cards_in_meld_draft:index_of(action)) table.remove(cards_in_meld_draft, cards_in_meld_draft:index_of(action))
@ -401,7 +451,7 @@ function handler_player_secondary_action(action)
return { redirect=handler_discard_confirm } return { redirect=handler_discard_confirm }
end end
elseif table_index_of(melds_on_tabletop, action) then elseif table_index_of(melds_on_tabletop, action) then
local meld=point_of_interest.interest local meld=action
local meld_with_draft=meld:with(cards_in_meld_draft) local meld_with_draft=meld:with(cards_in_meld_draft)
if rummy_is_valid_meld(meld_with_draft) then if rummy_is_valid_meld(meld_with_draft) then
for i,card in ipairs(cards_in_meld_draft) do for i,card in ipairs(cards_in_meld_draft) do
@ -425,9 +475,12 @@ function handler_player_secondary_action(action)
end end
function handler_discard_confirm(action) function handler_discard_confirm(action)
local cards_in_hand=players_hands[current_player]
if action=="End\nTurn" then if action=="End\nTurn" then
hovered = nil hovered = nil
return { redirect=handler_draw_card } current_player=((current_player%#players_hands)+1)
return { redirect=handler_start_of_turn }
end end
local elements={} local elements={}