#include <stdio.h>
void getstart(int board[][8], int *row, int *col, int movelist[][2], int *movelist_index);
void makemove(int *row, int *col, int board[][8], int movelist[][2], int *movelist_index);
int nummoves(int row, int col, int board[][8]);
void main()
{
int board[8][8]={0};
int movelist[64][2]={0};
int movelist_index=0;
int row;
int col;
int loop_count;
getstart(board,&row,&col,movelist,&movelist_index);
printf("\nStarting square= %c%d\n",movelist[0][0],movelist[0][1]);
for(loop_count=1;loop_count<63;loop_count++) {
makemove(&row,&col,board,movelist,&movelist_index);
// printf("%c%d\n",movelist[loop_count][0],movelist[loop_count][1]);
printf("%d\t%c%d\n",movelist_index,(movelist[loop_count][0])+97,(movelist[loop_count][1])+1);
}
}
void getstart(int board[][8], int *row, int *col, int movelist[][2], int *movelist_index)
{
printf("Please select the starting move by following the instructions!\n\n");
printf(" 7 a8 b8 c8 d8 e8 f8 g8 h8\n");
printf(" 6 a7 b7 c7 d7 e7 f7 g7 h1\n");
printf("R 5 a6 b6 c6 d6 e6 f6 g6 h1\n");
printf("O 4 a5 b5 c5 d5 e5 f5 g5 h1\n");
printf("W 3 a4 b4 c4 d4 e4 f4 g4 h1\n");
printf("S 2 a3 b3 c3 d3 e3 f3 g3 h1\n");
printf(" 1 a2 b2 c2 d2 e2 f2 g2 h1\n");
printf(" 0 a1 b1 c1 d1 e1 f1 g1 h1\n\n");
printf(" 0 1 2 3 4 5 6 7\n\n");
printf("\t\tCOLUMNS\n");
printf("INSTRUCTIONS:\n");
printf("- look at the square you want to start at\n");
printf("- note the COLUMN and the ROW where it is located\n");
printf("- enter the COLUMN, then the ROW as promted\n\n");
do {
printf("Please enter the COLUMN: ");
scanf("%d",col);
if (*col<0||*col>7) {
printf("COLUMN is out of range, it must be between 0 and 7 inclusively\n");
}
} while (*col<0||*col>7);
do {
printf("Please enter the ROW: ");
scanf("%d",row);
if (*row<0||*row>7) {
printf("ROW is out of range, it must be between 0 and 7 inclusively\n");
}
} while (*row<0||*row>7);
board[*row][*col]=1;
movelist[*movelist_index][0]=((*col)+97);
movelist[*movelist_index][1]=((*row)+1);
(*movelist_index)++;
}
void makemove(int *row, int *col, int board[][8], int movelist[][2], int *movelist_index)
{
int move[8]={0};
int counter;
int temp;
int num_moves=10;
// printf("%d\n",*movelist_index);
if (((*row)-2>=0)&&((*col)-1>=0)&&(board[(*row)-2][(*col)-1]!=1)) {
// printf("move[0] is getting called\n");
move[0]=nummoves((*row)-2,(*col)-1,board);
}
if (((*row)-2>=0)&&((*col)+1<=7)&&(board[(*row)-2][(*col)+1]!=1)) {
move[1]=nummoves((*row)-2,(*col)+1,board);
// printf("move[1] is getting called\n");
}
if (((*row)+2<=7)&&((*col)-1>=0)&&(board[(*row)+2][(*col)-1]!=1)) {
move[2]=nummoves((*row)+2,(*col)-1,board);
// printf("move[2] is getting called\n");
}
if (((*row)+2<=7)&&((*col)+1<=7)&&(board[(*row)+2][(*col)+1]!=1)) {
move[3]=nummoves((*row)+2,(*col)+1,board);
// printf("move[3] is getting called\n");
}
if (((*row)-1>=0)&&((*col)-2>=0)&&(board[(*row)-1][(*col)-2]!=1)) {
move[4]=nummoves((*row)-1,(*col)-2,board);
// printf("move[4] is getting called\n");
}
if (((*row)-1>=0)&&((*col)+2<=7)&&(board[(*row)-1][(*col)+2]!=1)) {
move[5]=nummoves((*row)-1,(*col)+2,board);
// printf("move[5] is getting called\n");
}
if (((*row)+1<=7)&&((*col)-2>=7)&&(board[(*row)+1][(*col)-2]!=1)) {
move[6]=nummoves((*row)+1,(*col)-2,board);
// printf("move[6] is getting called\n");
}
if (((*row)+1<=7)&&((*col)+2<=7)&&(board[(*row)+1][(*col)+2]!=1)) {
move[7]=nummoves((*row)+1,(*col)+2,board);
// printf("move[7] is getting called\n");
}
for(counter=0;counter<=7;counter++) {
if (move[counter]<num_moves&&move[counter]>0) {
temp=counter;
num_moves=move[counter];
}
}
printf("temp=%d\n",temp);
switch (temp) {
case 0:
(*row)-=2;
(*col)-=1;
break;
case 1:
(*row)-=2;
(*col)+=1;
break;
case 2:
(*row)+=2;
(*col)-=1;
break;
case 3:
(*row)+=2;
(*col)+=1;
break;
case 4:
(*row)-=1;
(*col)-=2;
break;
case 5:
(*row)-=1;
(*col)+=2;
break;
case 6:
(*row)+=1;
(*col)-=2;
break;
case 7:
(*row)+=1;
(*col)+=2;
break;
}
board[*row][*col]=1;
movelist[*movelist_index][0]=(*col);
movelist[*movelist_index][1]=(*row);
(*movelist_index)++;
}
int nummoves(int row, int col, int board[][8])
{
int count=0;
if (((row)-2>=0)&&((col)-1>=0)&&(board[row-2][col-1]!=1))
count++;
if (((row)-2>=0)&&((col)+1<=7)&&(board[row-2][col+1]!=1))
count++;
if (((row)+2<=7)&&((col)-1>=0)&&(board[row+2][col-1]!=1))
count++;
if (((row)+2<=7)&&((col)+1<=7)&&(board[row+2][col+1]!=1))
count++;
if (((row)-1>=0)&&((col)-2>=0)&&(board[row-1][col-2]!=1))
count++;
if (((row)-1>=0)&&((col)+2<=7)&&(board[row-1][col+2]!=1))
count++;
if (((row)+1<=7)&&((col)-2>=0)&&(board[row+1][col-2]!=1))
count++;
if (((row)+1<=7)&&((col)+2<=7)&&(board[row+1][col+2]!=1))
count++;
// printf("count= %d\n",count);
return (count);
}