Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a very limited implementation of the popular 6x6 Sudoku board game. The

ID: 3827806 • Letter: T

Question

This is a very limited implementation of the popular 6x6 Sudoku board game. The rules of the game are:

Each row must contain the numbers 1 - 6

Each column of the board must contain those numbers

Each 2x3 rectangle must contain those numbers.

You are given an initial sparse board and the task is to complete all missing entries according to the above rules.

You should concentrate on dealing with just one single row of the board.

Step 1 removes all the entries in that row that are covered by a square that has already been identified and resolved (contains a single value).

Step 2 should deal with a doublet (two same values in two squares in a given row) by eliminating those two entries in remaining squares in that row.

Your proposed solution should be based on Boolean algebra concepts and solutions, not a numeric approach. You may assume that the current state of a square in the 6x6 board is represented by a set of 6 D flip-flops (one for each potential value).

Explanation / Answer

import java.util.*; //for random number

class Logic
{
int[][] blockS={{4, 3, 5, 8, 7, 6, 1, 2, 9},{8, 7, 6, 2, 1, 9, 3, 4, 5},{2, 1, 9, 4, 3, 5, 7, 8, 6},
{5, 2, 3, 6, 4, 7, 8,9, 1},{9, 8, 1, 5, 2, 3, 4, 6, 7},{6, 4, 7, 9, 8, 1, 2, 5,3},{7, 5, 4, 1, 6,8, 9, 3, 2},
{3, 9, 2, 7, 5, 4, 6, 1, 8},{1, 6, 8, 3, 9 ,2, 5, 7, 4}};

Random R_num=new Random(); //random numbers to exchange rows
Random Grid_R_num=new Random();//random numbers to exchange GRIDS
Random R_exnum=new Random();
Random H_Rnum=new Random();

int firstrow,secondrow,firstcol,secondcol,firstgrid,secondgrid,gc=0;
int carry[]=new int[9];
int blockh[][]=new int[9][9];
int blockc[][]=new int [9][9];

int[][] generate()
{ ////switching the rows
//randomly choosing one of the tow rows to be changed
int x=10+R_num.nextInt(10);
for(int y=0;y<x;y++)
{
for(int a=0;a<3;a++)
{
//System.out.println("a="+a);

if(a==0)
{
firstrow=R_num.nextInt(3);
secondrow=R_num.nextInt(3);
}

else if(a==1)
{
firstrow=3+R_num.nextInt(3);
secondrow=3+R_num.nextInt(3);
}

else if(a==2)
{
firstrow=6+R_num.nextInt(3);
secondrow=6+R_num.nextInt(3);
}

for(int i=0;i<9;i++)
{
carry[i]=blockS[firstrow][i];
blockS[firstrow][i]=blockS[secondrow][i];
blockS[secondrow][i]=carry[i];
}
}
//switching the rows complete

//Switchicng the column
for(int a=0;a<3;a++)
{
if(a==0)
{
firstcol=R_num.nextInt(3);
secondcol=R_num.nextInt(3);
}

else if(a==1)
{
firstcol=3+R_num.nextInt(3);
secondcol=3+R_num.nextInt(3);
}

else if(a==2)
{
firstcol=6+R_num.nextInt(3);
secondcol=6+R_num.nextInt(3);
}

for(int i=0;i<9;i++)
{
carry[i]=blockS[i][firstcol];
blockS[i][firstcol]=blockS[i][secondcol];
blockS[i][secondcol]=carry[i];
}
}
}
//Switchicng the column complet
//Switchicng the grids
firstgrid=1+Grid_R_num.nextInt(3);
secondgrid=1+Grid_R_num.nextInt(3);

if((firstgrid==1&&secondgrid==2)||(firstgrid==2&&secondgrid==1))
{
for(int i=0;i<3;i++)
for(int j=0;j<9;j++)
{
carry[j]=blockS[i][j];
blockS[i][j]=blockS[i+3][j];
blockS[i+3][j]=carry[j];
}
}
else if((firstgrid==2&&secondgrid==3)||(firstgrid==3&&secondgrid==2))
{
for(int i=3;i<6;i++)
for(int j=0;j<9;j++)
{
carry[j]=blockS[i][j];
blockS[i][j]=blockS[i+3][j];
blockS[i+3][j]=carry[j];
}
}
else if((firstgrid==1&&secondgrid==3)||(firstgrid==3&&secondgrid==1))
{
for(int i=0;i<3;i++)
for(int j=0;j<9;j++)
{
carry[j]=blockS[i][j];
blockS[i][j]=blockS[i+6][j];
blockS[i+6][j]=carry[j];
}
}
//Swithing complete of tow grids

//suffling the puzzle
int firstnum,secondnum,shuffle;

shuffle=3+R_num.nextInt(6);

for(int k=0;k<shuffle;k++)
{
firstnum=1+R_exnum.nextInt(9);
secondnum=1+R_exnum.nextInt(9);

for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
{
if(blockS[i][j]==firstnum)
{
blockS[i][j]=secondnum;
continue;
}

if(blockS[i][j]==secondnum)
blockS[i][j]=firstnum;
}
}
return blockS;
}
//will save the complet puzzle
int[][] save()
{
if(gc==0)
blockc=generate();

gc=1;

return blockc;
}

//will hide number of puzzle

int[][] hide()
{
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
blockh[i][j]=blockc[i][j];

int row,column,hidingnum;

hidingnum=50+R_num.nextInt(10);

for(int i=0;i<hidingnum;i++)
{
row=H_Rnum.nextInt(9);
column=H_Rnum.nextInt(9);
blockh[row][column]=0;
}
return blockh;
}

//will show main puzzle

/*void show()
{
for(int i=0;i<9;i++)
{
System.out.println();
for(int j=0;j<9;j++)
System.out.print(" "+blockS[i][j]);
}
System.out.println(" ");
System.out.println(" ");
blockS=save();
for(int i=0;i<9;i++)
{
System.out.println();
for(int j=0;j<9;j++)
System.out.print(" "+blockS[i][j]);
}
System.out.println(" ");
System.out.println(" ");
blockS=hide();
for(int i=0;i<9;i++)
{
System.out.println();
for(int j=0;j<9;j++)
System.out.print(" "+blockS[i][j]);
}
}*/
/*public static void main (String[] args) {

Logic ob=new Logic();
//ob.generate();
ob.show();
//System.out.println();
//ob.save();
//ob.show();
//ob.hide();
//ob.show();
}*/
}

