function frequency = monopoly(n,play_time)
% monopoly should output the proportion of the time that is spent on square
% n of the monopoly board after play_time rolls of the two dice, where "Go"
% corresponds to n = 0 and "Boardwalk" corresponds to n = 39. If you land
% on "Go To Jail" (which corresponds to n = 30), then you go directly to
% jail (which is square n = 10).
%
% If you land on "community chest" (squares 2,17, or 33), you must do what
% the card you pull says to do. The options that affect your position are
% as follows:
% 1) advance to go (probability 1/16 given that you landed on community chest)
% 2) go to jail (probability 1/16)
%
% If you land on "chance" (squares 7, 22, and 36), the options that affect
% your position are:
% 1) advance to go -- probability = 1/16
% 2) advance token to nearest utility (utilities are located at 12 and 28)
% -- probability = 1/16
% 3) advance token to nearest railroad (railroads are located at 5, 15,
% 25,and 35) -- probability = 2/16
% 4) advance to Illinois Ave. (located at n = 24) -- probability = 1/16
% 5) advance to Boardwalk (located at n = 39) -- probability = 1/16
% 6) advance to Reading Railroad (locatd at n = 5) -- probability = 1/16
% 7) go back 3 spaces -- probability = 1/16
% 8) go directly to jail (located at n = 10) -- probability = 1/16
% 9) advance to St. Charles Place (located at n = 11) -- probability = 1/16
%
% if you are in jail, assume that you buy your way out on the next turn, so
% you only spend one turn in jail at a time.


positions = zeros(1,play_time+1);
positions(1) = 1;

i = 2;
while i <= play_time + 1
    roll = randi(6) + randi(6);
    positions(i) = mod(positions(i-1) + roll,40) ;
    
    if positions(i) == 2 || positions(i) == 17 || positions(i) == 33 %comm. chest
        
        x = randi(16);
        
        if x == 1
            positions(i) = 0; % advance to go
        elseif x == 2 
            positions(i) = 10; %go to jail 
        end
    elseif positions(i) == 7 || positions(i) == 22 || positions(i) == 36 %chance
        x = randi(16);
        
        if x == 1
            positions(i) = 0; %advance to go
        elseif x == 2 %go to nearest utility
            if positions(i) == 7 || positions(i) == 33
                positions(i) = 12;
            elseif positions(i) == 22
                positions(i) = 28;
            end
            
        elseif x == 3 || x == 4 %go to nearest railroad
            if positions(i) == 7
                positions(i) = 15;
            elseif positions(i) == 22
                positions(i) = 25;
            elseif positions(i) == 36
                positions(i) = 5;
            end
            
        elseif x == 5 %go to boardwalk
            positions(i) = 39;
            
        elseif x == 6%go to illinois
            positions(i) = 24;
            
        elseif x == 7%go to reading
            positions(i) = 5;
            
        elseif x == 8 %go back 3
            positions(i) = mod(positions(i) - 3,40);
            
        elseif x == 9%go to jail
            positions(i) = 10;
            
        elseif x ==10 %st charles
            positions(i) = 11;
        end
            
        
        
    elseif positions(i) == 30 %landed on "go directly to jail"
        positions(i) = 10;
    end
    
    i = i +1;
end

frequencies = zeros(1,40);
for i = 1:40
    frequencies(i) = numel(find(positions == i))/length(positions);
end

frequency = frequencies(n);
hist(positions,39);
end