2014-12-05 18 views
-3

저는 Tic-Tac-Toc 게임을하고 있고, 현재 상태에서 가능한 모든 동작을 반환하는 메서드가 있습니다. 나는 개체를 recive 및 복사, 그것을 변경하고 배열 목록에 그것을 입력하려고합니다.원본을 chainging하지 않고 개체를 복사하십시오. -

@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 효과에 대한 모든 변경을 수행합니다. try() 및 복사 생성자를 복제했습니다. "State"클래스가 추상 클래스이고 "TicTacTicState"클래스가 "State"클래스라는 것을 언급하기 만하면됩니다. 자세한 내용은

는 - 여기

당신은이
+1

당신이 또한 클래스의 코드를 게시 할 수있는 좋은 아이디어라고 생각하지를? –

답변

0

물리적 배열을 직접 복사

공공 추상 클래스 상태이 복제 {

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); 

}를 구현하는 클래스 상태입니다. 당신은 java.lang.System.arraycopy()으로이 작업을 수행 할 수 있습니다

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

고마워, 잘 했어 –

+0

내가 당신의 질문에 답했다면, 체크 표시를 눌러서 그것을 수락하십시오. –