Graphic representation:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class GraphicallyRepresentation extends JFrame implements ActionListener
{
Container con;
JButton b[][]=new JButton[9][9];
TextField t[]=new TextField[61];
JMenuBar mbar;
JMenu file,help,option,setting;
JMenuItem submit,exit,about,Reset;

int[][] cp=new int[9][9];
int[][] ip=new int[9][9];

GraphicallyRepresentation()
{
super("Play Sudoku .............(Created by farsim & hasib)");
setSize(500,500);
//setresizeable(false);

con=getContentPane();
con.setLayout(new GridLayout(9,9));

Mylogic ob1=new Mylogic();
ob1.complet_puzzle();
ob1.puzzle();

int c=0;
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
{
b[i][j]=new JButton(""+ip[i][j]);
b[i][j].setFont(new Font("ARIALBD",Font.BOLD,20));
b[i][j].setForeground(Color.BLUE);
if(ip[i][j]==0)
{
//b[i][j]=new JButton("");
b[i][j].setText("");
b[i][j].setBackground(Color.WHITE);
b[i][j].addActionListener(this);

}

con.add(b[i][j]);

if(i==3 || i==4 || i==5 || j==3 || j==4 || j==5)
{
if(2<i></i> {
b[i][j].setBackground(Color.CYAN);
continue;
}
b[i][j].setBackground(Color.pink);

}

else
b[i][j].setBackground(Color.CYAN);
}

mbar=new JMenuBar();
setJMenuBar(mbar);

file=new JMenu("File");
option=new JMenu("Option");
setting=new JMenu("Setting");
help=new JMenu("Help");

about=new JMenuItem("About");
Reset=new JMenuItem("reset");
submit=new JMenuItem("Submit");
exit=new JMenuItem("Exit");

submit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
int r=0;

for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
if(cp[i][j]!=Integer.parseInt(b[i][j].getText()))
{r=1;
break;}

for(int i=0;i<9;i++)
{
System.out.println();
for(int j=0;j<9;j++)
{
System.out.print(cp[i][j]);
System.out.print(Integer.parseInt(b[i][j].getText())+" ");
}}
System.out.print(" "+r);
if(r==0)
JOptionPane.showMessageDialog(GraphicallyRepresentation.this,"You won the Game");
//System.out.println("You won the Game");
else
//System.out.println("You lose the Game");
JOptionPane.showMessageDialog(GraphicallyRepresentation.this,"You lose the Game"); }

});
exit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}

});

about.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog( GraphicallyRepresentation.this,
"FARSiM & HASiB JIgni Dost! SUST CSE 15batch PRODUCT....Our first project of java language ",
"About", JOptionPane.PLAIN_MESSAGE );
}
}
);
Reset.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//recall();
}
});

file.add(Reset);
file.add(submit);
file.addSeparator();
file.add(exit);
mbar.add(file);
mbar.add(option);
mbar.add(setting);
mbar.add(help);
mbar.add(about);

show();

//ob1.complet_puzzle();

MyWindowAdapter mwa=new MyWindowAdapter();
addWindowListener(mwa);

}

class Mylogic extends Logic
{
void complet_puzzle()
{
cp=save();

/*for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
System.out.print(cp[i][j]+" ");
System.out.println();
}*/
}
void puzzle()
{
ip=hide();
/*System.out.print(" "+"nhiding puzzle : ");
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
System.out.print(ip[i][j]+" ");
System.out.println();
}*/
}

}

class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}

public void actionPerformed(ActionEvent e)
{
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
{
if(e.getSource()==b[i][j])
{
String s=JOptionPane.showInputDialog("enter your number");
int c=Integer.parseInt(s);
if(0c)
{
b[i][j].setText(s);
//b[i][j].setBackground(Color.GRAY);
b[i][j].setFont(new Font("ARIALBD",Font.BOLD,25));
b[i][j].setForeground(Color.RED);
}

break;
}
}
}
void recall()
{
GraphicallyRepresentation rs=new GraphicallyRepresentation();
}

public static void main (String[] args) {
GraphicallyRepresentation ob=new GraphicallyRepresentation();

}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote