Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Bryan Manhollan
PLUS
Bryan Manhollan
Courses Plus Student 9,666 Points

Could anyone recommend a way to write this code a bit cleaner?

So what I'm experiencing. I'm doing some coding games on the internet to help me get better with writing in various languages. And one of the challenges I'm attempting right now is a Code Size challenge. The goal is to have the lowest character length as possible at the end of your code and still be functional.

My code is listed below and is several times larger than what others on the site have placed. (me at 1028, top php code at 68).

Was wondering if anyone could help me simplify my code without losing functionality.

<?php
fscanf(STDIN, "%d %d %d %d",
    $LX,
    $LY,
    $TX,
    $TY
);
$lx=$LX;
$ly=$LY;
while (TRUE)
{
    fscanf(STDIN, "%d",
        $E
    );
    for ($i=0; $i < count($E); $i++){
        if ($lx<$TX and $ly==$TY){
            echo "W\n";
            $lx++;
        }
        elseif ($lx>$TX and $ly==$TY){
            echo "E\n";
            $lx=$lx-1;
        }
        elseif ($lx==$TX and $ly>$TY){
            echo "S\n";
            $ly++;
        }
        elseif ($lx==$TX and $ly<$TY){
            echo "N\n";
            $ly=$ly-1;
        }
        elseif ($lx<$TX and $ly>$TY){
            echo "SW\n";
            $lx++;
            $ly=$ly-1;
        }
        elseif ($lx>$TX and $ly>$TY){
            echo "SE\n";
            $lx=$lx-1;
            $ly=$ly-1;
        }
        elseif ($lx<$TX and $ly<$TY){
            echo "NW\n";
            $lx++;
            $ly++;
        }
        elseif ($lx>$TX and $ly<$TY){
            echo "NE\n";
            $lx=$lx-1;
            $ly++;
        }
    }
}
?>

What's the purpose of the code? It is hard to make out by reading the code, but I see there are cardinal points involved.

Bryan Manhollan
Bryan Manhollan
Courses Plus Student 9,666 Points

It takes inputs generated by the game that are coordinates on a game map ($LX, $LY, $TX, $TY). You then need to have the code guide a character around the map to a specific point. Start and endpoints can change. and $E is the amount of moves your allowed to use to get from point a to point b.

The code I have posted above works as is. But I'm looking for tips on how someone a bit more skilled than I am would go about organizing the code and making it more seamless but still functional.

What website offers this?

Ahhh - didn't read your comment with extra info - that sheds a little light on what is going on :) my code would need some tweeking then, as now it just keeps moving until til it reaches a constant target.

Bryan Manhollan
Bryan Manhollan
Courses Plus Student 9,666 Points

Yeah, sorry about the confusion. I should of outlined a bit more information in my original post. Both answer raise good points though.

While stripping down tabs/returns/spaces/etc. would cut down a bit, I'm not sure it would shed 900+ characters from the existing 1028 character code.

So my real question is, Is there a better way of structuring the code or phrasing the methods that would remove some redundancy? The creating methods for some of the repeated actions is a strong possibility, though I still don't think it would cut down character length to that extent.

I will probably reattempt this challenge later this weekend and use some of the tips supplied here to see how short I can get it. I'll provide any updates if there are any.

:)

2 Answers

Couple ways to reduce this code, first and easiest remove all returns, they count as a charater. Makes it hard to read but reduces it, and tabs.

One way I would is make methods for what your are doing, Take sw and w for example...

private void p{lx++;}

        if ($lx<$TX and $ly==$TY){
            echo "W\n";
            p();
        }

With this any redundant tasks can take 4 chars intead of 5. Also shorten your variables do a,b,c,d istead of lx,ly,tx,ty. That is killing off half of the chars everytime you call to it. You could also make a echo method might save a few more, but maybe.

Hehe intriguing! I should check that out some day - code size challenges...

Anyway - I looked through the code, and I am unsure of whether what you posted really is working as intended? The while loop is infinite, no?. And you make no use of $i in the for loop, so I'm not sure I quite understand how it all fits together...

Anyway I played around and came up with the following, which I am sure can be improved upon - since I don't know the specifics of the challenge, I can't write code that can be used as is - but I hope you get the idea of what I am trying to do. Otherwise, just ask. I kept the variable names that are 1-to-1 with the ones in your code,

Best of luck - it's fun :)

<?php
header("Content-Type: text/plain");

$d;
$x=['E','','W'];
$y=['N','','S'];

$TX = rand(0,1000);
$TY = rand(0,1000);

$lx = rand(0,1000);
$ly = rand(0,1000);

echo $TX.','.$TY."\n\n";
do {
    echo ($lx.','.$ly." ");
    $p=[p($lx,$TX),p($ly,$TX)];
    $lx+=$p[0];
    $ly+=$p[1];
    $d =$y[$p[1]+1].$x[$p[0]+1];
    echo ($d."\n");
} while ($d!=='');
echo 'DONE!';

function p ($a,$b) {  
    return ($a<$b)?1:(($a>$b)?-1:0);
}
?>