Note that there are some explanatory texts on larger screens.

plurals
  1. POTroubles with battleship game in C
    text
    copied!<p>So i've been trying to write a sink the battleship game in C. I already wrote a simple version with randomly generated booleans however i was not happy with ships being only one block in size and there were too many of them, but i digress. </p> <p>Here i've wrote what i believe is a messy piece of code, and it works, sometimes...</p> <p>Here it is:</p> <pre><code>void generate_field(int *i, int *j, int n) { *i=rand()%n; *j=rand()%n; } void map_gen(struct game *data,int n) { int i,j,k,l; int return_value=0; for(i=0;i&lt;n;i++) { for(j=0;j&lt;n;j++) { data-&gt;tiles[i][j].ship=0; data-&gt;tiles[i][j].uncovered=0; } } // **4** generate_field(&amp;k,&amp;l,n); if(k==0 || k==1) { data-&gt;tiles[k][l].ship=4; data-&gt;tiles[k+1][l].ship=4; data-&gt;tiles[k+2][l].ship=4; data-&gt;tiles[k+3][l].ship=4; data-&gt;shipcount++; } else if(k==(n-1) || k==(n-2)) { data-&gt;tiles[k][l].ship=4; data-&gt;tiles[k-1][l].ship=4; data-&gt;tiles[k-2][l].ship=4; data-&gt;tiles[k-3][l].ship=4; data-&gt;shipcount++; } else if(l==0 || l==1) { data-&gt;tiles[k][l].ship=4; data-&gt;tiles[k][l+1].ship=4; data-&gt;tiles[k][l+2].ship=4; data-&gt;tiles[k][l+3].ship=4; data-&gt;shipcount++; } else if(l==(n-1) || l==(n-2)) { data-&gt;tiles[k][l].ship=4; data-&gt;tiles[k][l-1].ship=4; data-&gt;tiles[k][l-2].ship=4; data-&gt;tiles[k][l-3].ship=4; data-&gt;shipcount++; } // **3** do{ generate_field(&amp;k,&amp;l,n); }while(data-&gt;tiles[k][l].ship!=0 &amp;&amp; (data-&gt;tiles[k+1][l].ship!=0 || data-&gt;tiles[k-1][l].ship!=0 || data-&gt;tiles[k][l+1].ship!=0 || data-&gt;tiles[k][l-1].ship!=0) &amp;&amp; (data-&gt;tiles[k+2][l].ship!=0 || data-&gt;tiles[k-2][l].ship!=0 || data-&gt;tiles[k][l+2].ship!=0 || data-&gt;tiles[k][l-2].ship!=0)); if((k==0 || k==1) &amp;&amp; (data-&gt;tiles[k+1][l].ship==0 &amp;&amp; data-&gt;tiles[k+2][l].ship==0)) { data-&gt;tiles[k][l].ship=3; data-&gt;tiles[k+1][l].ship=3; data-&gt;tiles[k+2][l].ship=3; data-&gt;shipcount++; } else if((k==(n-1) || k==(n-2)) &amp;&amp; (data-&gt;tiles[k-1][l].ship==0 &amp;&amp; data-&gt;tiles[k-2][l].ship==0)) { data-&gt;tiles[k][l].ship=3; data-&gt;tiles[k-1][l].ship=3; data-&gt;tiles[k-2][l].ship=3; data-&gt;shipcount++; } else if((l==0 || l==1) &amp;&amp; (data-&gt;tiles[k][l+1].ship==0 &amp;&amp; data-&gt;tiles[k][l+2].ship==0)) { data-&gt;tiles[k][l].ship=3; data-&gt;tiles[k][l+1].ship=3; data-&gt;tiles[k][l+2].ship=3; data-&gt;shipcount++; } else if((l==(n-1) || l==(n-2)) &amp;&amp; (data-&gt;tiles[k][l-1].ship==0 &amp;&amp; data-&gt;tiles[k][l-2].ship==0)) { data-&gt;tiles[k][l].ship=3; data-&gt;tiles[k][l-1].ship=3; data-&gt;tiles[k][l-2].ship=3; data-&gt;shipcount++; } // **2** do{ generate_field(&amp;k,&amp;l,n); }while(data-&gt;tiles[k][l].ship!=0 &amp;&amp; (data-&gt;tiles[k+1][l].ship!=0 || data-&gt;tiles[k-1][l].ship!=0 || data-&gt;tiles[k][l+1].ship!=0 || data-&gt;tiles[k][l-1].ship!=0)); if((k==0 || k==1) &amp;&amp; (data-&gt;tiles[k+1][l].ship==0)) { data-&gt;tiles[k][l].ship=2; data-&gt;tiles[k+1][l].ship=2; data-&gt;shipcount++; } else if((k==(n-1) || k==(n-2)) &amp;&amp; (data-&gt;tiles[k-1][l].ship==0)) { data-&gt;tiles[k][l].ship=2; data-&gt;tiles[k-1][l].ship=2; data-&gt;shipcount++; } else if((l==0 || l==1) &amp;&amp; (data-&gt;tiles[k][l+1].ship==0)) { data-&gt;tiles[k][l].ship=2; data-&gt;tiles[k][l+1].ship=2; data-&gt;shipcount++; } else if((l==(n-1) || l==(n-2)) &amp;&amp; (data-&gt;tiles[k][l-1].ship==0)) { data-&gt;tiles[k][l].ship=2; data-&gt;tiles[k][l-1].ship=2; data-&gt;shipcount++; } // **1** do{ generate_field(&amp;k,&amp;l,n); }while(data-&gt;tiles[k][l].ship!=0); data-&gt;tiles[k][l].ship=1; data-&gt;shipcount++; } </code></pre> <p>the <code>**#**</code> are ship sizes.</p> <p>the <code>int n</code> is the size of a dimension of the matrix array(i have two sizes:Normal which is 5x5 and large which is 8x8)</p> <p>Anyway i know this could be written in a way simpler way and that it could actually work. The do-while loops are way too long and a lot of the times one or two my ships don't generate. I think it's because i somewhat limited their spawn by using(<code>k==0</code> or <code>k==n-1</code>) stuff, however i have no idea what to do. Can anyone here give me some hints of how could have i written this differently and more compact and in a way in which it actually works right?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload