Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- p1 = [17,19,30,45,25,48,8,6,39,36,28,5,47,26,46,20,18,13,7,49,34,23,43,22,4]';
- p2 = [44,10,27,9,14,15,24,16,3,33,21,29,11,38,1,31,50,41,40,32,42,35,37,2,12]';
- [p1,p2,winner] = playSubGame(p1,p2);
- if isempty(p1)
- score = (length(p2):-1:1)*p2;
- else
- score = (length(p1):-1:1)*p1;
- end
- disp(score)
- function [p1,p2,winner] = playSubGame(p1,p2)
- deckHistory1 = cell(1,2500);
- deckHistory2 = cell(1,2500);
- dhEnd = 0;
- while ~isempty(p1)>0 && ~isempty(p2)>0
- winner = 0;
- for i=1:dhEnd
- if isequal(deckHistory1{i},p1) && isequal(deckHistory2{i},p2)
- winner = 1;
- return
- end
- end
- dhEnd = dhEnd+1;
- deckHistory1{1,dhEnd} = p1;
- deckHistory2{1,dhEnd} = p2;
- c1 = p1(1);
- c2 = p2(1);
- p1 = p1(2:end);
- p2 = p2(2:end);
- if length(p1)>=c1 && length(p2)>=c2
- [p1New,p2New,winner] = playSubGame(p1(1:c1),p2(1:c2));
- if winner==1
- p1 = [p1;c1;c2];
- else
- p2 = [p2;c2;c1];
- end
- else
- if c1>c2
- p1 = [p1;c1;c2];
- winner = 1;
- else
- p2 = [p2;c2;c1];
- winner = 2;
- end
- end
- end
- if isempty(p1); winner = 2; else winner = 1; end
- end
Advertisement
Add Comment
Please, Sign In to add comment