Permalink
Browse files

Merge pull request #2 from fronx/master

more tests, different default parameter handling
  • Loading branch information...
moink committed Oct 31, 2011
2 parents bc21c39 + e7f0bd2 commit 766a0cf7dcbcfc3b134b0ae3b59914f873605b2f
View
@@ -0,0 +1,19 @@
+function [sidelength,delta,freespace] = bingocarddefaults(...
+ sidelength,delta,freespace)
+% Default values: sidelength=5, delta=3*sidelength, freespace=1
+
+if ~exist('sidelength', 'var') || isempty(sidelength)
+ sidelength = 5;
+end
+if ~exist('delta', 'var') || isempty(delta)
+ delta = 3*sidelength;
+end
+if ~exist('freespace', 'var') || isempty(freespace)
+ freespace = mod(sidelength, 2);
+end
+if delta<sidelength
+ error('Second argument must be >= first argument')
+end
+if freespace && ~mod(sidelength, 2)
+ error('Freespace only allowed with odd-sized cards')
+end
View
Binary file not shown.
View
@@ -1,6 +1,6 @@
-%Monte Carlo simulation of Bingo games
-%Response to http://programmingpraxis.com/2009/02/19/bingo/
-%Output is :
+% Monte Carlo simulation of Bingo games
+% Response to http://programmingpraxis.com/2009/02/19/bingo/
+% Output is :
%{
averagefor1card =
@@ -14,12 +14,12 @@
Elapsed time is 219.205069 seconds.
%}
-%Of course, since it's stochastic, answer will vary slightly every time
-%Also, elapsed time will depend on the speed of your computer and will also
-%vary from run to run.
+% Of course, since it's stochastic, answer will vary slightly every time
+% Also, elapsed time will depend on the speed of your computer and will also
+% vary from run to run.
tic;
-averagefor1card=bingoprob(1)
+averagefor1card = bingoprob(1)
toc
tic;
-averagefor500cards=bingoprob(500)
-toc
+averagefor500cards = bingoprob(500)
+toc
View
@@ -1,4 +1,4 @@
-function won=checkbingocard(card)
+function won = checkbingocard(card)
-won=any(all(card==0)) || any(all(card'==0)) || ...
+won = any(all(card==0)) || any(all(card'==0)) || ...
all(diag(card)==0) || all (diag(card(:,size(card,2):-1:1))==0);
View
@@ -0,0 +1,12 @@
+function varargout = defaults(varnames, defaultfun)
+
+for i=1:size(varnames,2)
+ if ~exist(varnames{i}, 'var')
+ % ^-- doesn't work, because if they exist,
+ % then in the calling scope.
+ eval([varnames{i} '=[];']);
+ end
+end
+varargout = varnames;
+
+% call defaultfun on them
View
@@ -1,30 +1,20 @@
-function card=makebingocard(sidelength,delta,freespace)
-%makes a bingo card of size sidelength x sidelength, with each column i
-%randomly choosing sidelength numbers between (i-1)*delta+1 and i*delta,
-%with or without a free space in the middle
-%
-%Default values: sidelength=5, delta=3*sidelength, freespace=1
-if nargin<3
- if nargin<2
- if nargin<1
- sidelength=5;
- end
- delta=3*sidelength;
- end
- freespace=mod(sidelength,2);
-end
+function [card] = makebingocard(sidelength,delta,freespace)
+% makes a bingo card of size sidelength x sidelength, with each column i
+% randomly choosing sidelength numbers between (i-1)*delta+1 and i*delta,
+% with or without a free space in the middle
-if delta<sidelength
- error('Second argument must be >= first argument')
-end
-if freespace && ~mod(sidelength,2)
- error('Freespace only allowed with odd-sized cards')
-end
-card=zeros(sidelength);
+if ~exist('sidelength','var'), sidelength = []; end
+if ~exist('delta','var'), delta = []; end
+if ~exist('freespace','var'), freespace = []; end
+
+[sidelength,delta,freespace] = ...
+ bingocarddefaults(sidelength,delta,freespace);
+
+card = zeros(sidelength);
for i=1:sidelength
- rp=randperm(delta);
- card(:,i)=delta*(i-1)+rp(1:sidelength)';
+ rp = randperm(delta);
+ card(:,i) = delta*(i-1)+rp(1:sidelength)';
end
if freespace
- card((sidelength+1)/2,(sidelength+1)/2)=0;
+ card((sidelength+1)/2, (sidelength+1)/2) = 0;
end
View
@@ -1,36 +1,33 @@
function numturns=playbingo(numcards,sidelength,delta,freespace)
-%plays a single game of bingo with numcards cards of size
-%sidelength x sidelength, with each column i randomly choosing sidelength
-%numbers between (i-1)*delta+1 and i*delta, with or without a free space in
-%the middle
-if nargin<4
- if nargin<3
- if nargin<2
- if nargin<1
- numcards=1;
- end
- sidelength=5;
- end
- delta=3*sidelength;
- end
- freespace=mod(sidelength,2);
-end
+% plays a single game of bingo with numcards cards of size
+% sidelength x sidelength, with each column i randomly choosing sidelength
+% numbers between (i-1)*delta+1 and i*delta, with or without a free space in
+% the middle
+
+if ~exist('numcards','var'), numcards = 1; end
+
+if ~exist('sidelength','var'), sidelength = []; end
+if ~exist('delta','var'), delta = []; end
+if ~exist('freespace','var'), freespace = []; end
-maxnum=sidelength*delta;
-callseq=randperm(maxnum);
-wins=0;
-numturns=0;
-for i=1:numcards
- cards{i}=makebingocard(sidelength,delta,freespace);
+[sidelength,delta,freespace] = ...
+ bingocarddefaults(sidelength,delta,freespace);
+
+maxnum = sidelength*delta;
+callseq = randperm(maxnum);
+wins = 0;
+numturns = 0;
+for i = 1:numcards
+ cards{i} = makebingocard(sidelength,delta,freespace);
end
while(~wins)
- numturns=numturns+1;
- for i=1:numcards
- cards{i}=updatebingocard(cards{i},callseq(numturns));
- wins=wins || checkbingocard(cards{i});
- %Next line: slightly more efficient but awfully inelegant
- %if(wins) break;
- %I could also change the for loop to a while loop and separately
- %increment i, but I also find that inelegant
- end
-end
+ numturns = numturns+1;
+ for i = 1:numcards
+ cards{i} = updatebingocard(cards{i},callseq(numturns));
+ wins = wins || checkbingocard(cards{i});
+ % Next line: slightly more efficient but awfully inelegant
+ % if(wins) break;
+ % I could also change the for loop to a while loop and separately
+ % increment i, but I also find that inelegant
+ end
+end
View
@@ -0,0 +1,5 @@
+bingocarddefaults_test;
+makebingocard_test;
+checkbingocard_test;
+playbingo_test;
+% bingoprob_test;
@@ -0,0 +1,19 @@
+addpath ..
+global context
+
+test = 'bingocarddefaults:';
+
+context = [test 'empty arguments'];
+[sidelength,delta,freespace] = bingocarddefaults;
+ shouldequal('sidelength = 5', sidelength, 5);
+ shouldequal('delta = 3*sidelength', delta, 15);
+ shouldequal('freespace = 1', freespace, 1);
+
+context = [test 'even-sized cards'];
+[sidelength,~,freespace] = bingocarddefaults(6);
+ shouldequal('can be created', sidelength, 6);
+ shouldequal('freespace defaults to 0', freespace, 0);
+
+context = [test 'validations'];
+ shouldfail('delta must be >= sidelength',
+ (@() bingocarddefaults(5,4)));
@@ -1,7 +1,9 @@
addpath ..
global context
-context='won';
+test = 'checkbingocard:';
+
+context = [test 'won'];
shouldequal('for one row of zeros',
checkbingocard([
1 2 3
@@ -30,7 +32,7 @@
7 8 0
]), 1);
-context='not won';
+context = [test 'not won'];
shouldequal('in an initial state with center zero',
checkbingocard([
1 2 3
View
@@ -1,36 +1,20 @@
addpath ..
global context
-context='defaults';
- shouldequal('sidelength is 5',
- size(makebingocard), [5 5]);
+test = 'makebingocard:';
- context='defaults,sidelength:odd';
- shouldequal('doesn''t fail; freespace is 0',
- size(makebingocard(6)), [6 6]);
+context = [test 'defaults'];
+ shouldequal('sidelength is 5', size(makebingocard), [5 5]);
-context='validations';
- shouldfail('delta must be >= sidelength',
- (@() makebingocard(5,4)));
+context = [test 'freespace=1'];
+card=makebingocard(5,[],1);
+ shouldequal('the center is 0', card(3,3), 0);
-context='sidelength=5';
-card=makebingocard(5);
- shouldequal('the center is 0',
- card(3,3), 0);
+context = [test 'freespace=0'];
+card = makebingocard(5,[],0);
+ shouldequal('the center is not 0', card(3,3) == 0, 0);
- context='sidelength=5,freespace=0';
- card=makebingocard(5,15,0);
- shouldequal('the center is not 0',
- card(3,3) == 0, 0);
-
-context='sidelength=7';
-card=makebingocard(7);
- shouldequal('sidelength is 7',
- size(card), [7 7]);
-
- shouldequal('the center is 0',
- card(4,4), 0);
-
-context='freespace=1';
- shouldfail('sidelength must be odd',
- (@() makebingocard(6,12,1)));
+context = [test 'sidelength=7'];
+card = makebingocard(7);
+ shouldequal('sidelength is 7', size(card), [7 7]);
+ shouldequal('the center is 0', card(4,4), 0);
View
@@ -0,0 +1,7 @@
+addpath ..
+global context
+
+test = 'playbingo:';
+
+context = [test 'with defaults'];
+ shouldequal('numruns < (15 * 5)', playbingo < 15 * 5, 1);
View
@@ -3,5 +3,6 @@ function shouldequal(description, actual, expected)
global context
if ~(all(actual==expected))
- disp(strcat('FAILED: (', context, ')-', description))
+ disp(['FAILED: ' context ': "' description ...
+ '" ' mat2str(actual) ' != ' mat2str(expected)])
end

0 comments on commit 766a0cf

Please sign in to comment.