Compare a bidimensional array to 0 (c++) -
i made loop do.while, trying make change turns of game. now, want stop when of both array comes 0, , show winner. battleship game.
do{ for(int k;toc=1;k++){ objec1(); if(toc!=1) break; } for(int k;toc2=1;k++){ objec2(); if(toc2!=1){ break; } } if(jug1[i][j]==0){ //here loop must stop correct1=true; cout<<"\n\tel player 2 winner\n"; } if(jug2[i][j]==0){ correct1=true; cout<<"\n\tplayer 1 winner \n"; } } while(!correct1);
in way have code, not stop loop when must. now, want check if when array full of 0, stop loop, change flag , show winner. original code more extense. consider correctly initialized.
firstly, think you'll have easier time problem if fix formatting, break things functions, , use better variable names. pretty hard read , understand.
secondly, operation talking pretty expensive. if can avoid doing check way, that'd better. example, maintain count of non-zero entries, , instead use check condition. or, if have information battleships in play, check when none of them left.
anyway, here function find out "when [two-dimensional] array full of 0". can call function.
/** * array 2-d array used battleship game * outer array size * j inner array size */ bool isallzeros(int **array, int i, int j) { (int m = 0; m < i; m++) { (int n = 0; n < j; n++) { if (0 != array[i][j]) { return false; } } } return true; }
Comments
Post a Comment