Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 116474

Submitted on 20 Jul 2012

Incorrect

37Size
Leading solution size is 29.
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);

                    
2
Pass
 
%%
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);

                    
3
Pass
 
%%
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);

                    
4
Pass
 
%%
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);

                    
5
Pass
 
%%
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);

                    
6
Pass
 
%%
fh=fopen('sean.m','wt');
fprintf(fh, '%s \n', 'function choice = sean(yourHist, theirHist)');
fprintf(fh, '%s \n', '    if isempty(theirHist)');
fprintf(fh, '%s \n', '        [~,choice] = histc(rand,0:0.33:1);');
fprintf(fh, '%s \n', '        choice = choice-2;');
fprintf(fh, '%s \n', '    else');
fprintf(fh, '%s \n', '        [~, idx] = max(histc(theirHist,[-1 0 1]));');
fprintf(fh, '%s \n', '        choice = [1 0 -1];');
fprintf(fh, '%s \n', '        choice = choice(idx);');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', 'end');
fclose(fh);

                    
7
Pass
 
%%
fh=fopen('vincent.m','wt');
fprintf(fh, '%s \n', 'function choice = vincent(yourHist, theirHist)');
fprintf(fh, '%s \n', '% This function will be called 10,000 times.');
fprintf(fh, '%s \n', '% the hist vectors will start as [], and get longer each time through.');
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; What you get if you steal and they share');
fprintf(fh, '%s \n', '%catchVal = 2; What you get if you catch and they steal');
fprintf(fh, '%s \n', '%shareVal = 1; What you both get if both share');
fprintf(fh, '%s \n', '%Otherwise no one gets anything!');
fprintf(fh, '%s \n', 'if all(theirHist == -1)');
fprintf(fh, '%s \n', 'choice = 1;');
fprintf(fh, '%s \n', 'elseif all(theirHist == 0);');
fprintf(fh, '%s \n', 'choice = -1;');
fprintf(fh, '%s \n', 'elseif all(theirHist == 1);');
fprintf(fh, '%s \n', 'choice = 1;');
fprintf(fh, '%s \n', 'else');
fprintf(fh, '%s \n', 'choice = 0;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', 'end');
fclose(fh);

                    
8
Pass
 
%%
fh=fopen('james.m','wt');
fprintf(fh, '%s \n', 'function choice = james(yourHist, theirHist)');
fprintf(fh, '%s \n', '% This function will be called 10,000 times.');
fprintf(fh, '%s \n', '% the hist vectors will start as [], and get longer each time through.');
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; What you get if you steal and they share');
fprintf(fh, '%s \n', '%catchVal = 2; What you get if you catch and they steal');
fprintf(fh, '%s \n', '%shareVal = 1; What you both get if both share');
fprintf(fh, '%s \n', '%Otherwise no one gets anything!');
fprintf(fh, '%s \n', 'if(length(theirHist)<2)');
fprintf(fh, '%s \n', '  choice = 0;');
fprintf(fh, '%s \n', 'elseif( theirHist(end-1:end) == -1 )');
fprintf(fh, '%s \n', '  choice = 1;');
fprintf(fh, '%s \n', 'else');
fprintf(fh, '%s \n', '  choice = theirHist(end);');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', 'end');
fclose(fh);

                    
9
Pass
 
