姜运涛 发表于 2006 年 4 月 17 日 13:01:19

PHP猜拳游戏

此游戏只能运行在*nix的console下

#!/usr/local/bin/php -q
<?php
$print_str['help'] = "--------------------- A Finger-Guessing Game ---------------------
So this is a finger-guessing game written by Yeto Chiang.
You can input 'help' or '?' for help and 'exit' to quit the game.
Input '1' for Stone, '2' for Scissors, and '3' for Cloth.
After inputting, please press 'Enter' button.\n\n";
$print_str['win']= "You Win!\n";
$print_str['lose'] = "You Lose!\n";
$print_str['draw'] = "The game was drawn!\n";
$print_str['1']    = "Stone";
$print_str['2']    = "Scissors";
$print_str['3']    = "Cloth";

echo $print_str['help'];

$fp = fopen("php://stdin", "r");
while (!feof($fp)) {
        $computer = rand(1,3);
        echo "AFGG > ";
        switch (fgets($fp)) {
                case 1:
                        if ($computer == 1) {
                                echo "Both you and computer are " . $print_str['1'] . "\n";
                                echo $print_str['draw'];
                        } elseif ($computer == 2) {
                                echo "You are " . $print_str['1'] . "\n";
                                echo "Computer is " . $print_str['2'] . "\n";
                                echo $print_str['win'];
                        } else {
                                echo "You are " . $print_str['1'] . "\n";
                                echo "Computer is " . $print_str['3'] . "\n";
                                echo $print_str['lose'];
                        }
                        break;
                case 2:
                        if ($computer == 1) {
                                echo "You are " . $print_str['2'] . "\n";
                                echo "Computer is " . $print_str['1'] . "\n";
                                echo $print_str['lose'];
                        } elseif ($computer == 2) {
                                echo "Both you and computer are " . $print_str['2'] . "\n";
                                echo $print_str['draw'];
                        } else {
                                echo "You are " . $print_str['2'] . "\n";
                                echo "Computer is " . $print_str['3'] . "\n";
                                echo $print_str['win'];
                        }
                        break;
                case 3:
                        if ($computer == 1) {
                                echo "You are " . $print_str['3'] . "\n";
                                echo "Computer is " . $print_str['1'] . "\n";
                                echo $print_str['win'];
                        } elseif ($computer == 2) {
                                echo "You are " . $print_str['3'] . "\n";
                                echo "Computer is " . $print_str['2'] . "\n";
                                echo $print_str['lose'];
                        } else {
                                echo "Both you and computer are " . $print_str['3'] . "\n";
                                echo $print_str['draw'];
                        }
                        break;
                case "exit\n":
                        break 2;
                case "?\n":
                        echo $print_str['help'];
                        break;
                case "help\n":
                        echo $print_str['help'];
                        break;
                default:
                        echo "Wrong input.\n";
                        break;
        }
}

fclose($fp);
?>

oghuz 发表于 2006 年 4 月 17 日 13:02:05

演示看看

姜运涛 发表于 2006 年 4 月 17 日 13:04:17

等下我截图去

姜运涛 发表于 2006 年 4 月 17 日 13:12:19

截图
文字版的
输入1, 2, 3分别代表石头、剪刀和布
输入?或help得到帮助
输入exit结束程序
电脑出拳是随机算得的

waitme 发表于 2006 年 4 月 17 日 13:13:07

一片漆黑啊~~~~~~~~`

姜运涛 发表于 2006 年 4 月 17 日 13:15:45

原帖由 waitme 于 2006-4-17 13:13 发表
一片漆黑啊~~~~~~~~`
什么黑?
是说我图?

伤心♂oO○ 发表于 2006 年 4 月 17 日 13:17:28

是Linux下玩的,我还以为是网页程序呢

姜运涛 发表于 2006 年 4 月 17 日 13:17:34

其实做个console下的dialog版也行
不过还要PHP支持system函数

伤心♂oO○ 发表于 2006 年 4 月 17 日 13:20:13

姜运涛 发表于 2006 年 4 月 17 日 13:20:20

目的不是玩
而是为了学习
这里面有几个知识点呢
比如break 2;
我见过的程序里都很少出现
这是我翻手册知道滴
再就是关于我昨天发的那篇用PHP做Shell Script的了
感觉用PHP控制输入蛮有意思的
页: [1] 2
查看完整版本: PHP猜拳游戏