% Original author: En Yi % The card container class used in the solitaire game % Not optimised though % Anyone can modify it, just need to give credits to the original author classdef cardHolder < handle %% Deck Properties properties (SetAccess = private) %Deck position x % x position of the top left of the holder y % y position of the top left of the holder % Card dimension and offset card_width card_height offset % The offset between display each card in a deck cards % The cards is currently holding % Card deck orientation and dimensions deck_orientation % Either display horizontal or vertical start_display_index % Display only a number of cards in a deck, -1 for display all current_display_index % Current number of cards to be displayed % Overall deck dimension, used for collision purposes deck_width deck_height % Deck properties always_hidden % Never open the hidden cards receivable % Deck graphics handle, used for updating the deck's graphics card_graphics_data = {} card_draw_handle = [] card_text = [] end properties (SetAccess = public) selected_start_index % The card index which is selected hidden_start_index % The number of cards that is hidden end methods %% Constructor function cH = cardHolder(x,y,cards,card_width,card_height,offset,deck_orientation,start_display_index,hidden_cards,always_hidden,receivable) cH.x = x; cH.y = y; cH.deck_orientation = deck_orientation; if start_display_index<1 start_display_index = -1; end cH.start_display_index = start_display_index; cH.current_display_index = start_display_index; cH.cards = cards; cH.card_width = card_width; cH.card_height = card_height; cH.offset = offset; cH.receivable = receivable ; cH.always_hidden = always_hidden; hidden_cards = min(hidden_cards,length(cards)); cH.hidden_start_index = hidden_cards; cH.selected_start_index = 0; cH.update_deck_dimensions() end %% Deck Get Functions % Get the number of cards in the deck function n_of_cards = get_Number_Of_Cards(cH) n_of_cards = length(cH.cards); end function receive = is_Receivable(cH) receive = cH.receivable; end % Get the card at the bottom of the selected cards function card = get_bottom_selected(cH) card = cH.cards(end-cH.selected_start_index+1); end % Get the last card, which is top of the deck function lastcard = get_Last_Cards(cH) % If empty, return 0 if cH.is_Empty() lastcard = 0; return end if (cH.get_Number_Of_Cards()-cH.hidden_start_index)>0 lastcard = cH.cards(end); % Return the card number if not hidden else lastcard = -1; % If hidden, return -1; end end %% Deck Check Functions %Check if the deck is empty function empty = is_Empty(cH) empty = (cH.get_Number_Of_Cards() == 0); end %Check if there is a collision with a deck function collide = check_Deck_Collision(cH,x,y,type) if strcmp(type,'full') %Check for entire deck collision xrange = [cH.x cH.x+cH.deck_width]; yrange = [cH.y-cH.deck_height cH.y]; elseif strcmp(type,'first') %Check for only first card collision xoffset = cH.deck_width - cH.card_width; yoffset = cH.deck_height - cH.card_height; xrange = [cH.x+xoffset cH.x+cH.deck_width]; yrange = [cH.y-cH.deck_height cH.y-yoffset]; end collide = (x>xrange(1) && xyrange(1) && y