%%
fh=fopen('dougExploiter.m','wt');
fprintf(fh, '%s \n', 'function out = dougExploiter(myHist, theirHist)');
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', 'threshID = 3;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if numel(myHist) < threshID');
fprintf(fh, '%s \n', '    out = 0;');
fprintf(fh, '%s \n', '    return');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if     isGood(theirHist)');
fprintf(fh, '%s \n', '    out = -1;');
fprintf(fh, '%s \n', '    return');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'elseif isEvil(theirHist)');
fprintf(fh, '%s \n', '    out = 1;');
fprintf(fh, '%s \n', '    return');
fprintf(fh, '%s \n', '    ');
fprintf(fh, '%s \n', 'elseif isTitForTat(myHist, theirHist)');
fprintf(fh, '%s \n', '    switch myHist(end)');
fprintf(fh, '%s \n', '        case -1 %I stole so he wil steal');
fprintf(fh, '%s \n', '            out =  1; %catch');
fprintf(fh, '%s \n', '            return');
fprintf(fh, '%s \n', '        case  0 %I shared so he will share');
fprintf(fh, '%s \n', '            out = -1;');
fprintf(fh, '%s \n', '            return');
fprintf(fh, '%s \n', '        case  1 %I caught so he will catch');
fprintf(fh, '%s \n', '            out =  0; %Induce a share');
fprintf(fh, '%s \n', '            return');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', '    ');
fprintf(fh, '%s \n', 'elseif isChaos(theirHist)');
fprintf(fh, '%s \n', '    out = 1;');
fprintf(fh, '%s \n', '    return');
fprintf(fh, '%s \n', '  ');
fprintf(fh, '%s \n', 'else %general unexploitable strategy');
fprintf(fh, '%s \n', '    out = dougGeneral(myHist, theirHist);');
fprintf(fh, '%s \n', '    return');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function flag = isGood(in)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'flag = all(in == 0);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function flag = isEvil(in)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'flag = all(in == -1);');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function flag = isTitForTat(myHist, theirHist)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'window = 4;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if numel(myHist) < window');
fprintf(fh, '%s \n', '    flag = false;');
fprintf(fh, '%s \n', '    return');
fprintf(fh, '%s \n', 'else');
fprintf(fh, '%s \n', '    theirRecent = theirHist(end-(window-1):end);');
fprintf(fh, '%s \n', '       myRecent =    myHist(end-(window-1):end);');
fprintf(fh, '%s \n', '       ');
fprintf(fh, '%s \n', '    flag = isequal(myRecent(1:end-1), theirRecent(2:end));');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'function flag = isChaos(theirHist)');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'num = numel(theirHist);');
fprintf(fh, '%s \n', 'perShare = nnz(theirHist ==  0)/num;');
fprintf(fh, '%s \n', 'perSteal = nnz(theirHist == -1)/num;');
fprintf(fh, '%s \n', 'perCatch = nnz(theirHist ==  1)/num;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'thresh = 0.05;');
fprintf(fh, '%s \n', 'target = 0.33;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'difference = [perShare perSteal perCatch] - target;');
fprintf(fh, '%s \n', 'flag = all(abs(difference) < thresh);');
fprintf(fh, '%s \n', '    ');
fprintf(fh, '%s \n', 'function out = dougGeneral(histA, histB)');
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');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', '    ');
fclose(fh);

                    
10
Pass
 
%%
fh=fopen('JamesE.m','wt');
fprintf(fh, '%s \n', 'function choice = jamesE(yourHist, theirHist)');
fprintf(fh, '%s \n', '% This function will be called 10,000 times.');
fprintf(fh, '%s \n', '% the hist vectors will start as [], and get longer each time through.');
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; What you get if you steal and they share');
fprintf(fh, '%s \n', '%catchVal = 2; What you get if you catch and they steal');
fprintf(fh, '%s \n', '%shareVal = 1; What you both get if both share');
fprintf(fh, '%s \n', '%Otherwise no one gets anything!');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if length(theirHist)>10');
fprintf(fh, '%s \n', '    [yourguess yourcounts]=hist(theirHist(end-9:end),-1:1);');
fprintf(fh, '%s \n', '    [countmax countval]=max(yourguess);');
fprintf(fh, '%s \n', '    yourpopguess=yourcounts(countval);');
fprintf(fh, '%s \n', '    if yourpopguess==-1');
fprintf(fh, '%s \n', '        choice=1;');
fprintf(fh, '%s \n', '    elseif yourpopguess==0');
fprintf(fh, '%s \n', '        choice=-1;');
fprintf(fh, '%s \n', '    else');
fprintf(fh, '%s \n', '        choice=floor(3*rand)-1;');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', 'else');
fprintf(fh, '%s \n', '	choice=floor(3*rand)-1;');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'end');
fclose(fh);

                    
11
Pass
 
