clone - copy an object without chainging the original one - java -


i'm doing tic-tac-toc game , have method returns possible moves current state. i'm trying recive object , copy it, change , enter array list.

@override public arraylist<state> getallpossiblemoves(state current) {     arraylist<state> moves = new arraylist<state>();     (int i=0; i<current.getstate().length; i++){         (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; } 

no matter try - change on tmp1 effects on "current". tryed clone() , copy constructor. mention class "state" abstract , "tictacticstate" extends "state".

for more information - here class state

public abstract class state implements 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 it's "terminal node"  public abstract arraylist<state> getallpossiblemoves(state current); 

}

you have physically copy array yourself. can java.lang.system.arraycopy():

import java.util.arrays;  public class arraycopy {      public static void main(string[] args) {          // set empty source array         char[][] src = new char[5][3];          // fill random digits         for(int = 0 ; < src.length; i++) {             for(int j = 0 ; j < src[0].length; j++) {                 src[i][j] = (char) (math.random() * 10 + 48);             }         }          // show looks         printarray(src);          // create empty destination array same dimensions         char[][] dest = new char[src.length][src[0].length];          // walk on array , copy subarrays using arraycopy         (int = 0; < src.length; i++) {             system.arraycopy(src[i], 0, dest[i], 0, src[0].length);         }          // make change copy         dest[0][0] = 'x';          // source array still same         printarray(src);          // hey presto!         printarray(dest);     }      private static void printarray(char[][] array) {         for(int = 0 ; < array.length; i++) {             system.out.println(arrays.tostring(array[i]));         }         system.out.println();     } }    

example output:

[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] 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -