Guest User

Untitled

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