in c#, you are to design the interface for a Sudoku 4x4 game. Click on this link
ID: 3820835 • Letter: I
Question
in c#, you are to design the interface for a Sudoku 4x4 game. Click on this link for the rules of the game.
A 4x4 Sudoku contains 4 rows and four columns. It has also four boxes
“Each game consists of a 4x4 grid containing given clues in various places. The object is to fill all empty squares (cells) so that the numbers 1 to 4 appear exactly once in each row, column and box.(each colored in the picture above)”
Each cell has a location as follows
1
2
5
6
3
4
7
8
9
10
13
14
11
12
15
16
Attached to this HW, you will find a text file that contains several configurations of puzzles. Each line in the file is a puzzle configuration consisting of 16 number.
This is an example of the first line (from the file attached)
2103002000100400
Each non-zero number is placed in its appropriate square according to its location. For example, the above line should fill out the puzzle as follows
2
1
3
2
4
1
This is a two part HW. Your job for part 1 is to create the interface similar to the one below
Your GUI should create the board with all 16 cells (you can use labels, buttons, or whatever you like as long as is looks great), a “check solution” button (to check the solution), a restart button to restart the game with a different configuration, and an exit button.
Now, let’s talk about programming. Your program should start by reading the configuration text file at the start of the program, and save all configuration (lines) in an array of type char (figure out the dimension(s) to use), then close the file. The file that you read could have a dynamic number of lines. Make sure you do not fix (hard code) the number of lines that can be read from the file.
Explanation / Answer
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
void main(void)
{
int sudoku[9][9],gen=0,check,a,b,c;
bool marker[9]={0,0,0,0,0,0,0,0,0};
clrscr();
srand(time(NULL)*10000000);//trying to convert to milliseconds.
for(a=0;a<=2;a++)//first 3x3 block
{
for(b=0;b<=2;b++)
{
do{
gen=rand()%9+1;
sudoku[a][b]=gen;
}while(marker[gen-1]==1);
marker[gen-1]=1;//'marker' checks if the number has already been generated for this block
}
}
for(a=0;a<=2;a++)//second 3x3 block
{
for(b=3;b<=5;b++)
{
do{
do{
check=0;
gen=rand()%9+1;
sudoku[a][b]=gen;
for(c=0;c<=2;c++)
{
if(sudoku[a][c]==sudoku[a][b])
{
++check;//'check' checks if there's an equivalent value on the other block.
}
}
}while(check>0);
}while(marker[gen-1]==0);
marker[gen-1]=0;
}
}
for(a=0;a<=2;a++)//and so on...
{
for(b=6;b<=8;b++)
{
do{
do{
check=0;
gen=rand()%9+1;
sudoku[a][b]=gen;
for(c=0;c<=5;c++)
{
if(sudoku[a][c]==sudoku[a][b])
{
++check;
}
}
}while(check>0);
}while(marker[gen-1]==1);
marker[gen-1]=1;
}
}
for(a=3;a<=5;a++)
{
for(b=0;b<=2;b++)
{
do{
do{
check=0;
gen=rand()%9+1;
sudoku[a][b]=gen;
for(c=0;c<=2;c++)
{
if(sudoku[c][b]==sudoku[a][b])
{
++check;
}
}
}while(check>0);
}while(marker[gen-1]==0);
marker[gen-1]=0;
}
}
for(a=3;a<=5;a++)
{
for(b=3;b<=5;b++)
{
do{
do{
check=0;
gen=rand()%9+1;
sudoku[a][b]=gen;
for(c=0;c<=2;c++)
{
if(sudoku[a][c]==sudoku[a][b])
{
++check;
}
}
for(c=0;c<=2;c++)
{
if(sudoku[c][b]==sudoku[a][b])
{
++check;
}
}
}while(check>0);
}while(marker[gen-1]==1);
marker[gen-1]=1;
}
}
for(a=3;a<=5;a++)
{
for(b=6;b<=8;b++)
{
do{
do{
check=0;
gen=rand()%9+1;
sudoku[a][b]=gen;
for(c=0;c<=5;c++)
{
if(sudoku[a][c]==sudoku[a][b])
{
++check;
}
}
for(c=0;c<=2;c++)
{
if(sudoku[c][b]==sudoku[a][b])
{
++check;
}
}
}while(check>0);
}while(marker[gen-1]==0);
marker[gen-1]=0;
}
}
for(a=0;a<=9;a++)//this loop should display the generated numbers...
{
for(b=0;b<=8;b++)
{
cout<<sudoku[a][b]<<" ";
}
cout<<endl<<endl;
}
getch();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.