I don't see anything wrong after a (very) cursory glance.

One red flag, though, is that you are making your variables public. This
means there could be code elsewhere that sets them to be identical to
each other. Because of issues like this, I think it's a good practice
never to make variables public (except in some cases, like static
pseudo-constants in AS2.0; but AS3.0 has real constants, so this problem
goes away). There are two things you could do with an array: 1) make it
read-only, offering a "defensive copy" with a getter; 2) provide methods
for accessing and updating its data:

1)

public function get board():Array {
        return _board.concat();
}
private var _board:Array;


2)

public function getBoardStatus(x:Number; y:Number):Number {
        return _board[x][y];
}
public function setBoardStatus(x:Number; y:Number, value:Number):Void {
        _board[x][y] = value;
}

Actually, looking at it further, Board should probably be a class
composed by Connect4State. Oh, damn, and I realized why you are having
that problem! See copy function below (it's because you are copying an
array of arrays):

class connect4.BoardStatus {
        private function BoardStatus() {
        }
        public static var BLACK:BoardStatus = new BoardStatus();
        public static var EMPTY:BoardStatus = new BoardStatus();
        public static var RED:BoardStatus = new BoardStatus();
}
class connect4.Board {
        public function Board(source:Board) {
                if (source instanceof Board) {
                        copy(source);
                } else {
                        // Default initialization;
                }
        }
        public function copy(source:Board):Void {
                _status = new Array();
                for (var i:Number = 0; i < source._status.length; ++i) {
                        _status[i] = source._status[i].concat();
                }
        }
        public function getStatus(row:Number, col:Number):BoardStatus {
                return _status[row][col];
        }
        public function setStatus(row:Number, col:Number,
                                value:BoardStatus):Void {
                _status[row][col] = value;
        }
        private var _status:Array;
}
class connect4.GameState {
        public function GameState(source:GameState) {
                if (source instanceof GameState) {
                        copy(source);
                } else {
                        _board = new Board();
                        // Initialize other fields.
                }
        }
        public function get board():Board {
                return _board;
        }
        // Other properties.
        public function copy(source:GameState):Void {
                _board = new Board(source._board);
                // Copy other fields.
        }
        private var _board:Board;
        // Other private fields.
}

―
Mike Keesey

> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:flashcoders-
> [EMAIL PROTECTED] On Behalf Of Paul Steven
> Sent: Friday, November 03, 2006 10:44 AM
> To: 'Flashcoders mailing list'
> Subject: RE: [Flashcoders] Copy Constructor
> 
> I think the problem may be that I am recursively creating the
copies.....
> 
> Here are the two classes in full if anyone would be kind enough to see
if
> they can help. Been on this all day and have made no progress
> 
> http://www.mediakitchen.co.uk/Connect4Engine.as
> http://www.mediakitchen.co.uk/Connect4State.as
> 
> Thanks
> 
> Paul
> 
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Paul
> Steven
> Sent: 03 November 2006 17:55
> To: 'Flashcoders mailing list'
> Subject: RE: [Flashcoders] Copy Constructor
> 
> Seem to be having problems with this still.
> 
> It seems like every time I change a value in the main constructor
objects
> array, it also changes the array values in the copies.
> 
> Are these copies essentially just pointers to the same array?
> 
> Basically the reason I need copies is so it can simulate future moves
in
> the
> game by updating a copy of the game state arrays.
> 
> So essentially I need to create copies of these arrays that I can
> manipulate
> without these manipulations affecting the arrays they were copied
from.
> 
> I hope that makes sense
> 
> Thanks
> 
> Paul
> 
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On Behalf Of Mike
> Keesey
> Sent: 02 November 2006 22:53
> To: 'Flashcoders mailing list'
> Subject: RE: [Flashcoders] Copy Constructor
> 
> I might do it like this:
> 
> class mypackage.Connect4State extends Object {
>       public function Connect4State(state:Connect4State) {
>               super();
>               if (state instanceof Connect4State) {
>                       copy(state);
>               } else {
>                       board = new Array();
>                       score = new Array();
>                       // Default initialization code.
>               }
>       }
>       public function copy(state:Connect4State):Void {
>               board = state.board.concat();
>               score = state.score.concat();
>       }
>       public var board:Array;
>       public var score:Array;
> }
> 
> Then you could call the construct with or without a parameter.
> 
> (Actually, I would never use public variables, but that's another
> topic....)
> ―
> Mike Keesey
> 
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:flashcoders-
> > [EMAIL PROTECTED] On Behalf Of Paul Steven
> > Sent: Thursday, November 02, 2006 2:31 PM
> > To: 'Flashcoders mailing list'
> > Subject: RE: [Flashcoders] Copy Constructor
> >
> > Perhaps it is easier if I explain what I need:
> >
> > I am rewriting some Java code into AS2.
> >
> > Basically I have a class called Connect4State
> >
> > class Connect4State {
> >
> >     public function Connect4State() {
> >
> >             //  -------------------------
> >             // Initialize the board array
> >             // --------------------------
> >
> >             board = new Array();
> >
> >             code goes here
> >
> >
> >             // --------------------------
> >             // Initialize the score array
> >             // --------------------------
> >
> >             score = new Array();
> >
> >             code goes  here
> >     }
> >
> >     public var board:Array; // A 7 by 6 two dimensional array of
> > integers representing the state of game
> >
> >     public var score:Array; // A 2 dimensional array of integers
> > representing the "score" for the players
> >
> > }
> >
> > The Java code has a function called a "Copy Constructor" that
enables
> you
> > to
> > create new objects that are copies of existing objects. The copy
> > constructor
> > for Connect4State just copies the contents of each member variable.
It
> is
> > necessary to have a copy constructor for Connect4State because the
AI
> > algorithms use temporary state objects a great deal
> >
> > This is the "copy constructor" code translated to AS2
> >
> >     public function Connect4State(state:Connect4State) {
> >
> >             // --------------
> >             // Copy the board
> >             // --------------
> >
> >             board = new Array();
> >
> >             for (var i:Number = 0; i < 7; i++) {
> >
> >                     board[i] = new Array();
> >
> >                             for (j:Number = 0; j < 6; j++) {
> >
> >                                     board[i][j] = state.board[i][j];
> >
> >                     }
> >
> >             }
> >
> >
> >             // ---------------
> >             // Copy the scores
> >             // ---------------
> >
> >             for (var i:Number = 0; i < 2; i++) {
> >
> >                     score = new Array();
> >
> >                             for (var j:Number = 0; j < winPlaces;
j++) {
> >
> >                             score[i][j] = state.score[i][j];
> >                             numPieces = state.numPieces;
> >                     }
> >
> >             }
> >
> >
> >     }
> >
> > AS2 seems not to like this as I get the following error
> >
> > A class must have only one constructor.
> >             public function Connect4State(state:Connect4State)
> >
> > Anyone suggest an alternative to this "Copy Constructor"?
> >
> >
> >
> >
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
> > [mailto:[EMAIL PROTECTED] On Behalf Of Paul
> > Steven
> > Sent: 02 November 2006 20:01
> > To: 'Flashcoders mailing list'
> > Subject: [Flashcoders] Copy Constructor
> >
> > Hi there
> >
> > Is there an equivalent of the Java Copy constructor in Flash AS2?
> >
> > Thanks
> >
> > Paul
> >
> > _______________________________________________
> > Flashcoders@chattyfig.figleaf.com
> > To change your subscription options or search the archive:
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > Brought to you by Fig Leaf Software
> > Premier Authorized Adobe Consulting and Training
> > http://www.figleaf.com
> > http://training.figleaf.com
> >
> > _______________________________________________
> > Flashcoders@chattyfig.figleaf.com
> > To change your subscription options or search the archive:
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > Brought to you by Fig Leaf Software
> > Premier Authorized Adobe Consulting and Training
> > http://www.figleaf.com
> > http://training.figleaf.com
> 
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
> 
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
> 
> _______________________________________________
> Flashcoders@chattyfig.figleaf.com
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> 
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com

_______________________________________________
Flashcoders@chattyfig.figleaf.com
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com

Reply via email to