帅的不能再输啦
新手上路

初级会员
UID 67369
精华
0
积分 38
帖子 34
金钱 38 喜悦币
威望 0
人脉 0
阅读权限 10
注册 2005-12-21
状态 离线
|
[广告]: 代充Paypal帐号美元
php,命令行下石头剪刀游戏!最终版!
增加了胜率和败率的统计。
增加了总局数的统计。
对显示效果做了调整。
<?php #!/usr/bin/php <?php /** * 石头剪刀布游戏(单机版) * * @author [email]genghonghao@gmail.com[/email] * * @date 2008/07/03 * * @version v,1.0 * */
set_time_limit(0);
$input['s'] = array(1, '石头'); $input['j'] = array(2, '剪刀'); $input['b'] = array(3, '布');
define('INTEGRAL', 'INTEGRA_LOG');
define('SUCCESS_MARK', 5); define('FAILURE_MARK', -5); define('TIE_MARK', 0);
define('SUCCESS', "n::::::您在这次战斗中取得了胜利!::::::n"); define('FAILURE', "n::::::您在这次战斗中被系统战败!::::::n"); define('TIE', "n::::::您和系统战成了平局!::::::n");
// 石头=1, 剪刀=2, 布=3 $stone[-1] = '>'; $stone[1] = '<'; $stone[-2] = '<'; $stone[2] = '>'; $stone[0] = '=';
fwrite(STDOUT,"n出招的规则是: s代表石头 j代表剪刀 b代表布n退出请输入 : quitn积分的规则是: 胜利+5分 失败-5分 平局不加分也不减分n"); while (true) { fwrite(STDOUT, "n请 出 招 : ");
$inputu = trim(fgets(STDIN)); if($inputu == 'quit') { fwrite(STDOUT, "nBye Byenn"); break; }
$key = array_rand($input, 1); $syst_out = $input[$key]; if(array_key_exists($inputu, $input)) { $user_out = $input[$inputu]; $diff_err = $user_out[0] - $syst_out[0]; if(array_key_exists($diff_err, $stone)) { switch($diff_err) { case -1: $marke = SUCCESS_MARK; $if = SUCCESS; break; case 1: $marke = FAILURE_MARK; $if = FAILURE; break; case -2: $marke = FAILURE_MARK; $if = FAILURE; break; case 2: $marke = SUCCESS_MARK; $if = SUCCESS; break; case 0: $marke = TIE_MARK; $if = TIE; break; } write_integral($marke . "n"); $total = read_integral(); $result = read_result(); $mark_content = "本局获得积分: {$marke} 剩余积分: {$total}n成绩: 总局数:{$result[3]} 胜率: %{$result[0]} 败率: %{$result[1]} 平率: %{$result[2]}n"; fwrite(STDOUT, "n{$if}n你的出招:\"{$user_out[1]}\"n系统出招:\"{$syst_out[1]}\"n{$mark_content}"); }
} else { fwrite(STDOUT, "n请输入有效的值!n"); } }
function write_integral($content) {
$handle = @fopen(INTEGRAL, 'a');
@fwrite($handle, $content);
@fclose($handle);
}
function read_integral() {
$file = @file(INTEGRAL); $count = 0;
foreach($file as $k => $v) { $count += !empty($v) ? (int)(str_replace('n', '', $v)) : 0; } return $count; }
function read_result() { $file = @file(INTEGRAL); $count = count($file); $s = 0; $f = 0; $t = 0;
foreach($file as $k => $v) { $value = (int)(str_replace('n', '', $v)); switch($value) { case SUCCESS_MARK: ++$s; break; case FAILURE_MARK: $f++; break; case TIE_MARK: $t++; break;
} } $sd = sprintf("%2f", $s/$count); $sd = substr((string)$sd, 2, 2); $fd = sprintf("%2f", $f/$count); $fd = substr((string)$fd, 2, 2); $td = sprintf("%2f", $t/$count); $td = substr((string)$td, 2, 2); return array($sd, $fd, $td,$count); } ?>
|  ONLYCORE |
|