By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,741 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,741 IT Pros & Developers. It's quick & easy.

PHP and Chess

P: n/a
I am trying to display a position, on a chessboard, corresponding to a
sequence of moves recorded in a MySql database.
Any clue?
Michel
Jul 17 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Michel" <Mi**************@wanadoo.fr> schrieb:
I am trying to display a position, on a chessboard, corresponding to a
sequence of moves recorded in a MySql database.


http://www.php.net/manual/en/function.imagecopy.php

Regards,
Matthias
Jul 17 '05 #2

P: n/a
I noticed that Message-ID: <bn**********@news-reader3.wanadoo.fr> from
Michel contained the following:
I am trying to display a position, on a chessboard, corresponding to a
sequence of moves recorded in a MySql database.
Any clue?


Not sure If I understand the question but here goes.

If you have images of the pieces and an 8 by 8 table
<?php

function display($image){
if(isset($image)){
$showpiece="<img src=\"$image\" alt=\"\">";}
else{
$showpiece="empty";}
return $showpiece;
}
//information from database
$img=array();
$img[1][1]="rook_black.jpg";
$img[1][3]="rook_white.jpg";
$img[1][6]="king_white.jpg";
//etc, etc
?>

<table border="1" cellspacing="0" cellpadding="0">
<tr align="center" valign="middle">
<td width="50" height="50"><?php echo display($img[1][1]);
?></td>
<td width="50" height="50"><?php echo
display($img[1][2]);?></td>
<td width="50" height="50"><?php echo
display($img[1][3]);?></td>
<td width="50" height="50"><?php echo
display($img[1][4]);?></td>
<td width="50" height="50"><?php echo display($img[1][5]);
?></td>
<td width="50" height="50"><?php echo display($img[1][6]);
?></td>
<td width="50" height="50"><?php echo
display($img[1][7]);?></td>
<td width="50" height="50"><?php echo display($img[1][8]);
?></td>

</tr>
// and so on for another 7 rows
--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3

P: n/a
Thanks for this, not sure it's what I'm looking for but interesting anyway
Jul 17 '05 #4

P: n/a
Hi Geoff

Ouaouh ! You did all that for me ! That sounds exactly what I'm looking for!
No time right now to work on it but I'll tell you as soon as I have made
some progerss
Meantime to get an idea of wht I'm working on 5 (for fun when I've some free
time) you can go there: http://mdevat.phpnet.org/echecs/seqmove.php
Also my main page is there http://perso.wanadoo.fr/mdv/ (click on Chess
image to get some interesting content (if you like chess...)
Michel
Jul 17 '05 #5

P: n/a
I noticed that Message-ID: <bn**********@news-reader2.wanadoo.fr> from
Michel contained the following:

Ouaouh ! You did all that for me ! That sounds exactly what I'm looking for!
No time right now to work on it but I'll tell you as soon as I have made
some progerss


It was an interesting problem. I'm just a beginner myself and find that
working on problems like this helps me learn. :-)

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #6

P: n/a
Geoff Berrow wrote:
I noticed that Message-ID: <bn**********@news-reader2.wanadoo.fr> from
Michel contained the following:
Ouaouh ! You did all that for me ! That sounds exactly what I'm looking for!
No time right now to work on it but I'll tell you as soon as I have made
some progerss


It was an interesting problem. I'm just a beginner myself and find that
working on problems like this helps me learn. :-)


