-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.php
90 lines (81 loc) · 2.35 KB
/
game.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
// Demand a GET parameter
if ( ! isset($_GET['name']) || strlen($_GET['name']) < 1 ) {
die('Name parameter missing');
}
// If the user requested logout go back to index.php
if ( isset($_POST['logout']) ) {
header('Location: index.php');
return;
}
// Set up the values for the game...
// 0 is Rock, 1 is Paper, and 2 is Scissors
$names = array("0"=>"Rock -> <img src ='http://localhost:8888/rps/img/rock.png'>", "1"=>"Paper -> <img src ='http://localhost:8888/rps/img/paper.png' >", "2"=>"Scissor -> <img src ='http://localhost:8888/rps/img/scissors.png'>");
//$names = array('Rock', 'Paper', 'Scissors');
$human = isset($_POST["human"]) ? $_POST['human']+0 : -1;
$computer = 0;
$computer = rand(0,2);
// This function takes as its input the computer and human play
// and returns "Tie", "You Lose", "You Win" depending on play
// where "You" is the human being addressed by the computer
function check($computer, $human) {
if ( $human == $computer ) {
return "Tie";
} else if ( ($human == 1 && $computer == 2) ||
($human == 0 && $computer == 1) ||
($human == 2 && $computer == 0)) {
return "You lose";
} else{
return "You win";
}
return false;
}
// Check to see how the play happenned
$result = check($computer, $human);
?>
<!DOCTYPE html>
<html>
<head>
<title>Kabish bhattarai Rock, Paper, Scissors Game 37a35100</title>
<?php require_once "bootstrap.php"; ?>
</head>
<body>
<div class="container">
<h1>Rock Paper Scissors</h1>
<?php
if ( isset($_REQUEST['name']) ) {
echo "<p>Welcome: ";
echo htmlentities($_REQUEST['name']);
echo "</p>\n";
}
?>
<form method="post">
<select name="human">
<option value="-1">Select</option>
<option value="0">Rock</option>
<option value="1">Paper</option>
<option value="2">Scissor</option>
<option value="3">Test</option>
</select>
<input type="submit" value="Play">
<input type="submit" name="logout" value="Logout">
</form>
<pre>
<?php
if ( $human == -1 ) {
print "Please select a strategy and press Play.\n";
} else if ( $human == 3 ) {
for($c=0;$c<3;$c++) {
for($h=0;$h<3;$h++) {
$r = check($c, $h);
print "Human=$names[$h] Computer=$names[$c] Result=$r\n";
}
}
} else {
print "Your Play=$names[$human] Computer Play=$names[$computer] Result=$result\n";
}
?>
</pre>
</div>
</body>
</html>