Bowling score in C;

Asked

Viewed 937 times

2

I’m a beginner in C And I’m having problems in an exercise, I can’t see a way to solve it. Following:

In each stage the player has 2 balls with which to knock down 10 pins. The stage ends when: the player knocks down the 10 pins or the player uses the 2 balls.

i = bola1;
j = bola2;

pontuação
    1 – STRIKE   se i = 10 (a 2a bola não é usada); 
    2 – SPARE    se i < 10, mas  i + j = 10;
    3 – MISS       se i + j < 10;
Valor pontuação
STRIKE - 10 + números de pinos que o jogador derrubar com as duas bolas  seguintes.  
SPARE - 10 + número de pinos derrubados com a próxima bola   
MISS - i + j


a) Leia uma seqüência de números inteiros que descreve  um jogo completo. O i-ésimo termo da sequência é o número de pinos derrubados pela i-ésima bola. Qual será o tamanho máximo da seqüência? 
b) Identifique a quantidade de pinos derrubados por cada etapa e classifique como STRIKE, SPARE ou MISS. 
c) Calcule o número de pontos por etapa 
d) Calcule o total de pontos no jogo 
e) Imprima os resultados   

Numero de exemplo;
    1o jogo: 10, 9, 1, 6, 3, 7, 0, 8, 2, 0, 8, 2, 0, 8, 2, 0, 8, 1, 0, 9, 1;   
    2o jogo: 9, 1, 0, 10, 10, 10, 6, 2, 7, 3, 8, 2, 10, 9, 0, 9, 1, 2; 
    3o jogo: 10, 9, 0, 8, 2, 10, 10, 7, 3, 4, 6, 10, 9, 1, 10, 7, 3;

Well, I tried to use one for to check the value of each vector position, so far so good, my problem is to compare the next two values if the first is not a Strike.

main() {
    int game1[20] = {10, 9, 1, 6, 3, 7, 0, 8, 2, 0, 8, 2, 0, 8, 2, 0, 8, 10, 9, 1};
    int game2[18] = {9, 1, 0, 10, 10, 10, 6, 2, 7, 3, 8, 2, 10, 9, 0, 9, 1, 2};
    int game3[17] = {10, 9, 0, 8, 2, 10, 10, 7, 3, 4, 6, 10, 9, 1, 10, 7, 3};
    int i, j;

    for(i=0; i<20; i++) {
        for(j=0; j<20; j++) {
            if(game1[i] == 10) {
                printf("Strike");
            }
        }
    }   
} 

This is the code I started to do, but my doubt is more in the logical part, how to compare the value of i and j to know if it is a Strike, Spare or Miss;

  • 1

    Edit your question and post the code you already tried for the staff to help you, ;)

  • edited, thanks ^^

  • I do not see why to use 2 is, just 1 and follow its logic: if Game1[i] = 10 then it is strike, otherwise if Game1[i] + Game1[i+1] = 10 then it is Spare, and so it goes. All you have to do is look at Game1[i + 1], because it will burst the vector when it is at the end, so you have to check.

  • I tried the way you said Read More, it even worked, but, for example, after comparing the first 10, it goes on to compare the 9 with the 1, and then the 1 with the 6. After comparing the 9 with the 1 it compares the 6 with the 3 respectively. I don’t know if I understand it. But your answer has already given me a good light here. Thank you ^^

2 answers

2

For him to walk more than a house just increase the i when it’s not "STRIKE".

#include <stdio.h>
#define JOGADAS 20

int main(void) {
    int game1[JOGADAS] = {10, 9, 1, 6, 3, 7, 0, 8, 2, 0, 8, 2, 0, 8, 2, 0, 8, 10, 9, 1};
    //int game2[18] = {9, 1, 0, 10, 10, 10, 6, 2, 7, 3, 8, 2, 10, 9, 0, 9, 1, 2};
    //int game3[17] = {10, 9, 0, 8, 2, 10, 10, 7, 3, 4, 6, 10, 9, 1, 10, 7, 3};
    int i;

    for(i=0; i<JOGADAS; i++) {
        if(game1[i] == 10) {
            printf("STRIKE\n");
        }
        else if (game1[i]+game1[i+1] == 10) {
            printf("SPARE\n");
            i++; //avança mais uma casa para o próximo jogo.
        }
        else {
            printf("MISS\n");
            i++; //avança mais uma casa para o próximo jogo.
        }

    }
    return 0;
}

Behold here to work.

  • Man, thank you very much, alias, very originated to everyone who helped. It worked out here too, now the rest of the exercise I can do in a good. ^^

  • @Yutosan From what you’re saying, it seems to be the case mark an answer as accepted. If you have an answer that really helped you, mark it as accepted. So content is more organized and easier to find in the future by other people with similar problems.

0

Dude, you should review the structure you used to store the dice for each move. The way the data is being saved, you can’t know which round the score refers to unless you do a full execution of the algorithm, which makes it extremely inefficient (after all save the result of the plays serves exactly so you do not need to do everything again).

I recommend that you create a custom structure, for example:

struct rodada{
    int jogada1;
    int jogada2;
};

struct rodada game1[20];

That way the information about the outcome of the i-th move will be only one instruction away. I mean, you just do a resultado = game1[10].jogada1; to know if it was a strike or not. No need to read ALL MOVES to the tenth for such simple information.

I also recommend setting some invalid value (such as an "x" or a boolean variable) in the second move to signal that there was a strike in the round.

Obs: Sorry if there’s some error in the code.
Obs2: You could also add another variable in the struct to keep the partial score of each round.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.