Greetings, I am writing a naval battle for the academy, here’s a working piece of my draft project, the problem is that only the first line of the two-dimensional deck5 array is copied to the map.
#include <stdlib.h> #include <time.h> /* #include <conio.h> */ #define s 13 #define c 28 char deck5[3][7] = { { '0', '0', '0', '0', '0', '0', '0', '0' }, { '0', '*', '*', '*', '*', '*', '*', '0' }, { '0', '0', '0', '0', '0', '0', '0', '0' } }; char map[s][c]= { " 0123456789 0123456789 ", " ---------- ---------- ", "0| | 0| |", "1| | 1| |", "2| | 2| |", "3| | 3| |", "4| | 4| |", "5| | 5| |", "6| | 6| |", "7| | 7| |", "8| | 8| |", "9| | 9| |", " ---------- ---------- ", }; void show(char (*pmap)[c]) { int i,j; for(i=0; i<s; ++i) { for(j=0; j<c; ++j) { printf("%c",pmap[i][j]); } printf("\n"); } } h_ship5(char (*pmap)[c]) { int i=0,j=0,x=0,y=0,ii=0,jj=0,yes=0; do//зацикливает проверку правильности расположения корабля { x=rand()%7+2; y=rand()%7+2; yes=0; for(; j<(y+5); j++) { if(pmap[x][j]==' ') { yes++; } } } while(yes<5); x--,y--; i=x,j=y; for(;i<(x+3);i++){ if(jj==7){ ii++; jj=0;} for(;j<(y+7);j++){ jj++; if(pmap[i][j]==' ') pmap[i][j]=deck5[ii][jj]; } } return pmap; } int main() { srand( (unsigned)time( NULL ) ); char*pmap=map; pmap=h_ship5(pmap); show(pmap); return 0; }
Here is a simple copy of the array into the array, but for some reason only the first line is copied.
#define s 13 #define c 28 #define a 3 #define b 8 char deck5[a][b] = { "0000000", "0*****0", "0000000",}; char map[a][b] = { "1111111", "1*****1", "1111111",}; int main(int argc, char** argv) { int i = 0, j = 0; for (; i < 3; i++) { for (; j < 7; j++) { map[i][j] = deck5[i][j]; //if(j==7)deck5[i][j]='\0'; или map[i][j]='\0'; или оба варианта вместе не помогают } } for (i = 0; i < a; ++i) { for (j = 0; j < b; ++j) { printf("%c", map[i][j]); } printf("\n"); } return (EXIT_SUCCESS); }
Compiler Warnings:
\Desktop\test\test_fight\main.c||In function 'show':| \Desktop\test\test_fight\main.c|41|warning: implicit declaration of function 'printf'| \Desktop\test\test_fight\main.c|41|warning: incompatible implicit declaration of built-in function 'printf'| \Desktop\test\test_fight\main.c|43|warning: incompatible implicit declaration of built-in function 'printf'| \Desktop\test\test_fight\main.c|49|warning: return type defaults to 'int'| \Desktop\test\test_fight\main.c||In function 'h_ship5':| \Desktop\test\test_fight\main.c|82|warning: return makes integer from pointer without a cast| \Desktop\test\test_fight\main.c||In function 'main':| \Desktop\test\test_fight\main.c|91|warning: initialization from incompatible pointer type| \Desktop\test\test_fight\main.c|93|warning: passing argument 1 of 'h_ship5' from incompatible pointer type| \Desktop\test\test_fight\main.c|48|note: expected 'char (*)[28]' but argument is of type 'char *'| \Desktop\test\test_fight\main.c|93|warning: assignment makes pointer from integer without a cast| \Desktop\test\test_fight\main.c|94|warning: passing argument 1 of 'show' from incompatible pointer type| \Desktop\test\test_fight\main.c|33|note: expected 'char (*)[28]' but argument is of type 'char *'| ||=== Build finished: 0 errors, 9 warnings ===|
implicit declaration of function 'printf'
- no printf prototype (it is in stdio.h)warning: return type defaults to 'int' In function 'h_ship5'
- no description of type h_ship5. Better add. Well, etc. It is better to fix, there will be less problems. - alexlz pm