【发布时间】:2016-01-08 04:08:43
【问题描述】:
我正在构建一个数独游戏。我来这里是为了寻求帮助,因为我完全陷入了我的代码中。我不是要你完成我的代码,我知道那不是你的工作。只需几个提示,下一步该做什么就太好了!
我为 GUI 使用 MVC 和 Swing 组件以使代码更轻。我划分了每个领域和方法,所以我可以理解下一步该做什么,但我很困惑。我特别难以理解如何执行以下方法:
初始化网格
选择游戏难度
makeMove
取消移动
型号
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--;
}
}
【问题讨论】:
-
好吧,我先摆脱静态引用
-
模型存储和管理数据的状态,并包含确定如何更改的规则;视图表示模型的状态;控制器控制视图和模型如何交互;所以从你的代码 sn-p,不,这不是我真正认为的 mvc
-
如果假设这是一个基于 gui 的程序,为什么你有一个用于用户输入的扫描仪?为什么它在模型中?
-
当你有多个模型实例时会发生什么?突然间他们都拥有相同的数据,所以当你创建3个不同难度设置的游戏时,它们都拥有相同的数据并同时更新,这真的是你想要的吗?
static通常是糟糕设计的标志。static永远不应该用于跨对象通信 -
“扫描仪是我知道如何获取用户输入的唯一方法。” 那么这与 Swing 有什么关系呢?如果你不知道如何使用 Swing 从用户那里获取输入,那么你有更大的问题需要解决,那么如何实现 MVC
标签: java swing netbeans-8 sudoku