hangman.ts (3901B)
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 | import { Message, User } from 'discord.js';
import { FORMATS } from '../extensions';
declare global {
namespace Types {
type ScoreStats = {
missed: number,
scored: number
};
export type Mask = '_' | '.' | '-' | ':';
export interface Score {
[id: string]: ScoreStats;
}
export interface Guess {
id: string;
answer: string;
attempt?: string;
}
export interface HangmanConfig {
attempt_limit: number;
mask: Mask;
}
export interface Messages {
help: string;
lose: string;
miss: string;
in_progress: string;
win: (id: string) => string;
start: (word: string) => string;
guess: (attempt: string) => string;
result: (missed: number,
scored: number) => string;
}
export enum Status {
Started,
InProgress
}
}
interface Array<T> {
result(): string;
pick(): string;
}
interface String {
mask_with(mask: Types.Mask): string[];
}
}
Array.prototype.result = function () {
return this.join('');
};
Array.prototype.pick = function (): string {
return this[Math.floor(Math.random() * this.length)];
};
String.prototype.mask_with = function (mask: Types.Mask = '_') {
return [...(mask.repeat(this.length))];
};
const WORDS: string[] = [
"hello",
"world"
];
let GAME_STATUS: Types.Status;
const scores: Types.Score = {
"29138138129139128": {
missed: 0,
scored: 0
}
};
const CONFIG: Types.HangmanConfig = {
attempt_limit: 10,
mask: '_'
};
const MESSAGES: Types.Messages = {
help: "To start a new hangman game"
+ ".hangman new\n".format(FORMATS.block)
+ "Make a guess: "
+ ".hangman [@letter]\n".format(FORMATS.block)
+ "Guess the full word: "
+ ".hangman [@word]".format(FORMATS.block),
in_progress: "A hangman game is already in progress!",
lose: "You reached the number of attempts. You lose!",
miss: "You missed :(",
start: (word) =>
`The word is ${word.length} letters long.\n` +
`Make a guess!`,
win: (id) =>
`Congrats ${id}, you won the game!`,
guess: (attempt) =>
`You guessed: ${attempt}`,
result: (missed, scored) =>
`Missed: ${missed}\n` +
`Scored: ${scored}`,
};
// export default (homescope: HomeScope) => {
// const { message, args } = homescope;
// if (args.length === 0 || args[0] === 'help')
// message.channel.send(MESSAGES.help);
const args = ['new'];
const start = (w: string) => {
console.log(MESSAGES.start(w));
GAME_STATUS = Types.Status.Started;
};
const answer = WORDS.pick();
const [id, attempts, output]: [string, number, string[]] = [
// message.author,
"494924924924999",
CONFIG.attempt_limit,
answer.mask_with(CONFIG.mask)
];
const score = (u_id: string) =>
scores[u_id].scored++;
const miss = (u_id: string) =>
scores[u_id].missed++;
const guessed = (attempt: string) =>
MESSAGES.guess(attempt);
const hasWon = () =>
!output.includes(CONFIG.mask);
const lose = () =>
console.log(MESSAGES.lose);
// message.channel.send(MESSAGES.lose);
const win = (u_id: string) => {
score(u_id);
const { missed, scored } = scores[u_id];
MESSAGES.win(u_id);
MESSAGES.result(missed, scored);
};
// FIX THESE SHADOW VARIABLES (id, answer)
const guess = ({ id, answer, attempt }: Types.Guess) => {
if (GAME_STATUS !== (Types.Status.Started | Types.Status.InProgress)) {
console.log(MESSAGES.in_progress);
return;
}
guessed(attempt);
if (attempt.length === answer.length)
(attempt === answer) ? win(id) : miss(id);
if (answer.indexOf(attempt) !== -1) {
miss(id);
return (scores[id].missed >= attempts)
? lose()
: output.result();
}
[...answer]
.each((letter: string, i: number) => {
if (letter === attempt)
output[i] = attempt.toUpperCase();
if (typeof (output[i]) === 'undefined')
output[i] = CONFIG.mask;
});
score(id);
return output.result();
};
if (args[0] === 'new')
start(answer);
else {
guess({ id, answer, attempt: args[0] });
if (hasWon())
win(id);
}
// };
|