Rock Paper Scissors written in PHP
Posted by hawk under Dirty Scripts , GamesLet Me Hear Your Thoughts >>
Rock Paper Scissors is a simple script (no mysql needed) written in PHP for your downloading and playing pleasure. This is mainly just an illustration of a simple login and execution. It uses a text file named users.txt that contains only user logins and scores for a simple session based tracking. No passwords needed.
“R~P~S is a slick improvement over the old R~R~R games we
used to play back in the Pleistocene.”
~ Grognar the Barbarian
$v){
$user_parts = explode(':',$v);
$users[$user_parts[0]] = $user_parts[1]; // users[username] = score
}
if(array_key_exists($login_user,$users)){
session_start();
$_SESSION['score'] = $users[$login_user];
$_SESSION['user'] = $login_user;
}
}
if($_POST['action'] == 'judge' && isset($_POST['choice'])){
$winner=false;
$choices = array('rock','paper','scissors');
$player_choice = $_POST['choice'];
if(in_array($player_choice,$choices)){
session_start();
$comp_choice = $choices[array_rand($choices)];
echo 'Computer Picked :'.$comp_choice.'';
echo 'You Picked :'.$player_choice.'';
if($comp_choice == 'rock' && $player_choice == 'paper'){
$winner = true;
}elseif($comp_choice == 'scissors' && $player_choice == 'rock'){
$winner = true;
}elseif($comp_choice == 'paper' && $player_choice == 'scissors'){
$winner = true;
}
if($winner == true){
$score_t = $_SESSION['score'] + 1;
$_SESSION['score'] = $score_t;
echo 'You won. Your score is now:'.$_SESSION['score'];
}else{
$score_t = $_SESSION['score'] -1;
$_SESSION['score'] = $score_t;
echo 'You lost. Your score is now:'.$_SESSION['score'];
}
}
$auth_arr = file(AUTHFILE);
foreach($auth_arr as $k=>$v){
$user_parts = explode(':',$v);
$users[$user_parts[0]] = $user_parts[1]; // users[username] = score
}
$users[$_SESSION['user']] = $_SESSION['score'];
foreach($users as $k=>$v){
$temp_array = array($k,$v);
$user_str = implode(':',$temp_array);
$authstr .= $user_str . "\n";
}
file_put_contents(AUTHFILE,$authstr);
}
if($_POST['action'] == 'logout'){
session_start();
@session_destroy();
}
?>
There will also be a demo and download version coming shortly so be sure to check back soon.
“Now, what beat rock ag’in?”
~ BillyBob











