我在GameController.java类中创建堆栈实例时遇到问题:
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;
}
}
我还想知道我是否以正确的方式定义了我的堆栈类?我们将不胜感激。
答案 0 :(得分:0)
堆栈无法正常工作,因为您需要在构造函数中将内部位置字段设置为0,而不是参数size
。
另外[size] [size]是无用的,它总是堆叠对象中的对角线,只需使用[size](1维数组)作为堆栈,下面是T类型的工作堆栈:
public class MyStack<T> implements Stack<T> {
private Object[] array;
private int size, int position;
public MyStack(int size) {
this.size = size;
position = 0;
array = new Object[size];
}
public boolean isEmpty() {
return position < 1;
}
public T peek() {
if (isEmpty())
throw new RuntimeException("Stack is empty");
return (T)array[position];
}
public T pop() {
if (isEmpty())
throw new RuntimeException("Stack is empty");
return array[position--];
}
public void push(T element) {
if (position >= size - 1)
throw new RuntimeException("Stack is full");
array[++position]=element;
}
}
此外,对于棋盘游戏,您可以使用堆栈,只需使用二维数组,如:
public class Dot {
//whatever information you need about a dot here
//As example just say Hello
public void sayHello() {
System.out.println("Hello");
}
}
并将其用作2D阵列:
//this are chess board dimensions (8 x 8)
public static final int SIZE_X = 8;
public static final int SIZE_Y = 8;
//create the board, made of Dot instances
Dot[][] board = new Dot[SIZE_X][SIZE_Y];
查找并使用给定的Dot:
//get reference of dot at position x, y
Dot dot = board[x][y];
//check if it exists, and if it does, make it sayHello()
if (dot != null) {
dot.sayHello();
}