Page 2 of 2
Re: [Challenge - Beginner] Draw a chess field
Posted: Wed Sep 02, 2009 11:07 am
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++?
Re: [Challenge - Beginner] Draw a chess field
Posted: Wed Sep 02, 2009 11:17 am
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
Re: [Challenge - Beginner] Draw a chess field
Posted: Wed Sep 02, 2009 11:22 am
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.
Re: [Challenge - Beginner] Draw a chess field
Posted: Wed Mar 09, 2011 6:36 pm
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) ? "[#]" : "[ ]";
}
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
Posted: Thu Mar 10, 2011 4:51 am
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.
Re: [Challenge - Beginner] Draw a chess field
Posted: Thu Mar 10, 2011 4:53 pm
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