Bowling score in C;

2

I'm new to C and I'm having problems in an exercise, I can not see a way to solve it. Next:

At each stage the player has 2 balls with which he must drop 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 a for to check the value of each vector position, so that's fine, my problem is to compare the next two values if the first one 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 that 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;

    
asked by anonymous 24.04.2015 / 18:51

2 answers

2

To walk more than one house just increment i when it is 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;
}

See here to work .

    
29.04.2015 / 10:13
0

Dude, you should review the structure you used to save the data for each move. In the way that the data is being saved, you can not know which round the punctuation refers to unless you do a complete execution of the algorithm, which makes it extremely inefficient (after all, saving the result of the moves is exactly what you need not need do it all over again.)

I recommend you create a custom structure, for example:

struct rodada{
    int jogada1;
    int jogada2;
};

struct rodada game1[20];

In this way the information about the result of the i-th move will be only one distance instruction. I mean, you just have to resultado = game1[10].jogada1; to know if it was a strike or not. There's no need to read ALL PLAYS up 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.

Note: Sorry if there are any errors in the code, I have not programmed in C.
Obs2: You could also add another variable in the struct to keep the partial score of each round.

    
24.04.2015 / 23:11