Hi,
I have some thoughts to improve your code a bit.
I completely forgot about that... too much coding. |
Yes, IMO you are writing too much hard to understand code 8+)
After writing a bunch of comments below, I decide it might be better for you to write some pseudo-code. Start with very general instructions (as comments in your editor) for completing your program, then go back & repeatedly refine them, until you are happy to write code. Leave the comments there as they serve as documentation.
Sorry if it sounds as though I have poured icy water all over your code. I know people spend spends hours writing lots of code, then someone comes along & basically says "start again". I urge you to take on board this advice, as your learning outcome will be much better if you actually learn how to write code well.
232 LOC is too much for one function - there is a "rule" that functions should be less than 40 LOC. Also functions should do one thing, and one should use them to do a "Divide & Conquer" approach. Both these ideas will make your code easier to read, understand & debug.
Candidates for functions include compound statements (code inside braces), such as in loops and
switch
statement cases - although you might not need any switches in your code.
So, to me lines 10 - 16, 40 - 46, etc all look very similar, as does the code surrounding them. The first number starts at 3 and increases by 1 in each successive group of code. The second number starts at 1 and increases by 7 on each line, these numbers are same for each group of code. Btw it is not a good idea to have "magic numbers" like this in your code - consider making them variables or enums instead. Lines 4 - 30 look similar, can you figure out how to write 1 function that can be called multiple times to achieve the same thing? If you are writing the same code over & over then that means there is a better way.
With this:
1 2 3 4 5 6 7 8 9 10 11
|
j = rand() % 7;
switch (j)
{
case 0: pcship5[0] = rand() % 3 + 1;
case 1: pcship5[0] = rand() % 3 + 8;
case 2: pcship5[0] = rand() % 3 + 15;
case 3: pcship5[0] = rand() % 3 + 22;
case 4: pcship5[0] = rand() % 3 + 29;
case 5: pcship5[0] = rand() % 3 + 36;
case 6: pcship5[0] = rand() % 3 + 43;
}
|
You are repeatedly setting
pcship5[0]
so the result is the same as setting it once in
case 6:
.
1 2 3 4 5 6 7 8 9 10 11
|
j = rand() % 7;
switch (j)
{
case 0: pcship5[0] = rand() % 3 + 1;
case 1: pcship5[0] = rand() % 3 + 8;
case 2: pcship5[0] = rand() % 3 + 15;
case 3: pcship5[0] = rand() % 3 + 22;
case 4: pcship5[0] = rand() % 3 + 29;
case 5: pcship5[0] = rand() % 3 + 36;
case 6: pcship5[0] = rand() % 3 + 43;
}
|
Did you mean
pcship5[j]
?
If so, you are generating a random number which is used to set an array subscript, but it looks as though you just want to set all the values in the array - why not do that with a for loop, not a switch?
Try giving your variable more meaningful names - that will making understanding easier for everyone - including yourself. Good variable names make the code self documenting. There can be problems using
i
and
j
for variable names - they look too similar, one typo can cause all kinds of problems that are really hard to see. Use words like
row
or
col
if that is what the variable is for. I always use words or words joined together for variable names.
Also, your code is in desperate need of comments, notwithstanding what I said about variable names.
Hope this helps a lot, and I look forward to seeing your new code.