function winnnerRecord(win,player1,player2,currentPlayer)
%winnerRecord(win)
%This function is responsible for loading, modifying, and re-saving the data
%from past games.
%if win == 1 then the data will be modified.
%if win == 0 the data will be displayed.
nameOne = get(player1,'name'); %Get the first player's name
nameTwo = get(player2,'name'); %Get the second player's name
names = lower(sortrows({nameOne; nameTwo})); %Sorted cell array of the two names
recordName = [names{1} names{2}]; %recordName is the filename that the record will be saved under.
w = what('records'); %Find the file named 'records'
saveLoc = [w.path filesep recordName '.mat']; %This is the save location for the record
records = {}; %Initialize cell array.
sortedCaps = sortrows({nameOne; nameTwo}); %Used in the plot.
try %Attempt to load the data
load(saveLoc);
catch %If the data is not there, then initialize it.
records{1,1} = sortedCaps{1};
records{2,1} = sortedCaps{2};
records{1,2} = [];
records{2,2} = [];
end
winnerName = get(currentPlayer,'name');
winnerLoc = find(strcmpi(winnerName,names));
switch winnerLoc
case 1
winner = 1;
loser = 2;
case 2
winner = 2;
loser = 1;
end
if win == 1
%Update winner's record
winnerRecord = records{winner,2};
if length(winnerRecord) > 0 %If the record is atleast one game
winnerTotal = winnerRecord(end) + 1;
else %If this is the first game
winnerTotal = [1];
end
winnerRecord = [winnerRecord winnerTotal];
records{winner,2} = winnerRecord;
%Update loser's record
loserRecord = records{loser,2};
if length(loserRecord) > 0 %If the record is atleast one game
loserTotal = loserRecord(end);
else %If this is the first game
loserTotal = [0];
end
loserRecord = [loserRecord loserTotal];
records{loser,2} = loserRecord;
%Save this updated information
save(saveLoc,'records')
end
%Load this information
try
load(saveLoc);
catch
msgbox('No such player combination exists...Check your data.','Who?','error');
return
end
%Plot this information
firstPlayer = records{1,1};
secondPlayer = records{2,1};
firstPlayerRecord = records{1,2};
secondPlayerRecord = records{2,2};
gameNumber = [1:length(firstPlayerRecord)];
recordPlot = figure;
set(recordPlot,...
'Menubar','none',...
'Name','Head to Head',...
'NumberTitle','off',...
'Position', [582 196 394 268]);
plot(gameNumber,firstPlayerRecord,gameNumber,secondPlayerRecord);
xlabel('Game Number');
ylabel('Number of games won');
title(['Head to head comparison of ' firstPlayer ' and ' secondPlayer]);
legend(firstPlayer,secondPlayer,'Location','NorthWest');