-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.php
53 lines (49 loc) · 1.85 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
<?php
/**
* Austin Jenchi
* CSE 154 19sp AQ
* 05/31/2019
* Makes a random decision for a color, and checks if the given player guess matches
* the guess made by the computer.
*
* API Doc:
* ============================================================================================
* GET /game.php?guess={color}
* Parameters:
* - color: a valid color guess, either "red" or "blue" (case-sensitive)
* Response: Plain Text
* Responds the string "true" if the player's guess matches the computer's choice, or
* "false" if it does not.
*
* Responds with HTTP 400 for any other request type, if the guessed color is not valid, or
* if the guess parameter is missing from a GET request.
*/
include "common.php";
define("VALID_COLORS", array("red", "blue"));
define("GAME_TA_CHEAT_MODE", false); // always sets cpu guess to red
if (isset($_GET["guess"])) {
$color = $_GET["guess"];
if (in_array($color, VALID_COLORS)) {
output_text(make_color_decision($color));
} else {
output_error(400, "Invalid guess - must be either 'red' or 'blue'.");
}
} else {
output_error(400, "Invalid request type or guess parameter missing.");
}
/**
* Makes a random choice of color and checks if it matches the given player guess.
*
* @param {string} $guess - A valid player guess
* @return {string} either "true" or "false" if the guess matches the cpu's choice
*/
function make_color_decision($guess) {
$rand = VALID_COLORS[array_rand(VALID_COLORS)];
if ((GAME_TA_CHEAT_MODE && $guess === "red")
|| (!GAME_TA_CHEAT_MODE && $guess === $rand)) {
return "true";
} else {
return "false";
}
}
?>