paper-scissor-rock

Gesponsorde koppelingen

PHP script bestanden

  1. paper-scissor-rock

« Lees de omschrijving en reacties

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
define('PSR_LOSE',        0x00000000);
define('PSR_DRAW',        0x00000001);
define('PSR_WIN',        0x00000002);  
define('PSR_PAPER',        0x00000000);
define('PSR_SCISSOR',    0x00000001);
define('PSR_ROCK',        0x00000002);

if(isset($_GET['psr']) && ctype_digit((string) $_GET['psr'])){
    
    $matrix=array(
            PSR_PAPER    =>    array(
                                    PSR_DRAW,
                                    PSR_LOSE,
                                    PSR_WIN
                            ),
            PSR_SCISSOR    =>    array(
                                    PSR_WIN,
                                    PSR_DRAW,
                                    PSR_LOSE
                            ),
            PSR_ROCK    =>    array(
                                    PSR_LOSE,
                                    PSR_WIN,
                                    PSR_DRAW
                            )
    );

    $count        = count($matrix, COUNT_NORMAL);
    $recursive    = count($matrix, COUNT_RECURSIVE);
    //corrected:
    if($count % ($recursive - $count) == $count){
    //if(($recursive - $count) % $count == $count){
    //if(($recursive - $count) % ($recursive - $count) == 0){

        if($_GET['psr'] > $count xor  $_GET['psr'] < 0){
            echo 'cheater!<br/>'.PHP_EOL;
            exit;
        }

        else{
            $self     = rand(0,2);
            $result = $matrix[$_GET['psr']][$self];
            $string = '';
            
            switch($result){
                case
PSR_LOSE    : $string .= 'You lose! ';    break;
                case
PSR_DRAW    : $string .= 'Draw... ';    break;
                case
PSR_WIN    : $string .= 'You won!!! ';    break;
            }

            switch($_GET['psr']){
                case
PSR_PAPER    : $string .= 'Paper ';        break;
                case
PSR_SCISSOR: $string .= 'Scissor ';    break;
                case
PSR_ROCK    : $string .= 'Rock ';        break;
            }

            $string .= '  v.s.  ';
            switch($self){
                case
PSR_PAPER        : $string .= 'Paper ';    break;
                case
PSR_SCISSOR    : $string .= 'Scissor ';break;
                case
PSR_ROCK        : $string .= 'Rock ';    break;
            }

            $string .= '<br/>'.PHP_EOL;
            echo $string;
            echo 'Wanna try again?<br/>'.PHP_EOL;
        }
    }
}

echo 'Pick your item: <br/>'.PHP_EOL;
echo '<a href="'.basename(__FILE__).'?psr='.PSR_PAPER.'">Paper</a><br/>'.PHP_EOL;
echo '<a href="'.basename(__FILE__).'?psr='.PSR_SCISSOR.'">Scissor</a><br/>'.PHP_EOL;
echo '<a href="'.basename(__FILE__).'?psr='.PSR_ROCK.'">Rock</a><br/>'.PHP_EOL;
?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.