%%
fh=fopen('philipp.m','wt');
fprintf(fh, '%s \n', 'function choice = philipp(yourHist, theirHist)');
fprintf(fh, '%s \n', '% This function will be called 10,000 times.');
fprintf(fh, '%s \n', '% the hist vectors will start as [], and get longer each time through.');
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; What you get if you steal and they share');
fprintf(fh, '%s \n', '%catchVal = 2; What you get if you catch and they steal');
fprintf(fh, '%s \n', '%shareVal = 1; What you both get if both share');
fprintf(fh, '%s \n', '%Otherwise no one gets anything!');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'histLength = numel(theirHist);');
fprintf(fh, '%s \n', 'window = histLength-1;');
fprintf(fh, '%s \n', 'if histLength > 10');
fprintf(fh, '%s \n', '    window = randi([5 10]);');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', ' if isempty(yourHist)');
fprintf(fh, '%s \n', '     % No choices made yet');
fprintf(fh, '%s \n', '     choice = randi([-1 1]);');
fprintf(fh, '%s \n', ' else');
fprintf(fh, '%s \n', '     switch mode(theirHist(end-window:end))');
fprintf(fh, '%s \n', '         case -1');
fprintf(fh, '%s \n', '             choice = 1;');
fprintf(fh, '%s \n', '         case 0');
fprintf(fh, '%s \n', '             choice = -1;');
fprintf(fh, '%s \n', '         case 1');
fprintf(fh, '%s \n', '             choice = randi([0 1]);');
fprintf(fh, '%s \n', '         otherwise');
fprintf(fh, '%s \n', '             choice = 0;');
fprintf(fh, '%s \n', '     end');
fprintf(fh, '%s \n', ' end');
fclose(fh);

                    
12
Pass
 
%%
fh=fopen('bert.m','wt');
fprintf(fh, '%s \n', 'function choice = bert(yourHist, theirHist)');
fprintf(fh, '%s \n', '% This function will be called 10,000 times.');
fprintf(fh, '%s \n', '% the hist vectors will start as [], and get longer each time through.');
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; What you get if you steal and they share');
fprintf(fh, '%s \n', '%catchVal = 2; What you get if you catch and they steal');
fprintf(fh, '%s \n', '%shareVal = 1; What you both get if both share');
fprintf(fh, '%s \n', '%Otherwise no one gets anything!');
fprintf(fh, '%s \n', 'choice = -1;');
fprintf(fh, '%s \n', '');
fprintf(fh, '%s \n', 'if numel(theirHist)>2');
fprintf(fh, '%s \n', '    ');
fprintf(fh, '%s \n', '    %check for tit4Tat stratagy');
fprintf(fh, '%s \n', '    isTit4Tat = isequal(yourHist(end-2:end-1), theirHist(end-1:end));');
fprintf(fh, '%s \n', '    count=histc(theirHist,-1:1);');
fprintf(fh, '%s \n', '    stealPercent = count(1)/numel(theirHist);');
fprintf(fh, '%s \n', '    sharePercent = count(2)/numel(theirHist);');
fprintf(fh, '%s \n', '    catPercent = count(3)/numel(theirHist);');
fprintf(fh, '%s \n', '    ');
fprintf(fh, '%s \n', '    choice = -theirHist(end); %general behavior ');
fprintf(fh, '%s \n', '    if isTit4Tat %check for tit4tat');
fprintf(fh, '%s \n', '        switch yourHist(end)');
fprintf(fh, '%s \n', '        case -1 %he wil steal');
fprintf(fh, '%s \n', '            choice =  1; %catch');
fprintf(fh, '%s \n', '            return');
fprintf(fh, '%s \n', '        case  0 %he will share');
fprintf(fh, '%s \n', '            choice = -1;');
fprintf(fh, '%s \n', '            return');
fprintf(fh, '%s \n', '        case  1 %he will catch');
fprintf(fh, '%s \n', '            choice =  0;');
fprintf(fh, '%s \n', '            return');
fprintf(fh, '%s \n', '        end');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', '    if choice == -1 %dont steal if the last was catch');
fprintf(fh, '%s \n', '        choice = 0;');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', '    ');
fprintf(fh, '%s \n', '    if rand(1)>0.9 %random behavior to pass exploiters test');
fprintf(fh, '%s \n', '     choice = 0;');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', '    ');
fprintf(fh, '%s \n', '    if stealPercent > 0.7   %if they steal often, I catch     ');
fprintf(fh, '%s \n', '        choice = 1;');
fprintf(fh, '%s \n', '    elseif sharePercent >0.7 %if they share often, I steal');
fprintf(fh, '%s \n', '        choice = -1;');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', '    if numel(theirHist)>9000 %for the last 1000 round, steal');
fprintf(fh, '%s \n', '        choice = 1;');
fprintf(fh, '%s \n', '    end');
fprintf(fh, '%s \n', 'end');
fprintf(fh, '%s \n', 'end');
fclose(fh);

                    
13
Pass
 
