repl.it
@Vandesm14/

Lock Picker

Nodejs

No description

fork
loading
Files
  • index.js
  • package-lock.json
  • package.json
  • rand-lite.js
index.js
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
/* jshint esversion: 6 */
const keypress = require('keypress');
const c = require('ansi-colors');
const readlineSync = require('readline-sync');
const fs = require('fs');
const randLite = require('./rand-lite.js');

var locks = [];
var lock = '';
var score = 0;
var lives = 4;
var level = 0;
var slot = 0;
var globalSeed = 0;
var newStart = false;

start();

keypress(process.stdin);
process.stdin.setRawMode(true);
process.stdin.resume();

process.stdin.on('keypress', function (ch, key) {
	switch (true) {
		case key && key.ctrl && key.name == 'c':
			process.stdin.pause();
			break;
		case key && key.name == 'q':
			tryState(key.name);
			break;
		case key && key.name == 'w':
			tryState(key.name);
			break;
		case key && key.name == 'e':
			tryState(key.name);
			break;
		case key && key.name == 'r':
			tryState(key.name);
			break;
		default:
			console.log(c.red('Invalid Key'));
	}
});

function tryState(key) {
	if (key == lock[slot]) {
		score += 4;
		if (slot < lock.length) {
			nextSlot();
		} else {
			nextLevel();
		}
	} else {
		if (lives < 1) {
			console.log(c.red(`Game over! Ending Score: ${score}`));
			process.stdin.pause();
			if (readlineSync.keyInYN('New Game?')) {
				keypress(process.stdin);
				process.stdin.setRawMode(true);
				process.stdin.resume();
				newStart = true;
				start();
			}
		} else {
			lives--;
			score -= 2;
			printScore();
			console.log(c.red(`Pick Failed!`));
			console.log(lock.substr(0, slot));
		}
	}
}

function start(m = false) {
	score = 0;
	level = 0;
	console.log(c.white('Use the keys "q", "w", "e", and "r" to pick the lock! Keep going to earn more points!\nIf the first key fails, try another one. But be mindful of your lives!'));
	if (!newStart) {
		process.stdin.pause();
		console.log(c.yellow('Valid characters: 0-9, max digits: 16'));
		console.log(c.yellow('(Hit Enter to generate random seed)'));
		locks = useRand(readlineSync.question('Input Level Seed:'));
		keypress(process.stdin);
		process.stdin.setRawMode(true);
		process.stdin.resume();
	}
	loadLevel(level);
}

function useRand(seed) {
	// 948 total chars
	globalSeed = new Date().getTime();
	if (seed == '') {seed = globalSeed}
	var seed = randLite(237, seed, 16).join('');

	var sets = [2, 2, 4, 4, 4, 4, 8, 8, 8, 8, 16, 16, 16, 16, 16, 16, 16, 16, 24, 24, 24, 24, 24, 24, 24, 24, 32, 32, 32, 32, 32, 32, 32, 32, 40, 40, 40, 40, 40, 40, 40, 40];
	var letters = 'qwer';
	var index = 0;
	var items = [];
	var x = 0;
	var y = '';
	for (i = 0; i < sets.length; i++) {
		x = seed.substr(index, sets[i]).split('');
		y = [];
		for (k = 0; k < x.length; k++) {
			y[k] = letters[x[k] % letters.length];
		}
		items.push(y.join(''));
		index += sets[i];
	}
	return items;
}

function loadLevel(round) {
	console.clear();
	slot = 0;
	lock = locks[round];
	lives = lock.length;
	printScore();
	if (level !== 0) {
		console.log(c.green(`Lock Picked! Next, Level ${level}`));
	}
}

function loadRandLock() {
	let text = '';
	let possible = 'qwer';
	for (i = 0; i < 8; i++) {
		text += possible.charAt(Math.floor(Math.random() * possible.length));
	}
	return text;
}

function printScore() {
	console.clear();
	console.log(c.gray('Seed: ' + globalSeed));
	console.log(c.yellow(`Level: ${c.green(level + 1)} Slot: ${c.green(slot + 1)} "${lock.substr(0, slot)}"`));
	console.log(c.yellow(`Score: ${c.green(score)} Lives: ${c.green(lives)}`));
}

function nextSlot() {
	slot++;
	printScore();
	console.log(c.green(`Slot ${slot} Picked!`));
	console.log(lock.substr(0, slot));
	if (slot >= lock.length) {
		level++;
		loadLevel(level);
	}
}

function nextLevel() {
	level++;
	loadLevel(level);
}
node v10.16.0