Skip to content

Commit

Permalink
Guard alien row issue (#7470)
Browse files Browse the repository at this point in the history
* Guard alien row issue

* Guard alien row issue
  • Loading branch information
iHiD authored Feb 8, 2025
1 parent 481136c commit 9fd07a6
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Exercise } from '../Exercise'
import { ExecutionContext } from '@/interpreter/executor'
import { cloneDeep, random } from 'lodash'
import { d } from '@codemirror/legacy-modes/mode/d'
import { deepTrim } from '@/interpreter/describers/helpers'
import { isNumber } from '@/interpreter/checks'

type GameStatus = 'running' | 'won' | 'lost'
type AlienStatus = 'alive' | 'dead'
Expand Down Expand Up @@ -294,7 +296,23 @@ export default class SpaceInvadersExercise extends Exercise {
this.moveLaser(executionCtx)
}

public getStartingAliensInRow(_: ExecutionContext, row: number) {
public getStartingAliensInRow(executionCtx: ExecutionContext, row: number) {
console.log(executionCtx, row)
if (!isNumber(row)) {
executionCtx.logicError(
'Oh no, the row input you provided is not a number.'
)
}

if (row < 1 || row > this.startingAliens.length) {
executionCtx.logicError(
deepTrim(`
Oh no, you tried to access a row of aliens that doesn't exist.
You asked for row ${row}, but there are only ${this.startingAliens.length} rows of aliens.
`)
)
}

return this.startingAliens
.slice()
.reverse()
Expand Down

0 comments on commit 9fd07a6

Please sign in to comment.