Why it doesn't work? I try to make sudoku but I have many of lack
I must make a program sudoku that would read the elements from a text file


Code:
#include <stdio.h>
#include <stdlib.h>

#define SLP_INTERVAL 1

// task defined functions
int validNumber(int a);
int emptyCell(int array[9][9], int i, int j);
int numberInRow(int array[9][9], int i, int val);
int numberInCol(int array[9][9], int j, int val);
int numberIn3x3(int array[9][9], int i, int j, int val);
int completed(int array[9][9]);

// wrap functions to ease things up...
void echoMatrix(int array[9][9]);
int inTableBounds(int i, int j);
int freeCellSum(int array[9][9]);

char errMsg[] = "";

int main(int argc, char **argv) {
	int cX, cY, val;
	int isInterrupted = 0;
	int y,x;
	  char buf[19];
  FILE *fp = fopen("sudoku.txt", "r");

	do {
		do {

			#ifdef INTERACTIVE
				system("clear");
			#endif

			echoMatrix(sudoku);

			printf("Please give me your movement (%d cells): ", freeCellSum(sudoku));
			fscanf(stdin, "%d,%d=%d", &cX, &cY, &val);

			if(cX == -1 && cY == -1 && val == -1) {
				isInterrupted = 1;
				goto finished;
			}

			// notify user on error
			if(!inTableBounds(cX, cY)) {
				printf("(!) Out of table bounds box number set!\n");
				continue;
			}

			if(!validNumber(val)) {
				printf("(!) Erroneous value for sudoku cell!\n");
				continue;
			}

			if(emptyCell(sudoku, cX - 1, cY - 1)) {
				printf("(!) Cell not empty!\n");
				continue;
			}

			if(numberInRow(sudoku, cX - 1, val) || numberInCol(sudoku, cY - 1, val) || numberIn3x3(sudoku, cX - 1, cY - 1, val)) {
				printf("(!) Movement brakes game rules (%s)!\n", errMsg);
			}

		} while((emptyCell(sudoku, cX - 1, cY - 1) || !validNumber(val) || emptyCell(sudoku, cX - 1, cY - 1) || numberInRow(sudoku, cX - 1, val) || numberInCol(sudoku, cY - 1, val) || numberIn3x3(sudoku, cX - 1, cY - 1, val)));

		sudoku[cX - 1][cY - 1] = val;

		printf("Value accepted! Waiting for your next movement.\n");

		#ifdef INTERACTIVE
			sleep(SLP_INTERVAL);
		#endif

	} while(!completed(sudoku));

	finished:

	if(!isInterrupted)
		printf("Congratulations! You\'ve come to the end of this game!\n");
	else
		printf("Game exiting... Good bye!\n");

	exit(EXIT_SUCCESS);
}

//{{
//checks if a (x,y) coordinate set is within the sudoku matrix bounds
//}}
int inTableBounds(int i, int j) {
	return((i >= 1 && i <= 9 && j >= 1 && j <= 9 && (i != -1 && j != -1)) ? 1 : 0);
}

// {{
// returns number of available free cells
// }}
int freeCellSum(int array[9][9]) {
	int p = 0, i, j;

	for(i = 0; i <= 8; i++) {
		for(j = 0; j <= 8; j++) {
			if(array[i][j] == 0)
				p++;
		}
	}

	return(p);
}

//{{
//prints current sudoku matrix on stdout
//}}
void echoMatrix(int array[9][9]) {
	int i, j;

	for(i = 0; i <= 8; i++) {
		for(j = 0; j <= 8; j++) {
			printf("%i ", array[i][j]);
		}

		printf("\n");
	}
}

//{{
//checks whether a is between 1 and 9 (sudoku matrix bounds)
//}}
int validNumber(int a) {
	return((a >= 1 && a <= 9) ? 1 : 0);
}

//{{
// checks if cell already has a non-zero value
//}}
int emptyCell(int array[9][9], int i, int j) {
	return((array[i][j] != 0) ? 1 : 0);
}

// {{
// checks if the game matrix contains val in row i
// }}
int numberInRow(int array[9][9], int i, int val) {
	int k = 0, p = 0;

	for(; k <= 8; k++) {
		if(array[i][k] == val) {
			p++;
			sprintf(errMsg, "Equal value at (%d,%d) in row check", i + 1, k + 1);
			break;
		}
	}

	return((p >= 1) ? 1 : 0);
}

// {{
// checks if the sudoku matrix already contains value val in col j
// }}
int numberInCol(int array[9][9], int j, int val) {
	int k = 0, p = 0;

	for(; k <= 8; k++) {
		if(array[k][j] == val) {
			p++;
			sprintf(errMsg, "Equal value at (%d,%d) in col check", k + 1, j + 1);
			break;
		}
	}

	return((p >= 1) ? 1 : 0);
}

// {{
// checks whether value val corresponding to (i,j) exists in 3x3 tbl
// }}
int numberIn3x3(int array[9][9], int i, int j, int val) {
	int sCoordX, sCoordY, coordX, coordY;
	int p = 0;

	if(i >= 0 && i <= 2) sCoordX = 0;
	if(i >= 3 && i <= 5) sCoordX = 3;
	if(i >= 6 && i <= 8) sCoordX = 6;
	if(j >= 0 && j <= 2) sCoordY = 0;
	if(j >= 3 && j <= 5) sCoordY = 3;
	if(j >= 6 && j <= 8) sCoordY = 6;

	for(coordX = sCoordX; coordX <= (sCoordX + 2); coordX++) {
		for(coordY = sCoordY; coordY <= (sCoordY + 2); coordY++) {
			// avoid marking the examined value as existing two times
			// in our logic
			if((array[coordX][coordY] == val) && coordX != i && coordY != j) {
				p++;
				sprintf(errMsg, "Value allready exists in 3x3 at (%d,%d)", coordX + 1, coordY + 1);
				break;
			}
		}
	}

	return((p >= 1) ? 1 : 0);
}

// {{
// checks whether the game's finished
// }}
int completed(int array[9][9]) {
	int i, j, zp = 0;

	for(i = 0; i < 9; i++) {
		for(j = 0; j < 9; j++) {
			if(array[i][j] == 0)
				zp++;
		}
	}

	return((zp > 0) ? 0 : 1);
}

Last edited by shabbir; 28Oct2007 at 23:43.. Reason: Code block