1

I'm building a Sudoku Game. I came here to get some help because I'm completely stuck in my code. I'm not asking for you to complete my code, I know that's not your job. Just few hints as what to do next would be great!

I use MVC and Swing Components for GUI to make the code lighter. I divided each field and method so I can understand what to do next but I'm confused. I'm particularly having trouble understanding how to do the following methods:

  • initializeGrid

  • chooseGameDifficulty

  • makeMove

  • cancelMove

Model

public class GameSudokuModel {



    // states -- fields
    Scanner userInput = new Scanner (System.in); // accept user input

    // int levelDifficulty = 0; // level of difficulties

    int [] gridSize ; // Sudoku 9x9 == 81 cells -- used to initialize grid or solve puzzle --

    int [] subGridSize ; // a sub-grid = 9 cells

    int gameMove = 0; // calculate the total number of moves per game // ++makeMove and --cancelMove
    int [] gameCell = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // a cell contain a list of choices numbers 1-9

    int currentGameTime = 0; // calculates the total time to complete a puzzle

    String currentPlayerName = userInput.nextLine(); // player name
    // end of fields





    //behaviors -- methods

    /******************************************************
     * 
     * Method calculateGameTime (initialiserGrille)
     * 
     * 
     * Calculates time
     * 
     * The stopwatch starts when the player makes ​​his first move
     * 
     *
     * 
     ******************************************************/



    public class calculateGameTime{

}





    /******************************************************
     * 
     * Method initializeGrid (initialiserGrille)
     * 
     * 
     * Used to initialize a grid
     * 
     * Reset the grid ( back to the original Sudoku grid ) using the list of moves .
     * 
     * 
     * 
     *
     * 
     ******************************************************/


    public class initializeGrid {

    }





    /******************************************************
     * 
     * Method levelDifficulty
     * 
     * 
     * Established the parameters of level of difficulty
     *
     * 
     * @param beginner
     * @param expert
     * @return 
     ******************************************************/


     public int levelDifficulty (int beginner, int expert){

  while(true)
  {
    int levelDifficulty = 0;

    levelDifficulty= userInput.nextInt();
    System.out.println (" ");

    if(levelDifficulty < beginner || levelDifficulty> expert){
        System.out.print (" You must choose 1, 2 or 3." + "Please try again : ");
        System.out.println (" ");


    }

    else

        return levelDifficulty;

  }


 }




     /****************************************************
     * Method chooseGameDifficulty (chosisirNiveauDifficulte)
     * 
     * The method makes possible to choose the level of complexity of a grid
     * 
     * (1) beginner: the player starts the game with a grid made ​​up to 75% (81 * 0.75) 
     * 
     * (2) Intermediate : the player starts the game with a grid made ​​up to 50% (81 * 0.50)
     * 
     * (3) Expert : the player starts the game with a grid made ​​up to 25% (81 * 0.25)
     * 
     * Numbers are set randomly on the grid every new game
     * 
     * @param beginner
     * @param intermediate
     * @param expert
     ******************************************************/


public void chooseGameDifficulty(int beginner, int intermediate, int expert){

    boolean entreeValide;
    int levelDifficulty;
    String reponse;
    levelDifficulty= levelDifficulty(beginner,expert); // call function levelDifficulty()

        if(levelDifficulty==beginner)
            //get easy level grid (getter)

            //set easy level grid (setter)


    if(levelDifficulty==intermediate)
        //get intermediate level grid  (getter)

            //set intermediate level grid (setter)


    if(levelDifficulty==expert)
        //get expert level grid (getter)

            //set easy expert grid (setter)
            }



    /****************************************************
     * Method solvePuzzle (resoudrePuzzle)
     * 
     * This method makes possible to solve the entire grid meaning all the 81 cells
     * 
     ******************************************************/


    public class solvePuzzle {
    }




    /****************************************************
     * Method makeMove (fairePlacement)
     * 
     * Save a record of the player's actions on the grid.
     * 
     * 
     * 
     * (1) make move on the grid ; 
     * (2) save moves in an array list
     * 
     ******************************************************/



