Same number in box
Jan 6, 2016 at 7:37pm UTC
Hi, I am making a sudoku game and I have tried to make a function so the same number dosent go into the same box(3x3) But I have a problem, it dosent work and I dont know why. Here is my code (I am drawing it in another function) I am checking in every box one box is 3x3:
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 102 103 104 105 106 107 108 109 110
const static int BOARD_SIZE = 9;
int board[BOARD_SIZE][BOARD_SIZE];
bool isValid() {
std::cout << "Start\n" ;
int i = 0;
int j = 0;
while (i < 82){
i += 3;
std::cout << i << "\n" ;
for (int j = 0; j < 82; j += 3){
std::cout << j << " " ;
if (board[0 + i][0 + j] != 0){
if (board[0 + j][0 + j] == board[0+i][1+j] || board[0+i][0+j] == board[0+i][2+j] ||
board[0 + j][0 + j] == board[1+i][0+j] || board[0+i][0+j] == board[1+i][1+j] || board[0+i][0+j] == board[1+i][2+j] ||
board[0 + j][0 + j] == board[2+i][0+j] || board[0+i][0+j] == board[2+i][1+j] || board[0+i][0+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[0+i][1+j] != 0){
if (board[0+i][1+j] == board[0+i][0+j] || board[0+i][1+j] == board[0+i][2+j] ||
board[0+i][1+j] == board[1+i][0+j] || board[0+i][1+j] == board[1+i][1+j] || board[0+i][1+j] == board[1+i][2+j] ||
board[0+i][1+j] == board[2+i][0+j] || board[0+i][1+j] == board[2+i][1+j] || board[0+i][1+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[0+i][2+j] != 0){
if (board[0+i][2+j] == board[0+i][1+j] || board[0+i][2+j] == board[0+i][0+j] ||
board[0+i][2+j] == board[1+i][0+j] || board[0+i][2+j] == board[1+i][1+j] || board[0+i][2+j] == board[1+i][2+j] ||
board[0+i][2+j] == board[2+i][0+j] || board[0+i][2+j] == board[2+i][1+j] || board[0+i][2+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[1+i][0+j] != 0){
if (board[1+i][0+j] == board[0+i][1+j] || board[1+i][0+j] == board[0+i][2+j] ||
board[1+i][0+j] == board[0+i][0+j] || board[1+i][0+j] == board[1+i][1+j] || board[1+i][0+j] == board[1+i][2+j] ||
board[1+i][0+j] == board[2+i][0+j] || board[1+i][0+j] == board[2+i][1+j] || board[1+i][0+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[1+i][1+j] != 0){
if (board[1+i][1+j] == board[0+i][1+j] || board[1+i][1+j] == board[0+i][2+j] ||
board[1+i][1+j] == board[1+i][0+j] || board[1+i][1+j] == board[0+i][0+j] || board[1+i][1+j] == board[1+i][2+j] ||
board[1+i][1+j] == board[2+i][0+j] || board[1+i][1+j] == board[2+i][1+j] || board[1+i][1+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[1+i][2+j] != 0){
if (board[1+i][2+j] == board[0+i][1+j] || board[1+i][2+j] == board[0+i][2+j] ||
board[1+i][2+j] == board[1+i][0+j] || board[1+i][2+j] == board[1+i][1+j] || board[1+i][2+j] == board[0+i][0+j] ||
board[1+i][2+j] == board[2+i][0+j] || board[1+i][2+j] == board[2+i][1+j] || board[1+i][2+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[2+i][0+j] != 0){
if (board[2+i][0+j] == board[0+i][1+j] || board[2+i][0+j] == board[0+i][2+j] ||
board[2+i][0+j] == board[1+i][0+j] || board[2+i][0+j] == board[1+i][1+j] || board[2+i][0+j] == board[1+i][2+j] ||
board[2+i][0+j] == board[0+i][0+j] || board[2+i][0+j] == board[2+i][1+j] || board[2+i][0+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[2+i][1+j] != 0){
if (board[2+i][1+j] == board[0+i][1+j] || board[2+i][1+j] == board[0+i][2+j] ||
board[2+i][1+j] == board[1+i][0+j] || board[2+i][1+j] == board[1+i][1+j] || board[2+i][1+j] == board[1+i][2+j] ||
board[2+i][1+j] == board[2+i][0+j] || board[2+i][1+j] == board[0+i][0+j] || board[2+i][1+j] == board[2+i][2+j])
return true ;
break ;
}
if (board[2+i][2+j] != 0){
if (board[2+i][2+j] == board[0+i][1+j] || board[2+i][2+j] == board[0+i][2+j] ||
board[2+i][2+j] == board[1+i][0+j] || board[2+i][2+j] == board[1+i][1+j] || board[2+i][2+j] == board[1+i][2+j] ||
board[2+i][2+j] == board[2+i][0+j] || board[2+i][2+j] == board[2+i][1+j] || board[2+i][2+j] == board[0+i][0+j])
return true ;
break ;
}
}
}
return false ;
}
void boardNumbers(){ //Add 22-32 numbers
srand(time(NULL));
int maxNumber = rand() % 10 + 22;
for (int i = 0; i <= maxNumber; i++){
int randX = rand() % 9;
int randY = rand() % 9;
int randNumber = rand() % 9 + 1;
board[randX][randY] = randNumber;
//check if same number on 2 lines
while (checkLine()){
int randNumber = rand() % 9 + 1;
board[randX][randY] = randNumber;
}
while (isValid()){
int randNumber = rand() % 9 + 1;
board[randX][randY] = randNumber;
}
}
}
Last edited on Jan 6, 2016 at 8:01pm UTC
Topic archived. No new replies allowed.