No BSD License  

Highlights from
GUI Multi-Media Countdown Letters Game

image thumbnail
from GUI Multi-Media Countdown Letters Game by Murphy O'Brien
GUI, Multi-Media version of the letters game from the TV Show, Countdown.

playletters(nplay)
function playletters(nplay)
%  function matches=lettersgame(letters,[nletters])
% solves the countdown letters game
% e.g.a=lettersgame('cedeinter')
% the optional argument nletters gives smallest output word size (default is 4)
%
rand('state',sum(100*clock))    % randomise
sc1=0;sc2=0;

if nargin<1
    nplay=4;                        % four rounds by default
end

for jj=1:nplay          
    r=3*rand;
    if r<1.5
        prelet='k_';                % random letters voice
    elseif r>1.5
        prelet='s_';
    else
        prelet='m_';
    end

    fig1=findobj('UserData','CountdownFigure');             % Use countdown figure if there already?
    if isempty(fig1)
        fig1=figure;clf;                                    % create a new one if not              
        set(fig1,'UserData','CountdownFigure')          
        set(fig1,'Position',[512 40 640 480])
    end
    shg;
    score1h=findobj('UserData','CountDownScore1');          % Get score 1 so far if it exists
    if isempty(score1h)
        sc1=0;
     else
        % read the score off the scoreboard
        sc1=str2num(get(score1h,'String'));
     end
    score2h=findobj('UserData','CountDownScore2');          % Get score 1 so far if it exists
    if isempty(score2h)
        sc2=0;
    else
        % read the score off the scoreboard
        sc2=str2num(get(score2h,'String'));
    end
    % Now draw all the fig1 buttons, etc.
    clf(fig1)
    % Player 1 Score 
    score1h=uicontrol('Style', 'Text', 'String', [num2str(sc1)],'FontSize',18,'UserData','CountDownScore1','Position', [160 30 80 36], 'Callback', '');
    % Player 2 Score 
    score2h=uicontrol('Style', 'Text', 'String', [num2str(sc2)],'FontSize',18,'UserData','CountDownScore2','Position', [400 30 80 36], 'Callback', '');
    % Consonant push button
    cbuttonh = uicontrol('Style', 'pushbutton', 'String', 'Consonant','FontSize',14,'Position', [120 110 160 40],'UserData',0,'Callback', 'flagbutton');
    % Vowel push button
    vbuttonh = uicontrol('Style', 'pushbutton', 'String', 'Vowel','FontSize',14,'Position', [360 110 160 40],'UserData',0,'Callback', 'flagbutton');
    % Letters board (and Reset Button to start with)
    letterh = uicontrol('Style', 'pushbutton','String','Reset Scores','HorizontalAlignment','left','FontSize',32,'FontName','Lucida Console','Position',  [90 200 460 80],'UserData',0,'Callback','flagbutton');
    % Labels
    p1ha=uicontrol('Style','Text','Position',[160 70 80 24],'String','Player1','FontSize',14,'BackgroundColor',[0.8 0.8 0.8]);
    p2ha=uicontrol('Style','Text','Position',[400 70 80 24],'String','Player2','FontSize',14,'BackgroundColor',[0.8 0.8 0.8]);
    vowels=[];nvowel=0;
    consts=[];nconst=0;
    letters=[];
    while length(letters)<9                                 % Get 9 letters
        if get(letterh,'UserData')==1                       % Was RESET button pressed?
            sc1=0;
            sc2=0;
            set(score1h,'String',num2str(sc1))
            set(score2h,'String',num2str(sc2))
            set(letterh,'UserData',0,'Callback','');        % Letters Board not a reset button anymore
         end

        if get(vbuttonh,'UserData')==1                      % Was VOWEL button pressed?
            set(vbuttonh,'UserData',0)                      % unpress it
            vowels=newvowel(vowels);                        % Pick a new vowel
            nvowel=nvowel+1;
            letters=[letters vowels(nvowel)];
            set(letterh,'Style', 'Text','String',upper(letters),'FontSize',64)
            [lsound,fs]=mp3read(['samples\' prelet letters(end) '.mp3']);  % Read in letter sound
            sound(lsound,fs)                                % and play it
            set(letterh,'UserData',0,'Callback','');        % Letters Board not a reset button anymore
        end
        if get(cbuttonh,'UserData')==1                      % Was CONSONANT button pressed?
            set(cbuttonh,'UserData',0)                      % unpress it
            consts=newconst(consts);                        % Pick a new consonant
            nconst=nconst+1;
            letters=[letters consts(nconst)];
            set(letterh,'Style', 'Text','String',upper(letters),'FontSize',64)  
            [lsound,fs]=mp3read(['samples\' prelet letters(end) '.mp3']); % Read in letter sound
            sound(lsound,fs)                                % and play it
            set(letterh,'UserData',0,'Callback','');        % Letters Board not a reset button anymore
        end
        pause(0.1);drawnow;
    end        
    p1h=uicontrol('Style','Text','Position',[20 150 80 24],'String','Player1','FontSize',14,'BackgroundColor',[0.8 0.8 0.8]);
    p2h=uicontrol('Style','Text','Position',[330 150 80 24],'String','Player2','FontSize',14,'BackgroundColor',[0.8 0.8 0.8]);
    set(cbuttonh,'Style', 'Edit', 'String', '','HorizontalAlignment','left','UserData',0,'Position', [20 110 290 40], 'Callback', '');
    set(vbuttonh,'Style', 'Edit', 'String', '','HorizontalAlignment','left','UserData',0,'Position', [330 110 260 40], 'Callback', '');
    pause(1)
    tic
    [roundn,fs]=mp3read('samples\begin_m2.mp3');            % The Countdown Sound
    wavplay(roundn,fs);
    fig2=clocktick(30);                                     % The clock ticking
    [nextsay,fs]=mp3read('samples\p1howmany.mp3');
    sound(nextsay,fs);
    %             Use Editable Text String     The Message              Left Justify            itialise UserData           Place  Size       Callback Function      
    set(cbuttonh,'String', 'How Many Letters: ','UserData',0,'Callback', 'flagbutton');
    uicontrol(cbuttonh);    
    while get(cbuttonh,'UserData')==0
        pause(0.1);drawnow;
    end
    p1=get(cbuttonh,'String');
    p1=str2num(p1(end));
    [nextsay,fs]=mp3read('samples\p2howmany.mp3');
    sound(nextsay,fs);
    set(vbuttonh,'String', 'How Many Letters: ','UserData',0,'Callback', 'flagbutton');
    uicontrol(vbuttonh);
    while get(vbuttonh,'UserData')==0
        pause(0.1);drawnow;
    end
    p2=get(vbuttonh,'String');
    p2=str2num(p2(end));
    close(fig2);

    if p1<p2
        w1=getw1(cbuttonh);
        w2=getw2(vbuttonh);
    else
        w2=getw2(vbuttonh);
        w1=getw1(cbuttonh);
    end        

    allw=lettersgame(letters,2);         % Get all the possible words from these letters
    if p1~=numel(w1)|(p1<2)     
        s1=p1*(p1==numel(w1));           % 1 point for any 1 letter word, 0 otherwise
    else
        if any(all((allw{p1}==repmat(w1,size(allw{p1},1),1))'))
            s1=p1+9*(p1==9);            % 18 points for a 9 letter word
        else
            s1=0;
        end
    end
    if p2~=numel(w2)|(p2<2)
        s2=p2*(p2==numel(w2));          % 1 point for any 1 letter word, 0 otherwise
    else
        if any(all((allw{p2}==repmat(w2,size(allw{p2},1),1))'))
            s2=p2+9*(p2==9);            % 18 points for a 9 letter word
        else
            s2=0;
        end
    end
    bestw=[];
    for ii=1:9
        if ~isempty(allw{ii})
            nextbest=bestw;
            bestw=allw{ii};
        end
    end
    if s1>s2    % no points if less letters than the other player
        s2=0;
    end
    if s1<s2
        s1=0;
    end
    sc1=s1+sc1;
    sc2=s2+sc2;
    set(score1h,'String',num2str(sc1))
    set(score2h,'String',num2str(sc2))
    b1=uicontrol('Style','Text','Position',[40 450 200 24],'String',['Best Words (' num2str(numel(bestw(1,:))) ')'],'FontSize',14,'BackgroundColor',[0.8 0.8 0.8]);
    b2=uicontrol('Style','Edit','Position',[40 300 200 150],'max',3,'min',1, 'String', bestw,'FontSize',18,'Callback', '');
    b3=uicontrol('Style','Text','Position',[260 450 200 24],'String',['Next Best (' num2str(numel(nextbest(1,:))) ')'],'FontSize',14,'BackgroundColor',[0.8 0.8 0.8]);
    b4=uicontrol('Style','Edit','Position',[260 300 200 150],'max',3,'min',1, 'String', nextbest,'FontSize',18,'Callback', '');

    conth = uicontrol('Style', 'pushbutton', 'String', 'Continue','FontSize',20,'Position', [480 300 140 40],'UserData',0,'Callback', 'flagbutton');
    while(get(conth,'UserData')==0)     % wait until continue button pressed
        pause(0.1);drawnow;
    end    
end % next round



%  **************************************************************************************************************
%
%  **********************************     Supporting functions    ***********************************************
%
%  **************************************************************************************************************
%
%
%
function consts=newconst(consts);
    % pick a new consonant
    % Frequencies of consonants used by countdown TV game (e.g. cannot get 2 z's in a word)
    C='bbbccccdddddddffffgggghhjkllllllmmmmnnnnnnnppppqrrrrrrrrssssssssstttttttttvvwwxyyz';
    C=setdrop(C,consts);    % remove consonants already chosen
    consts=[consts C(ceil(rand*length(C)))];

function vowels=newvowel(vowels);
    % pick a new vowel
    % Frequencies of vowels used by countdown tv game
    V='aaaaaaaaaaaaaaaeeeeeeeeeeeeeeeeeeeeiiiiiiiiiiiiiioooooooooooooouuuuuuuuuu';
    V=setdrop(V,vowels);    % remove vowels already chosen
    vowels=[vowels V(ceil(rand*length(V)))];

function newset=setdrop(oldset,subset);
    % drop the elements subset from oldset
    newset=oldset;
    for ii=1:length(subset)
        a=findstr(newset,subset(ii));
        newset(a(1))=[];        % remove each vowel or consonant already used up
    end

function showletters(letterh,letters)

function flagbutton
    set(gcbo,'UserData',1)      % set user data to 1 so we'll know an action was done.

function fig2=clocktick(secs)
    [countclock,fs]=mp3read('samples\clock.mp3');
    while toc<1;end
    sound(countclock,fs);
    fig2=figure;
    set(fig2,'Position',[0 0 450 450])
    set(gca,'XTick',[])
    set(gca,'YTick',[])
    t=0:.05:2*pi+0.05;
    for ii=0:secs                     % Countdown the 30 seconds
        tic
        hold off
        plot(sin(t),-cos(t),'k','LineWidth',4)
        hold on
        plot(0.1*sin(t),-0.1*cos(t),'k','LineWidth',4)
        plot([0 -cos(pi/2+ii*pi/30)]*0.95,[0 sin(pi/2+ii*pi/30)]*0.95,'k','LineWidth',4)
        for jj=1:12;
            plot([0.8 0.9]*sin(jj*pi/6),[0.8 0.9]*cos(jj*pi/6),'k','LineWidth',2)
        end
        set(gca,'XTick',[])
        set(gca,'YTick',[])
        axis equal;
        shg;
        pause(0.3);
        while toc<1;end
    end

function w2=getw2(vbuttonh)
    [nextsay,fs]=mp3read('samples\p2word.mp3');
    sound(nextsay,fs);
    set(vbuttonh,'String','Your Word: ','UserData',0);
    uicontrol(vbuttonh);
    drawnow
    set(vbuttonh,'UserData',0);         % for some strange reason need to clear user data again here???
    while get(vbuttonh,'UserData')==0
        pause(0.1);drawnow;
    end
    w2=get(vbuttonh,'String');
    w2=w2(findstr(w2,':')+2:end);

function w1=getw1(cbuttonh)
    [nextsay,fs]=mp3read('samples\p1word.mp3');
    sound(nextsay,fs);
    set(cbuttonh,'String','Your Word: ','UserData',0);
    uicontrol(cbuttonh);    
    while get(cbuttonh,'UserData')==0
        pause(0.1);drawnow;
    end
    w1=get(cbuttonh,'String');
    w1=w1(findstr(w1,':')+2:end);

Contact us at files@mathworks.com