mastermind

Gesponsorde koppelingen

PHP script bestanden

  1. mastermind

« 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php

session_start();

?>


<html>
<head>
<title>PHP-based mastermind - By KoenB</title>
</head>
<body>

<?php

if($_SESSION['poging'] == "" || $_SESSION['poging'] == 0){

    $_SESSION['poging'] = 1;

    $_SESSION['g1'] = rand(1,6);

    $_SESSION['g2'] = rand(1,6);
    while($_SESSION['g2'] == $_SESSION['g1']){
        $_SESSION['g2'] = rand(1,6);
    }


    $_SESSION['g3'] = rand(1,6);
    while($_SESSION['g3'] == $_SESSION['g1'] || $_SESSION['g3'] == $_SESSION['g2']){
        $_SESSION['g3'] = rand(1,6);
    }


    $_SESSION['g4'] = rand(1,6);
    while($_SESSION['g4'] == $_SESSION['g1'] || $_SESSION['g4'] == $_SESSION['g2'] || $_SESSION['g4'] == $_SESSION['g3']){
        $_SESSION['g4'] = rand(1,6);
    }

}


if($_POST['speel']){

    $n1 = $_POST['n1'];
    $n2 = $_POST['n2'];
    $n3 = $_POST['n3'];
    $n4 = $_POST['n4'];

    if(!(is_numeric($n1) && is_numeric($n2) && is_numeric($n3) && is_numeric($n4))){

        echo "Vul getallen in.<p>";

        echo "<a href='index.php'>Terug.</a>";

    }
else if(($n1 > 6 || $n1 < 1) || ($n2 > 6 || $n2 < 1) || ($n3 > 6 || $n3 < 1) || ($n4 > 6 || $n4 < 1)){

        echo "Vul getallen van 1 t/m 6 in.<p>";

        echo "<a href='index.php'>Terug.</a>";

    }
else {

        $this_poging = $_SESSION['poging'];
    
        $_SESSION['een'.$this_poging] = $n1;
        $_SESSION['twee'.$this_poging] = $n2;
        $_SESSION['drie'.$this_poging] = $n3;
        $_SESSION['vier'.$this_poging] = $n4;
    
        $_SESSION['goed'.$this_poging] = 0;
        $_SESSION['half'.$this_poging] = 0;
    
        if($n1 == $_SESSION['g1']){
            $_SESSION['goed'.$this_poging] ++;
        }

        if($n2 == $_SESSION['g2']){
            $_SESSION['goed'.$this_poging] ++;
        }

        if($n3 == $_SESSION['g3']){
            $_SESSION['goed'.$this_poging] ++;
        }

        if($n4 == $_SESSION['g4']){
            $_SESSION['goed'.$this_poging] ++;
        }

    
        if($n1 == $_SESSION['g2'] || $n1 == $_SESSION['g3'] || $n1 == $_SESSION['g4']){
            $_SESSION['half'.$this_poging]++;
        }

        if($n2 == $_SESSION['g3'] || $n2 == $_SESSION['g4'] || $n2 == $_SESSION['g1']){
            $_SESSION['half'.$this_poging]++;
        }

        if($n3 == $_SESSION['g4'] || $n3 == $_SESSION['g1'] || $n3 == $_SESSION['g2']){
            $_SESSION['half'.$this_poging]++;
        }

        if($n4 == $_SESSION['g1'] || $n4 == $_SESSION['g2'] || $n4 == $_SESSION['g3']){
            $_SESSION['half'.$this_poging]++;
        }
        
    
        $_SESSION['poging'] ++;
    
        echo "<script language='JavaScript'> document.location = 'index.php'; </script>";

    }

}
else {

    $check_poging = $_SESSION['poging'] - 1;

    if($_SESSION['goed'.$check_poging] == 4){

        echo "Gewonnen!";

    }
else if($_SESSION['poging'] < 7){

        ?>


        <form method='post' target='_self'>

        <input type='text' name='n1' size='1' maxlength='1'>
        <input type='text' name='n2' size='1' maxlength='1'>
        <input type='text' name='n3' size='1' maxlength='1'>
        <input type='text' name='n4' size='1' maxlength='1'><p>

        <input type='submit' name='speel' value='Speel'>

        </form>

        <?

    } else {

        echo "Verloren!";

    }


    echo "<hr>";

    if($_SESSION['poging'] == 1){

        echo "<li>Raad een code van 4 verschillende cijfers van 1 t/m 6.<br>";
        echo "<li>Vul 4 getallen in in bovenstaande vakjes en klik op 'Speel'.<br>";
        echo "<li>Voor elk getal wat op de goede plaats staat, komt er een zwart hokje te staan.<br>";
        echo "<li>Voor elk getal wat niet op de goede plaats staat, maar wel in de code zit, komt er een wit hokje te staan.<br>";
        echo "<li>Voor elk getal wat niet in de code zit, komt er geen hokje te staan.";

    }
else {

        echo "<table bgcolor='gray' border='1' cellspacing='2' cellpadding='0'>";
    
        $teller = 1;
        while($teller < $_SESSION['poging']){
    
            echo "<tr>";
    
            echo "<td align='center' width='70' rowspan='2'>Poging ".$teller."</td>";
    
            echo "<td align='center' width='24' rowspan='2'>".$_SESSION['een'.$teller]."</td>";
            echo "<td align='center' width='24' rowspan='2'>".$_SESSION['twee'.$teller]."</td>";
            echo "<td align='center' width='24' rowspan='2'>".$_SESSION['drie'.$teller]."</td>";
            echo "<td align='center' width='24' rowspan='2'>".$_SESSION['vier'.$teller]."</td>";
    
            $teller_g = 1;
            while($teller_g <= 4){
    
                if($teller_g <= $_SESSION['goed'.$teller]){
                    echo "<td align='center' width='12' height='12' bgcolor='black'>";
                    echo "</td>";
                }
else {
                    echo "<td align='center' width='12' height='12'>";
                    echo "</td>";
                }

    
                $teller_g++;
    
            }

    
            echo "</tr>";
            echo "<tr>";
    
            $teller_h = 1;
            while($teller_h <= 4){
    
                if($teller_h <= $_SESSION['half'.$teller]){
                    echo "<td align='center' width='12' height='12' bgcolor='white'>";
                    echo "</td>";
                }
else {
                    echo "<td align='center' width='12' height='12'>";
                    echo "</td>";
                }

    
                $teller_h++;
    
            }

    
            echo "</tr>";
    
            $teller++;
    
        }

    
        echo "</table>";

    }


    echo "<hr>";

    if($_POST['opnieuw']){

        session_destroy();

        echo "<script language='JavaScript'> document.location = 'index.php'; </script>";

    }
else {

        echo "<form method='post' target='_self'>";
        echo "<input type='submit' name='opnieuw' value='Opnieuw spelen'>";
        echo "</form>";

    }

}


?>


</body>
</html>

 
 

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.