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";


Wait! This isn't a Parachute, this is a Backpack!

In reply to 2D Arrays as a Gameboard by gregor42

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.