    public class makeMove {




        //choose a cell , enter a number on the cell and confirm the selection



        // adds move to the array list
        int makeMove = gameMove++;
    }




     /****************************************************
     * Method cancelMove (annulerPlacement)
     * 
     * 
     * 
     * (1) retrieve the last instance in the arraylist (using the remove method and the size method to determine the number of elements )
     * (2) cancel the move in the grid.
     * 
     ******************************************************/


    public class cancelMove {


        //choose a cell , remove the number on the cell and confirm the cancellation



        //substracts move from array list
        int cancelMove = gameMove--;

    }






}
JavaNoob
  • 13
  • 3
  • 1
    Well, I'd start by getting rid of the static references – MadProgrammer Jan 08 '16 at 04:11
  • The model stores and manages the state if the data and contains the rules that determine how it's changed; the view represents the state of the model; the controller controls how the view and model interact; so from your code snippet, no, this isn't really what I'd consider a mvc – MadProgrammer Jan 08 '16 at 04:13
  • If this is suppose to be a gui based program, why do you have a Scanner for user input and why is it in the model? – MadProgrammer Jan 08 '16 at 04:16
  • Thanks for replying. I know how the concept of MVC works. I'm just a lil confused. Why you suggest getting rid of all the static references? I thought it was useful for sharing between all instances? – JavaNoob Jan 08 '16 at 04:18
  • 1
    And what happens when you have multiple instance of the model? Suddenly they ALL have the same data, so when you create 3 games of different difficulty settings and they all have the same data and are updated at the same time, is this really what you want? `static` is generally an sign of a bad design. `static` should never be used for cross object communication – MadProgrammer Jan 08 '16 at 04:22
  • @MadProgrammer Scanner is the only way I know how to get user input... I thought model was suppose to take care of the initial states and their methods? – JavaNoob Jan 08 '16 at 04:23
  • Yes, but that information comes from the controller or some other factory process – MadProgrammer Jan 08 '16 at 04:23
  • Whether or not you use a Scanner or other forms of user interactions, these user interactions have no business being in the model. The model is for logic only, and should have no dealings with UI -- that is all the view's business. – Hovercraft Full Of Eels Jan 08 '16 at 04:31
  • 1
    *"Scanner is the only way I know how to get user input."* Then what does this have to do with Swing? If you don't know how to get input from the user using Swing, then you have bigger problems to resolve then how to implement a MVC – MadProgrammer Jan 08 '16 at 04:32
  • @MadProgrammer: _ActionListener_ I know. I understand I will remove the scanner from the model. – JavaNoob Jan 08 '16 at 04:37

1 Answers1

3

initializeGrid and chooseGameDifficulty aren't really features of the model. The model maintains the current state of the data and the rules uses to manage it.

Technically, these features should be functions of some kind of factory that given a difficult level will return a instance of the model

public class SudokuFactory { 
    public enum Difficulty {
        HARD,
        MODERATE,
        EASY
    }

    public SudokuModel createModel(Difficulty difficult) {
        // Make a new model based on the rules for your difficulty
        // settings
    }
}

The model would then simply contain the information and functionality to manage it

You should also avoid static where practically possible, it should never be used as a cross class communication mechanism, if you need to share data, you should pass it. static just makes the whole thing a lot more difficult to manage and debug

The view would get the information from the user (like the difficulty level), which would be used by the controller to build a new model. The model would then be passed to a new controller, which would generate a new view, which should present the current state of the model.

The controller would then respond to changes in the view, updating the model and the controller would respond to changes in the model and update the view.

You should also prefer using interfaces over implementation

So, based on my (rather pathetic) understanding of Sudoku you could use a model as simple as ...

public interface SudokuModel {
    public void setValueAt(int value, int row, int col) throws IllegalArgumentException;
    public int getValueAt(int row, int col);
}

Now, me, personally, I'd have an implementation that had two buffers, one which represents the actual game/solution and one which represents the player data (pre-filled based on the difficulty level), now you could have a single buffer, but you'd have constantly scan the grid to see if the new value was valid and I'm just too lazy ;)

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366