#include "sudoku.h"

typedef char CELL[9];
static CELL this_game[9][9];

//========================================================

FUNC char get_cell_valcode(int rowcol, int num, int idx, int val)
{
char *pcell;
int x, y;

if (rowcol == ROW)
    pcell = this_game[num][idx];
else if (rowcol == COL)
    pcell = this_game[idx][num];
else
    {
    get_box_coords(num, &x, &y);
    pcell = this_game[x+idx/3][y+idx%3];
    }
return pcell[val-1];
} 

FUNC set_cell_valcode(int rowcol, int num, int idx, int val, char valcode)
{
char *pcell;
int x, y;

if (rowcol == ROW)
    pcell = this_game[num][idx];
else if (rowcol == COL)
    pcell = this_game[idx][num];
else
    {
    get_box_coords(num, &x, &y);
    pcell = this_game[x+idx/3][y+idx%3];
    }
pcell[val-1] = valcode;
} 

FUNC bot_set_val_code(int x, int y, int val, char val_flag)
{
char *pcell;    

if (val < 1 || val > 9) printf("BAD CELL VALUE!!!\n");
pcell = this_game[x][y];
pcell[val-1] = val_flag;	 
}

FUNC bot_get_val_code(int x, int y, int val, char *p_code) 
{
char *pcell;

if (val < 1 || val > 9) printf("BAD CELL VALUE!!!\n");
pcell = this_game[x][y];
*p_code = pcell[val-1];
}

FUNC bot_get_cell_code(int x, int y, char *code)
{
int ii;
char *pcell;

pcell = this_game[x][y];
for (ii = 0; ii < 9; ii++)
    code[ii] = pcell[ii];
code[9] = '\0';
}

FUNC int bot_get_cell_val(int x, int y)	// 0 if no value ON
{
int ii;
char *pcell;

pcell = this_game[x][y];
for (ii = 0; ii < 9; ii++)
    if (pcell[ii] == VAL_YES)
         return (ii+1);
return 0;
}

FUNC bool other_vals_in_cell_are_off(int x,int y,int val)
{
int ii;  
char *pcell;

pcell = this_game[x][y];                 
for (ii = 0; ii < 9; ii++)
    if (ii != (val-1) && pcell[ii] != VAL_NO)
        return 0;
return 1;
}

FUNC bool others_in_row_are_off(int x,int y,int val)
{
int yy;
char *pcell;

// printf("doing others_in_row_are_off(%d, %d, %d)\n", x, y, val);
for (yy = 0; yy < 9; yy++)
    {
    pcell = this_game[x][yy];
    if (yy != y && pcell[val-1] != VAL_NO)
        return 0; 
    }
return 1;
}

FUNC bool others_in_col_are_off(int x, int y,int val)
{
int xx;
char *pcell;

// printf("doing others_in_col_are_off(%d, %d, %d)\n", x, y, val);
for (xx = 0; xx < 9; xx++)
    {
    pcell = this_game[xx][y];
    if (xx != x && pcell[val-1] != VAL_NO)
        return 0; 
    }
return 1;
}
 
//==============================================================

FUNC bot_set_box_off(int xlow,int ylow,int val)
{
int ii, jj;

for (ii = 0; ii < 3; ii++)
    for (jj = 0; jj < 3; jj++)
         bot_set_val_code(ii+xlow, jj+ylow, val, VAL_NO);
}

FUNC get_box_coords(int box, int *p_x, int *p_y)
{
*p_x = (box / 3) *3;
*p_y = (box % 3) *3;
}

