First Commit - Add the main file, cardHolder class, Card class, and the card images file

master
Howard T 2017-04-26 21:50:09 +01:00
commit 37b4a8a477
4 changed files with 641 additions and 0 deletions

27
Cards.m 100644
View File

@ -0,0 +1,27 @@
classdef Cards
properties(SetAccess = private)
value
image_data
backimage_data
end
methods
function crd = Cards(value,image_data,backimage_data)
crd.value = value;
crd.image_data = image_data;
crd.backimage_data = backimage_data;
end
function [num,colour,suit] = get_Card_Info(crd)
num = mod(crd.value,100);
suit = floor(crd.value/100);
colour = mod(suit,2);
end
function img_data = get_Card_Image(crd,side)
if strcmp(side,'back')
img_data = crd.backimage_data;
else
img_data = crd.image_data;
end
img_data = (double(flipud(img_data)))/255;
end
end
end

296
cardHolder.m 100644
View File

@ -0,0 +1,296 @@
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) && x<xrange(2)&& y>yrange(1) && y<yrange(2));
end
%Check the selected card index
function sel_num = check_selection(cH,sel_x,sel_y)
% If the deck is empty, don't proceed to calculations
if cH.is_Empty()
sel_num = 0;
return
end
% Set up for the calculation depending on orientation
if strcmp(cH.deck_orientation,'vertical')
sel_point = sel_y;
ref_point = cH.y-cH.deck_height;
first_card_range = cH.card_height;
elseif strcmp(cH.deck_orientation,'horizontal')
sel_point = sel_x;
ref_point = cH.x+cH.deck_width;
first_card_range = cH.card_width;
end
% Check which card is selected
relative_sel_point = abs(sel_point-ref_point);
if relative_sel_point<=first_card_range
sel_num = 1;
else
sel_num = 1+ceil((relative_sel_point - first_card_range)/cH.offset);
end
% Check if the selected card is hidden
if sel_num>cH.get_Number_Of_Cards()-cH.hidden_start_index
sel_num = -1;
end
end
%% Deck Modifying Functions
% Add cards to the end, which is the top of the deck
function append_Cards(cH,new_cards)
cH.cards = [cH.cards new_cards];
if cH.start_display_index>=0
% Increase the current number of cards to display
cH.current_display_index = cH.current_display_index+length(new_cards);
% This is to limit how many cards can be display at a time,may make it conditional
cH.current_display_index = min(cH.current_display_index,cH.start_display_index);
end
cH.update_deck_dimensions(); %Update the deck dimensions
end
% Remove the selected cards
function remove_Selected_Cards(cH)
cH.cards = cH.cards(1:end-cH.selected_start_index);
if cH.current_display_index>0
% Decrease the current number of cards to display
cH.current_display_index = cH.current_display_index-cH.selected_start_index;
cH.current_display_index = max(cH.current_display_index,1);
end
%fprintf('Current display index: %d\n',cH.current_display_index)
%cH.current_display_index = min(cH.current_display_index,cH.start_display_index);
%cH.selected_start_index = 0;
cH.update_deck_dimensions()
end
% Reveal a specified number of hidden cards from the top
function reveal_Hidden_Card(cH,amount)
cH.hidden_start_index = cH.hidden_start_index - amount;
end
% Transfer selected cards to another deck
function transfer_Selected_Cards(cH,cH_to,varargin)
selected_cards = cH.cards(end-cH.selected_start_index+1:end);
if ~isempty(varargin)
selected_cards = fliplr(selected_cards);
end
cH_to.append_Cards(selected_cards);
cH.remove_Selected_Cards();
end
% Reset the number of cards to display
function set_Current_Display(cH,num)
if num<0
cH.current_display_index = cH.start_display_index;
else
cH.current_display_index = max(num,1);
end
cH.update_deck_dimensions()
end
%% Deck Update/Reset Functions
% Update the deck dimensions. MUST be called when cards are altered
function update_deck_dimensions(cH)
% Check how many are being display
if cH.start_display_index>0
cards_to_display = min(cH.current_display_index,cH.get_Number_Of_Cards());
else
cards_to_display = cH.get_Number_Of_Cards();
end
%If there is cards to display
if cards_to_display>0
if strcmp(cH.deck_orientation,'vertical')
cH.deck_width = cH.card_width;
cH.deck_height = cH.card_height+cH.offset*(cards_to_display-1);
elseif strcmp(cH.deck_orientation,'horizontal')
cH.deck_width = cH.card_width+cH.offset*(cards_to_display-1);
cH.deck_height = cH.card_height;
end
else
cH.deck_width = cH.card_width;
cH.deck_height = cH.card_height;
end
end
function update_Deck_Graphics(cH,disp_axes)
cH.wipe_Deck_Graphics();
cH.render_Deck(disp_axes);
end
% Clear the cards in the deck
function clear_Deck(cH)
cH.cards = [];
end
%% Deck Console Functions
% Display the cards on console
function display_Cards(cH)
disp(cH.cards)
end
%% Rendering functions
% Render the deck outline
function render_deck_outline(cH,disp_axes)
line(disp_axes,[cH.x cH.x+cH.card_width cH.x+cH.card_width cH.x cH.x],...
[cH.y cH.y cH.y-cH.card_height cH.y-cH.card_height cH.y],...
'PickablePart','none','Color',[1 1 1],'LineWidth',1)
end
% Render the cards in the deck
function render_Deck(cH,disp_axes)
% Draw shadow
% if cH.get_Number_Of_Cards()>0
% deck_vertices(1,:) = [cH.x cH.x+cH.deck_width cH.x+cH.deck_width cH.x]+5;
% deck_vertices(2,:) = [cH.y cH.y cH.y-cH.deck_height cH.y-cH.deck_height]+5;
% cH.card_draw_handle(1) = patch(deck_vertices(1,:),deck_vertices(2,:),[1 0 0],'Parent',disp_axes,'PickableParts','none');
% end
if cH.current_display_index>0
start_card = cH.get_Number_Of_Cards()-cH.current_display_index+1;
start_card = max(start_card,1);
else
start_card = 1;
end
for i = start_card:cH.get_Number_Of_Cards()
if strcmp(cH.deck_orientation,'vertical')
botleft_x = cH.x+0.5;
botright_y = cH.y-cH.card_height -cH.offset*(i-start_card)+0.5;
% text_pos = [cH.x cH.y-cH.offset*(i-start_card)-0.5];
elseif strcmp(cH.deck_orientation,'horizontal')
botleft_x = cH.x +cH.offset*(i-start_card)+0.5;
botright_y = cH.y-cH.card_height+0.5;
% text_pos = [cH.x+cH.offset*(i-start_card) cH.y-0.5];
end
% The selection starts from the top of the stack
if (i<=cH.hidden_start_index || cH.always_hidden)
%set(cH.card_draw_handle(i),'AlphaData',0.5);
img = cH.cards(i).get_Card_Image('back');
else
img = cH.cards(i).get_Card_Image('front');
end
if i > length(cH.cards)- cH.selected_start_index
dark = ones(size(img))*0.2;
img = img - dark;
end
cH.card_draw_handle(i+1) = image(disp_axes,botleft_x,botright_y,...
img,'PickablePart','none');
% if i > length(cH.cards)- cH.selected_start_index
% set(cH.card_draw_handle(i),'AlphaData',0.5);
% end
% cH.card_text(i) = text(disp_axes,text_pos(1),text_pos(2),num2str(cH.cards(i)),'PickableParts','none');
end
end
% Delete the display cards in the deck, used for updating the deck
function wipe_Deck_Graphics(cH)
if ~isempty(cH.card_draw_handle)
delete(cH.card_draw_handle)
cH.card_draw_handle = [];
end
if ~isempty(cH.card_text)
delete(cH.card_text)
cH.card_text = [];
end
end
end
end

