MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / main

Function main

javascript_games/deepcave.js:17–52  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15const PAUSE_AMOUNT = 0.05; // (!) Try changing this to 0 or 1.0.
16
17async function main() {
18 console.log('Deep Cave, by Al Sweigart al@inventwithpython.com');
19 console.log('Press Ctrl-C to stop.');
20 await sleep(2);
21
22 let leftWidth = 20;
23 let gapWidth = 10;
24
25 while (true) {
26 // Display the tunnel segment:
27 let rightWidth = WIDTH - gapWidth - leftWidth;
28 process.stdout.write('#'.repeat(leftWidth));
29 process.stdout.write(' '.repeat(gapWidth));
30 process.stdout.write('#'.repeat(rightWidth));
31 process.stdout.write('\n'); // Print a newline.
32
33 await sleep(PAUSE_AMOUNT);
34
35 // Adjust the left side width:
36 let diceRoll = Math.floor(Math.random() * 6) + 1;
37 if (diceRoll === 1 && leftWidth > 1) {
38 leftWidth = leftWidth - 1 // Decrease left side width.
39 } else if (diceRoll === 2 && leftWidth + gapWidth < WIDTH - 1) {
40 leftWidth = leftWidth + 1 // Increase left side width.
41 }
42
43 // Adjust the gap width:
44 // (!) Try uncommenting out all of the following code:
45 diceRoll = Math.floor(Math.random() * 6) + 1;
46 if (diceRoll === 1 && gapWidth > 1) {
47 gapWidth = gapWidth - 1; // Decrease gap width.
48 } else if (diceRoll === 2 && leftWidth + gapWidth < WIDTH - 1) {
49 gapWidth = gapWidth + 1; // Increase gap width.
50 }
51 }
52}
53
54main();

Callers 1

deepcave.jsFile · 0.70

Calls 1

sleepFunction · 0.70

Tested by

no test coverage detected