all I have to do is the royal flush
poker game
You are generous with the likes, but it is not acceptable to make your helpers guess. You must be generous with your code, if you wish to succeed.
@pbivens67 I think a very good excersize for you would to reduce the duplicated code, eg from:
if (arr[0] == 1 && arr[1] == 2 && arr[2] == 3 && arr[3] == 4 && arr[4] == 5)
{
surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
}
if (arr[0] == 2 && arr[1] == 3 && arr[2] == 4 && arr[3] == 5 && arr[4] == 6)
{
surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
}
if (arr[0] == 3 && arr[1] == 4 && arr[2] == 5 && arr[3] == 6 && arr[4] == 7)
{
surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
}
...
to a loop around a single “if” test. It may not be entirely possible, but any reduction in duplicated code is welcome.
Something like below may work for many of the “if”s that you have.
for (int c = ..; c < ...; c++)
{
if (....)
{
surface_fourteen = TTF_RenderText_Solid(font, text_seven, textColor);
SDL_BlitSurface(surface_fourteen, NULL, gScreenSurface, &score_one);
}
}
To give you a start, try to find a pattern in the “if” lines.
if (arr[0] == 1 && arr[1] == 2 && arr[2] == 3 && arr[3] == 4 && arr[4] == 5)
if (arr[0] == 2 && arr[1] == 3 && arr[2] == 4 && arr[3] == 5 && arr[4] == 6)
if (arr[0] == 3 && arr[1] == 4 && arr[2] == 5 && arr[3] == 6 && arr[4] == 7)
I pasted a few above. As you can see, the numbers you compare against increase in the same way in almost all “if” lines in your code.
Assume the value of “c” is used to compare against the first value in the array. Can you see a way to have the same comparison but in terms of a value with “c”?
In other words, think that “c = 1”, and then do the same compare as the first pasted “if” line by comparing with “c” and a correction. Something like
if (arr[0] == c && arr[1] == c + ... && arr[2] == c + ... && arr[3] == c + ... && arr[4] == c + ...)
To check your work, you can write a small program, like
int arr[5] = {1, 2, 3, 4, 5};
// Compute what your old code is doing:
if (arr[0] == 1 && arr[1] == 2 && arr[2] == 3 && arr[3] == 4 && arr[4] == 5)
printf("hooray 1\n");
int c = 1;
// Compute what the test with the c value is doing:
if (arr[0] == c && arr[1] == c + ... && arr[2] == c + ... && arr[3] == c + ... && arr[4] == c + ...)
printf("hooray with c=%d\n", c);
Once this works, test the second “if” line in a similar way . Change the value of “c” to 2 (the second “if” tests arr[0] against that value), but do not change the “if (arr[0] == c && arr[1] == c + ... && …..)” line again.
See if you can cover more of your “if”-lines by the same “if (arr[0] == c && arr[1] == c + ... && …..)” line (with a different value for c for each different “if” line in your old code). In the best case, you can make it work for all “if”-lines that you have, but that may be too tricky for now, for you, so don't worry if an “if”-line doesn't fit the pattern. Keep that case like you have it.
Once you decided which of your “if” lines fit the pattern, try to collapse all of them to one “if with a c value” line, and wrap a “for” loop that changes the “c” at each iteration around it (like I showed above).
The above is basically how you improve code. You look at it again, and try to find code that looks very similar. Then you try to find a way to move the differences out of the code and into (for example), a variable value.
If you find such a way, you can make an improvement that reduces duplication.
Often, there are more such opportunities to improve the code. So don't stop looking for it after one time.
As you do this more often, your experience grows. Eventually, you may already try to reduce duplication at the moment you are tempted to copy code, but that takes a while to grow as a habit.