1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
// constants
#define DIM_MIN 3
#define DIM_MAX 9
int board[DIM_MAX][DIM_MAX];
int main(int argc, string argv[])
{
// ensure proper usage
if (argc != 2)
{
printf("Usage: fifteen d\n");
return 1;
}
// ensure valid dimensions
d = atoi(argv[1]);
if (d < DIM_MIN || d > DIM_MAX)
{
printf("Board must be between %i x %i and %i x %i, inclusive.\n",
DIM_MIN, DIM_MIN, DIM_MAX, DIM_MAX);
return 2;
}
// greet user with instructions
greet();
// initialize the board
init();
// accept moves until game is won
while (true)
{
// clear the screen
clear();
// draw the current state of the board
draw();
// log the current state of the board (for testing)
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
fprintf(file, "%i", board[i][j]);
if (j < d - 1)
{
fprintf(file, "|");
}
}
fprintf(file, "\n");
}
fflush(file);
}
}
void init(void)
{
nombre = d*d-1;
for(int ligne = 0; ligne<d; ligne++){
for (int colonne= 0;colonne<d;colonne++){
board[ligne][colonne]=nombre;
nombre = nombre-1;
}
}
if(d%2==0){
printf("on a un nombre paire");
board[d-1][d-3]=1;
board[d-1][d-2]=2;
}
//i will always remain the same
int i = d * d - 1;
srand(time(NULL));
while (i > 0) {
int j = (rand() * i)%((d*d)-1);
int xi = i % d;
int yi = i / d;
int xj = j % d;
int yj = j / d;
if(xj<0){
printf("\nproblème, xj = %i\n",xj);
printf("i = %i\n",i);
break;
}
if(yj<0){
printf("\nproblème, yj = %i\n",yj);
printf("j = %i\n",j);
break;
}
swapTiles(xi, yi, xj, yj);
--i;
}
} |
Partager