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
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]) {