Skip to content

Commit dca28d5

Browse files
committed
add thing
1 parent 1b1a617 commit dca28d5

2 files changed

Lines changed: 158 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Ce portfolio présente mes compétences, projets et expériences professionnelle
1919
| `contact` | Informations de contact |
2020
| `social` | Liens vers mes réseaux sociaux |
2121
| `fastfetch` | Affiche les informations système |
22+
| `life` | Jeu de la Vie de Conway |
2223
| `theme <nom>` | Change le thème du terminal |
2324
| `restart` | Redémarre le terminal |
2425
| `banner` | Affiche le banner de bienvenue |

script.js

Lines changed: 157 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ Tapez 'help' pour voir les commandes disponibles.
326326
[[;#00ff00;]social] Liens vers mes réseaux sociaux
327327
[[;#00ff00;]theme] Change le thème du terminal
328328
[[;#00ff00;]fastfetch] Affiche les informations système
329+
[[;#00ff00;]life] Jeu de la Vie de Conway
329330
[[;#00ff00;]restart] Redémarre le terminal
330331
[[;#00ff00;]clear] Efface l'écran
331332
[[;#00ff00;]help] Affiche cette aide
@@ -604,6 +605,161 @@ Suivez-moi pour voir mes derniers projets et articles !`;
604605
}
605606
},
606607

608+
life: function(terminal) {
609+
terminal.clear();
610+
611+
// Configuration du jeu
612+
const WIDTH = 60;
613+
const HEIGHT = 25;
614+
let generation = 0;
615+
let grid = [];
616+
let running = true;
617+
let isPaused = false;
618+
let gameInterval;
619+
620+
// Initialiser la grille avec des cellules aléatoires
621+
const initGrid = function() {
622+
grid = [];
623+
for (let y = 0; y < HEIGHT; y++) {
624+
grid[y] = [];
625+
for (let x = 0; x < WIDTH; x++) {
626+
grid[y][x] = Math.random() < 0.3; // 30% de chances d'être vivante
627+
}
628+
}
629+
};
630+
631+
// Compter les voisins vivants
632+
const countNeighbors = function(x, y) {
633+
let count = 0;
634+
for (let dy = -1; dy <= 1; dy++) {
635+
for (let dx = -1; dx <= 1; dx++) {
636+
if (dx === 0 && dy === 0) continue;
637+
638+
const nx = (x + dx + WIDTH) % WIDTH;
639+
const ny = (y + dy + HEIGHT) % HEIGHT;
640+
641+
if (grid[ny][nx]) count++;
642+
}
643+
}
644+
return count;
645+
};
646+
647+
// Calculer la prochaine génération
648+
const nextGeneration = function() {
649+
const newGrid = [];
650+
651+
for (let y = 0; y < HEIGHT; y++) {
652+
newGrid[y] = [];
653+
for (let x = 0; x < WIDTH; x++) {
654+
const neighbors = countNeighbors(x, y);
655+
const alive = grid[y][x];
656+
657+
// Règles du Jeu de la Vie
658+
if (alive) {
659+
newGrid[y][x] = neighbors === 2 || neighbors === 3;
660+
} else {
661+
newGrid[y][x] = neighbors === 3;
662+
}
663+
}
664+
}
665+
666+
grid = newGrid;
667+
generation++;
668+
};
669+
670+
// Dessiner la grille
671+
const draw = function() {
672+
let output = '╔' + '═'.repeat(WIDTH) + '╗\n';
673+
674+
for (let y = 0; y < HEIGHT; y++) {
675+
output += '║';
676+
for (let x = 0; x < WIDTH; x++) {
677+
if (grid[y][x]) {
678+
output += '[[;#00ff00;]█]';
679+
} else {
680+
output += ' ';
681+
}
682+
}
683+
output += '║\n';
684+
}
685+
686+
output += '╚' + '═'.repeat(WIDTH) + '╝\n';
687+
const status = isPaused ? '[[;#ffaa00;]PAUSE]' : '[[;#00ff00;]EN COURS]';
688+
output += `[[;#00ff00;]Génération:] ${generation} | Status: ${status}\n`;
689+
output += `[[;#888888;]Commandes: r=réinitialiser | p=pause/play | q=quitter]`;
690+
691+
terminal.clear();
692+
terminal.echo(output);
693+
};
694+
695+
// Gérer la fin du jeu
696+
const endGame = function() {
697+
running = false;
698+
if (gameInterval) {
699+
clearInterval(gameInterval);
700+
gameInterval = null;
701+
}
702+
703+
terminal.pop();
704+
terminal.clear();
705+
terminal.echo('');
706+
terminal.echo(' [[;#888888;]Simulation terminée]');
707+
terminal.echo('');
708+
terminal.echo(` [[;#00ff00;]Générations simulées:] ${generation}`);
709+
terminal.echo('');
710+
terminal.set_prompt('[[;#00ff00;]visitor@portfolio][[;#ffffff;]:~$] ');
711+
};
712+
713+
// Mode interactif avec barre de texte
714+
terminal.push(function(command) {
715+
const cmd = command.toLowerCase().trim();
716+
717+
if (!running) return;
718+
719+
if (cmd === 'q' || cmd === 'quit' || cmd === 'exit') {
720+
endGame();
721+
} else if (cmd === 'r' || cmd === 'restart' || cmd === 'reset') {
722+
generation = 0;
723+
initGrid();
724+
draw();
725+
} else if (cmd === 'p' || cmd === 'pause' || cmd === 'play') {
726+
if (gameInterval) {
727+
clearInterval(gameInterval);
728+
gameInterval = null;
729+
isPaused = true;
730+
} else {
731+
isPaused = false;
732+
gameInterval = setInterval(function() {
733+
nextGeneration();
734+
draw();
735+
}, 200);
736+
}
737+
draw();
738+
} else if (cmd === 'h' || cmd === 'help') {
739+
terminal.echo('[[;#00ff00;]Commandes disponibles:]');
740+
terminal.echo(' [[;#ffaa00;]r/restart/reset] - Réinitialiser avec une nouvelle grille');
741+
terminal.echo(' [[;#ffaa00;]p/pause/play] - Mettre en pause ou reprendre');
742+
terminal.echo(' [[;#ffaa00;]q/quit/exit] - Quitter la simulation');
743+
terminal.echo(' [[;#ffaa00;]h/help] - Afficher cette aide');
744+
} else if (cmd !== '') {
745+
terminal.error(`Commande inconnue: '${cmd}'. Tapez 'h' pour l'aide.`);
746+
}
747+
}, {
748+
prompt: '[[;#00ff00;]life>] ',
749+
name: 'game_of_life'
750+
});
751+
752+
// Démarrer le jeu
753+
initGrid();
754+
draw();
755+
gameInterval = setInterval(function() {
756+
nextGeneration();
757+
draw();
758+
}, 200);
759+
760+
return '';
761+
},
762+
607763
restart: function(terminal) {
608764
terminal.clear();
609765
terminal.pause();
@@ -709,7 +865,7 @@ Suivez-moi pour voir mes derniers projets et articles !`;
709865
if (commands[cmd]) {
710866
if (cmd === 'theme') {
711867
term.echo(commands[cmd](term));
712-
} else if (cmd === 'restart') {
868+
} else if (cmd === 'restart' || cmd === 'life') {
713869
commands[cmd](term);
714870
} else {
715871
term.echo(commands[cmd]());

0 commit comments

Comments
 (0)