2014-12-05 14 views
-3

我正在做一個井字遊戲的Toc和我有一個方法返回從當前狀態的所有可能的行動。我試圖重新獲取一個對象並將其複製,然後將其更改並輸入到數組列表中。複製對象,而不chainging原來的 - java的

@Override 
public ArrayList<State> getAllPossibleMoves(State current) { 
    ArrayList<State> moves = new ArrayList<State>(); 
    for (int i=0; i<current.getState().length; i++){ 
     for (int j=0; j<current.getState().length; j++){ 
      if (current.getState(i, j) == '-'){ 
       char[][] tmp1; 
       tmp1 = current.getState().clone(); 
       tmp1[i][j] = 'O'; 
       TicTacToeState tmp2 = new TicTacToeState(tmp1); 
       tmp2.setState(tmp1); 
       moves.add(tmp2); 
       //current.setState(i, j, '-'); 
      } 
     } 
    } 
    return moves; 
} 

無論我嘗試什麼 - 我對tmp1對「當前」效果所做的任何更改。 我嘗試克隆()並複製構造函數。只是提到「國家」這個類是抽象的,「TicTacTicState」擴展了「國家」。

更多信息 - 這裏是類國家

公共抽象類國家實行了Cloneable {

protected char[][] state; 
protected int evaluation; 


@Override 
public Object clone() throws CloneNotSupportedException { 
    // TODO Auto-generated method stub 
    return super.clone(); 
} 

@Override 
public boolean equals(Object obj) { 
    return state.equals(((State)obj).getState()); 
    //return super.equals(obj); 
} 

public State(){ 

} 

public State(State s){ 
    this.state = s.state; 
    this.evaluation = s.evaluation; 
} 

public State(char[][] state){ 
    this.state = state; 
    this.evaluation = 0; 
} 

public State(char[][] state, int evaluation){ 
    this.state = state; 
    this.evaluation = evaluation; 

} 

public char[][] getState(){ 
    return this.state; 
} 

public char getState(int row, int column) { 
    return state[row][column]; 
} 


public void setState(char[][] state) { 
    this.state = state; 
} 

public void setState(int row, int col, char player){ 
    this.state[row][col] = player; 
} 
public abstract int getEvaluation(State state); 

public abstract boolean isStateFull(State current); //returns true is it's a "terminal node" 

public abstract ArrayList<State> getAllPossibleMoves(State current); 

}

+1

難道你不覺得這是一個好主意,還張貼你的類的代碼? – 2014-12-05 20:56:42

回答

0

你必須自己身體複製您的陣列。你可以用​​3210做到這一點:

import java.util.Arrays; 

public class ArrayCopy { 

    public static void main(String[] args) { 

     // set up an empty source array 
     char[][] src = new char[5][3]; 

     // fill it with random digits 
     for(int i = 0 ; i < src.length; i++) { 
      for(int j = 0 ; j < src[0].length; j++) { 
       src[i][j] = (char) (Math.random() * 10 + 48); 
      } 
     } 

     // show what it looks like 
     printArray(src); 

     // create an empty destination array with the same dimensions 
     char[][] dest = new char[src.length][src[0].length]; 

     // walk over array and copy subarrays using arraycopy 
     for (int i = 0; i < src.length; i++) { 
      System.arraycopy(src[i], 0, dest[i], 0, src[0].length); 
     } 

     // make a change to the copy 
     dest[0][0] = 'X'; 

     // the source array is still the same 
     printArray(src); 

     // hey presto! 
     printArray(dest); 
    } 

    private static void printArray(char[][] array) { 
     for(int i = 0 ; i < array.length; i++) { 
      System.out.println(Arrays.toString(array[i])); 
     } 
     System.out.println(); 
    } 
} 

輸出示例:

[5, 5, 9] 
[0, 4, 7] 
[4, 8, 6] 
[1, 5, 4] 
[3, 9, 3] 

[5, 5, 9] 
[0, 4, 7] 
[4, 8, 6] 
[1, 5, 4] 
[3, 9, 3] 

[X, 5, 9] 
[0, 4, 7] 
[4, 8, 6] 
[1, 5, 4] 
[3, 9, 3] 
+0

非常感謝,偉大工程 – 2014-12-06 13:28:39

+0

如果我回答你的問題,請通過點擊對號接受它。 – 2014-12-07 14:27:18

相關問題