[Challenge - Beginner] Draw a chess field

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

mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: [Challenge - Beginner] Draw a chess field

Post by mattpointblank »

Ollie Saunders wrote:The shortest solution I can come up with:

Code: Select all

for ($w = 8, $i = 0; $i < $w * $w; ++$i) { 
    if ($i % $w === 0 && $i) { echo "\n"; } 
    echo ($i % 2 === 0 xor $i / $w % 2 === 0) ? '?' : ' '; }
And the first time I've ever used the xor operator. I knew it had to be useful for something!
Epic. I'm not well versed enough in PHP: what does '$i % $w' mean? And what's the syntactic advantage of ++$i rather than $i++?
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: [Challenge - Beginner] Draw a chess field

Post by flying_circus »

mattpointblank wrote:
Ollie Saunders wrote:The shortest solution I can come up with:

Code: Select all

for ($w = 8, $i = 0; $i < $w * $w; ++$i) { 
    if ($i % $w === 0 && $i) { echo "\n"; } 
    echo ($i % 2 === 0 xor $i / $w % 2 === 0) ? '?' : ' '; }
And the first time I've ever used the xor operator. I knew it had to be useful for something!
Epic. I'm not well versed enough in PHP: what does '$i % $w' mean? And what's the syntactic advantage of ++$i rather than $i++?
% = modulus operator. It's like dividing 2 numbers but keeping only the remainder.

http://www.php.net/manual/en/language.o ... hmetic.php




++$i and $i++ is basically pre-increment or post-increment. In a for loop, the ++$i increments $i before the logic is executed, and $i++ increments it after the logic is executed.

http://www.php.net/manual/en/language.o ... rement.php
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: [Challenge - Beginner] Draw a chess field

Post by Ollie Saunders »

And what's the syntactic advantage of ++$i rather than $i++
In this case there is no functional difference between the two. Either is acceptable.
Aristona
Forum Commoner
Posts: 33
Joined: Thu Dec 02, 2010 8:14 am

Re: [Challenge - Beginner] Draw a chess field

Post by Aristona »

Pretty late but that's what I came up with.

Code: Select all

<?php
Class Chess
{	
	function __Construct()
	{
		echo "Chess started.<br><hr>";
		$this->makeTable();
	}
	
	function makeTable()
	{
		echo 'Showing an <b> (' . $this->getMaxSize() . '/' . $this->getMaxSize() . ') matris.<hr>';
		for($i = 1; $i < $this->getMaxSize(); $i++)
		{
			for($j = 1; $j < $this->getMaxSize(); $j++)
			{
				($j + $i) % 2 == 0 ? print $this->makeCol('1') : print $this->makeCol('2');	
			}
			echo "<br>";
		}
	}
	
	function makeCol($in)
	{
		return ($in == 1) ? "[#]" : "[&nbsp;&nbsp;]";
	}
	
	function getMaxSize()
	{
		if (isset($_GET['size']))
			return $_GET['size'];
		else
			echo 'Enter size: <form action="index.php" method="get"><input type="text" name="size"></form>';
	}
}
$chess = new Chess();
?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: [Challenge - Beginner] Draw a chess field

Post by VladSun »

flying_circus wrote:In a for loop, the ++$i increments $i before the logic is executed, and $i++ increments it after the logic is executed.
That's not true.

Many people tend to use ++i instead of i++ in a for loop (and everywhere the ++ operator order doesn't matter) because they know it's the "right" way to do it (http://stackoverflow.com/questions/2020 ... -why-is-it) though I am pretty sure the code optimizer would use the appropriate one.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: [Challenge - Beginner] Draw a chess field

Post by social_experiment »

Code: Select all

<html>
<head>
<title>Chess board</title>
<style type="text/css">
table {
 border: 1px solid #a9a9a9;
 margin: 1% auto;	
}
td {		
 width: 60px;
 height: 60px;		
}
.white {	
 background-color: #ffc;
}
.black {
 background-color: #960;
}
</style>
</head>
<body>
<?php
// a chess board has 64 squares, 32 black & 32 white
// start with a white square, end with a white square
echo '<table>';
echo '<tr>';		
$j = 1;
for ($i = 1; $i <= 64; $i++) {	
	($class == 'white') ? $class = 'black' : $class = 'white';
	echo '<td class="' . $class . '"></td>';	
	if ($i % 8 == 0) {			
		echo '</tr><tr>';
		$j++;	
		($j % 2 == 0) ? $class = 'white' : $class = 'black';			
	}
}
echo '</tr>';
echo '</table>';	
?>
</body>
</html>	
Another table based chessboard, brown / light as opposed to black / white
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply