2017-02-23 61 views
-1

我有一個問題,同時在類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

您的代碼是否編譯?由於'dots'被定義爲Stack,其中每個元素都是DotInfo對象的二維數組,所以在構造函數中初始化'dots'應該是Java 8中的'dots = new MyStack <>();' 'dots = new MyStack ();'在較老的Java實現中。 –

回答

0

堆棧無法正常工作,因爲您需要在構造函數中將內部位置字段設置爲0,而不是參數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"); 
    } 
} 

並以此爲二維數組:

//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]; 

查找並使用給定的點:

//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(); 
} 
+0

我必須使用一個堆棧陣列它的任務 –

+0

然後,我將如何初始化我的堆棧在我的GameController.java類 –

+0

只是使用stackArr = new MyStack [size]; – CrowsNet