game.js
// Alows the user to place each of their ships by entering a range (A1-A5).  It validates size of the range and open spaces
Alows the user to place each of their ships by entering a range (A1-A5).  It validates size of the range and open spaces
Alows the user to place each of their ships by entering a range ( A1 - A5 ) . It validates size of the range and open spaces
placeShips(ships, player, board) {
		let regex1 = new RegExp (/^([A-T]([1-9]|[1][0-9]|[2][0]))\-/);
		let regex2 = new RegExp (/\-([A-T]([1-9]|[1][0-9]|[2][0]))$/);
		let spaceRegex = new RegExp(/[A-Z]|[0-9]+/g);
		let nameColor = "";
		if (player.name === this.playerOne.name) {
			nameColor = Color.FgGreen;
		}
		else {
			nameColor = Color.FgBlue;
		}
		for (let i = 0; i < ships.length; i++) {
			this.drawGameBoard(board);
			console.log(this.addColor(nameColor, player.name, Color.Reset) + ", please choose a range of spaces for your " + ships[i].name + " (" + ships[i].size + " spaces total) using the format 'A1-A5' or 'B10-F10' that are located inside the board.");
			let response = this.validateRange(prompt());
			let response1 = response.match(regex1)[1].match(spaceRegex);
			let response2 = response.match(regex2)[1].match(spaceRegex);
			let individualSpacesArr = this.convertRangeToSpaces(response1, response2);
			while (!this.rangeEqualsShipSize(ships[i], individualSpacesArr) || !this.isFreeLocation(individualSpacesArr, board)) {
				console.log("There is an error with the range you entered. Please enter a range of " + ships[i].size + " spaces that are not already occupied by another ship.");
				response = this.validateRange(prompt());
				response1 = response.match(regex1)[1].match(spaceRegex);
				response2 = response.match(regex2)[1].match(spaceRegex);
				individualSpacesArr = this.convertRangeToSpaces(response1, response2);
			}
			for (let r = 0; r < individualSpacesArr.length; r++) {
				let oneSpace = individualSpacesArr[r].match(spaceRegex);
				let rowIndex = this.findRow(oneSpace, board);
				let columnIndex = oneSpace[1];
				board[rowIndex][columnIndex] = this.addColor(Color.BgCyan + Color.FgBlack, ships[i].initials, Color.Reset);
			}
			this.drawGameBoard(board);
		}
	}
placeShips ( ships , player , board ) { let regex1 = new RegExp ( / ^([A-T]([1-9]|[1][0-9]|[2][0]))\- / ) ; let regex2 = new RegExp ( / \-([A-T]([1-9]|[1][0-9]|[2][0]))$ / ) ; let spaceRegex = new RegExp ( / [A-Z]|[0-9]+ / g ) ; let nameColor = "" ; if ( player . name === this . playerOne . name ) { nameColor = Color . FgGreen ; } else { nameColor = Color . FgBlue ; } for ( let i = 0 ; i < ships . length ; i ++ ) { this . drawGameBoard ( board ) ; console . log ( this . addColor ( nameColor , player . name , Color . Reset ) + ", please choose a range of spaces for your " + ships [ i ] . name + " (" + ships [ i ] . size + " spaces total) using the format 'A1-A5' or 'B10-F10' that are located inside the board." ) ; let response = this . validateRange ( prompt ( ) ) ; let response1 = response . match ( regex1 ) [ 1 ] . match ( spaceRegex ) ; let response2 = response . match ( regex2 ) [ 1 ] . match ( spaceRegex ) ; let individualSpacesArr = this . convertRangeToSpaces ( response1 , response2 ) ; while ( ! this . rangeEqualsShipSize ( ships [ i ] , individualSpacesArr ) || ! this . isFreeLocation ( individualSpacesArr , board ) ) { console . log ( "There is an error with the range you entered. Please enter a range of " + ships [ i ] . size + " spaces that are not already occupied by another ship." ) ; response = this . validateRange ( prompt ( ) ) ; response1 = response . match ( regex1 ) [ 1 ] . match ( spaceRegex ) ; response2 = response . match ( regex2 ) [ 1 ] . match ( spaceRegex ) ; individualSpacesArr = this . convertRangeToSpaces ( response1 , response2 ) ; } for ( let r = 0 ; r < individualSpacesArr . length ; r ++ ) { let oneSpace = individualSpacesArr [ r ] . match ( spaceRegex ) ; let rowIndex = this . findRow ( oneSpace , board ) ; let columnIndex = oneSpace [ 1 ] ; board [ rowIndex ] [ columnIndex ] = this . addColor ( Color . BgCyan + Color . FgBlack , ships [ i ] . initials , Color . Reset ) ; } this . drawGameBoard ( board ) ; } }
Alows the user to place each of their ships by entering a range (A1-A5).
Alows the user to place each of their ships by entering a range ( A1 - A5 ) .