I need help with an error

1 次查看(过去 30 天)
mn n
mn n 2018-4-27
回答: Siyu Guo 2018-4-28
This is my code. It is functional, but whenever I run it, there is an error. I am stimulating the dart game cricket.
This is the error.
darts(17,19)
Index in position 1 exceeds array bounds (must not exceed 1).
Error in darts (line 22)
wedge = (wedges(xw,yw));
Code is:
function [score1,score2] = darts(wedges,rings)
% goal: Close out numbers 15-20
% Hit each three times (three singles, double and a single or a triple)
m1 =[15:20,25;ones(3,7)];
m2 =[15:20,25;ones(3,7)];
I = [15:21];
j = length(I);
k = 0;
n = 0;
score1 = 0;
score2 = 0;
while m1(4,1) + m1(4,2) + m1(4,3) + m1(4,4) + m1(4,5) + m1(4,6) + m1(4,7)~=0 && m2(4,1) + m2(4,2) + m2(4,3) + m2(4,4) + m2(4,5) + m2(4,6) + m2(4,7)~=0
n = n+1;
for i = 1:3
n = n +1;
xr = randi([1 512],1,1);
yr = randi([1 512],1,1);
xw = randi([1 512],1,1);
yw = randi([1 512],1,1);
wedge = (wedges(xw,yw));
wedgex = wedge-(j+7) ;
ring = rings(xr,yr);
if wedge >14 && wedge <21
if ring == 3
m1(2:4,wedgex) = 0;
else
if m1(2,wedgex)==0 && m1(3,wedgex)==0 && m1(4,wedgex)==0
score1 = score1 + wedge*ring
else
if ring >1
if m1(1+ring,wedgex)~=0 & m1(2+ring,wedgex)~=0
m1(2:ring+1,wedgex)=0;
display(m1);
elseif m1(2:ring+1,wedgex)==0
if m1(2:ring+2,wedgex)==0
m1(2:ring+3,wedgex)=0;
display(m1);
elseif m1(ring+2,wedgex)~=0
m1(2:ring+2,wedgex)=0;
display(m1);
end
end
elseif ring ==1
if m1(2:ring+1,wedgex)~=0
m1(2:ring+1,wedgex)=0;
else
if m1(2:ring+2,wedgex)==0
m1(2:ring+3,wedgex)=0;
display(m1);
else
m1(2:ring+2,wedgex)=0;
display(m1);
end
end
end
end
end
if m1(2:4,wedgex) == 0
end
end
if wedge == 25
wedgex = 7;
if ring == 2 || ring ==3
if m1(2,7)==0 && m1(3,7) == 0 && m1(4,7) == 0
score1 = score1 + wedge*2
end
else
score1 = score1 + 25*ring
if m1(2:ring+1,7)~=0
m1(2:ring+1,7)=0;
display(m1);
else
if m1(2:ring+2,7)~=0
m1(2:ring+2,7) = 0;
display(m1);
else
m1(2:ring+3,7) ~= 0;
m1(2:ring+3,7) = 0;
display(m1);
end
end
end
end
if wedge < 15
display('You did not score');
end
fprintf('The dart landed on the %d ring in the %d wedge \n',ring,wedge)
end
score1
score2
for i = 1:3
n = n +1;
xr = randi([1 512],1,1);
yr = randi([1 512],1,1);
xw = randi([1 512],1,1);
yw = randi([1 512],1,1);
wedge = (wedges(xw,yw));
wedgex = wedge-(j+7);
ring = rings(xr,yr);
if wedge >14 && wedge <21
if ring == 3
m2(2:4,wedgex) = 0;
else
if m2(2,wedgex)==0 && m2(3,wedgex)==0 && m2(4,wedgex)==0
score2 = score2 + wedge*ring
else
if ring >1
if m2(1+ring,wedgex)~=0 & m2(2+ring,wedgex)~=0
m2(2:ring+1,wedgex)=0;
display(m2);
elseif m2(2:ring+1,wedgex)==0
if m2(2:ring+2,wedgex)==0
m2(2:ring+3,wedgex)=0;
display(m2);
elseif m2(ring+2,wedgex)~=0
m2(2:ring+2,wedgex)=0;
display(m2);
end
end
elseif ring ==1
if m2(2:ring+1,wedgex)~=0
m2(2:ring+1,wedgex)=0;
else
if m2(2:ring+2,wedgex)==0
m2(2:ring+3,wedgex)=0;
display(m2);
else
m2(2:ring+2,wedgex)=0;
end
end
end
end
end
if m2(2:4,wedgex) == 0
end
end
if wedge == 25
wedgex=7;
if ring == 2 || ring ==3
if m2(2,7)==0 && m2(3,7) == 0 && m2(4,7) == 0
score2 = score2 + wedge*2
end
else
score2 = score2 + 25*ring
if m2(2:ring+1,7)~=0
m2(2:ring+1,7)=0;
display(m2);
else
if m2(2:ring+2,7)~=0
m2(2:ring+2,7) = 0;
display(m2);
else
m2(2:ring+3,7) ~= 0;
m2(2:ring+3,7) = 0;
display(m2);
end
end
end
end
if wedge < 15
display('You did not score');
end
fprintf('The dart landed on the %d ring in the %d wedge \n',ring,wedge)
end
score1
score2
end
if m1(4,1) + m1(4,2) + m1(4,3) + m1(4,4) + m1(4,5) + m1(4,6) + m1(4,7)==0
fprintf('Player 1 closed his board out first and has a score of %d \n',score1)
fprintf('Player 2 has a score of %d \n',score2)
if score1 > score2
fprintf('Player 1 wins with a score of %d \n',score1)
else
if score1 < score2 || m2(4,1) + m2(4,2) + m2(4,3) + m2(4,4) + m2(4,5) + m2(4,6) + m2(4,7)~=0
disp('Player 1 closed out there board but player 2s score is higher, keep playing')
while score1 < score2 && m2(4,1) + m2(4,2) + m2(4,3) + m2(4,4) + m2(4,5) + m2(4,6) + m2(4,7)~=0
n = n +1;
for i = 1:3
n = n +1;
xr = randi([1 512],1,1);
yr = randi([1 512],1,1);
xw = randi([1 512],1,1);
yw = randi([1 512],1,1);
wedge = (wedges(xw,yw));
wedgex = wedge-(j+7) ;
ring = rings(xr,yr);
if wedge >14 && wedge <21
if ring == 3
m1(2:4,wedgex) = 0;
else
if m1(2,wedgex)==0 && m1(3,wedgex)==0 && m1(4,wedgex)==0
score1 = score1 + wedge*ring
else
if ring >1
if m1(1+ring,wedgex)~=0 & m1(2+ring,wedgex)~=0
m1(2:ring+1,wedgex)=0;
display(m1);
elseif m1(2:ring+1,wedgex)==0
if m1(2:ring+2,wedgex)==0
m1(2:ring+3,wedgex)=0;
display(m1);
elseif m1(ring+2,wedgex)~=0
m1(2:ring+2,wedgex)=0;
display(m1);
end
end
elseif ring ==1
if m1(2:ring+1,wedgex)~=0
m1(2:ring+1,wedgex)=0;
else
if m1(2:ring+2,wedgex)==0
m1(2:ring+3,wedgex)=0;
display(m1);
else
m1(2:ring+2,wedgex)=0;
display(m1);
end
end
end
end
end
if m1(2:4,wedgex) == 0
end
end
if wedge == 25
wedgex = 7;
if ring == 2 || ring ==3
if m1(2,7)==0 && m1(3,7) == 0 && m1(4,7) == 0
score1 = score1 + wedge*2
end
else
score1 = score1 + 25*ring
if m1(2:ring+1,7)~=0
m1(2:ring+1,7)=0;
display(m1);
else
if m1(2:ring+2,7)~=0
m1(2:ring+2,7) = 0;
display(m1);
else
m1(2:ring+3,7) ~= 0;
m1(2:ring+3,7) = 0;
display(m1);
end
end
end
end
if wedge < 15
display('You did not score');
end
fprintf('The dart landed on the %d ring in the %d wedge \n',ring,wedge)
end
score1
score2
for i = 1:3
n = n +1;
xr = randi([1 512],1,1);
yr = randi([1 512],1,1);
xw = randi([1 512],1,1);
yw = randi([1 512],1,1);
wedge = (wedges(xw,yw));
wedgex = wedge-(j+7);
ring = rings(xr,yr);
if wedge >14 && wedge <21
if ring == 3
m2(2:4,wedgex) = 0;
else
if m2(2,wedgex)==0 && m2(3,wedgex)==0 && m2(4,wedgex)==0
score2 = score2 + wedge*ring
else
if ring >1
if m2(1+ring,wedgex)~=0 & m2(2+ring,wedgex)~=0
m2(2:ring+1,wedgex)=0;
display(m2);
elseif m2(2:ring+1,wedgex)==0
if m2(2:ring+2,wedgex)==0
m2(2:ring+3,wedgex)=0;
display(m2);
elseif m2(ring+2,wedgex)~=0
m2(2:ring+2,wedgex)=0;
display(m2);
end
end
elseif ring ==1
if m2(2:ring+1,wedgex)~=0
m2(2:ring+1,wedgex)=0;
else
if m2(2:ring+2,wedgex)==0
m2(2:ring+3,wedgex)=0;
display(m2);
else
m2(2:ring+2,wedgex)=0;
end
end
end
end
end
if m2(2:4,wedgex) == 0
end
end
if wedge == 25
wedgex=7;
if ring == 2 || ring ==3
if m2(2,7)==0 && m2(3,7) == 0 && m2(4,7) == 0
score2 = score2 + wedge*2
end
else
score2 = score2 + 25*ring
if m2(2:ring+1,7)~=0
m2(2:ring+1,7)=0;
display(m2);
else
if m2(2:ring+2,7)~=0
m2(2:ring+2,7) = 0;
display(m2);
else
m2(2:ring+3,7) ~= 0;
m2(2:ring+3,7) = 0;
display(m2);
end
end
end
end
if wedge < 15
display('You did not score');
end
fprintf('The dart landed on the %d ring in the %d wedge \n',ring,wedge)
end
score1
score2
end
if score1 > score2
fprintf('Player 1 reached a higher score before player 2 could close out his board \n');
fprintf('Player 1 wins with a score of %d to % d \n',score1,score2);
else
fprintf('Player 2 closed out his board with a higher score then player 1 \n ');
fprintf('Player 2 wins with a score of %d to %d \n',score2,score1);
end
end
end
else
fprintf('Player 2 closed his board out first and has a score of %d \n',score2)
fprintf('Player 1 has a score of %d \n',score1)
if score2 > score1
fprintf('Player 2 wins with a score of %d \n',score2)
else
if score2 < score1 || m1(4,1) + m1(4,2) + m1(4,3) + m1(4,4) + m1(4,5) + m1(4,6) + m1(4,7)~=0
disp('Player 2 closed out there board but player 1s score is higher, keep playing')
while score2 < score1 && m1(4,1) + m1(4,2) + m1(4,3) + m1(4,4) + m1(4,5) + m1(4,6) + m1(4,7)~=0
n = n+1;
for i = 1:3
n = n +1;
xr = randi([1 512],1,1);
yr = randi([1 512],1,1);
xw = randi([1 512],1,1);
yw = randi([1 512],1,1);
wedge = (wedges(xw,yw));
wedgex = wedge-(j+7) ;
ring = rings(xr,yr);
if wedge >14 && wedge <21
if ring == 3
m1(2:4,wedgex) = 0;
else
if m1(2,wedgex)==0 && m1(3,wedgex)==0 && m1(4,wedgex)==0
score1 = score1 + wedge*ring
else
if ring >1
if m1(1+ring,wedgex)~=0 & m1(2+ring,wedgex)~=0
m1(2:ring+1,wedgex)=0;
display(m1);
elseif m1(2:ring+1,wedgex)==0
if m1(2:ring+2,wedgex)==0
m1(2:ring+3,wedgex)=0;
display(m1);
elseif m1(ring+2,wedgex)~=0
m1(2:ring+2,wedgex)=0;
display(m1);
end
end
elseif ring ==1
if m1(2:ring+1,wedgex)~=0
m1(2:ring+1,wedgex)=0;
else
if m1(2:ring+2,wedgex)==0
m1(2:ring+3,wedgex)=0;
display(m1);
else
m1(2:ring+2,wedgex)=0;
display(m1);
end
end
end
end
end
if m1(2:4,wedgex) == 0
end
end
if wedge == 25
wedgex = 7;
if ring == 2 || ring ==3
if m1(2,7)==0 && m1(3,7) == 0 && m1(4,7) == 0
score1 = score1 + wedge*2
end
else
score1 = score1 + 25*ring
if m1(2:ring+1,7)~=0
m1(2:ring+1,7)=0;
display(m1);
else
if m1(2:ring+2,7)~=0
m1(2:ring+2,7) = 0;
display(m1);
else
m1(2:ring+3,7) ~= 0;
m1(2:ring+3,7) = 0;
display(m1);
end
end
end
end
if wedge < 15
display('You did not score');
end
fprintf('The dart landed on the %d ring in the %d wedge \n',ring,wedge)
end
score1
score2
for i = 1:3
n = n +1;
xr = randi([1 512],1,1);
yr = randi([1 512],1,1);
xw = randi([1 512],1,1);
yw = randi([1 512],1,1);
wedge = (wedges(xw,yw));
wedgex = wedge-(j+7);
ring = rings(xr,yr);
if wedge >14 && wedge <21
if ring == 3
m2(2:4,wedgex) = 0;
else
if m2(2,wedgex)==0 && m2(3,wedgex)==0 && m2(4,wedgex)==0
score2 = score2 + wedge*ring
else
if ring >1
if m2(1+ring,wedgex)~=0 & m2(2+ring,wedgex)~=0
m2(2:ring+1,wedgex)=0;
display(m2);
elseif m2(2:ring+1,wedgex)==0
if m2(2:ring+2,wedgex)==0
m2(2:ring+3,wedgex)=0;
display(m2);
elseif m2(ring+2,wedgex)~=0
m2(2:ring+2,wedgex)=0;
display(m2);
end
end
elseif ring ==1
if m2(2:ring+1,wedgex)~=0
m2(2:ring+1,wedgex)=0;
else
if m2(2:ring+2,wedgex)==0
m2(2:ring+3,wedgex)=0;
display(m2);
else
m2(2:ring+2,wedgex)=0;
end
end
end
end
end
if m2(2:4,wedgex) == 0
end
end
if wedge == 25
wedgex=7;
if ring == 2 || ring ==3
if m2(2,7)==0 && m2(3,7) == 0 && m2(4,7) == 0
score2 = score2 + wedge*2
end
else
score2 = score2 + 25*ring
if m2(2:ring+1,7)~=0
m2(2:ring+1,7)=0;
display(m2);
else
if m2(2:ring+2,7)~=0
m2(2:ring+2,7) = 0;
display(m2);
else
m2(2:ring+3,7) ~= 0;
m2(2:ring+3,7) = 0;
display(m2);
end
end
end
end
if wedge < 15 || ring==0
display('You did not score');
end
fprintf('The dart landed on the %d ring in the %d wedge \n',ring,wedge)
end
score1
score2
end
if score1 > score2
fprintf('Player 1 reached a higher score before player 2 could close out his board \n');
fprintf('Player 1 wins with a score of %d to % d \n',score1,score2);
else
fprintf('Player 2 closed out his board with a higher score then player 1 \n ');
fprintf('Player 2 wins with a score of %d to %d \n',score2,score1);
end
end
end
end
end

回答(1 个)

Siyu Guo
Siyu Guo 2018-4-28
Your function call is darts(17,19), which means the wedges parameter passed into the function is the scalar 17, represented in MATLAB as a 1-by-1 matrix. Then, in line 22, you try to acccess wedges(xw,yw), where xw and yw are both random intergers in the range of [1, 512], and very likely both xw and yw are greater than 1, exceeding the size of the matrix wedges, and the error shows up.

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by