这是主要课程:
public class Sudoku extends JFrame {
MenuBar Bar;
MenuItem itemFile[];
MenuItem itemHelp[];
Menu file;
Menu help;
public Sudoku() {
super("Sudoku");
setLayout(new FlowLayout());
setResizable(false);
itemFile =new MenuItem[2];
itemHelp =new MenuItem[2];
itemFile[0]=new MenuItem("New");
itemFile[1]=new MenuItem("Exit");
itemHelp[0]= new MenuItem("Rules");
itemHelp[1]= new MenuItem("Creators");
file=new Menu("File");
help=new Menu("Help");
file.add(itemFile[0]);
help.add(itemHelp[0]);
file.add(itemFile[1]);
help.add(itemHelp[1]);
Bar =new MenuBar();
Bar.add(file);
Bar.add(help);
setMenuBar(Bar);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(new BorderLayout());
Game game = new Game();
ButtonController buttonController = new ButtonController(game);
ButtonPanel buttonPanel = new ButtonPanel();
buttonPanel.setController(buttonController);
add(buttonPanel, BorderLayout.EAST);
SudokuPanel sudokuPanel = new SudokuPanel();
SudokuController sudokuController = new SudokuController(sudokuPanel, game);
sudokuPanel.setGame(game);
sudokuPanel.setController(sudokuController);
add(sudokuPanel, BorderLayout.CENTER);
game.addObserver(buttonPanel);
game.addObserver(sudokuPanel);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public boolean action(Event evt, Object arg){
if(evt.target instanceof MenuItem){
String text=(String)arg;
System.out.println("Selected="+arg);
if(text.equals("Exit"))
System.exit(0);
else if(text.equals("New"))
// Get newGame() of Game class to run here.
}
return true;
}
这是游戏类:
package sudoku.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
public class Game extends Observable {
private int[][] solution;
private int[][] game;
private boolean[][] check;
private int selectedNumber;
private boolean help;
public Game() {
newGame();
check = new boolean[9][9];
help = true;
}
public void newGame() {
solution = generateSolution(new int[9][9], 0);
game = generateGame(copy(solution));
setChanged();
notifyObservers(UpdateAction.NEW_GAME);
}
public void checkGame() {
selectedNumber = 0;
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
check[y][x] = game[y][x] == solution[y][x];
}
setChanged();
notifyObservers(UpdateAction.CHECK);
}
public void setHelp(boolean help) {
this.help = help;
setChanged();
notifyObservers(UpdateAction.HELP);
}
public void setSelectedNumber(int selectedNumber) {
this.selectedNumber = selectedNumber;
setChanged();
notifyObservers(UpdateAction.SELECTED_NUMBER);
}
public int getSelectedNumber() {
return selectedNumber;
}
public boolean isHelp() {
return help;
}
public boolean isSelectedNumberCandidate(int x, int y) {
return game[y][x] == 0 && isPossibleX(game, y, selectedNumber)
&& isPossibleY(game, x, selectedNumber) && isPossibleBlock(game, x, y, selectedNumber);
}
public void setNumber(int x, int y, int number) {
game[y][x] = number;
}
public int getNumber(int x, int y) {
return game[y][x];
}
public boolean isCheckValid(int x, int y) {
return check[y][x];
}
private boolean isPossibleX(int[][] game, int y, int number) {
for (int x = 0; x < 9; x++) {
if (game[y][x] == number)
return false;
}
return true;
}
private boolean isPossibleY(int[][] game, int x, int number) {
for (int y = 0; y < 9; y++) {
if (game[y][x] == number)
return false;
}
return true;
}
private boolean isPossibleBlock(int[][] game, int x, int y, int number) {
int x1 = x < 3 ? 0 : x < 6 ? 3 : 6;
int y1 = y < 3 ? 0 : y < 6 ? 3 : 6;
for (int yy = y1; yy < y1 + 3; yy++) {
for (int xx = x1; xx < x1 + 3; xx++) {
if (game[yy][xx] == number)
return false;
}
}
return true;
}
private int getNextPossibleNumber(int[][] game, int x, int y, List<Integer> numbers) {
while (numbers.size() > 0) {
int number = numbers.remove(0);
if (isPossibleX(game, y, number) && isPossibleY(game, x, number) && isPossibleBlock(game, x, y, number))
return number;
}
return -1;
}
private int[][] generateSolution(int[][] game, int index) {
if (index > 80)
return game;
int x = index % 9;
int y = index / 9;
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 9; i++) numbers.add(i);
Collections.shuffle(numbers);
while (numbers.size() > 0) {
int number = getNextPossibleNumber(game, x, y, numbers);
if (number == -1)
return null;
game[y][x] = number;
int[][] tmpGame = generateSolution(game, index + 1);
if (tmpGame != null)
return tmpGame;
game[y][x] = 0;
}
return null;
}
private int[][] generateGame(int[][] game) {
List<Integer> positions = new ArrayList<Integer>();
for (int i = 0; i < 81; i++)
positions.add(i);
Collections.shuffle(positions);
return generateGame(game, positions);
}
private int[][] generateGame(int[][] game, List<Integer> positions) {
while (positions.size() > 0) {
int position = positions.remove(0);
int x = position % 9;
int y = position / 9;
int temp = game[y][x];
game[y][x] = 0;
if (!isValid(game))
game[y][x] = temp;
}
return game;
}
private boolean isValid(int[][] game) {
return isValid(game, 0, new int[] { 0 });
}
private boolean isValid(int[][] game, int index, int[] numberOfSolutions) {
if (index > 80)
return ++numberOfSolutions[0] == 1;
int x = index % 9;
int y = index / 9;
if (game[y][x] == 0) {
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 9; i++)
numbers.add(i);
while (numbers.size() > 0) {
int number = getNextPossibleNumber(game, x, y, numbers);
if (number == -1)
break;
game[y][x] = number;
if (!isValid(game, index + 1, numberOfSolutions)) {
game[y][x] = 0;
return false;
}
game[y][x] = 0;
}
} else if (!isValid(game, index + 1, numberOfSolutions))
return false;
return true;
}
private int[][] copy(int[][] game) {
int[][] copy = new int[9][9];
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++)
copy[y][x] = game[y][x];
}
return copy;
}
}
我想从我的菜单栏中点击“新建”(其操作在主类中),newGame();要执行的Game类的方法。我尝试了很多方法,但都失败了......真是太头疼......
有什么想法吗?
答案 0 :(得分:0)
在构造函数中,您创建了Game对象的实例 - 但它在方法结束时超出了范围。你应该在Sudoku类中保留一个引用:
public class Sudoku extends JFrame {
...
private Game game;
public Sudoku() {
...
game = new Game();
然后:
public boolean action(Event evt, Object arg) {
if(evt.target instanceof MenuItem) {
String text=(String)arg;
System.out.println("Selected="+arg);
if(text.equals("Exit")) {
System.exit(0);
} else if(text.equals("New")) {
game.newGame();
}
}
return true;
}
答案 1 :(得分:0)
添加“Game”对象作为主类的成员类,然后您可以在班级的任何位置访问其方法:
public class Sudoku extends JFrame {
private Game game;
// rest of your Sudoku class code as it currently is, but change:
Game game = new Game();
// to:
this.game = new Game();
// In your 'action' method, you can now call like this:
this.game.newGame();