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++?Ollie Saunders wrote:The shortest solution I can come up with:And the first time I've ever used the xor operator. I knew it had to be useful for something!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) ? '?' : ' '; }
[Challenge - Beginner] Draw a chess field
Moderator: General Moderators
-
mattpointblank
- Forum Contributor
- Posts: 304
- Joined: Tue Dec 23, 2008 6:29 am
Re: [Challenge - Beginner] Draw a chess field
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: [Challenge - Beginner] Draw a chess field
% = modulus operator. It's like dividing 2 numbers but keeping only the remainder.mattpointblank wrote: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++?Ollie Saunders wrote:The shortest solution I can come up with:And the first time I've ever used the xor operator. I knew it had to be useful for something!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) ? '?' : ' '; }
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
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
Re: [Challenge - Beginner] Draw a chess field
In this case there is no functional difference between the two. Either is acceptable.And what's the syntactic advantage of ++$i rather than $i++
Re: [Challenge - Beginner] Draw a chess field
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) ? "[#]" : "[ ]";
}
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();
?>Re: [Challenge - Beginner] Draw a chess field
That's not true.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.
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
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: [Challenge - Beginner] Draw a chess field
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>
“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