package ProgramasPersonales;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class ResuelveSudoku {
public static void main(String[] args) {
// TODO Auto-generated method stub
Marco1 marco=new Marco1();
marco.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class Marco1 extends JFrame{
public Marco1(){
setTitle("Sudoku");
setResizable(false);
setBounds(450,150,400,400);
lamina1 lamina=new lamina1();
add(lamina);
setVisible(true);
}
}
class lamina1 extends JPanel{
public lamina1() {
EventosRaton click=new EventosRaton();
for(x=0;x<9;x++) {
for(y=0;y<9;y++) {
boton[x][y]=new JButton("0");
add(boton[x][y]);
Sudoku.add(boton[x][y]);
boton[x][y].addMouseListener(click);
}
}
ResuelveSudoku resuelve=new ResuelveSudoku();
setLayout(new BorderLayout());
BotonResolver=new JButton("Resolver");
Sudoku.setLayout(new GridLayout(9,9));
add(Sudoku,BorderLayout.CENTER);
add(BotonResolver,BorderLayout.SOUTH);
BotonResolver.addActionListener(resuelve);
}
class EventosRaton extends MouseAdapter implements MouseMotionListener{
public void mousePressed(MouseEvent e) {
if(contador==0) {
if(e.getModifiersEx()==MouseEvent.BUTTON1_DOWN_MASK) {
int xx=e.getXOnScreen();
int yy=e.getYOnScreen();
yy=yy-177;
xx=xx-456;
if(xx<=42) {
xx=0;
}else if(xx<=85) {
xx=1;
}else if(xx<=128) {
xx=2;
}else if(xx<=171) {
xx=3;
}else if(xx<=214) {
xx=4;
}else if(xx<=257) {
xx=5;
}else if(xx<300) {
xx=6;
}else if(xx<343) {
xx=7;
}else {
xx=8;
}
if(yy<=37) {
yy=0;
}else if(yy<=75) {
yy=1;
}else if(yy<=113) {
yy=2;
}else if(yy<=151) {
yy=3;
}else if(yy<=189) {
yy=4;
}else if(yy<=227) {
yy=5;
}else if(yy<265) {
yy=6;
}else if(yy<303) {
yy=7;
}else {
yy=8;
}
String entrada=boton[yy][xx].getText();
int cambio=Integer.parseInt(entrada);
if(cambio<=8) {
cambio++;
}else {
cambio=0;
}
boton[yy][xx].setText(""+cambio);
}
}
}
}
private class ResuelveSudoku implements ActionListener{
public void actionPerformed(ActionEvent e) {
if(contador==0) {
for(int a=0;a<9;a++) {
for(int b=0;b<9;b++) {
boton[a][b].setEnabled(false);
numeral[a][b]=Integer.parseInt(boton[a][b].getText());
if(numeral[a][b]>0) {
constante[a][b]=numeral[a][b];
}else {
numeral[a][b]=1;
constante[a][b]=0;
}
}
}
}
for(int cont=0;cont<9;cont++) {
for(int n=0;n<9;n++) {
if(n==cont) {
n++;
if(n>8) {
n=0;
}
}
if(numeral[0][cont]==numeral[0][n]) {
if(constante[0][cont]>0) {
n=9;
}else {
numeral[0][cont]++;
if(numeral[0][cont]==10) {
numeral[0][cont]=1;
n=0;
}
}
}
}
}
for(int imx=0;imx<9;imx++) {
for(int imy=0;imy<9;imy++) {
cambio2[imx][imy]=""+numeral[imx][imy];
boton[imx][imy]=boton[imx][imy].setText(cambio2[imx][imy]);
}
}
contador=1;
}
}
String cambio2[][]=new String[9][9];
int constante[][]=new int[9][9];
int numeral[][]=new int[9][9];
int x,y,contador=0,cont=0;
JButton boton[][]=new JButton [9][9];
private JPanel Sudoku=new JPanel();
private JButton BotonResolver;
}