-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.php
More file actions
executable file
·55 lines (52 loc) · 1017 Bytes
/
Copy pathhangman.php
File metadata and controls
executable file
·55 lines (52 loc) · 1017 Bytes
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
<?php
class Dictionary{
public $filename;
public $handler;
public $size;
public function __construct($filename)
{
if(file_exists($filename))
{
$this->filename = $filename;
}
else
{
echo("No such file ".$filename);
}
$this->determineDictionarySize();
}
public function getWordFromDict()
{
$which = rand(1, $this->size);
$this->handler = fopen($this->filename, 'r');
while($which--)
{
$word = fgets($this->handler);
}
fclose($this->handler);
return trim($word);
}
private function determineDictionarySize()
{
$words = 0;
$this->handler = fopen($this->filename, 'r');
while(!feof($this->handler))
{
fgets($this->handler);
$words++;
}
fclose($this->handler);
$this->size = $words;
}
}
class Word{
public $answer;
public function __construct($word)
{
$this->answer = $word;
}
}
$dict = new Dictionary('resources/dictionary');
$ans = new Word($dict->getWordFromDict());
echo($ans->answer);
?>