Hello. I want to create sudoku game. I am thinking which game library choose. Maybe Allegro? Anyone know good book/course about creating games in Allegro ? Thanks for answer !
I start from create matrix 9x9 and I am wondering how to fill this matrix. It is quite hard because in every new game this matrix should be filling new numbers but I can't just fill matrix random numbers because in each matrix 3x3 numbers have to be diffrent
The easiest thing to do is to break the 9x9 array into a 3x3 array of 3x3 arrays.
Here's something to get you started. Not terribly efficient, but generates unique 3x3 grids that add up to 15. Note: A nonet is a 3x3 grid.
#include <cstdlib>
class nonet
{ int m_cells[3][3] = {};
public:
bool is_legal ();
void populate ();
};
// Test if the cells in the 3x3 grid are a legal 3x3 array
bool nonet::is_legal ()
{ int used[10] = {}; // 0 not used
int row_sum;
int col_sum;
// Check that each cell is unique
for (int r=0; r<3; r++)
{ for (int c=0; c<3; c++)
{ // Mark the numbers used
if (used[m_cells[r][c]])
returnfalse;
used[m_cells[r][c]]++;
}
}
// Check if the all the rows add up to 15
for (int r=0; r<3; r++)
{ row_sum = 0;
for (int c=0; c<3; c++)
row_sum += m_cells[r][c];
if (row_sum != 15)
returnfalse;
}
// Check that all the columns add up to 15
for (int c=0; c<3; c++)
{ col_sum = 0;
for (int r=0; r<3; r++)
col_sum += m_cells[r][c];
if (col_sum != 15)
returnfalse;
}
// Passes all the tests
returntrue;
}
void nonet::populate ()
{ do // iterate until we have a legal nonet
{ for (int r=0; r<3; r++)
for (int c=0; c<3; c++)
m_cells[r][c] = rand() % 9 + 1;
} while (! is_legal());
}
The next step is to create a grid class that consts of a 3x3 array of nonets.
Use the same idea to make sure each nonet is unique within the 3x3 array of nonets.