1

I made a little dice game where I should get Grand if I throw 5 times the same number and a Poker if I throw 4 times the same number.

My problem is that my code only work with "1" and "2" if I try to get a Poker with 4 it doesn't count


#include <stdio.h>

void abfrage(int wurf[], int size){
    int i;
        for (i=0; i<size; i++){
            printf("Würfel %i: ", i+1);
            scanf("%i", &wurf[i]);
        }
}

// switch int*
void swap(int *xp, int *yp) 
{ 
    int temp = *xp; 
    *xp = *yp; 
    *yp = temp; 
} 
// Sort the array
void bubbleSort(int wurf[], int size) 
{ 
    int i, j; 
    for (i = 0; i < size-1; i++){          
        for (j = 0; j < size-i-1; j++){ 
            if (wurf[j] > wurf[j+1])
            {
                swap(&wurf[j], &wurf[j+1]); 
            }
        }
    }
} 

void arrayausgabe(int wurf[], int size){
    int u = 0;
    for (u=0;u<size;u++) {
        printf("array nummer %i: ", u); // err with "&"
        printf("%i \n", wurf[u]);
    }
}

void bewertung(int wurf[]){
    int *x;
    x = wurf;

    if (*x==*(x+1)==*(x+2)==*(x+3)==*(x+4)) {
        printf("Grand");
    }else if (*x==*(x+1)==*(x+2)==*(x+3) || *(x+1)==*(x+2)==*(x+3)==*(x+4)) {
        printf("Poker");
    }else if ((*x==*(x+1)==*(x+2) && *(x+3)==*(x+4) )||(*(x+2)==*(x+3)==*(x+4) &&  *x==*(x+1))) {
        printf("Full House");
    }else {
        printf("HAAA Verloren");
    }
}

int main() {
    int  wurf[5];
    printf("Programm Würfelspiel\nGrand\tgleiche Augenzahl auf allen 5 Würfeln\nPoker\tgleiche Augenzahl auf 4 Würfeln\nFull House\tgleiche und 2 gleiche Augenzahlen\n\nBitte gibt deine gewürfelten zahlen ein\n");
    abfrage(wurf, 5);
    bubbleSort(wurf, 5);
    arrayausgabe(wurf, 5);
    bewertung(wurf);
}

I'm in freshman year so sorry if the code look's a bit trashy

4
  • Could you add comments explaining what different sections of this code is attempting to do? And explain a little clearer what the problem is, and how one would go about re-creating it? Commented Nov 18, 2019 at 19:38
  • 1
    Doing *(x+1) is completely unnecessary and confusing; access the array with array notation: wurf[0] == wurf[1]... Commented Nov 18, 2019 at 19:41
  • Didn't have time to inspect all the code but there is a definite error in your bubble sort function. (1) Revisit the upper & lower bounds for the for loops and (2) the indexes of the conditional line if (wurf[j] > wurf[j+1]) as well. Commented Nov 18, 2019 at 19:45
  • You should also check the return value from scanf() to make sure it read the number of fields you expect. Commented Nov 18, 2019 at 19:50

1 Answer 1

2

This logic:

if (*x==*(x+1)==*(x+2)==*(x+3)==*(x+4)) {

does not do what you expect in C (though it does in Python): a comparison with == returns either 0 or 1, according to whether the values are different or equal. Chaining them doesn't check whether they're all equal to each other; instead, it checks if the first two are equal, and then compares the result of that equality (which can be 0 or 1) against the value of the third value, and so on.

You need to check each comparison separately:

if (wurf[0] == wurf[1] && wurf[0] == wurf[2] && wurf[0] == wurf[3] && wurf[0] == wurf[4]) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.