Sudoku Programmers Forum Index

 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log inLog in          Games  Calendar

Log in to check your private messagesLog in to check your private messages   

Few Issues with my Java Sudoku Program

 
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku
View previous topic :: View next topic  
Author Message
badgerking

Joined: 02 Apr 2009
Posts: 8
:

Items
PostPosted: Thu May 21, 2009 2:12 pm    Post subject: Few Issues with my Java Sudoku Program Reply with quote

Hello I hope someone can point why my program isn't doing certain things. I will try to keep this clear and simple as possible

The first problem I have is I can only use a Column Row mini-grid Elimination technique (CRME). By this I mean the user can only call to the method once while the program is running, therefore the user could generate a new grid but would not be able to use this feature if previously used on another grid

The code for the CRME is below, this is currently held in the class Grid

Code:


    public void CRME ()
    { 
 
    while (changed)
    {
        changed = false;
       
        for (row = 0; row < boardSize; row++)
        {
            for (col = 0; col < boardSize; col++)
            {

                if (grid[row][col] == noValue)    // check cell to see if it
                                                     // has no value then do the
                                                        // following
                {
                    CheckRow();                    // call to checkrow method
                    CheckColumn();                 // call to checkcolumn method
                    CheckSubgrid();                // call to checksubgrid method
                    System.out.println("Candidates for cell "+ cellNum
                       + ":"+possible);
                    int count = possible.size();
                   
                    if (count == 1)
                    {
                        Integer x=(Integer)possible.get(0);
                        int answer = x.intValue();
                          grid[row][col] = answer; 
                    }
                    cellNum++;
                    removePossibilities();
   
                }
   
                else
                {
                    cellNum++;
                }
               
                   
            }
           
        }
    }
}


The CRME is then called to in SudokuGUI like such

