java - Proper way to deep copy a multi-dimensional array of objects with varying classes -


i implementing stratego ai in java. 1 of things ai needs do, @ possible future moves. this, should able copy of current board position, move pieces around there (without influencing main board, of course), , pick best move looking multiple moves ahead (using minimax algorithm).

however, having trouble copying board part. board stored two-dimensional array of strategopieces:

strategopiece board[][]; 

now, pieces in stratego have special rules. i've created special classes spy, scout, miner, flag , bomb, inherit strategopiece class.

so, board contains 'null' empty square, , object child class of strategopiece if occupied.

i want copy on board, cannot figure out how make proper deep copy. reading on stackoverflow , other places on internet, came across following pieces of information:

  • to make deep copy of (non-primitive) array (in 1 or more dimensions), 1 needs copy on objects one-by-one.
  • object.clone() not work people want, , implementing difficult.
  • it advised create 'factory' or constructor copy method instead.

what wondering about, however, how can create such factory or constructor copy method when i'm having array of objects multiple classes (that inherit same base class, have logic attached).

any appreciated.

deep copying complex objects don't want implement on own, because it's hard correctly , there frameworks solve problem you.

you dozer or kryo, example.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -