lib/game.js
/**
	 * Opens the given door in the initial stage of the game and
	 * opens all other non-goat doors but one.
	 * An error will be thrown if the game is not in the initial state anymore
	 * or when the given door index is outside of the valid range.
	 * @param {number} doorIndex	index of the door to open
	 * @return {number}		the index of the remaining closed door
	 */
Opens the given door in the initial stage of the game and
opens all other non-goat doors but one.
An error will be thrown if the game is not in the initial state anymore
or when the given door index is outside of the valid range.
@param {number} doorIndex    index of the door to open
@return {number}             the index of the remaining closed door
Opens the given door in the initial stage of the game and opens all other non - goat doors but one . An error will be thrown if the game is not in the initial state anymore or when the given door index is outside of the valid range . @param { number } doorIndex index of the door to open @return { number } the index of the remaining closed door
selectInitialDoor(doorIndex) {
		if (this.state !== State.STATE_START) {
			throw new Error('cannot select initial door anymore')
		}
		if (doorIndex < 0 || doorIndex > this.doorStates.length - 1) {
			throw new Error('door position needs to be in range [0;doorCount[')
		}
		const self = this
		this.playerPos = doorIndex
		this.state = State.STATE_QUESTION
		const goatDoorIndices = this.doorStates
			.map((value, index) => index)
			.filter((index) => index !== self.playerPos && index !== self.carPos)
		const hasPlayedSelectedCar = self.playerPos === self.carPos
		const randomGoatDoorIndicesIndex = Math.floor(Math.random() * goatDoorIndices.length)
		const randomGoatToHidePos = hasPlayedSelectedCar
			? goatDoorIndices[randomGoatDoorIndicesIndex]
			: null
		goatDoorIndices.forEach((goatDoorIndex) => {
			this.doorStates[goatDoorIndex] = goatDoorIndex !== randomGoatToHidePos
		})
		const freeDoorIndex = this.doorStates
			.map((value, index) => index)
			.filter((index) => index !== self.playerPos && !self.doorStates[index])[0]
		this.switchPos = freeDoorIndex
		this.changeListener()
		return freeDoorIndex
	}
selectInitialDoor ( doorIndex ) { if ( this . state !== State . STATE_START ) { throw new Error ( 'cannot select initial door anymore' ) } if ( doorIndex < 0 || doorIndex > this . doorStates . length - 1 ) { throw new Error ( 'door position needs to be in range [0;doorCount[' ) } const self = this this . playerPos = doorIndex this . state = State . STATE_QUESTION const goatDoorIndices = this . doorStates . map ( ( value , index ) => index ) . filter ( ( index ) => index !== self . playerPos && index !== self . carPos ) const hasPlayedSelectedCar = self . playerPos === self . carPos const randomGoatDoorIndicesIndex = Math . floor ( Math . random ( ) * goatDoorIndices . length ) const randomGoatToHidePos = hasPlayedSelectedCar ? goatDoorIndices [ randomGoatDoorIndicesIndex ] : null goatDoorIndices . forEach ( ( goatDoorIndex ) => { this . doorStates [ goatDoorIndex ] = goatDoorIndex !== randomGoatToHidePos } ) const freeDoorIndex = this . doorStates . map ( ( value , index ) => index ) . filter ( ( index ) => index !== self . playerPos && ! self . doorStates [ index ] ) [ 0 ] this . switchPos = freeDoorIndex this . changeListener ( ) return freeDoorIndex }
Opens the given door in the initial stage of the game and
 opens all other non-goat doors but one.
Opens the given door in the initial stage of the game and opens all other non - goat doors but one .