Matrix scriptje

Gestart doorOne2xs9 reacties74 weergaven

10 berichten · pagina 1 van 1

One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Hoi allemaal.

Omdat ik me verveelde in de les heb ik een klein PHP scriptje geschreven die in je console een coole matrix geeft. Geheel nutteloos, maar het leek me wel leuk om te delen aangezien je dan leert hoe dependency injection en interfaces werken.

[COLOR= #0000BB]<?php[/COLOR]
 
interface Randomizer [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] randomize[COLOR= #007700]();
}[/COLOR]
 
[URL="http://www.php.net/class"]class[/URL] PHPRandRandomizer implements Randomizer [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] randomize[COLOR= #007700]() {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/rand"]rand[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]10[/COLOR][COLOR= #007700]);
    }
}[/COLOR]
 
[URL="http://www.php.net/class"]class[/URL] LetterRandomizer implements Randomizer [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$letters[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]a[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]b[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]c[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]d[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]f[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]g[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]h[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]i[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]j[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]k[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]l[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]m[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]n[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]o[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]p[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]q[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]r[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]s[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]t[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]u[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]v[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]w[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]x[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]y[/COLOR][COLOR= #007700],[/COLOR][COLOR= #DD0000]z[/COLOR][COLOR= #007700]);[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] randomize[COLOR= #007700]() {[/COLOR]
        [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]letters[COLOR= #007700][[/COLOR][URL="http://www.php.net/rand"]rand[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700],[/COLOR][COLOR= #0000BB]24[/COLOR][COLOR= #007700])];
    }
}[/COLOR]
 
[URL="http://www.php.net/class"]class[/URL] Matrix [COLOR= #007700]{[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$randomizer[/COLOR][COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$running[/COLOR] [COLOR= #007700]=[/COLOR] false[COLOR= #007700];[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [COLOR= #0000BB]$counter[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700];[/COLOR]
 
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] __construct[COLOR= #007700]([/COLOR]Randomizer [COLOR= #0000BB]$randomizer[/COLOR][COLOR= #007700]) {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]randomizer [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$randomizer[/COLOR][COLOR= #007700];
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] start[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]running [COLOR= #007700]=[/COLOR] true[COLOR= #007700];[/COLOR]
        
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]run[COLOR= #007700]();
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] stop[COLOR= #007700]() {[/COLOR]
        [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]running [COLOR= #007700]=[/COLOR] false[COLOR= #007700];
    }[/COLOR]
    
    [URL="http://www.php.net/public"]public[/URL] [COLOR= #007700]function[/COLOR] run[COLOR= #007700]() {[/COLOR]
        [URL="http://www.php.net/while"]while[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]running[COLOR= #007700]) {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]counter[COLOR= #007700]++;[/COLOR]
        
                        [COLOR= #FF8000]// Alles in de console heel snel na elkaar uit echoen om een matrix effect te creeëren.[/COLOR]
            [URL="http://www.php.net/echo"]echo[/URL] [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]randomizer[COLOR= #007700]->[/COLOR]randomize[COLOR= #007700]();[/COLOR]
            
            [URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]counter [COLOR= #007700]>[/COLOR] [COLOR= #0000BB]50000000[/COLOR][COLOR= #007700]) {[/COLOR] [COLOR= #FF8000]// De matrix stopt na een x aantal keren.[/COLOR]
                [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]stop[COLOR= #007700]();
            }
        }
    }
}[/COLOR]
 
[COLOR= #0000BB]$matrix[/COLOR] [COLOR= #007700]= new[/COLOR] Matrix[COLOR= #007700](new[/COLOR] LetterRandomizer[COLOR= #007700]);[/COLOR] [COLOR= #FF8000]// Hier gebruiken we een letter randomizer, maar je kan ook de PHPRandRandomizer gebruiken, met cijfertjes.[/COLOR]
[COLOR= #0000BB]$matrix[/COLOR][COLOR= #007700]->[/COLOR]start[COLOR= #007700]();[/COLOR]


Als je vragen hebt hoor ik die graag.

Raoul
0
Berichten
14.025
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien
Welke console?
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Dylanh schreef:
Welke console?
cmd in windows of terminal in linux en mac
0
Berichten
14.025
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien
Raaoul schreef:
Eerdere citaten tonen
Dylanh schreef:
Welke console?
cmd in windows of terminal in linux en mac
Maar je kan toch geen php in cmd doen?
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Dylanh schreef:
Eerdere citaten tonen
Raaoul schreef:
Eerdere citaten tonen
Dylanh schreef:
Welke console?
cmd in windows of terminal in linux en mac
Maar je kan toch geen php in cmd doen?
dat kan je wel.
0
Berichten
8.649
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Codeblok (voorbeeld.php):

1
2
3
4
5
6
7

class LetterRandomizer implements Randomizer {
private $letters = array(a,b,c,d,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z);

public function randomize() {
echo $this->letters[rand(1,24)];
}
}

Moet die rand niet rand(0, count($this->letters) - 1) zijn?

toevoeging op 7 september 2013, 081234:
C:xamppphpphp.exe -f "C:UsersAntonDownloadsmatrixmatrix.php"

toevoeging op 7 september 2013, 081355:
Afbeelding

Leuk biggrin
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Ja die letter randomizer is heel snel snel gemaakt omdat ik niet goed wist hoe de indexes van een array werken emo
toevoeging op 7 september 2013, 09:48:36:
Oja en volgens mij kon ik voor die letter array ook range() gebruiken, maar de verbinding op school was zo traag dat ik geen zin had om de documentatie te openen. emo
0
Berichten
8.649
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Raaoul schreef:
Ja die letter randomizer is heel snel snel gemaakt omdat ik niet goed wist hoe de indexes van een array werken emo
toevoeging op 7 september 2013, 09:48:36:
Oja en volgens mij kon ik voor die letter array ook range() gebruiken, maar de verbinding op school was zo traag dat ik geen zin had om de documentatie te openen. emo
Haha, herkenbaar emo
Heb ik ook weer wat bijgeleerd, kan PHP gewoon als cmd runnen biggrin
0
Berichten
1.544
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Ik vat het praktisch nut van "PHPRandRandomizer" niet helemaal. Of is deze puur als voorbeeld van een Interface gebruik?
0
One2xs

Bot · Topicstarter

Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
Jenner schreef:
Ik vat het praktisch nut van "PHPRandRandomizer" niet helemaal. Of is deze puur als voorbeeld van een Interface gebruik?
Het heeft wel degelijk een nut. Een letter randomizer is een randomizer, en de php rand randomizer ook gewoon.
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren