MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Solution 84737

Submitted on 2 May 2012 by Doug Hull

Correct

123Size
`This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.`

### Test Suite

Test
Code Input and Output
1
Pass

```% THIS IS A HACK TO GET CODE ON THE PATH
% IGNORE THIS FIRST 'TEST'
%
%
fh=fopen('doug.m','wt');
fprintf(fh, '%s \n', 'function out = doug(histA, histB)');
fprintf(fh, '%s \n', '% A is self');
fprintf(fh, '%s \n', '%-1 steal');
fprintf(fh, '%s \n', '% 0 share');
fprintf(fh, '%s \n', '% 1 catch');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '%stealVal = 2;');
fprintf(fh, '%s \n', '%catchVal = 2;');
fprintf(fh, '%s \n', '%shareVal = 1;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'memoryThresh = 10;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'histB = [zeros(memoryThresh,1); histB];');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'recentB = histB(end-(memoryThresh-1) : end);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'numStealB = nnz(recentB == -1);');
fprintf(fh, '%s \n', 'numShareB = nnz(recentB ==  0);');
fprintf(fh, '%s \n', 'numCatchB = nnz(recentB ==  1);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'percentStealB = (numStealB/memoryThresh);');
fprintf(fh, '%s \n', 'percentShareB = (numShareB/memoryThresh);');
fprintf(fh, '%s \n', 'percentCatchB = (numCatchB/memoryThresh);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = 0; %default to share');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '            rollDice = rand;');
fprintf(fh, '%s \n', 'percentCatchStealers = 0.9;');
fprintf(fh, '%s \n', 'if (rollDice < percentStealB)');
fprintf(fh, '%s \n', '    rollDice = rand;');
fprintf(fh, '%s \n', '    if (rollDice < percentCatchStealers)');
fprintf(fh, '%s \n', '        out =  1; % catch');
fprintf(fh, '%s \n', '    else');
fprintf(fh, '%s \n', '        out = -1; % steal');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', 'end');
fclose(fh);
fh=fopen('runPair.m','wt');
fprintf(fh, '%s \n', 'function [scoreA, scoreB] = runPair(fhA, fhB);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'n = 10000;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'histA = [];');
fprintf(fh, '%s \n', 'histB = [];');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'scoreA = 0;');
fprintf(fh, '%s \n', 'scoreB = 0;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '%-1 steal');
fprintf(fh, '%s \n', '% 0 share');
fprintf(fh, '%s \n', '% 1 catch');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'stealVal = 2;');
fprintf(fh, '%s \n', 'catchVal = 2;');
fprintf(fh, '%s \n', 'shareVal = 1;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'for i = 1: n;');
fprintf(fh, '%s \n', '    histA(end+1,1) = fhA(histA,  histB         );');
fprintf(fh, '%s \n', '    histB(end+1,1) = fhB(histB,  histA(1:end-1)); %modified one not sent!');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '    switch histA(end)');
fprintf(fh, '%s \n', '        case -1 %a steal');
fprintf(fh, '%s \n', '            switch histB(end)');
fprintf(fh, '%s \n', '            case -1 %b steal');
fprintf(fh, '%s \n', '                scoreA = scoreA;');
fprintf(fh, '%s \n', '                scoreB = scoreB;');
fprintf(fh, '%s \n', '            case 0 %b share');
fprintf(fh, '%s \n', '                scoreA = scoreA + stealVal;');
fprintf(fh, '%s \n', '                scoreB = scoreB;');
fprintf(fh, '%s \n', '            case 1 %b catch');
fprintf(fh, '%s \n', '                scoreA = scoreA;');
fprintf(fh, '%s \n', '                scoreB = scoreB + catchVal;');
fprintf(fh, '%s \n', '            otherwise');
fprintf(fh, '%s \n', '                error(''This can not happen'') ');
fprintf(fh, '%s \n', '            end');
fprintf(fh, '%s \n', '        case 0 %a share');
fprintf(fh, '%s \n', '            switch histB(end)');
fprintf(fh, '%s \n', '            case -1 %b steal');
fprintf(fh, '%s \n', '                scoreA = scoreA;');
fprintf(fh, '%s \n', '                scoreB = scoreB + stealVal;');
fprintf(fh, '%s \n', '            case 0 %b share');
fprintf(fh, '%s \n', '                scoreA = scoreA + shareVal;');
fprintf(fh, '%s \n', '                scoreB = scoreB + shareVal;');
fprintf(fh, '%s \n', '            case 1 %b catch');
fprintf(fh, '%s \n', '                scoreA = scoreA;');
fprintf(fh, '%s \n', '                scoreB = scoreB;');
fprintf(fh, '%s \n', '            otherwise');
fprintf(fh, '%s \n', '                error(''This can not happen'')');
fprintf(fh, '%s \n', '            end');
fprintf(fh, '%s \n', '        case 1 %a catch');
fprintf(fh, '%s \n', '            switch histB(end)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '            case -1 %b steal');
fprintf(fh, '%s \n', '                scoreA = scoreA + catchVal;');
fprintf(fh, '%s \n', '                scoreB = scoreB;');
fprintf(fh, '%s \n', '            case 0 %b share');
fprintf(fh, '%s \n', '                scoreA = scoreA;');
fprintf(fh, '%s \n', '                scoreB = scoreB;');
fprintf(fh, '%s \n', '            case 1 %b catch');
fprintf(fh, '%s \n', '                scoreA = scoreA;');
fprintf(fh, '%s \n', '                scoreB = scoreB;');
fprintf(fh, '%s \n', '            otherwise');
fprintf(fh, '%s \n', '                error(''This can not happen'')');
fprintf(fh, '%s \n', '            end');
fprintf(fh, '%s \n', '        otherwise');
fprintf(fh, '%s \n', '            error(''This can not happen'')');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '');
fclose(fh);
fh=fopen('evil.m','wt');
fprintf(fh, '%s \n', 'function out = evil(histA, histB)');
fprintf(fh, '%s \n', '%-1 steal');
fprintf(fh, '%s \n', '% 0 share');
fprintf(fh, '%s \n', '% 1 catch');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '%stealVal = 2;');
fprintf(fh, '%s \n', '%catchVal = 2;');
fprintf(fh, '%s \n', '%shareVal = 1;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = -1;');
fclose(fh);
fh=fopen('good.m','wt');
fprintf(fh, '%s \n', 'function out = good(histA, histB)');
fprintf(fh, '%s \n', '%-1 steal');
fprintf(fh, '%s \n', '% 0 share');
fprintf(fh, '%s \n', '% 1 catch');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '%stealVal = 2;');
fprintf(fh, '%s \n', '%catchVal = 2;');
fprintf(fh, '%s \n', '%shareVal = 1;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = 0;');
fclose(fh);
fh=fopen('chaos.m','wt');
fprintf(fh, '%s \n', 'function out = chaos(histA, histB)');
fprintf(fh, '%s \n', '%-1 steal');
fprintf(fh, '%s \n', '% 0 share');
fprintf(fh, '%s \n', '% 1 catch');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '%stealVal = 2;');
fprintf(fh, '%s \n', '%catchVal = 2;');
fprintf(fh, '%s \n', '%shareVal = 1;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'out = ceil(rand*3)-2;');
fclose(fh);
rehash path
```
```{Warning: Function
has the same name as a MATLAB builtin. We suggest you rename
the function to avoid a potential name conflict.}
{> In verifyCode>evaluateCode at 189
In verifyCode at 37
In fevalJSON at 14}
{Warning: Function
has the same name as a MATLAB builtin. We suggest you rename
the function to avoid a potential name conflict.}
{> In verifyCode>evaluateCode at 189
In verifyCode at 37
In fevalJSON at 14}
{Warning: Function
same name as a MATLAB builtin. We suggest you rename the
function to avoid a potential name conflict.}
{> In verifyCode>evaluateCode at 189
In verifyCode at 37
In fevalJSON at 14}
{Warning: Function
the same name as a MATLAB builtin. We suggest you rename the
function to avoid a potential name conflict.}
{> In verifyCode>evaluateCode at 189
In verifyCode at 37
In fevalJSON at 14}
{Warning: Function
same name as a MATLAB builtin. We suggest you rename the
function to avoid a potential name conflict.}
{> In verifyCode>evaluateCode at 189
In verifyCode at 37
In fevalJSON at 14}
{Warning: Function
same name as a MATLAB builtin. We suggest you rename the
function to avoid a potential name conflict.}
{> In verifyCode>evaluateCode at 189
In verifyCode at 37
In fevalJSON at 14}
```
2
Pass

```%%
a        = runPair(@StealShareCatch, @chaos);
a(end+1) = runPair(@StealShareCatch, @good);
a(end+1) = runPair(@StealShareCatch, @evil);
a(end+1) = runPair(@StealShareCatch, @doug)
yourScore = sum(a)
terribleScore   =     0; % works
badScore        = 23000; % good  strategy scores this
okScore         = 26000; % evil  strategy scores this
decentScore     = 29000; % chaos strategy scores this
greatScore      = 42000; % doug  strategy scores this
assert(yourScore > terribleScore, 'Score: %d', yourScore)
assert(yourScore > badScore, 'Score: %d', yourScore)
assert(yourScore > okScore, 'Score: %d', yourScore)
assert(yourScore > decentScore, 'Score: %d', yourScore)
assert(yourScore > greatScore, 'Score: %d', yourScore)
```
```a =
4376       10000       17990       10000
yourScore =
42366
```