Hi guys!
I created the sudoku generator, but i don't know a way how to hide
the sudoku numbers. Is there a way to hide them?
I also wanted to swap a char x with an int sudoku[random][random],
but that seems impossible to be done and i know about typecasting.
Pls help me!!!
So what you're asking is the following: you've got some array int[x][y] sized 9 x 9, correct? and you want to print it to some textfile or just stdout, while hiding some of your solving numbers...correct?
I would - might not be the kings way - copy the whole thingy:
now work with your copy, so you wont waste the result.
Next step - clear some results, e.g. set them zero.
1 2 3 4 5 6
srand( someInitializer e.g. time_t );
for( i = 0; i < HOW_MANY_TO_HIDE; i++ ){
int x = rand() % 9;
int y = rand() % 9;
sudokuTemp[x][y] = 0;
}; //you can improve this one since some values now might be hidden twice...
finally print your stuff - probably case sensitive:
1 2 3 4 5 6 7
for(x = 0; x<9; x++ ){
for(y = 0; y<9; y++){
if( sudokuTemp[x][y] ) printf( "%i ", sudokuTemp[x][y] ); //print your number if there is still one
else printf( " " ); // is it 0 print a blank
}
printf( "\n" );
};
Thank you for your reply!
U are right. I actually made a 9x9 sudoku random generator with about 200 row codes, but
can you explain me a bit more of what you did, cuz i have made a
simplier sudoku, writting directly the numbers in the array[9][9].
In fact i want to know more about the 2nd and 3rd coding blocks u wrote.
I don't understand how it becomes blank space.
Well what it does is basically this: it rolls a dice ( rand() ) for a number 0...8! which is the valid index of a 9x9 array. The "%" is the modulo operator...meaning the rest of a division like 11%9 = 2 since 11/9 = 1 with some in-dividable rest (2) .
This hiding, setting values in your array to zero, which is not a valid sudoku value, is done HOW_MANY_TO_HIDE times. Well if you get the same random number twice you'll hide the same number twice, therefore probably less than HOW_MANY_TO_HIDE overall.
Now the last step. You want to print the whole thing to some interface. Use printf to stdout (terminal) or fprintf to some file. Now you cycle (again) through the whole array. If the value is a valid sudoku one (>0) it'll print like so, but if its 0 (therefore a hidden number) it'll decide to printf( " ") blanks and you can't see whats there!
BTW: if your sudoku is named sudoku and you would rplace HOW_MANY_TO_HIDE with an actual number and the stuff inside srand() for example with 1, it should compile if you just add both statements to your file...maybe it helps to actually see what it does
So after all in cmd (as i don't really think to put it in an interface) the "blank spaces" are 0 and not blank spaces?
Also, thank you very much for helping me out!
Can you help me with smth else?
I want to swap coloumns but i cannot do it, cuz i can change only rows :(
I mean i can swap only the numbers inside the {1,2,3,4,5,6,7,8,9} with {9,8,7,6,5,4,3,2,1}.
I want to swap them not only horizontally (rows), but vertically too (coloumns).
Is there a way i can do this? It seems impossible and the sudoku is very simple to complete.
Also i don't really know how to program an interface so i will do it after some other tuts :|
Your code also worked perfectly!
Well "interface" might be the wrong word....just use your basic i/o streams...thats what printf (a c function) or cout ( c++) will do...the just send some string to your terminal and display it.
Next thing: I don't understand your question as is...for my understanding you've got an array int sudoku[9][9], right? a row swap would be like:
1 2 3 4 5
// BUFFER one line
int lineBuffer[9];
for( int i = 0; i < 9; i++ ) lineBuffer[i] = sudoku[LINE_TO_BE_SWAPPED_A][i];
for( int i = 0; i < 9; i++ ) sudoku[LINE_TO_BE_SWAPPED_A][i] = sudoku[LINE_TO_BE_SWAPPED_B][i];
for( int i = 0; i < 9; i++ ) sudoku[LINE_TO_BE_SWAPPED_B][i] = lineBuffer[i];
If you want to swap rows its pretty much the same, just alter the indexes in the "matrix"/array
NVM i found the answer in another forum :)
Anyway i always get a weird problem from your first code:
srand( someInitializer e.g. time_t );
for( i = 0; i < 9; i++ ){
int x = rand() % 9;
int y = rand() % 9;
sudokuTemp[x][y] = choice; // I replaced 0 with choice
cin >> choice;
}
Now the problem is that it always show me a 0 whatever sudoku is run.
There is a 0 that the user cannot put the input in.
Do you know which is the problem here?
sudokuTemp[x][y] = choice; // I replaced 0 with choice
cin >> choice;
now you're first putting choice into your grid and then ask the user what to do, the logic is the other way round! BTW: I don't think your code will do what you intended, finally...
Lol looks like u are the only genious here :)
I am really greatful for your help!
Why in the world the code wont do what is intended??? O.o
EDIT: Nvm i know what u want to tell me!
Anyway i put 350 first of all, instead of putting 81 to erase all the
sudoku board. No prob man, just a simple sudoku where u can play in
and it generates
The only thing i dont understand now is how to generate the sudoku
first with the 0 and than use the cin to fill in the 0.
This is the real thing i intend to!
The code is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
for( i = 0; i < 9; i++ ){
int x = rand() % 8;
int y = rand() % 8;
cin >> choice;
sudoku[x][y] = choice;}
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
cout << " " << sudoku[i][j];
}
cout << endl;
}
why do you want the user to input a number in this part of the programm...actually the user didn't see the sudoku so far...how can he possibly give an answer?
In fact, i cannot put the cin AFTER it generates because there will be a 0
Also, i cannot catch all the 0 generated, because i dunno where they are.
Only way i know is to put the cin before it and that is all.
If i knew how to do that i wouldn't do this way
Can you help me out?