Comparing adjacent items on a grid. Best way to do this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LittleZephyr
Forum Newbie
Posts: 4
Joined: Mon Jun 23, 2003 7:02 pm
Location: http://lzlh.nillahood.net

Comparing adjacent items on a grid. Best way to do this?

Post by LittleZephyr »

I'm writing a program that's similar to the card game in the game Final Fantasy 9. The game is played on a 4x4 grid with 0-6 blocks randomly chosen where you cannot place a card. The game goes back and forth between two player which play at card at each turn. Each of the cards can have a point on each of the sides and corner. If you place a card next to one of your opponent's cards, and a point on your card touches a side or corner of the opponent's card that doesn't have a point, you capture that card. If your card is placed so that an empty corner or side is touching an already placed oppenant's card's point, nothing happens.

My question is how should I work the placing of cards and the checking of points adjecent cards points?

Currently I have it so the grid is an array like this:

Code: Select all

<?php
$map = array (
		array ( 0, 0, 0, 0 ),
		array ( 0, 0, 0, 0 ),
		array ( 0, 0, 0, 0 ),
		array ( 0, 0, 0, 0 ),
	     );
?>
And the with each on of the 0's representing a block on the grid. As mentioned before, the zero turns to a 1 if it has been randomly chosen to be an unusable block.

The data of the cards is stored in an array like this (all we really care about for this problem are the last 8 keys):

Code: Select all

<?php
$card1 = array ( name => "Monsieus",
			type => "fire",
			age => 2,
			attack => 2,
			defence => 1,
			ul => 0,
			u => 1,
			ur => 0,
			l => 1,
			r => 1,
			bl => 0,
			b => 1,
			br => 0 );
?>
Those last 8 keys are the places for the points, each on stand for a place where a point can be (ul=upper left corner, b=bottom, etc...), 0 if there's no point, 1 if there is.

So the first problem is I don't know how to assign a card to a spot on the grid. should I just put that array into the map's array and make it 3 dimentional? Can I just say like

Code: Select all

<?php
$map[0][0] = $card1
?>
?

And I have no clue on how I would check for points. Any suggestions would be appriated, no code is needed, just kinda suggestions on how I should approach this problem.
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Ok, it seems to me that you want a seperate $cards array. You don't want to overwrite what's in the $map array, nor do you want to make it three dimensional. Your $cards array should be a 2-D map of the cards that have been placed. Now, when a user places a card at point (x,y), you run a function placeCard(x, y) which checks cards $cards[x+1][y], $cards[x-1][y], $cards[x][y+1] and $cards[x][y-1] to see if they are removed.
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Don't forget to clamp [x] and [y] to the range 0-3, or you'll get some potentially weird happening (in C, at least, dunno about PHP, but it's good practive just incase ;))
Post Reply