-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMathGame.php
169 lines (141 loc) · 4.01 KB
/
MathGame.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* Math memory game
*
* This memory game I made for my doughter that started to attend the school.
* She had some difficulties with elementary counting and substracting, so
* I decided to make a memory game for her to play. You can randomly generate
* tiles, print them out, slice up, and start playing. The idea is to match
* formula with the result. The result of this game is suspected to be that
* doughter could remember these easy formulas by haert and she doesn't need to
* calculate them in hand as adults do. Who does still counting on fingers, how
* many is 4 + 3 ? Of course, it's 7.
*
* @copyright 2013 Roberts Rozkalns
* @version Release: @1@
* @since Class available since Release 1.0
*/
class Game {
/**
* Number of examples, two tiles per example
* @var int
*/
public $examples;
/**
* Smallest number to make calculations
* @var int
*/
public $nlow;
/**
* Largest number to make calculations
* @var int
*/
public $nhig;
/**
* Smallest result
* @var int
*/
public $rlow;
/**
* Larges result
* @var int
*/
public $rhig;
/**
* Store generated cards
* @var array
*/
public $cards = array();
/**
* Store generated results to avoid result overlaping
* @var array
*/
public $usedResults = array();
/**
* Fill array with signs you want to use
*
* @todo make available multiplication and division
* @var array
*/
public $signs;
public function __construct() {
$this->examples = 12;
$this->nlow = 1;
$this->nhig = 10;
$this->rlow = 0;
$this->rhig = 12;
$this->signs = array('+', '-');
$this->generateCards();
$this->display();
}
/**
* Generate array of cards
*
*/
public function generateCards() {
$n = 0;
while ($n < $this->examples) {
// Generate random set of numbers
$r1 = rand($this->nlow, $this->nhig);
$r2 = rand($this->nlow, $this->nhig);
// Randomly pick the sign
$sign = rand(0,1);
$s = $this->signs[$sign];
// Make result of generated set of numbers
switch ($s) {
case "+" : $r = $r1 + $r2;
break;
case "-" : $r = $r1 - $r2;
break;
}
// Check if the number fits in bounds and if already this result exist
if ($r >= $this->rlow && $r <= $this->rhig && !in_array($r, $this->usedResults)) {
$this->cards[] = array("formula" => $r1 . $s . $r2, "result" => $r);
$n++;
}
// Fill array with used results
$this->usedResults[] = $r;
}
return;
}
/**
* Display cards for printing
*
*/
public function display() {
$r = "<style type = \"text/css\">";
$r .= $this->style();
$r .= "</style>";
foreach ($this->cards as $card) {
$r .= "<div class=\"card\"><div class=\"center\">" . $card['formula'] . "</div></div>";
$r .= "<div class=\"card\"><div class=\"center\">" . $card['result'] . "</div></div>";
}
echo $r;
}
/**
* Style of cards
*
*/
public function style() {
$r = ".card {";
$r .= "font-size: 3em;";
$r .= "text-align: center;";
$r .= "border: 1px solid #fab;";
$r .= "width: 145px;";
$r .= "height: 145px;";
$r .= "display: inline-block;";
$r .= "}";
$r .= ".card:before {";
$r .= "content: '';";
$r .= "display: inline-block;";
$r .= "height: 100%;";
$r .= "vertical-align: middle;";
$r .= "}";
$r .= ".center {";
$r .= "display: inline-block;";
$r .= "vertical-align: middle;";
$r .= "}";
return $r;
}
}
$game = new Game();