No BSD License  

Highlights from
Pakau

image thumbnail
from Pakau by Chee Lam Ng
Pakau is a poker game.

pakau_gui(varargin)
function varargout = pakau_gui(varargin)
% PAKAU_GUI M-file for pakau_gui.fig
%      PAKAU_GUI, by itself, creates a new PAKAU_GUI or raises the existing
%      singleton*.
%
%      H = PAKAU_GUI returns the handle to a new PAKAU_GUI or the handle to
%      the existing singleton*.
%
%      PAKAU_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PAKAU_GUI.M with the given input arguments.
%
%      PAKAU_GUI('Property','Value',...) creates a new PAKAU_GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before pakau_gui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to pakau_gui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Copyright 2002-2003 The MathWorks, Inc.

% Edit the above text to modify the response to help pakau_gui

% Last Modified by GUIDE v2.5 10-Feb-2005 12:47:44

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pakau_gui_OpeningFcn, ...
                   'gui_OutputFcn',  @pakau_gui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before pakau_gui is made visible.
function pakau_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to pakau_gui (see VARARGIN)
 
handles.deck = zeros(52,1);
handles.deck_value = zeros(52,1);
for card=1:52
    % shuffle deck from 1 to 52.
    while(1)
        i = floor(rand(1) * 52)+1;        
        if handles.deck(i) == 0
            break;
        end
    end
    handles.deck(i) = card;
    
    % determine card's value
    temp = mod(card-1, 13) + 1;
    if temp > 9
        handles.deck_value(i) =  10;
    else
        handles.deck_value(i) = temp;
    end
end

handles.ncard = 1;          % starting card
handles.board = [];         % empty card board
handles.board_value = [];   % empty card board value
handles.situation = 0;      % non-answer situation

% Choose default command line output for pakau_gui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes pakau_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = pakau_gui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton3



function board_edit_Callback(hObject, eventdata, handles)
% hObject    handle to board_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of board_edit as text
%        str2double(get(hObject,'String')) returns contents of board_edit as a double


% --- Executes during object creation, after setting all properties.
function board_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to board_edit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on button press in deal_pushbutton.
function deal_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to deal_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

m = 3;  % 3-pairs pakau

% set all radiobuttons off 
for i=1:m
    set(eval(['handles.radiobutton' num2str(i)]),'Enable','off');
    set(eval(['handles.radiobutton' num2str(i)]),'Value',0);
end
set(handles.go_pushbutton,'Enable','off');

ncard = handles.ncard;  % current card
handles.board = [handles.board handles.deck(ncard)];    % adds card into board
handles.board_value = [handles.board_value handles.deck_value(ncard)];    
n = length(handles.board);  % numbers of cards inside board

% cards remaining
set(handles.r_text,'String',num2str(52-ncard));

% cards' symbol
symbols = [' A';' 2';' 3';' 4';' 5';' 6';' 7';' 8';' 9';'10';' J';' Q';' K'];

%-------- Summing, Checking, and Deleting Process -----------
% at least three cards are need to do the process
if n >= 3
    % summing:
    total = zeros(1,m);
    for situation=1:m
        total(situation) = sum(handles.board_value([1:situation-1, n-m+situation:n]));
    end

    % checking:
    answer = 10;    % when total can be divided by 10
    choice = [];
    for situation=1:m
        if mod(total(situation), answer) == 0
            choice = [choice situation];
        end
    end

    % choose situation
    for i=1:length(choice)
        set(eval(['handles.radiobutton' num2str(choice(i))]),'Enable','on');        
    end
    if length(choice) >= 1
        set(handles.go_pushbutton,'Enable','on');
    end
end
%------------------------------------------------------------

% show cards
out = '';
for i=1:length(handles.board);
    % newline when reach 13 cards 
    if mod(i-1,13) == 0
        out = sprintf('%s\n',out);
    end
    out = sprintf('%s%3s',out,symbols(mod(handles.board(i)-1,13)+1,:));    
end
set(handles.board_edit,'String',out);


% 'off' deal pushbutton when cards were finish dealed.
handles.ncard = handles.ncard + 1;


if handles.ncard > 52 
    set(handles.deal_pushbutton,'Enable','off');
    % win ?
    if length(handles.board) > 1 && length(choice) == 0        
        set(handles.result_text,'String','Game Over');
    end
end


% Update handles structure
guidata(hObject, handles);


% --------------------------------------------------------------------
function uipanel2_SelectionChangeFcn(hObject, eventdata, handles)
% hObject    handle to uipanel2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

switch get(hObject,'Tag')
    case 'radiobutton1'
        handles.situation = 1;
    case 'radiobutton2'             
        handles.situation = 2;
    case 'radiobutton3'        
        handles.situation = 3;
end

guidata(hObject, handles);


% --- Executes on button press in go_pushbutton.
function go_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to go_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

m = 3;
n = length(handles.board);
symbols = [' A';' 2';' 3';' 4';' 5';' 6';' 7';' 8';' 9';'10';' J';' Q';' K'];
situation = handles.situation;

if situation ~= 0
    handles.board([1:situation-1, n-m+situation:n]) = [];
    handles.board_value([1:situation-1, n-m+situation:n]) = [];

    % show cards
    out = '';
    for i=1:length(handles.board);
        % newline when reach 13 cards
        if mod(i-1,13) == 0
            out = sprintf('%s\n',out);
        end
        out = sprintf('%s%3s',out,symbols(mod(handles.board(i)-1,13)+1,:));
    end
    set(handles.board_edit,'String',out);

    % set all radiobuttons off
    for i=1:m
        set(eval(['handles.radiobutton' num2str(i)]),'Enable','off');
        set(eval(['handles.radiobutton' num2str(i)]),'Value',0);
    end
    set(handles.go_pushbutton,'Enable','off');

    %-------- Summing, Checking, and Deleting Process -----------    
    n = length(handles.board);
    % at least three cards are need to do the process
    if n >= 3
        % summing:
        total = zeros(1,m);
        for situation=1:m
            total(situation) = sum(handles.board_value([1:situation-1, n-m+situation:n]));
        end

        % checking:
        answer = 10;
        choice = [];
        for situation=1:m
            if mod(total(situation), answer) == 0
                choice = [choice situation];
            end
        end

        % choose situation
        for i=1:length(choice)
            set(eval(['handles.radiobutton' num2str(choice(i))]),'Enable','on');
        end
        if length(choice) >= 1
            set(handles.go_pushbutton,'Enable','on');
        end
    end
    %---------------------------------------------------------------
    
    % win ?
    if handles.ncard > 52 && length(choice) == 0
        if length(handles.board) == 1
            set(handles.result_text,'String','You WIN');
        else 
            set(handles.result_text,'String','Game Over');
        end
    end
end
handles.situation = 0;
guidata(hObject, handles);

Contact us at files@mathworks.com