Code:

 ActionListener CRMEListener = new ActionListener(){
        public void actionPerformed (ActionEvent actionEvent)
        {
          {
           int response = JOptionPane.showConfirmDialog(null, "Solve the grid
                            by CRME?" ,
              "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
           if (response == JOptionPane.YES_OPTION) // user selects no then...
           {
              _sudokuLogic.CRME();
              _sudokuLogic.displayGrid();
              _sudokuBoard.repaint();
           }
           else if (response == JOptionPane.NO_OPTION) // user selects yes then...
           {   
           }
           else if (response == JOptionPane.CLOSED_OPTION) // user selects X(close) button...
           { 
           }
     
        }
        }
     };



_sudokuLogic is defined as such, Display holds the information on how the board is drawn

Code:

   private Grid _sudokuLogic = new Grid();
   private Display _sudokuBoard = new Display(_sudokuLogic);



Another problem I am having is the creation of a Hint Mode: i.e. " If a cell has only one value notify the user if they require help (press hint button)"

I thought this would be a simple case of reusing some of the code used in the CRME, as such the hint method is defined as such:

Code:


 public void Hint()
        {
           
          int count=possible.size();
         
            for (row = 0; row < boardSize; row++)
            {
                for (col = 0; col < boardSize; col++)
                {
                    if(grid[row][col]==noValue && count == 1)
                    {
                        hint = ("Only one value at row" + "(" + row + ")"
                                + "and column" + "(" + col + ")");                         
                    }       
                 else
                    {
                       hint = ("No hints available...");
                    }             
                }
           }
        }



The code for calling to hintMode reads as such (just a simple message):

Code:

ActionListener HintListener = new ActionListener(){
              public void actionPerformed (ActionEvent actionEvent)
        {
               JOptionPane.showMessageDialog(null, _sudokuLogic.getHint(),
                 "Hint", JOptionPane.INFORMATION_MESSAGE);
               
        }
      };


  public String getHint()
          {
              Hint();
              return hint;
          }



Any help would be greatly appreciated. I hope you can understand what the issues I am having are
Back to top
View user's profile Send private message
hobiwan

Joined: 11 Feb 2008
Posts: 83
:

Items
PostPosted: Thu May 21, 2009 2:27 pm    Post subject: Reply with quote

Its impossible to really give you advice as long as you dont explain, what "possible" is. If it is a list holding all possible values for a cell then your CRME is equivalent to Naked Singles.

What do you mean by "the user can only call to the method once while the program is running"?

The Hint() method uses the same value of possible.size() for all cells - that cant be correct. Plus when you have found a hint you have to exit your method by using "return".
Back to top
View user's profile Send private message
badgerking

Joined: 02 Apr 2009
Posts: 8
:

Items
PostPosted: Thu May 21, 2009 2:51 pm    Post subject: Reply with quote

Apologies possible is a list holding the possible values for each cell

Code:

ArrayList possible = new ArrayList();


Well the CRME only works once when the program is running. I.e. the user can generate grid after grid but once they use CRME on a grid they cant use the method again on any grid.

Quote:
The Hint() method uses the same value of possible.size() for all cells - that cant be correct.


Are you saying it is setting the size of the possibilities to the same value? Is it not just reading the number of possibilities in each cell?

What should I then do about not using return in the hint mode?[/quote]
Back to top
View user's profile Send private message
hobiwan

Joined: 11 Feb 2008
Posts: 83
:

Items
PostPosted: Thu May 21, 2009 4:36 pm    Post subject: Reply with quote

badgerking wrote:
Apologies possible is a list holding the possible values for each cell

Code:

ArrayList possible = new ArrayList();

I still dont get it. You have 81 cells, but only one ArrayList possible?

badgerking wrote:
Well the CRME only works once when the program is running. I.e. the user can generate grid after grid but once they use CRME on a grid they cant use the method again on any grid.

Without seeing your code I cant really give you an answer, but I would guess it is a problem with initialization. Try generating a new Grid instance when genearating a new board.

badgerking wrote:
Quote:
The Hint() method uses the same value of possible.size() for all cells - that cant be correct.


Are you saying it is setting the size of the possibilities to the same value? Is it not just reading the number of possibilities in each cell?

You read possible.size() before you enter your loops and you use the same value of possible.size() for any cell in that loop. That can never work.

badgerking wrote:
What should I then do about not using return in the hint mode?

How about using "return"?
Back to top
View user's profile Send private message
badgerking

Joined: 02 Apr 2009
Posts: 8
:

Items
PostPosted: Thu May 21, 2009 6:04 pm    Post subject: Reply with quote

Thanks for the advice although i'm still a little confused at some of the points you are trying to make, but thats more down to me more likely (not a great programmer must be said).

Quote:

Without seeing your code I cant really give you an answer, but I would guess it is a problem with initialization. Try generating a new Grid instance when genearating a new board.


I have done this but for some reason it doesn't display any values. What parts of the code would you like to see?

I believe I misunderstood what you meant by using return.
Back to top
View user's profile Send private message
hobiwan

Joined: 11 Feb 2008
Posts: 83
:

Items
PostPosted: Fri May 22, 2009 4:20 am    Post subject: Reply with quote

At least the parts where "possible" is created and filled.

"return" exits a method immediately. You have to put it right behind the place where "hint" is filled with an actual hint (it still wont work though unless you handle possible correctly).

Its probably not enough to make a new Grid, you have you make a new Display with the new Grid as well (or write a method to set a Grid in a Display).
Back to top
View user's profile Send private message
lkSudoku

Joined: 16 May 2009
Posts: 60
:

Items
PostPosted: Fri May 22, 2009 8:26 am    Post subject: Reply with quote

My guess is that you can call the CRME function only once because there is no correct finalization of data before next call, that is, after you call it, the inner state of the Grid changes, and the next time you call CRME the Grid is not in a valid state, same goes for Display and all other inner state variables

You can probably go pass that by either cleaning the inner state after CRME have completed (the preferred option), or delete the Grid instance and create a new one before next call (same goes for display and other data)

The possible variable could be passed to the methods that may change it instead of being a class/global variable, thus the code will be more readable and it will be clearer what changes the possible options

Your Hint function indeed initialize count once for all the cells, thus, either all cells will be considered as hints, or none of them. ou should update the count value per each cell. In addition, if you did update the count variable per cell, the function would have found all the hints but return only the last one. It is more efficient to give the first hint and break from the loops (although it is not an error). In addition, Hint returns void and has no arguments, perhaps it should give the hint as return value/argument, again to make code more clear
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Sudoku Programmers Forum Index -> Programming sudoku All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Sudoku Programmers topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group

Igloo Theme Version 1.0 :: Created By: Andrew Charron