%%
fh=fopen('amro.m','wt');
fprintf(fh, '%s \n', 'function out = amro(histA, histB)');
fprintf(fh, '%s \n', '	% This function will be called 10,000 times.');
fprintf(fh, '%s \n', '	% the hist vectors will start as [], and get longer each time through.');
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', '	if rand<0.01 || isempty(histB)	% if starting (or flipCoin)');
fprintf(fh, '%s \n', '		% flipCoin: random move or "share"');
fprintf(fh, '%s \n', '		if rand<0.925');
fprintf(fh, '%s \n', '			out = randi([-1 1]);');
fprintf(fh, '%s \n', '		else');
fprintf(fh, '%s \n', '			out = -1;');
fprintf(fh, '%s \n', '		end');
fprintf(fh, '%s \n', '	elseif rand<0.01				% flipCoin');
fprintf(fh, '%s \n', '		% repeat his last move');
fprintf(fh, '%s \n', '		out = histB(end);');
fprintf(fh, '%s \n', '	else');
fprintf(fh, '%s \n', '		% in the first/last X moves (+flipCoin)');
fprintf(fh, '%s \n', '		if rand < 0.4 && numel(histB)>9900');
fprintf(fh, '%s \n', '			out = 1;	% catch');
fprintf(fh, '%s \n', '			return');
fprintf(fh, '%s \n', '		end');
fprintf(fh, '%s \n', '		if rand < 0.1 && numel(histB)<15');
fprintf(fh, '%s \n', '			out = 1;	% catch');
fprintf(fh, '%s \n', '			return');
fprintf(fh, '%s \n', '		end');
fprintf(fh, '%s \n', '		');
fprintf(fh, '%s \n', '		% check if player B has been repeating my moves (tit4tat)');
fprintf(fh, '%s \n', '		n = 2;');
fprintf(fh, '%s \n', '		if rand < 0.9 && numel(histA)>n');
fprintf(fh, '%s \n', '			if isequal(histA(end-n:end-1), histB(end-n+1:end))');
fprintf(fh, '%s \n', '				% predict his next move based on my previous move');
fprintf(fh, '%s \n', '				out = [1 -1 0];');
fprintf(fh, '%s \n', '				out = out( histB(end)+2 );');
fprintf(fh, '%s \n', '				return');
fprintf(fh, '%s \n', '			end');
fprintf(fh, '%s \n', '		end');
fprintf(fh, '%s \n', '		');
fprintf(fh, '%s \n', '		% frequency of moves over entire history of player B');
fprintf(fh, '%s \n', '		freq = histc(histB,-1:1) ./ numel(histB);');
fprintf(fh, '%s \n', '		');
fprintf(fh, '%s \n', '		% flipCoin: all history or last N only');
fprintf(fh, '%s \n', '		maxNum = randi([7 10]);');
fprintf(fh, '%s \n', '		if rand<0.98 && numel(histB)>maxNum');
fprintf(fh, '%s \n', '			histB = histB(end-maxNum+1:end);');
fprintf(fh, '%s \n', '		end');
fprintf(fh, '%s \n', '		');
fprintf(fh, '%s \n', '		% most frequent move in possibly filtered history');
fprintf(fh, '%s \n', '		%idx = mode(histB)+2;');
fprintf(fh, '%s \n', '		[~,idx] = max( histc(histB,-1:1) );');
fprintf(fh, '%s \n', '		');
fprintf(fh, '%s \n', '		% counter move');
fprintf(fh, '%s \n', '		out = [1 0 0];		% steal->catch, share->share, catch->share');
fprintf(fh, '%s \n', '		if freq(2)>0.7 && rand<0.995, out(2) = -1; end	% adjust if he shares alot');
fprintf(fh, '%s \n', '		if freq(1)>0.7 && rand<0.995, out(3) = 1; end	% adjust if he catches alot');
fprintf(fh, '%s \n', '		%{');
fprintf(fh, '%s \n', '		if rand < freq(1)');
fprintf(fh, '%s \n', '			out = [1 0 1];	% steal->catch, share->share, catch->catch');
fprintf(fh, '%s \n', '		elseif rand < freq(2)');
fprintf(fh, '%s \n', '			out = [1 -1 0];	% steal->catch, share->steal, catch->share');
fprintf(fh, '%s \n', '		else');
fprintf(fh, '%s \n', '			out = [1 0 randi([0 1])];	% steal->catch, share->steal, catch->random');
fprintf(fh, '%s \n', '		end');
fprintf(fh, '%s \n', '		%}');
fprintf(fh, '%s \n', '		out = out(idx);');
fprintf(fh, '%s \n', '	end');
fprintf(fh, '%s \n', '	');
fprintf(fh, '%s \n', '	% check if I am repeating myself in the last N moves');
fprintf(fh, '%s \n', '	%{');
fprintf(fh, '%s \n', '	n = 50;');
fprintf(fh, '%s \n', '	if numel(histA)>=n && all(histA(end-n+1:end)==out)');
fprintf(fh, '%s \n', '		out = randi([0 1]);');
fprintf(fh, '%s \n', '	end');
fprintf(fh, '%s \n', '	%}');
fprintf(fh, '%s \n', 'end');
fclose(fh);

                    
14
Pass
 
