Buatlah program untuk mendesain papan catur menggunakan PHP

0
(0)

Catur adalah permainan papan rekreasi dan kompetitif yang dimainkan antara dua pemain. Ini dimainkan di papan catur persegi dengan 64 kotak diatur dalam kotak delapan kali delapan hitam-putih sebagai alternatif. Pada artikel ini, kita akan belajar cara mendesain Papan Catur menggunakan PHP.

Mendekati:

Pembaca perhatian! Jangan berhenti belajar sekarang. Dapatkan semua konsep HTML penting dengan Desain Web untuk Pemula | HTML kursus.

  • Untuk membuat Catur di php; kita harus menjalankan 2 loop masing-masing akan membuat 8 blok.
  • Loop dalam akan menghasilkan baris tabel dengan warna latar hitam/putih berdasarkan nilai .
  • Jika nilainya genap, latar belakang Hitam dihasilkan .
  • Jika nilainya ganjil, latar belakang Putih dihasilkan

Papan Catur menggunakan For-Loop:

PHP

<!DOCTYPE html>

<html>

 

<body>

    <table width="400px" border="1px" cellspacing="0px">

        <?php

        echo "Chess by GeeksforGeeks";

        $value = 0;

 

        for($col = 0; $col < 8; $col++) {

            echo "<tr>";

            $value = $col;

 

            for($row = 0; $row < 8; $row++) {

                if($value%2 == 0) {

                    echo

"<td height=40px width=20px bgcolor=black></td>";

                    $value++;

                }

                else {

                    echo

"<td height=40px width=20px bgcolor=white></td>";

                    $value++;

                }

            }

            echo "</tr>";

        }

        ?>

    </table>

</body>

 

</html>

Papan Catur menggunakan while-Loop:

PHP

<!DOCTYPE html>

<html>

 

<body>

    <table width="400px" border="1px" cellspacing="0px">

        <?php

        echo "Chess by GeeksforGeeks";

        $value = 0;

        $col = 0;

 

        while($col < 8) {

            $row = 0;

            echo "<tr>";

            $value = $col;

         

            while($row < 8) {

                if($value%2 == 0) {

                    echo

"<td height=40px width=20px bgcolor=black></td>";

                    $value++;

                }

                else {

                    echo

"<td height=40px width=20px bgcolor=white></td>";

                    $value++;

                }

                $row++;

            }

            echo "</tr>";

            $col++;

        }

        ?>

    </table>

</body>

 

</html>

Papan Catur menggunakan loop do-while:

PHP

<!DOCTYPE html>

<html>

 

<body>

    <table width="400px" border="1px" cellspacing="0px">

        <?php

        echo "Chess by GeeksforGeeks";

        $value = 0;

        $col = 0;

 

        do {

            $row = 0;

            echo "<tr>";

            $value = $col;

 

            do {  

                if($value%2 == 0) {

                    echo

"<td height=40px width=20px bgcolor=black></td>";

                    $value++;

                }

                else {

                    echo

"<td height=40px width=20px bgcolor=white></td>";

                    $value++;

                }

                $row++;

            }while($row < 8);

            echo "</tr>";

            $col++;

        }while($col < 8);

        ?>

    </table>

</body>

 

</html>

Keluaran: Semua kode akan memberikan output yang sama.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.