Archive for 2014
Suduko in Java
It's my semester project to make a Suduko game in Java. I worked very hard for it and at last I completed it. And I am giving you a complete description of it, it's very helpful for you.
Suduko Main View |
- DESCRIPTION
- Making GUI Of Game
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
public class GridGuiOne
extends MouseAdapter implements KeyListener,ActionListener,FocusListener {
JButton b1,b2,b3;
int ce=3;
int gridsel=0;
JFrame frame = new JFrame("Suduko");
JPanel main=new JPanel(new BorderLayout()); // Main Panel
JPanel puzzle=new JPanel(new GridLayout(1,0)); // Holds Suduko Grid and Buttons
JPanel Options=new JPanel(new GridLayout(3,0)); // Panel For JBUttons
JPanel Timer=new JPanel(new FlowLayout());
final JTextField[][] subPanels = new JTextField[9][9]; // 2D Array Of JTextFields[][]
JPanel[][] grid=new JPanel[3][3]; // 2D Array of Grid to hold JButtons
JPanel panel1= new JPanel(new GridLayout(3,3));
GridGuiOne()
{
builtOption();
builtGrid();
main.setSize(400,400);
Timer.add(Options);
main.add(Timer,BorderLayout.EAST);
main.add(puzzle,BorderLayout.CENTER);
frame.add(main);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,450); frame.setVisible(true);}
- (a). Creating Options Panel OF the Game
In options Panel
I add three buttons:
Ø GENERATE
Ø CHECK
Ø EXIT
GENERATE: New Game Generate for the User
CHECK: Either Number is entered at right position or not
EXIT: Want To Quit
public void builtOption()
{
Options.setBorder(BorderFactory.createTitledBorder(" Options "));
b1=new JButton("GENERATE");
b2=new JButton("CHECK");
b3=new JButton("EXIT");
b1.setPreferredSize(new Dimension(100, 50));
b2.setPreferredSize(new Dimension(100, 50));
b3.setPreferredSize(new Dimension(100, 50));
Options.add(b1);
Options.add(b2);
Options.add(b3);
Options.setSize(400,400);
}
Option Panel For Game |
public void builtGrid()
{
Font s = new Font("Cooper", Font.ITALIC, 19);
Border bod = BorderFactory.createEtchedBorder( EtchedBorder.LOWERED, null,Color.white);
JPanel gridmain=new JPanel(new GridLayout(3,3));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
grid[i][j]=new JPanel(new GridLayout(3,3));
grid[i][j].setBorder( bod );
gridmain.add(grid[i][j]);
}
// Adding array of Jbuttons(9x9) into the panels of(3x3)
int a=0,b=0;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++,b++)
{
for(int k=0;k<3;k++)
{
for(int l=0;l<3; l++,a++)
{
if(b==9 )
break;
else
{
subPanels[b][a]=new JTextField("");
subPanels[b][a].setHorizontalAlignment(JTextField.CENTER);
subPanels[b][a].setForeground(Color.black);
subPanels[b][a].setFont(s);
subPanels[b][a].setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
subPanels[b][a].setText(null);
subPanels[b][a].addActionListener(this);
subPanels[b][a].addKeyListener(this);
subPanels[b][a].addMouseListener(this);
subPanels[b][a].addFocusListener(this);
subPanels[b][a].setFocusable(true);
grid[k][l].add(subPanels[b][a]);
if(a==0 || a==2 || a==4 || a==6 || a==8)
{
subPanels[b][a].setBackground(new Color(143,188,143));
}
else
{
subPanels[b][a].setBackground(new Color(255,231,186));
}
} }
}
a=0;
}
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
puzzle.add(gridmain);
frame.setVisible(true);
}
1 2. Main
Logic Of Board
I
made 2D array of J Text Fields[9][9], which is placed in 3x3 of J Panel Grid.
It means that in every grid there is 9 buttons, which is placed in Grid in such
a way as shown in fig.
According
to this table, every button have fixed position in grid. [cell][grid].
[0][0]
|
[1][0]
|
[2][0]
|
[0][1]
|
[1][1]
|
[2][1]
|
[0][2]
|
[1][2]
|
[2][2]
|
[3][0]
|
[4][0]
|
[5][0]
|
[3][1]
|
[4][1]
|
[5][1]
|
[3][2]
|
[4][2]
|
[5][2]
|
[6][0]
|
[7][0]
|
[8][0]
|
[6][1]
|
[7][1]
|
[8][1]
|
[6][2]
|
[7][2]
|
[8][2]
|
[0][3]
|
[1][3]
|
[2][3]
|
[0][4]
|
[1][4]
|
[2][4]
|
[0][5]
|
[1][5]
|
[2][5]
|
[3][3]
|
[4][3]
|
[5][3]
|
[3][4]
|
[4][4]
|
[5][4]
|
[3][5]
|
[4][5]
|
[5][5]
|
[6][3]
|
[7][3]
|
[8][3]
|
[6][4]
|
[7][4]
|
[8][4]
|
[6][5]
|
[7][5]
|
[8][5]
|
[0][6]
|
[1][6]
|
[2][6]
|
[0][7]
|
[1][7]
|
[2][7]
|
[0][8]
|
[1][8]
|
[2][8]
|
[3][6]
|
[4][6]
|
[5][6]
|
[3][7]
|
[4][7]
|
[5][7]
|
[3][8]
|
[4][8]
|
[5][8]
|
[6][6]
|
[7][6]
|
[8][6]
|
[6][7]
|
[7][7]
|
[8][7]
|
[6][8]
|
[7][8]
|
[8][8]
|
At that logic the J Text
Fields are inserted into the panels. In whole program these positions matters a
lot.
1 3. Keep
Tracking Of Key Listener and Mouse Listener In J Text Field
As there are
81 JTextfields , which placed in 2D array named “subPanels[][]” in code. When user
selects a specific JTextfield in game, then we have to keep record of
that, so I made this function which points to the specified position during
game play.
As there is a Listener in java, which is
called FocusListener, ithas two Function.
1. FocusGained(FocusEvent
ae)
2. FocusLost(FocusEvent
ae)
This listener interacts or gave signal when the required field is under
focus, the focusGained performed and when the focus is lost then
focusLost is performed.
Here is the
code:
public void focusLost(FocusEvent arg0) {
for(int i=0; i<=8; i++)
{
for(int j=0; j<=8;j++)
{
{
if(j==0 || j==2 || j==4 || j==6 || j==8)
{
subPanels[i][j].setBackground(new Color(143,188,143));
//
}
else
{
subPanels[i][j].setBackground(new Color(255, 231 ,186));
}
}}
}
public void focusGained(FocusEvent arg0) {
int flag=0,i,j = 0;
// Finding at whic point the cursor is.
for( i=0; i<=8; i++)
{
for(j=0; j<=8;j++)
{
if(arg0.getSource()==subPanels[i][j])
{
flag++;
break;
}}
if(flag==1)
break;
}
System.out.print("Cursor" + i + " " + j);
printEventInfo(arg0,null,null,i,j);
a b. printEventInfor(Key
Listener, Focus Listener , Mouse Listener, int a, int b)
I made this function to control the key
Listener, either cursor is in main grid or out of grid. It controls the
keylistener, either cursor is moving down or upward or left or right in
maingrid. It controls it.
private void printEventInfo(FocusEvent focus,MouseEvent mouseLocat,KeyEvent keyLocat,int a,int b) {
if (keyLocat==null)
{
ce=a;
gridsel=b;
}
else
try
{
if (keyLocat.getKeyCode()== KeyEvent.VK_DOWN)
{
if((ce>=6 && ce<=8) && gridsel>=6)
{
ce-=6;
gridsel-=6;
subPanels[ce][gridsel].requestFocus();
}
else if((ce>=6 && ce<=8))
{
ce-=6;
gridsel+=3;
subPanels[ce][gridsel].requestFocus();
}
else
{
ce+=3;
subPanels[ce][gridsel].requestFocus();
}
}
else
if (keyLocat.getKeyCode() == KeyEvent.VK_UP)
{
if((ce>=0 && ce<=2) && (gridsel==0 || gridsel==1 ||gridsel==2 ))
{
ce+=6;
gridsel+=6;
subPanels[ce][gridsel].requestFocus();
}
else if((ce>=0 && ce<=2) && (gridsel>=3 && gridsel<=8) )
{
ce+=6;
gridsel-=3;
subPanels[ce][gridsel].requestFocus();
}
else
{
ce-=3;
subPanels[ce][gridsel].requestFocus();
}
}
else if (keyLocat.getKeyCode() == KeyEvent.VK_LEFT)
{
{
if((ce==0 || ce==3 || ce==6) && (gridsel==0 || gridsel==3 ||gridsel==6 ))
{
ce+=2;
gridsel+=2;
subPanels[ce][gridsel].requestFocus();
}
else if((ce==0 || ce==3 || ce==6) )
{
ce+=2;
gridsel-=1;
subPanels[ce][gridsel].requestFocus();
}
else {
ce-=1;
subPanels[ce][gridsel].requestFocus();
}
}
}
else if (keyLocat.getKeyCode() == KeyEvent.VK_RIGHT)
{
{
if((ce==2 || ce==5 || ce==8) && (gridsel==2 || gridsel==5 ||gridsel==8 ))
{
ce-=2;
gridsel-=2;
subPanels[ce][gridsel].requestFocus();
}
else if((ce==2 || ce==5 || ce==8) )
{
ce-=2;
gridsel+=1;
subPanels[ce][gridsel].requestFocus();
}
else {
ce+=1;
subPanels[ce][gridsel].requestFocus();
}
}
}
}
catch (ArrayIndexOutOfBoundsException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
finally{
}
}
3. Game
Starting Number
There
are two conditions in the starting of the game.
a. Fixed Numbers at Fixed
Locations
In this Condition we know the starting position of
the fixed text fields. In this way we can make a different levels of the game.likewise. I t is applied if you know the whole game , then you just set the game according to your own will.
subPanels[2][0].setText("4");
subPanels[2][0].setEditable(false);
subPanels[2][8].setText("7");
subPanels[2][8].setEditable(false);
b. Random Numbers at Random Locations
In the start of the game a random number is
generated at the random location of Board. But these numbers are according to
the rules of the game. I made a function
“logicCheck(int number, int cell, int grid, int
convert)” to check the condition of the starting game.
void randomNumberGenerate()
{
try{
boolean possible=true;
int Min1=1;
int Min=0;
int Max=8;
int Max1=9;
int randomNum=0;
String n;
int randomcell=0;
int randomgrid=0;
randomNum=(Min1 + (int)(Math.random() * ((Max1 - Min1) + 1)));
n=Integer.toString(randomNum);
randomcell=(Min + (int)(Math.random() * ((Max - Min) + 1)));
randomgrid=(Min + (int)(Math.random() * ((Max - Min) + 1)));
subPanels[randomcell][randomgrid].setText(n);
subPanels[randomcell][randomgrid].setEditable(false);
subPanels[randomcell][randomgrid].setForeground(Color.blue);
for(int i=0; i<10;i++)
for(int j=0; j<2; j++)
{
randomNum=(Min1 + (int)(Math.random() * ((Max1 - Min1) + 1)));
n=Integer.toString(randomNum);
randomcell=(Min + (int)(Math.random() * ((Max - Min) + 1)));
randomgrid=(Min + (int)(Math.random() * ((Max - Min) + 1)));
possible=logicCheck(randomNum,randomcell,randomgrid,1);
if(possible==true)
{
}
else
{
subPanels[randomcell][randomgrid].setText(n);
subPanels[randomcell][randomgrid].setEditable(false);
subPanels[randomcell][randomgrid].setForeground(Color.blue);
}
}
}
catch(ArrayIndexOutOfBoundsException ex){
System.out.print(ex.getMessage());
};}
4. Logic Check for The Game
I made a
function named, “logicCheck(int number,
int cell, int grid, int convert)” to check that either number which is
entered correctly in board or not.
That
function have a flexibility of controlling two logics. If the parameter convert
have value one then it controls the starting of game, it generates different
numbers. If the user press GENERATE button then it parameter convert is ON, and
the random numbers are generated at random position. And when the user press
check button it checks that at which position the numbers is entered wrongly.
When the number is entered at wrong position and user check it then that place
becomes Red. As shown in fig.
boolean logicCheck(int num, int row, int col,int convert)
{
String d = null;
int x=0;
int a=row;
int b=col;
boolean flag=true;
int moreCheck=0;
boolean random=false;
int counter1 = 0;
try
{
// Grid Checking
for(int i=0; i<9; i++)
{
if(((subPanels[i][col].getText().equals("")) || i==row ))
{
// i++;
}
else
{
d=subPanels[i][col].getText();
x=Integer.parseInt(d);
// System.out.print("Grid " + x + " ");
if(convert==0)
{
moreCheck=numberCheck(i,col,num,x);
if(moreCheck>0)
{
flag=true;
break;
}
else
flag=false;
}
else
random=randomLogic(num,x);
if(random==false)
{
counter1++;
}
}
// Row Checking
if(!flag || convert==1)
{
// System.out.print(" \n"+flag);
for(int c=0;c<3 ;c++,col++)
{
for(int j=0; j<3; j++)
{
if((row==2 || row==5 || row==8))
{
if((subPanels[row][col].getText().equals("")) || (row==a && col==b))
{
row-=2;
}
else
{
d=subPanels[row][col].getText();
x= Integer.parseInt(d);
// System.out.print("ROW " + x + " ");
if(convert==0)
{
moreCheck=numberCheck(row,col,num,x);
if(moreCheck>0)
{
flag=true;
break;
}
else
flag=false;
}
else
{
random=randomLogic(num,x);
if(random==false)
{
counter1++;
}
}
row-=2;
}
}
else
{
if((subPanels[row][col].getText().equals("")) || (row==a && col==b))
{
row++;
}
else
{
d=subPanels[row][col].getText();
x= Integer.parseInt(d);
if(convert==0)
{
moreCheck=numberCheck(row,col,num,x);
if(moreCheck>0)
{
flag=true;
break;
}
else
flag=false;
}
else
{
random=randomLogic(num,x);
if(random==false)
{
counter1++;
}
}
row++;
}
}
}
if(col==2 || col==5 || col==8)
col-=3;
}
}
//Coloumn Check
if(flag || convert==1)
{
for(int e=0;e<3 ;e++)
{
for(int j=0; j<3; j++)
{
if((row==6 || row==7 || row==8))
{
if((subPanels[row][col].getText().equals(""))||(row==a && col==b))
{
row-=6;
}
else
{
d=subPanels[row][col].getText();
x= Integer.parseInt(d);
if(convert==0)
numberCheck(row,col,num,x);
else
{
random=randomLogic(num,x);
if(random==false)
{
counter1++;
}
}
row-=6;
}
}
else
{
if((subPanels[row][col].getText().equals("")) || (row==a && col==b))
{
row+=3;
}
else
{
d=subPanels[row][col].getText();
x= Integer.parseInt(d);
if(convert==0)
numberCheck(row,col,num,x);
else
{
random=randomLogic(num,x);
if(random==false)
{
counter1++;
}
}
row+=3;
}
}
}
if(col==6 || col==7 || col==8)
col-=6;
else
col+=3;
}
}
}}
catch(NumberFormatException ex){};
if(counter1>0)
return random=true; // Number present already at any location
else
return random=false; // Number not present already
}
a a. Random
Generate Number Control
// Random Generate Number Control
boolean randomLogic(int num, int x) {
boolean flag=true;
if(num==x)
{
flag=false;
}
return flag;
}
b. Number Entered Checker
// Number Check
int numberCheck(int c, int d,int a, int b)
{
int check=0;
if(a==b)
{
subPanels[c][d].setBackground(Color.RED);
check++;
}
return check;
}
public void keyTyped(KeyEvent arg0) {
String num ;
int entrdNum;
// Checking Either Entered number is Valid
if (subPanels[ce][gridsel].getText().trim().length() != 0)
arg0.consume();
else
if ((arg0.getKeyChar() > '0' && arg0.getKeyChar() <= '9') || (arg0.getKeyChar() == KeyEvent.VK_BACK_SPACE)
|| (arg0.getKeyChar() == KeyEvent.VK_DELETE)) {
}
else
{
JOptionPane.showMessageDialog(null, "Enter Number 1-9");
arg0.consume();
}
// Get Entered number in Current Cell
if(arg0.getKeyChar()>=49 && arg0.getKeyChar()<=57)
{
num= KeyEvent.getKeyText(arg0.getKeyChar());
entrdNum=Integer.parseInt(num);
// System.out.print(entrdNum + " ");
}
}
Entered number is not in range
It detects that which button is pressed, and then work according
to that button.
|
public void actionPerformed(ActionEvent arg0)
{
String text=null;
int ef=0;
if(arg0.getSource()==b1)
{
for(int i=0; i<9;i++)
for(int j=0; j<9; j++)
{
subPanels[i][j].setText("");
subPanels[i][j].setEditable(true);
if(j==0 || j==2 || j==4 || j==6 || j==8)
{
subPanels[i][j].setBackground(new Color( 139, 71 ,93));
// subPanels[b][a].setBackground(Color.PINK);
}
else
{
subPanels[i][j].setBackground(Color.WHITE);
}
}
randomNumberGenerate();
}
else if(arg0.getSource()==b2)
{ // String text=null;
// int ef=0;
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
if(subPanels[j][i].getText().equals(""))
{
subPanels[j][i].setBackground(Color.RED);
}
else
{
text=subPanels[j][i].getText();
ef = Integer.parseInt(text);
System.out.print(ef);
logicCheck(ef,j,i,0);
}
}
}
}
else
{
JOptionPane.showMessageDialog(null,"Are You Sure, You Want To Exit");
frame.setVisible(false);
frame.dispose();
}
Thank's for reading this. If you have an any query contact me.
Here it is full cod. You can download it.
Here are some tutorial which are helpful for you.
1. Logical Suduko Solver In java
2. Backtracking to solve a sudoku puzzle
3. Suduko Solver
Code : https://www.dropbox.com/s/md4uds882lx77yq/Sudoko%20Code.txt
Full Description: https://www.dropbox.com/s/7fm03z20xo3xsaj/SUDUKO.docx
Desk hop -Simple and Secure Screen Sharing
- Can we access Other Computer ?
- Can we share our desktop with others ?
- Can we see others desktop ?
These are the question which every one thinks now a days.
And the response is "Yes", there is a software, with the help of, we can do all the things.
desk hop - Secure screen sharing |
Here is a link which'll give the answer of your all question. Download it from here.