How to compute bowling game score

4 views (last 30 days)
Surya V
Surya V on 13 Jun 2015
Answered: Jenny Lay on 15 Apr 2016
Problem:
"The function should return the score of 'bowling game' from the given input argument vector. If the input is not a valid sequence it should return -1. example input [9 1 0 10 10 10 6 2 7 3 8 2 10 9 0 9 1 10] ; output score is 168."
I have tried to code for computing the game score for the above mentioned bowling problem and for some inputs it turns wrong score; for example for the input [1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 1 9 1 10 1 1]
[Conditions: Strike counts 10+sum of pins knocked down by the next two balls. Spare counts 10 + the pins knocked down by the next ball. open counts the number of pins knocked down in that frame. Bonus ball counts the number of pins it knocked down.
function scores = bowl(games)
dummy = games;
[r, c] = size(dummy);
games=dummy;
% reshaping the given input vector by plugging zeros after strikes:
for idx = 1 : 2 : 19
if games(idx) == 10
games = [games(1 : idx), 0, dummy(idx +1 :c)]
dummy = games
[r, c] = size(dummy);
end
end
% invalid input verification
if length(games)<20
scores=-1;
elseif length(games)>20 && (games(19)+games(20))~=10
scores=-1;
else
%score computation
games = [games, zeros(r, 1)];
lastwasstrike = games(:, 19) == 10;
games(lastwasstrike, 21 : 22) = games(lastwasstrike, 20 : 21);
games(lastwasstrike, 20) = 10;
startscores = sum(games(:, 1:20), 2);
isspare = games(:, 1:2:19) + games(:, 2:2:20) == 10;
sparescores = sum((games(:, 3:2:21).*isspare), 2);
isstrike = games(:, 1:2:19) == 10;
strikescores = sum((games(:, 4:2:22).*isstrike), 2);
isdoublestrike = games(:, 1:2:17) == 10 & games(:, 3:2:19) == 10;
doublestrikescores = sum((games(:, 5:2:21).*isdoublestrike), 2);
scores = startscores+sparescores+strikescores+doublestrikescores;
end
end
  1 Comment
Walter Roberson
Walter Roberson on 13 Jun 2015
Your code for plugging 0's is wrong. Consider what it does if the first two balls are both strikes.

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 14 Jun 2015
Surya - you may want to reconsider the lines
lastwasstrike = games(:, 19) == 10;
games(lastwasstrike, 21 : 22) = games(lastwasstrike, 20 : 21);
games(lastwasstrike, 20) = 10;
What is the purpose of determining whether the last frame generated a strike? Why replace the "eleventh frame" with one of the values from the tenth frame? And why initialize the 20th element of the games array to 10? If I comment out these lines, then I seem to get the correct answer for both your examples (from above).

Jenny Lay
Jenny Lay on 15 Apr 2016
I had a similar question and this was what i did:
function final_score=bowl(num_p)
clear all
clc
total = 0;
function initial_check()
if length_of_inputs>21 || length_of_inputs<12
total=-1;
end
for n0=1:length_of_inputs
if num_p(n0)>10 || num_p(n0)<0
total=-1;
end
end
end
function second_check()
if k1>10
total=-1;
end
end
num_p=input('Enter the number of pins: ');
length_of_inputs=length(num_p);
n=length_of_inputs-3;
x=1;
n1=1;
%frame 1~9
while n1<=n;
initial_check();
k1=num_p(n1)+num_p(n1+1);
if num_p(n1)==10 %strike
sum_k1(x)=num_p(n1)+num_p(n1+1)+num_p(n1+2);
elseif k1==10 %spare
second_check();
sum_k1(x)=k1+num_p(n1+2);
n1=n1+1;
else %open
second_check();
sum_k1(x)=k1;
n1=n1+1;
end
x=x+1;
n1=n1+1;
end
fsum_k1=cumsum(sum_k1);
%frame 10 (last frame)
n=n+1;
sum_k2=num_p(n)+num_p(n+1)+num_p(n+2);
k2ab=num_p(n)+num_p(n+1);
if k2ab<10
fsum_k2=k2ab;
elseif sum_k2>30
disp(-1)
else
fsum_k2=sum_k2;
end
last_score=fsum_k1(end)+fsum_k2;
final_score=[fsum_k1,last_score];
if total==-1
final_score=-1;
end
end

Categories

Find more on Board games in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!