1A2B 猜數字遊戲

edited 十月 2013 in 原創軟體分享區
在昨天的課程中,設計了一個小範例,希望展示 session 的使用,程式碼如下:
<?php
/*
 * 猜數字的規則可以參考這兒的說明:
 * http://zh.wikipedia.org/zh-tw/猜数字
 */
session_start();
/*
 * 用來產生 4 個不重複的數字
 */
function generateAnswer() {
    $numberStack = range(0, 9);
    shuffle($numberStack);
    $answer = '';
    for($i = 0; $i < 4; $i++) {
        $answer .= $numberStack[$i];
    }
    return $answer;
}

if(empty($_SESSION['answer'])) {
    /*
     * 如果答案不存在就產生一個
     */
    $_SESSION['answer'] = generateAnswer();
}
?>
<form method="POST" action="number.php">
	<input type="text" name="answer" />
	<input type="submit" value="送出" />
</form>
<?php
if(!empty($_POST['answer'])) {
    $reset = false;
    if($_POST['answer'] == $_SESSION['answer']) {
        /*
         * 猜對了,統計猜測的次數
         */
        $reset = true;
        /*
         * 重設答案與記錄
         */
        echo '<h3>猜對了,答案就是 ' . $_POST['answer'] . ' ,你猜了 ' . (count($_SESSION['history']) + 1) . '次</h3>';
    } else {
        $counterA = $counterB = 0;
        for($i = 0; $i < 4; $i++) {
            if(substr($_POST['answer'], $i, 1) == substr($_SESSION['answer'], $i, 1)) {
                ++$counterA;
            } elseif(FALSE !== strpos($_SESSION['answer'], substr($_POST['answer'], $i, 1))) {
                ++$counterB;
            }
        }
        $_SESSION['history'][] = array($_POST['answer'], $counterA . 'A' . $counterB . 'B');
        echo '<h3>這次提供的數字為 ' . $_POST['answer'] . ' ,結果是 ' . $counterA . 'A' . $counterB . 'B</h3>';
    }
    if(!empty($_SESSION['history'])) {
        echo '<ul>';
        foreach($_SESSION['history'] AS $history) {
            echo '<li>' . $history[0] . ' => ' . $history[1] . '</li>';
        }
        echo '</ul>';
    }
    if($reset) {
        unset($_SESSION['history']);
        $_SESSION['answer'] = generateAnswer();
    }
}

目前還沒有加入防作弊的部份,程式碼也比較粗糙些,適合新手研究 ;)

原始討論: http://twpug.net/x/modules/newbb/viewtopic.php?topic_id=4449

評論

  • edited 九月 2009
    順便研究一下陣列操作吧(笑
    <?php
    session_start();
    if(empty($_SESSION['number']) or !is_array($_SESSION['number']))
    {
        $_SESSION['number']=array();
        $rand_num=array(0,1,2,3,4,5,6,7,8,9);
        $drop_key=array(0,1,2,3);
        $left_key=1;
        $rand_num=array_rand($rand_num,4);
        while($left_key<=4)
        {
           $key=rand(0,3);
           if(!array_key_exists($key,$drop_key))
           {
               continue;
           }
           else
           {
                $_SESSION['number'][$left_key]=$rand_num[$key];
                unset($drop_key[$key]);
                $left_key++;
           } 
        }
    }
    //作弊碼↓
    /*foreach($_SESSION['number'] as $value)
    {
        echo $value;
    }*/
    echo '<form method="post">
            <input name="number">
            <input type="submit" name="submit">
            </form>';
    if(isset($_POST['submit']) and isset($_POST['number']))
    {
        $_POST['number']=chop($_POST['number']);
        if(is_numeric($_POST['number']) and strlen($_POST['number'])==4)
        {
            $A=0;
            $B=0;
            $input=array();
            $checker=0;
            for($i=1;$i<=4;$i++)
            {
                $start=$i-1;
                $input[$i]=substr($_POST['number'],$start,1);
            }
            for($i=1;$i<4;$i++)//反作弊
            {
                 for($x=$i+1;$x<=4;$x++)
                 {
                     if($input[$i]==$input[$x])
                     {
                         ++$checker;
                         $i=4;
                         break;
                     }
                 }  
            }
            if($checker>0)
            {
                echo '請勿作弊';
                exit();
            }
            for($i=1;$i<=4;$i++)
            {
                if($input[$i]==$_SESSION['number'][$i])
                {
                    $A++;
                }
                else
                {
                    if(array_search($input[$i],$_SESSION['number']))
                    {
                        $B++;
                    }
                }
            }
            $_SESSION['history'][]="{$_POST['number']}/{$A}A{$B}B<br>";
            $RE=array_reverse($_SESSION['history']);
            $times=0;
            $return_html_code='';
            foreach($RE as $value)
            {
                ++$times;
                $return_html_code.=$value;
            }
            if($A==4)
            {
                echo "正確<br>猜了{$times}次<br>";
                unset($_SESSION['number']);
                unset($_SESSION['history']);
            }
            echo $return_html_code;
        } 
    }  
    ?>
    
Sign In or Register to comment.