%%
rehash path
{Warning: Function
/opt/mlsedu/mdcsserver/latest/m/web_common/shadow/license.m
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
/opt/mlsedu/mdcsserver/latest/m/web_common/shadow/graphicsAndGuis/uicontrol.m
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
/opt/mlsedu/mdcsserver/latest/m/common/shadow/home.m 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
/opt/mlsedu/mdcsserver/latest/m/common/shadow/keyboard.m 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
/opt/mlsedu/mdcsserver/latest/m/common/shadow/more.m 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
/opt/mlsedu/mdcsserver/latest/m/common/shadow/pause.m 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}
15
Fail
 
%%
fh = @StealShareCatch;
a        = runPair(fh, @chaos)
a(end+1) = runPair(fh, @evil)
a(end+1) = runPair(fh, @StealShareCatch)
a(end+1) = runPair(fh, @good)
a(end+1) = runPair(fh, @doug)
a(end+1) = runPair(fh, @sean)
a(end+1) = runPair(fh, @james)
a(end+1) = runPair(fh, @vincent)
a(end+1) = runPair(fh, @JamesE)
a(end+1) = runPair(fh, @philipp)
a(end+1) = runPair(fh, @bert)
a(end+1) = runPair(fh, @amro)
yourScore = sum(a)
Score =      0; % works
Score =  80219; % Vincent
Score =  84000; % Sean
Score =  93000; % james
Score = 104000; % doug
Score = 118000; % JamesE
Score = 120000; % stealShareCatch
Score = 122000; % philipp
Score = 125000; % bert
Score = 135000; % amro
assert(yourScore > Score(end), 'Score: %d', yourScore)
Error: Score: 115513