User:Mafaraxas/Nine Rings

% Nine Rings (in GW) simulator. % There is a 3x3 grid of squares. On every round, a square is chosen % randomly, and 10 tickets are deducted from your total ticket count. % Tickets may then may be returned if you win; being on the correct square % gives most tickets, but being immediately adjacent (diagonally doesn't % count) gives some tickets as well. % % Specifically: % If on center: %   25 tickets for direct win %   15 tickets for adjacent win % If on side: %   40 tickets for direct win %   15 tickets for adjacent win % If on corner: %   55 tickets for direct win %   15 tickets for adjacent win % % Here, we enumerate the grid as: % % 1 | 5 | 2 % --- %  6 | 9 | 8 % --- %  3 | 7 | 4 % % So that corners are 1-4, sides are 5-8, and the center is 9.

clear all; close all; clc

tickets=4e4;  % number of tickets square=9;     % chosen square cost=10;      % cost to play. This stays fixed unless you want to mess % around with numbers in the theoretical case.

starting_tickets=tickets; % don't touch this.

% Get which "bracket" we're in (corner,center,side) based on square number. % Also, assign the ticket/lucky point payout scheme.

if square <= 4 bracket=[1 0 0]';  %corner elseif square <= 8 bracket=[0 1 0]';  %side else bracket=[0 0 1]';  %center end

payout=[55 40 25];

% Generate the adjacency matrix based on the above picture. This is a bit % of a mess, just bear with me til the good stuff. A=zeros(9); A(1,5)=1; A(1,6)=1; A(5,2)=1; A(5,9)=1; A(2,8)=1; A(6,3)=1; A(6,9)=1; A(9,7)=1; A(9,8)=1; A(3,7)=1; A(7,4)=1; A=A+A'; A=logical(A);

% Keep a history of the most recent 1e6 rounds. Also, total lucky/unlucky % points earned. The number of lucky points earned on each round is the % (gross, not net) number of tickets won. The number of unlucky points is % just the number of games where no tickets are won. history=zeros(1e6,1); history(1)=tickets; lucky=0; unlucky=0; count=1;

% Keep playing until we're out of tickets. while history(count) >= 10 count=count+1; winnar=ceil(9*rand); % Did we get a direct win? if square==winnar tickets=tickets + payout*bracket - 10; lucky=lucky + payout*bracket; history(count)=tickets; % If not, did we get an indirect win? (remember A is a logical    % true/false array indicating true if your square is adjacent to the     % winner, and false otherwise.    elseif A(square,winnar)        tickets=tickets + 5;  % Net change of tickets here is always 15-10.        lucky=lucky + 15;     % Ignoring cost, we still won 15 tickets, though.        history(count)=tickets;    % Otherwise, we lost.    else        tickets=tickets - 10;        unlucky=unlucky + 1;        history(count)=tickets;    end end

fprintf('You gained %0.f lucky points and %0.f unlucky points in %0.f rounds.\n',lucky,unlucky,count)

history=history(1:count); % Cut off excess zeros in our history. x=(1:count)';             % For plotting purposes.

plot(x,history) title('Total number of tickets over time') xlabel('Round number'); ylabel('Total tickets')

% We can compute the expected ticket gain/loss per round by hand, which % turns out to be the same for center/side/corner: -5/9 tickets per round. % Thus we can find the residual of our experimental results compared to the % expected linear decline.

% Define our expected function. Should be linear in x. Plot this expected % line on the graph. f=@(x) starting_tickets-5/9*x; hold on; plot(x,f(x),'k')

% Find the residual; f(x)-history. residual=f(x)-history; figure; plot(x,residual); hold on; plot([1 count],[0 0],'k') title('The deviation from the expected behavior over time') xlabel('Round number'); ylabel('Residual')