0

I am implementing a Stratego AI in Java. One of the things the AI needs to do, is to look at possible future moves. For this, it should be able to get a copy of the current board position, move pieces around there (without influencing the main board, of course), and pick the best move by looking multiple moves ahead (using the MiniMax algorithm).

However, I am having trouble with the copying the board part. My board is stored as a two-dimensional array of StrategoPieces:

StrategoPiece board[][];

Now, some pieces in Stratego have special rules. I've created special classes for the Spy, the Scout, the Miner, the Flag and the Bomb, that all inherit from the StrategoPiece class.

So, the board contains 'null' for an empty square, and an object that is a child class of StrategoPiece if it is occupied.

I want to copy over this board, but I cannot figure out how to make a proper deep copy. Reading on StackOverflow and other places on the Internet, I came across the following pieces of information:

  • To make a deep copy of a (non-primitive) array (in one or more dimensions), one needs to copy over all objects one-by-one.
  • Object.clone() does not work as most people want, and implementing this properly is difficult.
  • It is advised to create a 'Factory' or Constructor Copy method instead.

What I am wondering about, however, is how I can create such a Factory or Constructor Copy method when I'm having an array of objects with multiple classes (that all inherit from the same base class, but some have extra logic attached).

Any help would be appreciated.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
Qqwy
  • 5,214
  • 5
  • 42
  • 83
  • 1
    Why do you need to copy the objects? Are they mutable? Why? – Louis Wasserman May 30 '15 at 20:59
  • Do you actually have an actual need to do such a "deep copy"? Your question doesn't mention that you really need to do so – fge May 30 '15 at 20:59
  • If the individual items are _immutable_ then the deep copy only needs to account for the 2D array and preserve the same object references within and therefore their types/behaviors. For the array part, see: [How do I do a deep copy of a 2d array in Java?](http://stackoverflow.com/questions/1564832/how-do-i-do-a-deep-copy-of-a-2d-array-in-java) – William Price May 30 '15 at 21:00
  • @LouisWasserman and fge I think so... I want to try out other board positions, which would involve moving pieces around (e.g. changing the pieces themselves), and removing some of them when they are attacked. Doing these operations on the live board changes the graphical part of the program as well. Therefore my need to make copies. – Qqwy May 30 '15 at 21:25
  • It's not clear to me what part of that means you can't use immutable representations of the pieces. Copying the board is fine, but it's not clear why you need to copy the piece objects. – Louis Wasserman May 30 '15 at 21:41
  • @LouisWasserman Unlike other games such as Chess, in Stratego there are properties the pieces have, such as if the opponent has seen the value of the piece already or not. These might change when a move is made. Also, pieces are graphically animated when moving by increasing their screen positions using an easing function. – Qqwy May 30 '15 at 22:38
  • 1
    You should almost certainly separate the logical board (current state, future moves) from the on-screen representation (e.g. pixel coordinates, animation); that would eliminate the animation argument. You can still make state changes _after_ the deep copy _and_ use immutable objects **if** the state changes replace one immutable object with another; at least the unchanged items would remain and the deep copy is still limited to just the arrays. – William Price May 30 '15 at 23:17

1 Answers1

2

Deep copying complex objects is something you don't want to implement on your own, because it's hard to do correctly and there are good frameworks that solve the problem for you.

You could look into Dozer or Kryo, for example.

Mick Mnemonic
  • 7,808
  • 2
  • 26
  • 30