の定義:スタック参照変数
import java.awt.*;
public class GameController implements ActionListener {
private GameModel model;
private MyStack<DotInfo[][]> dots;
private int size;
/**
* Constructor used for initializing the controller. It creates the game's view
* and the game's model instances
*
* @param size
* the size of the board on which the game will be played
*/
public GameController(int size) {
this.size = size;
model = new GameModel(size);
dots = (MyStack<DotInfo[][]>) new DotInfo[size][size];
}
/**
* resets the game
*/
public void reset(){
model.reset();
}
/**
* Callback used when the user clicks a button (reset or quit)
*
* @param e
* the ActionEvent
*/
public void actionPerformed(ActionEvent e) {
}
/**
* <b>selectColor</b> is the method called when the user selects a new color.
* If that color is not the currently selected one, then it applies the logic
* of the game to capture possible locations. It then checks if the game
* is finished, and if so, congratulates the player, showing the number of
* moves, and gives two options: start a new game, or exit
* @param color
* the newly selected color
*/
public void selectColor(int color){
Stack<DotInfo[][]> newStack = new DotInfo[size][size];
for (int i=0;i<size;i++) {
for (int j=0;j<size;j++) {
if (model.dots[i][j].isCaptured()) {
dots.push(dots[i][j]);
}
}
}
while (model.getCurrentSelectedColor()!=color) {
color=model.setCurrentSelectedColor(color);
//for (int i=0;i<)
}
}
}
これは私のStack.javaクラスです:
public class MyStack<T> implements Stack<T> {
private T[][] array;
private int size;
public MyStack(int size) {
this.size = size;
array = (T[][])new Object[size][size];
}
public boolean isEmpty() {
return size==0;
}
public T peek() {
return array[size][size];
}
public T pop() {
T popped = null;
popped = array[size][size];
size--;
return popped;
}
public void push(T element) {
size++;
array[size][size]=element;
}
}
Iスタッククラスを正しい方法で定義したかどうかも疑問でした。ちょっとした助けに感謝します。
コードはコンパイルされますか? 'dots'はStackとして定義されています。各要素はDotInfoオブジェクトの2次元配列です。コンストラクタ内の' dots'の初期化は、Java 8とJavaの 'dots = new MyStack <>();'古いJavaの実装では 'dots = new MyStack(); –