gregor42 has asked for the wisdom of the Perl Monks concerning the following question:
As a follow-up to a previous posting where I learned a LOT...
It has brought me to this logical point in my project. I'm working with very simple RPG games. What I'm looking to do is to use the 2D-array as the Gameboard. Within each array element is either going to be a Data Structure defined to store all the game data on that "square" or simply a Grid object. Frankly I haven't decided which is the best way to approach this & why.
The problem I have depends on the type of gameboard you aspire to. If I wanted a 'globe', with fixed dimensions & complete wraparound behavior then I would use a single 2D array. Since negative values are allowed this statement is legal:
print "$grid[-5][-1]";However, if I want instead to create a gameboard that is an infinite plane, that is another matter entirely. You would never want to call any array element by a negative value. Instead, I think, you will need 4 2Darrays, one each to accomidate all of the neg/pos quadrants on a typical geometric graph:
(x,y) -1,1 | 1,1 ----------+---------- <-- origin (0,0) -1,-1 | 1,-1
Also, it gets tricky when you ask for element 0,0. Which array should it exist in?
Now, when I did this in Java, I created a Gameboard object & a Column object & a series of Row objects & a series of Grid objects. Each one contained 2 vectors (scalable arrays) one for negative & one for positive. The Gameboard contained the Column object. The Column Object contained the Row instances, and the Rows contained Grid objects. At each step going down I had to make a conditional based on pos/neg value & then use the absolute value to perform the operations on the vector.
Now, that was a lot of work. I have reduced it to about 1% of the source code size by using PERL & asking for help from the Bretheren. What I want to know is: Is there a "Mo' Better" way to approach this:
I have taken out the ambiguity that was in the last version of the code I posted, hopefully this will hone down what I'm aiming at.use strict; my (@pp_grid, @pn_grid, @nn_grid, @np_grid, @input); #INPUT push @pp_grid, [ @input ] for @input; push @pn_grid, [ @input ] for @input; push @nn_grid, [ @input ] for @input; push @np_grid, [ @input ] for @input; @pp_grid = ( [ qw( a b c d ) ], [ qw( e f g h ) ], [ qw( i j k l ) ], [ qw( m n o p ) ], ); @pn_grid = ( [ qw( q r s t ) ], [ qw( u v w x ) ], [ qw( y z 1 2 ) ], [ qw( 3 4 5 6 ) ], ); @nn_grid = ( [ qw( A B C D ) ], [ qw( E F G H ) ], [ qw( I J K L ) ], [ qw( M N O P ) ], ); @np_grid = ( [ qw( Q S T U ) ], [ qw( V W X Y ) ], [ qw( Z 7 8 9 ) ], [ qw( 0 ? ! & ) ], ); #OUTPUT print "pp_grid:\n"; print "@$_\n" for @pp_grid; print "\n"; print "np_grid:\n"; print "@$_\n" for @np_grid; print "\n"; print "nn_grid:\n"; print "@$_\n" for @nn_grid; print "\n"; print "pn_grid:\n"; print "@$_\n" for @pn_grid; print " and here is 1,-1: $pn_grid[1][1]\n"; print " and here is 1,1: $pp_grid[1][1]\n"; print " and here is -1,1: $np_grid[1][1]\n"; print " and here is -1,-1: $nn_grid[1][1]\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 2D Arrays as a Gameboard
by tachyon (Chancellor) on Jun 06, 2001 at 22:25 UTC | |
by tye (Sage) on Jun 06, 2001 at 22:51 UTC | |
by tachyon (Chancellor) on Jun 06, 2001 at 23:26 UTC | |
by tye (Sage) on Jun 07, 2001 at 00:00 UTC | |
|
Re: 2D Arrays as a Gameboard
by MeowChow (Vicar) on Jun 06, 2001 at 23:27 UTC |