diff --git a/src/game/number-round.ts b/src/game/number-round.ts new file mode 100644 index 0000000..d86aec1 --- /dev/null +++ b/src/game/number-round.ts @@ -0,0 +1,78 @@ +import { IGame } from "./types"; +import { Round } from "./round"; + +const numberRoundInstructions = [ + 'Use C-a and C-x to fix the number on the left side of the math equation', + "" +]; + +export class NumberRound extends Round { + + private firstNumber: number; + private secondNumber: number; + private answer: number; + private fakeAnswer: number; + private winLine: string; + + constructor() { + super(); + this.firstNumber = 0; + this.secondNumber = 0; + this.answer = 0; + this.fakeAnswer = 0; + this.winLine = ""; + } + + public getInstructions(): string[] { + return numberRoundInstructions; + } + + public async render(game: IGame): Promise { + this.firstNumber = Math.floor(Math.random() * 5 + 1); + this.secondNumber = Math.floor(Math.random() * 5 + 1); + + // To prevent neagtive numbers, which feel wield while playing game + if(this.secondNumber > this.firstNumber) { + let temp = this.secondNumber; + this.secondNumber = this.firstNumber; + this.firstNumber = temp; + } + + let addOrSubtract = "+"; + if(Math.random() > 0.5) { + // Addition + this.answer = this.firstNumber + this.secondNumber; + this.winLine = this.answer + " = " + this.firstNumber + " " + + addOrSubtract + " " + this.secondNumber; + } + else { + // Subtraction + this.answer = this.firstNumber - this.secondNumber; + addOrSubtract = "-"; + this.winLine = this.answer + " = " + this.firstNumber + " " + + addOrSubtract + " " + this.secondNumber; + } + + do { + this.fakeAnswer = Math.floor(Math.random() * 5 + 1); + } + while(this.answer == this.fakeAnswer); + + const lines = new Array(game.state.lineLength).fill(""); + lines[0] = this.fakeAnswer + " = " + this.firstNumber + " " + + addOrSubtract + " " + this.secondNumber; + + console.log("number -- run#rendering", game.state.lineLength, lines); + + game.gameBuffer.clearBoard + return lines; + } + + public async isRoundComplete(game: IGame): Promise { + const lines = await game.gameBuffer.getGameLines(); + console.log("NumberRound#isRoundComplete", lines.indexOf(this.winLine) !== -1, lines); + return lines.indexOf(this.winLine) !== -1; + } +} + + diff --git a/src/index.ts b/src/index.ts index 67e8db4..cbd34ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { Game, newGameState } from "./game/base"; import { GameBuffer } from "./game-buffer"; import { WhackAMoleRound } from "./game/whackamole-round"; import { CiRound } from "./game/ci-round"; +import { NumberRound } from "./game/number-round"; import { Menu } from "./menu"; // this is a comment @@ -185,13 +186,16 @@ export async function initializeGame( if (name === "whackamole" || isRandom) { roundSet.push(new WhackAMoleRound()); } + if (name === "number" || isRandom) { + roundSet.push(new NumberRound()); + } if (roundSet.length) { runGame(new Game(nvim, gameBuffer, state, roundSet, {difficulty})); } } -const availableGames = ["relative", "ci{", "whackamole", "random"]; +const availableGames = ["relative", "ci{", "whackamole", "number", "random"]; const availableDifficulties = ["easy", "medium", "hard", "nightmare", "tpope"]; const stringToDiff = {