You'll find that there a lot of people in here that do the same. In
fact, if I wasn't so busy, I'd have done the same. It's a very effective
way of learning different aspects of PHP and programming. (When it's
someone else's problem that is...) ;)

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 17 '05 #7

P: n/a
An other thing: How to erase the content of a square (after a piece has left
this square) ?

with your example, I tried this:

$img[i][j]="" but then I get an x in the square (image not known)

I can also do something like:

//
<td width="40" bgcolor="#CDCFD3" align="center">
<?php
if ($img[3][1]!= "")
{
echo display($img[3][1]);
}
?>
<td width="40" bgcolor="#003399" align="center">
<?php
if ($img[3][2]!="")
{
echo display($img[3][2]);
}
?>
//

bit il looks very cumbersome (at least not elegant)

Michel


Jul 17 '05 #8

P: n/a
I noticed that Message-ID: <bo**********@news-reader1.wanadoo.fr> from
Michel contained the following:
An other thing: How to erase the content of a square (after a piece has left
this square) ?

with your example, I tried this:

$img[i][j]="" but then I get an x in the square (image not known)


It all depends on how you are storing the information about the moves.
In my example, I assumed that the information in the database would be
parsed in such a way as to returning all the information on where the
pieces were e.g,
$img=array();
$img[1][1]="rook_black.jpg";
$img[1][3]="rook_white.jpg";
$img[1][6]="king_white.jpg";
//etc, etc.

(It's possibly confusing to call this variable $img because it's really
a position corresponding to a square on the board.)

Then each square calls the function

<td width="50" height="50">
<?php echo display($img[1][1]); ?></td>
<td width="50" height="50">
<?php echo display($img[1][2]);?></td>
<td width="50" height="50">
<?php echo display($img[1][3]);?></td>

//etc. etc.

The function checks if the array position is set

function display($image){
if(isset($image)){

//if it is set, we display the chesspiece image

$showpiece="<img src=\"$image\" alt=\"\">";}
else{

//if it isn't set, we display the word 'empty'

$showpiece="empty";}
return $showpiece;
}
end of function.

If you just want blank square simply change this to
$showpiece="";

see www.ckdog.co.uk/php/test/chess.php (with more suitable images of
course)

To do any more on this I'll have to know how you are storing the moves
in the database.

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #9

P: n/a
In fact I retrieve 2 sequences of white and black moves from 2 (serialized)
fields of a row of a table, then I convert them (unserialize) into 2 arrays:
$tabwhite and $tablack
Note that these arrays do not contain all pieces positions but only a few
opening moves like:
$tabwhite[1][1] = "c2c4"
$tabwhite[1][2] = "Ng1f3"
$tabblack[1][1] = "Ng8f6"
$tabwhite[1][2] = "e7e6"

If I take first move (c2c4), I can find out that I must put a pawn in c4
i.e. $img[5][3]="pawn.gif" but I don't see how to remove this pawn from c2
That's why I tried with $img[7][3]="" but it does not work.
May be what you say now about $showpiece="" is the solution but I don't well
understand how it works.

Note that the link that you mentioned: www.ckdog.co.uk/php/test/chess.php
does not work, chess.php is not there...

Thanks again for your help



"Geoff Berrow" <bl******@ckdog.co.uk> a écrit dans le message de
news:di********************************@4ax.com...
I noticed that Message-ID: <bo**********@news-reader1.wanadoo.fr> from
Michel contained the following:
An other thing: How to erase the content of a square (after a piece has leftthis square) ?

with your example, I tried this:

$img[i][j]="" but then I get an x in the square (image not known)


It all depends on how you are storing the information about the moves.
In my example, I assumed that the information in the database would be
parsed in such a way as to returning all the information on where the
pieces were e.g,
$img=array();
$img[1][1]="rook_black.jpg";
$img[1][3]="rook_white.jpg";
$img[1][6]="king_white.jpg";
//etc, etc.

(It's possibly confusing to call this variable $img because it's really
a position corresponding to a square on the board.)

Then each square calls the function

<td width="50" height="50">
<?php echo display($img[1][1]); ?></td>
<td width="50" height="50">
<?php echo display($img[1][2]);?></td>
<td width="50" height="50">
<?php echo display($img[1][3]);?></td>

//etc. etc.

The function checks if the array position is set

function display($image){
if(isset($image)){

//if it is set, we display the chesspiece image

$showpiece="<img src=\"$image\" alt=\"\">";}
else{

//if it isn't set, we display the word 'empty'

$showpiece="empty";}
return $showpiece;
}
end of function.

If you just want blank square simply change this to
$showpiece="";

see www.ckdog.co.uk/php/test/chess.php (with more suitable images of
course)

To do any more on this I'll have to know how you are storing the moves
in the database.

--
Geoff Berrow
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/

Jul 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.