Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/game/number-round.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
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<boolean> {
const lines = await game.gameBuffer.getGameLines();
console.log("NumberRound#isRoundComplete", lines.indexOf(this.winLine) !== -1, lines);
return lines.indexOf(this.winLine) !== -1;
}
}


6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down