Skip to content

Instantly share code, notes, and snippets.

@coryhofmann
Created Feb 19, 2017
Embed
What would you like to do?
%LCR.m
%By Cory Hofmann
%Last Updated: 2017-01-24
%
%This code is designed to run through a pre-determined number of LCR games
%for a pre-determined number of players. Designed to output game
%statistics for further analysis.
%
%Function call -> output = LCR(number of players, number of games)
%
%Output: data = [Winner #, deciding dice roll [1L, 2C, 3R], # rolls]
function data = LCR(n, n2)
for game = 1:n2 %active game
%initialize variables for each new game
score(1:n,1) = 3; %users start with three tokens
count = 0;
for round = 1:100*n %guess upper bound for number of rounds
for i = 1:n %active player
dice = score(i); %how many tokens?
if dice == 0 %to next player, if current player has no tokens
continue
end
for j = 1:dice %how many rolls they take
roll = randi(6,1,1); %1=L, 2=C, 3=R, 4,5,6=NO MOVE
%in case you want to display them!
if roll == 1
%disp('L')
elseif roll == 2
%disp('C')
elseif roll == 3
%disp('R')
else
%disp('DOT!')
end
if roll == 1 && i == 1
%if player 1 rolls L, last player gets the token
score(i) = score(i) - 1;
score(n) = score(n) + 1;
elseif roll == 1 && i ~= 1
score(i) = score(i) - 1;
score(i-1) = score(i-1) + 1; %player behind you gets it
elseif roll == 2 %token goes to center, no one gets it
score(i) = score(i) - 1;
elseif roll == 3 && i == n
%if last person rolls a R, player 1 gets the token
score(i) = score(i) - 1;
score(1) = score(1) + 1;
elseif roll == 3 && i ~= n
score(i) = score(i) - 1;
score(i+1) = score(i+1) + 1;
end
if j >= 3 %Can't roll more than three dice in a turn
break
end
end
if sum(score) == max(score) %is game over?
count = count + 1;
[maximum, index] = max(score);
%store the game stats
data(game,1) = index; %winner #
data(game,2) = roll; %deciding roll (L, C, or R)
data(game,3) = count; %number of total rolls
break
else
count = count + 1;
end
end
if sum(score) == max(score)
[maximum, index] = max(score);
break
else
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment