首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这种MVC方法是编写数独游戏的好方法吗?

这种MVC方法是编写数独游戏的好方法吗?
EN

Stack Overflow用户
提问于 2016-01-08 04:08:43
回答 1查看 525关注 0票数 1

我在建一个数独游戏。我来这里是为了寻求帮助,因为我完全被我的代码卡住了。我不是要你完成我的代码,我知道这不是你的工作。只是几个提示,作为下一步做什么将是很棒的!

我使用MVC和Swing组件的GUI使代码更轻松。我对每个领域和方法进行了划分,这样我就能理解下一步该做什么,但我很困惑。我特别难以理解如何执行以下方法:

  • initializeGrid
  • chooseGameDifficulty
  • makeMove
  • cancelMove

模型

代码语言:javascript
复制
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--;

    }






}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-08 04:30:45

initializeGridchooseGameDifficulty并不是模型的真正特性。模型维护数据的当前状态,规则用于管理数据。

从技术上讲,这些特性应该是某些工厂的函数,如果给出了一个困难的级别,就会返回模型的一个实例。

代码语言:javascript
复制
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
    }
}

然后,模型将简单地包含用于管理它的信息和功能。

在实际可行的情况下,您还应该避免使用static,它永远不应该用作跨类通信机制,如果您需要共享数据,则应该传递它。static只会使整件事情更难管理和调试。

视图将从用户那里获得信息(比如难度级别),控制器将使用这些信息来构建新的模型。然后将模型传递给一个新的控制器,该控制器将生成一个新的视图,该视图应该显示模型的当前状态。

然后控制器将响应视图中的更改,更新模型,控制器将响应模型中的更改并更新视图。

您也应该更喜欢使用interfaces over implementation

所以,根据我对数独(相当可怜)的理解,你可以使用一个简单的模型,就像.

代码语言:javascript
复制
public interface SudokuModel {
    public void setValueAt(int value, int row, int col) throws IllegalArgumentException;
    public int getValueAt(int row, int col);
}

现在,我个人认为,我有一个有两个缓冲区的实现,一个表示实际的游戏/解决方案,另一个表示玩家数据(根据难度级别预先填充),现在您可以有一个缓冲区,但是您必须不断地扫描网格,看看新值是否有效,我太懒了;

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34669335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档