php打印輸出棋盤的實現(xiàn)方法
更新時間:2014年12月23日 11:49:39 投稿:shichen2014
這篇文章主要介紹了php打印輸出棋盤的實現(xiàn)方法,以實例形式分析了兩種不同的實現(xiàn)方法,具有一定的參考借鑒價值,需要的朋友可以參考下
本文實例講述了php打印輸出棋盤的兩種實現(xiàn)方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
例子1,代碼如下:
復制代碼 代碼如下:
<?php
/**
* 隔行隔列換色
* string fun_table(int $rows=9,int $cols=9)
* $rows 表示行數(shù) 必須為整數(shù) 并且必須在1-20之間
* $cols 表示列數(shù) 必須為整數(shù) 并且必須在1-20之間
*/
function fun_table($rows=9,$cols=9){
if ($rows<1 || $rows>20){
return "必須為整數(shù) 并且必須在1-20之間";
}
if ($cols<1 || $cols>20){
return "必須為整數(shù) 并且必須在1-20之間";
}
if($rows!=(int)($rows)){
return '行數(shù) 必須為整數(shù)';
}
if($cols!=(int)($cols)){
return '列數(shù) 必須為整數(shù)';
}
$str="";
$str.= "<table cellspacing='0' width='500px' border = '1px' bordercolor='black'>";
for ($i=1;$i<=$rows;$i++){
$str.= "<tr>";
for ($j=1;$j<=$cols;$j++){
if(($i+$j)%2){
$str.= "<td height='50px' bgcolor='black'>";
}else{
$str.= "<td></td>";
}
}
$str.= "</tr>";
}
$str.= "</table>";
return $str;
}
echo fun_table();
?>
/**
* 隔行隔列換色
* string fun_table(int $rows=9,int $cols=9)
* $rows 表示行數(shù) 必須為整數(shù) 并且必須在1-20之間
* $cols 表示列數(shù) 必須為整數(shù) 并且必須在1-20之間
*/
function fun_table($rows=9,$cols=9){
if ($rows<1 || $rows>20){
return "必須為整數(shù) 并且必須在1-20之間";
}
if ($cols<1 || $cols>20){
return "必須為整數(shù) 并且必須在1-20之間";
}
if($rows!=(int)($rows)){
return '行數(shù) 必須為整數(shù)';
}
if($cols!=(int)($cols)){
return '列數(shù) 必須為整數(shù)';
}
$str="";
$str.= "<table cellspacing='0' width='500px' border = '1px' bordercolor='black'>";
for ($i=1;$i<=$rows;$i++){
$str.= "<tr>";
for ($j=1;$j<=$cols;$j++){
if(($i+$j)%2){
$str.= "<td height='50px' bgcolor='black'>";
}else{
$str.= "<td></td>";
}
}
$str.= "</tr>";
}
$str.= "</table>";
return $str;
}
echo fun_table();
?>
例子2 簡單實現(xiàn)棋盤-for循環(huán)
實現(xiàn)這個棋盤首先我們想想棋盤是怎么樣的,是有很多個方格組成,然后由黑色和白色的相間的方格組成,首先我們先把方格畫出來,代碼如下:
復制代碼 代碼如下:
<?php
echo "<table cellspacing='0' width='500px' border = '1px' bordercolor='black'>";
for ($i=1;$i<=10;$i++){
echo "<tr>";
for ($j=1;$j<=10;$j++){
echo "<td>54im</td>";
}
echo "</tr>";
}
echo "</table>";
?>
echo "<table cellspacing='0' width='500px' border = '1px' bordercolor='black'>";
for ($i=1;$i<=10;$i++){
echo "<tr>";
for ($j=1;$j<=10;$j++){
echo "<td>54im</td>";
}
echo "</tr>";
}
echo "</table>";
?>
看到上面棋盤后,考慮下黑白格排放位置,有個規(guī)律可以發(fā)現(xiàn),橫排和豎排上白色格子都是基數(shù),黑色的都是偶數(shù),我們可以用取余的方法來判斷這個格子該顯示什么顏色,基數(shù)單元格我讓他顯示白色,偶數(shù)單元格顯示黑色,基數(shù)+偶數(shù)=偶數(shù),所以偶數(shù)單元格(黑色)我們很好找出來了,剩余的就是基數(shù)格(白色),代碼如下:
復制代碼 代碼如下:
<?php
/**
通過for循環(huán)和html實現(xiàn)棋盤
**/
echo "<table cellspacing='0' width='500px' border = '1px' bordercolor='black'>";
for ($i=1;$i<=10;$i++){
echo "<tr>";
for ($j=1;$j<=10;$j++){
if(($i+$j)%2){
echo "<td height='50px' bgcolor='black'>";
}else{
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</table>";
?>
/**
通過for循環(huán)和html實現(xiàn)棋盤
**/
echo "<table cellspacing='0' width='500px' border = '1px' bordercolor='black'>";
for ($i=1;$i<=10;$i++){
echo "<tr>";
for ($j=1;$j<=10;$j++){
if(($i+$j)%2){
echo "<td height='50px' bgcolor='black'>";
}else{
echo "<td></td>";
}
}
echo "</tr>";
}
echo "</table>";
?>
希望本文所述對大家的php程序設(shè)計有所幫助。
相關(guān)文章
解析PHP函數(shù)array_flip()在重復數(shù)組元素刪除中的作用
本篇文章是對PHP函數(shù)array_flip()在重復數(shù)組元素刪除中的作用進行了詳細的分析介紹,需要的朋友參考下2013-06-06php基于PDO實現(xiàn)功能強大的MYSQL封裝類實例
這篇文章主要介紹了php基于PDO實現(xiàn)功能強大的MYSQL封裝類,結(jié)合完整實例形式分析了php基于pdo實現(xiàn)mysql數(shù)據(jù)庫連接、增刪改查、事務(wù)等操作的方法,需要的朋友可以參考下2017-02-02PHP實現(xiàn)UTF-8文件BOM自動檢測與移除實例
這篇文章主要介紹了PHP實現(xiàn)UTF-8文件BOM自動檢測與移除的方法,實例講述了UTF-8文件BOM信息的原理與PHP對此的檢測與刪除方法,是非常實用的技巧,需要的朋友可以參考下2014-11-11PHP和Shell實現(xiàn)檢查SAMBA與NFS Server是否存在
這篇文章主要介紹了PHP和Shell實現(xiàn)檢查SAMBA與NFS Server是否存在,本文分別給出了PHP檢查腳本和Shell檢查腳本,需要的朋友可以參考下2015-01-01