Осваиваю С, "среда" разработки Sublime Text 2 + GCC. Для знакомства с основами решил написать программу, заполняющую квадрат определённого размера числами по порядку (в данном случае квадрат 8 на 8, а числа от 1 до 64) ходом коня.
Проблема в том, что время от времени возникает ситуация, когда очередной элемент записывается в матрицу либо по "прямой" либо ходом коня, увеличенным в несколько раз.
При проверке каждой функции отдельно, все работают правильно. Во всяком случае мне так кажется.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
const short int s=8;
void PrintResult(short int [s][s]);
int Nullify(short int [s][s]);
bool CheckStep(short int *, short int *, short int [s][s], short int *);
short int MakeStep(short int *, short int *, short int [s][s], short int *, short int *);
short int GetMaxElemVal(short int [s][s]);
short int GetMaxElemX(short int [s][s], short int *);
short int GetMaxElemY(short int [s][s], short int *);
short int GetNewDirection(void);
int main(void)
{
srand((unsigned)time(NULL)); // that needs for "true" random
short int field [s][s];
short int step; // current number to put [1..s*s]
short int xCoord=0; // start | coordinats
short int yCoord=0; // | position |
short int dir=0;
short int failCount=0; // on 8-count set fail "true"
while(step<s*s)
{
Nullify(field);
step=1;
xCoord=0+rand()%(s-1);
yCoord=0+rand()%(s-1);
field[xCoord][yCoord]=step;
failCount=0;
while(failCount<8 & step<s*s)
{
dir=GetNewDirection();
if(CheckStep(&xCoord,&yCoord,field,&dir)==true)
{
step++;
MakeStep(&xCoord,&yCoord,field,&dir,&step);
failCount=0;
xCoord=GetMaxElemX(field, &step);
yCoord=GetMaxElemY(field, &step);
}
else
failCount++;
if(failCount==8)
PrintResult(field);
}
printf("this time maximum element is %d. \n Press any key to start again. \n",step);
(void)getchar();
}
printf("\n\n the end. Press Any key to exit.");
(void)getchar();
exit(0);
}
Листинг используемых функций.
void PrintResult(short int array[s][s]) // print matrix to screen
{//ok
for(int i=0;i<s;i++)
{
for(int j=0;j<s;j++)
printf("%2d|",array[i][j]);
printf("\n\n");
}
}
int Nullify(short int array[s][s]) // fill matrix with zeroes
{//ok
short int nullCount=0;
for(int i=0;i<s;i++)
for(int j=0;j<s;j++)
{
array[i][j]=0;
nullCount++;
}
return nullCount;
}
bool CheckStep(short int *currentXcoord, short int *currentYcoord, short int array[s][s], short int *direction) // check selected position
{//ok?
bool res=false;
switch(*direction)
{
case 1: // up-up-right
{
if(*currentXcoord+1<s & *currentYcoord+2<s & array[*currentXcoord+1][*currentYcoord+2]==0)
res=true;
else
res=false;
} break;
case 2: // up-right-right
{
if(*currentXcoord+2<s & *currentYcoord+1<s & array[*currentXcoord+2][*currentYcoord+1]==0)
res=true;
else
res=false;
} break;
case 3: // down-right-right
{
if(*currentXcoord+2<s & *currentYcoord-1>0 & array[*currentXcoord+2][*currentYcoord-1]==0)
res=true;
else
res=false;
} break;
case 4: // down-down-right
{
if(*currentXcoord+1<s & *currentYcoord-2>0 & array[*currentXcoord+1][*currentYcoord-2]==0)
res=true;
else
res=false;
} break;
case 5: // down-down-left
{
if(*currentXcoord-1>0 & *currentYcoord-2>0 & array[*currentXcoord-1][*currentYcoord-2]==0)
res=true;
else
res=false;
}
case 6: // down-left-left
{
if(*currentXcoord-2>0 & *currentYcoord-1>0 & array[*currentXcoord-2][*currentYcoord-1]==0)
res=true;
else
res=false;
} break;
case 7: // up-left-left
{
if(*currentXcoord-2>0 & *currentYcoord+1<s & array[*currentXcoord-2][*currentYcoord+1]==0)
res=true;
else
res=false;
} break;
case 8: // up-up-left
{
if(*currentXcoord-1>0 & *currentYcoord+2<s & array[*currentXcoord-1][*currentYcoord+2]==0)
res=true;
else
res=false;
} break;
default:res=false;
}
return res;
}
short int MakeStep(short int *currentXcoord, short int *currentYcoord, short int array[s][s], short int *direction, short int *value) // next step and inc
{//ok
switch(*direction)
{
// up-up-right
case 1: array[*currentXcoord+1][*currentYcoord+2]=*value; break;
// up-right-right
case 2: array[*currentXcoord+2][*currentYcoord+1]=*value; break;
// down-right-right
case 3: array[*currentXcoord+2][*currentYcoord-1]=*value; break;
// down-down-right
case 4: array[*currentXcoord+1][*currentYcoord-2]=*value; break;
// down-down-left
case 5: array[*currentXcoord-1][*currentYcoord-2]=*value; break;
// down-left-left
case 6: array[*currentXcoord-2][*currentYcoord-1]=*value; break;
// up-left-left
case 7: array[*currentXcoord-2][*currentYcoord+1]=*value; break;
// up-up-left
case 8: array[*currentXcoord-1][*currentYcoord+2]=*value; break;
default:;
}
return *value;
}
short int GetMaxElemX(short int array[s][s], short int *maxelem)
{//ok
short int x=-1;
for(int i=0;i<s;i++)
for(int j=0;j<s;j++)
if(*maxelem==array[i][j])
x=j;
return x;
}
short int GetMaxElemY(short int array[s][s], short int *maxelem)
{//ok
short int y=-1;
for(int i=0;i<s;i++)
for(int j=0;j<s;j++)
if(*maxelem==array[i][j])
y=i;
return y;
}
short int GetNewDirection(void)
{//ok
return 1+rand()%8; // for now
}