I am solving interesting yet easy program.
the program works almost fine but it has strange bug that occur and im lost cuz i have no idea why it happens
I have mine field that spread mines , o is for empty space , " . " is for space where cannot be placed mine and ! is where "miner is" , he spread bombs in this direction
o o o o
o . ! o
o o o o
he would spred mines around him if is possible so in this case it would spread like this
o o ! o
o . x !
o o ! o i put the x on the original place of miner , the spreading continue till every place where mine can reach is mined. But i do not know when and why somewhere it code it just breaks and the "miner" does not spread mine in the direction he should.
First in my code i find the position of miner/s i store their x and y position in
indexX[];
indexY[];
this arrays have same length of items , so i have variable index which incerement every time something should be added.
then i loop throught arrays and if i find match ( ! ) i check if surrounding can be mined if yes add it there , everything works but somehow sometimes it just broke and i do not know why Here is paste bin [C] #include <stdio.h> #include <stdlib.h> int main() { int days=0; i - Pastebin.com as you can see in 3rd and 5th mine field , it spreaded not how ist supposed to be , how can i fix it?
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int days=0;
int i;
int j;
int indexX[15];
int indexY[15];
int index=0;
int x=4;
int y=4;
int pocet_nakazenych=0;
char nakaza[4][4]={{'o','o','o','o'},{'o','.','o','o'},{'o','.','.','o'},{'!','o','.','o'}};
int tmp=0;
int changes=0;
int ass=0;
for(i=0;i<x;i++){
for(j=0;j<y;j++){
if(nakaza[i][j]=='!'){
nakaza[i][j]='x';
indexX[index]=i;
indexY[index]=j;
index++;
pocet_nakazenych++;
}
}
}
tmp=index;
do{
for(i=0;i<x;i++){
for(j=0;j<y;j++){
if(nakaza[i][j]=='!'){
nakaza[i][j]='x';
}
}
}
changes=0;
for(i=0;i<index;i++){
if(!(indexX[i]+1>x-1)){
if( nakaza[indexX[i]+1][indexY[i]]!='.' && nakaza[indexX[i]+1][indexY[i]]!='x' && nakaza[indexX[i]+1][indexY[i]]!='!'){
nakaza[indexX[i]+1][indexY[i]]='!';
pocet_nakazenych++;
indexX[tmp]=indexX[i]+1;
indexY[tmp]=indexY[i];
printf("Nakaza %d:%d xhore\n",indexX[tmp],indexY[tmp]);
printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
tmp++;
changes++;
}
}
if(!(indexX[i]-1<0)){
if( nakaza[indexX[i]-1][indexY[i]]!='.' && nakaza[indexX[i]-1][indexY[i]]!='x' && nakaza[indexX[i]-1][indexY[i]]!='!'){
nakaza[indexX[i]-1][indexY[i]]='!';
pocet_nakazenych++;
indexX[tmp]=indexX[i]-1;
indexY[tmp]=indexY[i];
printf("Nakaza %d:%d xdole\n",indexX[tmp],indexY[tmp]);
printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
tmp++;
changes++;
}
}
if(!(indexY[i]+1>y-1)){
if( nakaza[indexX[i]][indexY[i]+1]!='.' && nakaza[indexX[i]][indexY[i]+1]!='x' && nakaza[indexX[i]][indexY[i]+1]!='!'){
nakaza[indexX[i]][indexY[i]+1]='!';
pocet_nakazenych++;
indexX[tmp]=indexX[i];
indexY[tmp]=indexY[i]+1;
printf("Nakaza %d:%d ydoprava\n",indexX[tmp],indexY[tmp]);
printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
tmp++;
changes++;
}
}
if(!(indexY[i]-1<0)){
if( nakaza[indexX[i]][indexY[i]-1]!='.' && nakaza[indexX[i]][indexY[i]-1]!='x' && nakaza[indexX[i]][indexY[i]-1]!='!'){
nakaza[indexX[i]][indexY[i]-1]='!';
pocet_nakazenych++;
indexX[tmp]=indexX[i];
indexY[tmp]=indexY[i]-1;
printf("Nakaza %d:%d ydolava\n",indexX[tmp],indexY[tmp]);
printf("plati pre indexX=%d indexY = %d\n",indexX[i],indexY[i]);
tmp++;
changes++;
}
}
}
index=tmp;
tmp=0;
if (changes!=0){
days++;
}else{
break;
}
for(i=0;i<x;i++){
for(j=0;j<y;j++){
printf("%c ",nakaza[i][j]);
}
printf("\n");
}
printf("days = %d\n",days);
}
while(changes);
for(i=0;i<x;i++){
for(j=0;j<y;j++){
printf("%c ",nakaza[i][j]);
}
printf("\n");
}
printf("dni = %d\npocet nakazenych = %d",days,pocet_nakazenych);
}