Jump to content

Can't get my head around


almightyegg

Recommended Posts

Each player has a plot of land. Then each player can plant 5 different crops at one time.

I've used GD library to draw the image of the land which works fine.

my table is like this:

id

code1

start1

fin1

time1

code2

start2

fin2

time2

code3

start3

fin3

time3

code4

start4

fin4

time4

code5

start5

fin5

time5

 

The codes are the codes of items that they have planted, time is the time of planting, start is on which square of the plot of land the crop starts and fin is on which square the plot of land the crop ends.

 

 

My problem is this, I'm not sure how to find out which squares aren't in use using the start and finish columns. I just can't get my head around it, any ideas would be greatly received

Link to comment
Share on other sites

Nothing to do with your actual issue, but you really need to look into database normalization. Theres a link in my signiture to a free php book/wiki. Within that is an entire chapter devoted to the subject.

Link to comment
Share on other sites

as thorpe suggested use normalization

seperate the plots of land into there own db.

 

TABLE (

userid

position

crop

end_time

) KEY `userplot` (userid,position)

 

 

than ya can grab userid and position from the table. and build a positional array.

 

$res=mysql_query("SELECT position WHERE userid=$uid")
while($row=mysql_fetch_row($res))
{
    $plots[$row[0]]=true;
}

 

notice that the position of the crop is not a value. thus making a simple check if a plot is used by

 

if(isset($plots[5])) 
   echo "Plot 5 is used";
else
   echo "Plot 5 is not used";

 

 

Link to comment
Share on other sites

Right, with the whole finding out what's in use thing:

 

Your land is 15 by 15 squares.

and the crops are planted as such:

1  2  3  4  5  6  7  8  9  10 11 12 13 14 15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

31 32 33 ... etc...

 

crop1 = somevalue

start1 = 1

fin1 = 5

 

That means that land squares 1 to 5 are in use.

crop2 = somevalue

start3 = 6

fin4 = 19

That means that land squares 6 - 19 are in use

 

From this I need to find out which squares aren't in use

 

And I'll look into this normalizing thing

 

Link to comment
Share on other sites

Right I'll explain it in more detail because I think I've confused you on exactly what I want...

 

This is an example table row:

id = 1

code1 = mugh

start1 = 1

fin1 = 25

code2 = fevh

start2 = 26

fin2 = 144

code3 = wolh

start3 = 156

fin3 = 200

code4 = NULL

time4 = NULL

start4 = NULL

fin4 = NULL

code5 = wilh

start5 = 224

fin5 = 225

 

Crop 1 has squares 1-25 filled - meaning 25 squares in total.

Crop 2 has squares 26-144 filled - meaning 119 squares in total.

Crop 3 has squares 156-200 filled - meaning 45 squares in total.

Crop 4 - none.

Crop 5 has squares 224-225 filled - meaning 2 squares in total.

 

Now from this I can find out the total amount of free squares fine.

What I want to do is find how many gaps there are and how big they are. From the above example it would find 2 gaps:

$gap1 = 11;

$gap2 = 23;

 

Can anyone think of a way to  do this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.