BIN
card_images.mat 100644

Binary file not shown.

318
sulitear.m 100644
View File

@ -0,0 +1,318 @@
function sulitear()
clc;
% Construct the window with the axes
scrsz = get(0,'ScreenSize');
%TODO Find new method to draw the window
%start_dim = min(scrsz(3)/1.5,scrsz(4)/1.5);%Used for rescaling
win_ratio = scrsz(3:4)/scrsz(3);
win_size = scrsz(3:4)*0.8;
win = figure('ToolBar','none','Name','Solitaire',...
'NumberTitle','off','MenuBar','none',...
'Resize','off','Visible','off','Color',[0 0 0]/255,...
'Position',[scrsz(3:4)-win_size*1.05 win_size],...
'ButtonDownFcn',@check_clicked_deck,...
'KeyPressFcn',@restart);
%Prepare card decks and the playing field
playing_cards = prepare_playing_cards();
[playing_decks,draw_deck,discard_deck,goal_decks,playfield_size] = prepare_cardHolders(playing_cards,win_ratio);
disp_axes = axes('Parent',win,'Position',[0 0 1 1]);
set(disp_axes,'Xlim',[0 playfield_size(1)],'Ylim',[0 playfield_size(2)],...
'XLimMode','manual','YLimMode','manual','Visible','off','NextPlot','add');
set(win,'Visible','on')
% Prepare some variable to indicate cards are selected HERE
transferring_deck = 0;
% draw it on the axes
draw_playfield();
%% Callback functions
%%% Idea: can optimise by splitting the window into regions
function check_clicked_deck(~,~)
if ~strcmp(get(win,'selectiontype'),'normal')
return
end
[Xx,Yy] = get_mouse_pos();
if draw_deck.check_Deck_Collision(Xx,Yy,'first')
reset_card_selection();
if draw_deck.get_Number_Of_Cards() + discard_deck.get_Number_Of_Cards()>0
if draw_deck.get_Number_Of_Cards()>0 % If there's cards
transferring_deck = draw_deck;
draw_deck.selected_start_index = min(draw_deck.get_Number_Of_Cards(),3);
discard_deck.set_Current_Display(draw_deck.selected_start_index); % Set the discard to show that amount of cards transferred
draw_deck.transfer_Selected_Cards(discard_deck,'flip'); % Transfer cards to discard pile, up to 3
reset_card_selection();
else
transferring_deck = discard_deck; % Transfer back the cards from discard pile
discard_deck.selected_start_index = discard_deck.get_Number_Of_Cards();
discard_deck.transfer_Selected_Cards(draw_deck,'flip');
reset_card_selection();
end
draw_deck.update_Deck_Graphics(disp_axes);
discard_deck.update_Deck_Graphics(disp_axes);
end
return
end
if discard_deck.check_Deck_Collision(Xx,Yy,'first') % Else check for discard deck
if transferring_deck == discard_deck
reset_card_selection();
return
end
reset_card_selection();
if discard_deck.get_Number_Of_Cards() > 0 % Only allow selection, up to one card
discard_deck.selected_start_index = 1;
transferring_deck = discard_deck;
discard_deck.update_Deck_Graphics(disp_axes);
end
return
end
for i = 1:length(playing_decks)
% This part is only for the playing deck
if playing_decks(i).check_Deck_Collision(Xx,Yy,'full') %Check if any deck is clicked
selected_deck = playing_decks(i);
s_index = selected_deck.check_selection(Xx,Yy); %If so, check which card is selected
%If no selection was before or more than a card is
%selected and there are cards remaining in the deck
% if playing_decks(i).check_Deck_Collision(Xx,Yy,'first') && s_index == -1
% reset_card_selection();
% selected_deck.reveal_Hidden_Card(1)
% selected_deck.update_Deck_Graphics(disp_axes);
% break
% end
if (transferring_deck ~= 0)
%if s_index>=0
if transferring_deck ~= selected_deck % If the selected deck is not the previously selected deck
[transferring_num,transferring_col]= determine_card(get_bottom_selected(transferring_deck));
[destination_num,destination_col] = determine_card(selected_deck.get_Last_Cards());
if (transferring_col ~= destination_col &&... % If the colour alternates
transferring_num == destination_num-1) % If the number are in sequence
transfer_Selected_Cards(transferring_deck,selected_deck);
end
% Reveal a hidden card if there is one
auto_open_hiddencard();
end
%end
reset_card_selection();
else
% Otherwise move previously selected cards to the currently selected deck
if s_index>0
%Get the selected cards to be transfered
reset_card_selection();
selected_deck.selected_start_index = s_index;
transferring_deck = selected_deck;
end
end
selected_deck.update_Deck_Graphics(disp_axes);
return
end
end
for i = 1:length(goal_decks)
if goal_decks(i).check_Deck_Collision(Xx,Yy,'first')
selected_deck = goal_decks(i);
if (transferring_deck ~= 0)
if transferring_deck.selected_start_index == 1
[transferring_num,~,transferring_suit]= determine_card(get_bottom_selected(transferring_deck));
[destination_num,~,destination_suit] = determine_card(selected_deck.get_Last_Cards());
if (transferring_suit == destination_suit && transferring_num == destination_num+1)...
|| transferring_num == 1
transfer_Selected_Cards(transferring_deck,selected_deck);
end
end
auto_open_hiddencard();
reset_card_selection();
else
if ~selected_deck.is_Empty()
reset_card_selection();
selected_deck.selected_start_index = 1;
transferring_deck = selected_deck;
end
end
selected_deck.update_Deck_Graphics(disp_axes);
total_goal_cards = 0;
for j = 1:length(goal_decks)
total_goal_cards = total_goal_cards+goal_decks(j).get_Number_Of_Cards();
end
if total_goal_cards == 52
uiwait(msgbox('You Won! Press R to Try Again!','YAY!','modal'));
end
return
end
end
%%% TODO: check for winning condition
end
%% Non-Callback functions
%Prepare the card holders
function restart(~,evtdata)
if strcmp(evtdata.Key,'r')
reset_entire_game(playing_cards);
end
end
function [playing_decks,draw_deck,discard_deck,goal_decks,playfield_size] = prepare_cardHolders(cards,win_ratio)
%%% TODO: Automate the draw, discard piles and the goal pile
card_size = size(cards(1).get_Card_Image('front'));
card_width = card_size(2);
card_height = card_size(1);
offset = round(card_width);
n = 7;
border_offset = 10;
playfield_width = round((card_size(2)+offset)*n-offset+2*border_offset);
i = 1;
while(playfield_width-card_width*i>=0)
i = i+1;
end
playfield_width = card_width*i;
i = 1;
playfield_size = round([playfield_width playfield_width].*win_ratio);
while(playfield_size(2)-card_height*i>=0)
i = i+1;
end
playfield_size(2) = card_height*i;
% Input the number of decks and offset between deck position
% Compute the position and dimensions
start_x = border_offset;
start_y =playfield_size(2)-card_height-4*border_offset;
card_offset = (start_y-card_height-offset)/18;
% Initialise the card holders
% Manually initialise for testing purposes
draw_deck = cardHolder(start_x,playfield_size(2)-border_offset,...
[],card_width,card_height,card_offset,'horizontal',1,1,1,0);
discard_deck = cardHolder(start_x+card_width+offset,playfield_size(2)-border_offset,...
[],card_width,card_height,card_offset,'horizontal',3,0,0,0);
for i = 4:n
goal_decks(i-3) = cardHolder(start_x+(card_width+offset)*(i-1),...
playfield_size(2)-border_offset,[],card_width,card_height,card_offset,'vertical',1,0,0,1);
end
a = randperm(length(cards));
remaining_cards = cards(a);
%Loop method, which will be used
for i = 1:n
dealt_cards = remaining_cards(1:i);
playing_decks(i) = cardHolder(start_x+(card_width+offset)*(i-1),...
start_y,dealt_cards,card_width,card_height,card_offset,'vertical',-1,i-1,0,1);
remaining_cards = remaining_cards(i+1:end);
end
draw_deck.append_Cards(remaining_cards);
end
% Prepare a deck of cards
function all_cards = prepare_playing_cards()
card_values = cumsum(ones(13,4))'+ cumsum(ones(4,13)*100);
try
crd = load('card_images.mat');
card_images = crd.cards;
card_backimage = crd.card_backimage;
for i = 1:52
j = ceil(i/13);
k = mod(i-1,13)+1;
all_cards(i) = Cards(card_values(j,k),card_images{j,k},card_backimage);
end
catch
disp('Load failed')
close all
end
end
% Distribute the playing cards
function [dealt_cards,remaining_cards] = deal_cards(cards,amount)
remaining_cards = cards;
for i = 1:amount
n_of_cards = length(remaining_cards);
index = randi(n_of_cards);
dealt_cards(i) = remaining_cards(index);
remaining_cards = [remaining_cards(1:index-1) remaining_cards(index+1:end)];
end
end
function reset_entire_game(cards)
a = randperm(length(cards));
remaining_cards = cards(a);
for i = 1:length(playing_decks)
playing_decks(i).clear_Deck();
dealt_cards= remaining_cards(1:i);
playing_decks(i).append_Cards(dealt_cards);
playing_decks(i).hidden_start_index = i-1;
playing_decks(i).update_Deck_Graphics(disp_axes);
remaining_cards = remaining_cards(i+1:end);
end
for i = 1:length(goal_decks)
goal_decks(i).clear_Deck();
goal_decks(i).update_Deck_Graphics(disp_axes);
end
discard_deck.clear_Deck();
discard_deck.update_Deck_Graphics(disp_axes);
draw_deck.clear_Deck();
draw_deck.append_Cards(remaining_cards);
draw_deck.update_Deck_Graphics(disp_axes);
reset_card_selection();
end
% Reset the card selection to none and updating the deck graphic
function reset_card_selection()
if transferring_deck ~= 0
transferring_deck.selected_start_index = 0;
transferring_deck.update_Deck_Graphics(disp_axes)
transferring_deck = 0;
end
end
% Draw the play field
function draw_playfield()
cla(disp_axes); % Clear the play field axes
for i = 1:length(playing_decks)
playing_decks(i).render_deck_outline(disp_axes);
playing_decks(i).update_Deck_Graphics(disp_axes);
end
draw_deck.render_deck_outline(disp_axes);
draw_deck.update_Deck_Graphics(disp_axes);
%discard_deck.render_deck_outline(disp_axes);
discard_deck.update_Deck_Graphics(disp_axes);
for i = 1:length(goal_decks)
goal_decks(i).render_deck_outline(disp_axes);
goal_decks(i).update_Deck_Graphics(disp_axes);
end
end
% Determine the number, colour , and suit of the colours
function [num,colour,suit] = determine_card(card,varargin)
if isa(card, 'Cards')
[num,colour,suit] = card.get_Card_Info();
else
if card == 0
if ~isempty(varargin)
num = varargin{1};
else
num = 14;
end
else
num = -1;
end
suit = -1;
colour = -1;
end
end
function auto_open_hiddencard()
if (transferring_deck.hidden_start_index >0 ...
&& transferring_deck.get_Number_Of_Cards() == transferring_deck.hidden_start_index)
transferring_deck.reveal_Hidden_Card(1)
end
end
% Get the mouse position
function[X,Y]= get_mouse_pos()
mpos = get(disp_axes,'CurrentPoint');
X = mpos(1,1);
Y = mpos(1,2);
end
end