/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;



/**
 *
 * @author Francisco Matesanz & Benjamin Nogal
 */
public class listenerNewGame implements MouseListener {

    final slot[][] board;
    final JButton[][] button;
    final boolean[][][] errorBoard;

    public listenerNewGame(JButton[][] button, slot[][] board, boolean[][][] errorBoard) {
        this.board = board;
        this.button = button;
        this.errorBoard = errorBoard;
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        createNewSudoku();
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    private void createNewSudoku() {
        byte  aleat2, aleat3;

        //First reset values
        for (int x = 0; x < 9; x++) {
            for (int y = 0; y < 9; y++) {
                board[x][y].aviable = true;
                board[x][y].value = 0;
                button[x][y].setText("X");
                if(((x<3)&&(y<3))||(x>5)&&(y<3)||((x<3)&&(y>5))
                		||((x>5)&&(y>5))||(((x>2)&&(x<6))&&((y>2)&&(y<6)))                        )
                    button[x][y].setBackground(Color.CYAN);
                else
                    button[x][y].setBackground(Color.BLUE);
                errorBoard[x][y][0] = false;
                errorBoard[x][y][1] = false;
                errorBoard[x][y][2] = false;
            }
        }


        //We will try to put 20 different numbers in the sudoku board
        boolean nomorenumbers = false;
        for (int number = 0; (number < 20)&&(nomorenumbers==false); number++) {
            //first we select where
            do {
                aleat2 = (byte) (Math.random() * 9);//Row
                aleat3 = (byte) (Math.random() * 9);//Col
            } while (board[aleat2][aleat3].aviable == false);

            boolean nomore = false;
                        
            //Now we try to put some number in the box
            for (byte number = 1; (number < 10) && (nomore == false); pepon++) {
               if(number%2==0){
            	   if (checkIfPossible(number, aleat2, aleat3) == true) {
            		   nomore = true;
                    
                    	board[aleat2][aleat3].value = pepon;
                    	button[aleat2][aleat3].setText(Integer.toString(pepon));
                    
                    	board[aleat2][aleat3].aviable = false;
                    	button[aleat2][aleat3].setBackground(Color.GRAY);
                    
            	   }
               	}
            	   else
            		   if (checkIfPossible((byte)(10-number), aleat2, aleat3) == true) {
                           nomore = true;
                           board[aleat2][aleat3].value = (byte) (10 - pepon);
                           button[aleat2][aleat3].setText(Integer.toString((byte)(10-pepon)));	
                           board[aleat2][aleat3].aviable = false;
                           button[aleat2][aleat3].setBackground(Color.GRAY);
                           
                       }
            }
        }
    }

    private boolean checkIfPossible(int number, int row, int col) {
        int x, z;

        //first we check if is possible in row
        for (x = 0; x < 9; x++) {
            if (board[row][x].value == number) {
                return false;
            }
        }
        //second we check if is possible in col
        for (x = 0; x < 9; x++) {
            if (board[x][col].value == number) {
                return false;
            }
        }
        // WE SELECT THE MIDDLE SLOT OF THE 3X3 GRID
        int bigY;
        if (col < 3) {
            bigY = 1;
        } else if ((col < 6)&&(col>=3)) {
            bigY = 4;
        } else {
            bigY = 7;
        }
        int bigX;
        if (row < 3) {
            bigX = 1;
        } else if ((row < 6)&&(row>=3)) {
            bigX = 4;
        } else {
            bigX = 7;
        }
        //finally we check if is possible in 3x3
        for (z = -1; z < 2; z++) {
            for (x = -1; x < 2; x++) {//for each row
                if (board[bigX + z][bigY + x].value == number) //if condition then return false
                {
                    return false;
                }
            }
        }
        //if we are here is because is possible to fill it
        return true;
    }
}

