Questo è un semplice algoritmo che sto testando e cercando di migliorare pian piano, però tra gli errori durante la compilazione ne è uscito uno che non capisco, cioè [Error] too few arguments to function 'chooser' e [Error] expected expression before 'Curr' che penso siano correlati e si riferiscono alla riga 101.
Ecco il codice:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct curr {
int x;
int y;
}pos;
struct choice{
int x;
int y;
int z;
}best;
typedef struct curr Curr;
int mossa_cavallo (int board [8] [8]);
int set_board(int board [8] [8]); // numeri di accessibilità
int chooser (Curr pos, int board [8] [8]);
int x = 0;
int y = 0;
int main (void){
int board [8] [8];
set_board (board [8] [8]);
mossa_cavallo (board [8] [8]);
}
int chooser (Curr pos, int board [8] [8]){
best.z = board [pos.x+2] [pos.y+1];
best.y = pos.y+1;
best.x = pos.x+2;
if(board [pos.x+2] [pos.y-1] < best.z && board [pos.x+2] [pos.y-1] != -1){
best.z = board [pos.x+2] [pos.y-1];
best.y = pos.y-1;
best.x = pos.x+2;
}
if(board [pos.x+1] [pos.y-2] < best.z && board [pos.x+1] [pos.y-2] != -1){
best.z = board [pos.x+1] [pos.y-2];
best.x = pos.x+1;
best.y = pos.y-2;
}
if(board [pos.x+1] [pos.y+2] < best.z && board [pos.x+1] [pos.y+2] != -1){
best.z = board [pos.x+1] [pos.y+2];
best.x = pos.x+1;
best.y = pos.y+2;
}
if(board [pos.x-1] [pos.y-2] < best.z && board [pos.x-1] [pos.y-2] != -1){
best.z = board [pos.x-1] [pos.y-2];
best.x = pos.x-1;
best.y = pos.y-2;
}
if(board [pos.x-2] [pos.y-1] < best.z && board [pos.x-2] [pos.y-1] != -1){
best.z = board [pos.x+1] [pos.y-1];
best.x = pos.x-2;
best.y = pos.y-1;
}
if(board [pos.x-2] [pos.y+1] < best.z && board [pos.x-2] [pos.y+1] != -1){
best.z = board [pos.x-2] [pos.y+1];
best.x = pos.x-2;
best.y = pos.y+1;
}
if(board [pos.x-1] [pos.y+2] < best.z && board [pos.x-1] [pos.y+2] != -1){
best.z = board [pos.x-1] [pos.y+2];
best.x = pos.x-1;
best.y = pos.y+2;
}
x = best.x;
y = best.y;
return 0;
}
int mossa_cavallo (int board [8] [8]){ //funzione addetta a far muovere il cavallo
int i=0;
Curr pos;
srand(time(NULL)); //Inseminazione. Da chiamare solo una volta
//Generazione casuale della posizione 1 e 2
pos.x=rand () % 8;
pos.y=rand () % 8;
while (i <= 64){ //ciclo del programma
i++;
printf("%d, %d\n", pos.x, pos.y);
board [pos.x] [pos.y] = -1;
int bo = chooser (Curr pos, board [8] [8]); //chiamata alla funzione chooser
pos.x = x;
pos.y = y;
}
if (i==64){
printf("Caselle completate! Giro del cavallo risolto!\n");
}
else
printf("Non posso accedere a nessuna casella! Ciclo terminato!\n");
printf("Ho completato %d passaggi\n",i);
return board [8] [8];
}
int set_board (int board [8] [8]){
//2
board [0] [0] = 2;
board [0] [7] = 2;
board [7] [0] = 2;
board [7] [7] = 2;
//3
board [1] [0]= 3;
board [0] [1]= 3;
board [6] [0]= 3;
board [1] [7]= 3;
board [7] [6]= 3;
board [6] [7]= 3;
board [7] [1]= 3;
board [0] [6]= 3;
//4
for(int i = 2; i <= 5; i++){
board [i] [0] = 4;
}
for(int i = 2; i <= 5; i++){
board [i] [7] = 4;
}
for(int i = 2; i <= 5; i++){
board [0] [i] = 4;
}
for(int i = 2; i <= 5; i++){
board [7] [i] = 4;
}
board [6] [1] = 4;
board [6] [6] = 4;
board [1] [1] = 4;
board [1] [6] = 4;
//6
for(int i = 2; i <= 5; i++){
board [i] [1] = 4;
}
for(int i = 2; i <= 5; i++){
board [i] [6] = 4;
}
for(int i = 2; i <= 5; i++){
board [1] [i] = 4;
}
for(int i = 2; i <= 5; i++){
board [6] [i] = 4;
}
//8
for(int j = 2; j <= 5; j++ ){
for(int i = 2; i <= 5; i++){
board [i] [j] = 4;
}
}
return board [8] [8];
}