feat: created basic prompt for input

This commit is contained in:
Smigz 2023-11-26 16:22:45 -05:00
parent 3534828db1
commit 9103833e93

View file

@ -1,4 +1,12 @@
const game = (function() { const game = (function() {
// only needed for CLI game
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let gameBoard = new Array(['', '', ''], ['', '', ''], ['', '', '']); let gameBoard = new Array(['', '', ''], ['', '', ''], ['', '', '']);
const resetGameBoard = () => { const resetGameBoard = () => {
@ -9,14 +17,20 @@ const game = (function() {
}) })
} }
const playGame = function(){ const playGame = function(){
placeOnBoard(2, 'x'); rl.question('Where would you like the piece placed?', position => {
printGameBoard(); position = Number(position);
// for (i=0; i<9; i++) { placeOnBoard(position, 'x');
rl.close();
printGameBoard();
})
// for (let i=0; i<9; i++) {
// printGameBoard(); // printGameBoard();
// }; // // evalGameOutcome(gameBoard);
// evalGameOutcome(gameBoard); // };
}; };
const placeOnBoard = (position, marker) => { const placeOnBoard = (position, marker) => {
// places marker on the gameboard // places marker on the gameboard
@ -49,6 +63,7 @@ const game = (function() {
gameBoard[2][2] = marker; gameBoard[2][2] = marker;
break; break;
default: default:
console.log('Must enter a number 1 - 9');
break; break;
} }
} }
@ -65,13 +80,13 @@ const game = (function() {
let diagArray = [ let diagArray = [
[gameBoard[0][0], gameBoard[1][1], gameBoard[2][2]], [gameBoard[0][0], gameBoard[1][1], gameBoard[2][2]],
[gameBoard[0,2], gameBoard[1][1], gameBoard[2][0]] [gameBoard[0,2], gameBoard[1][1], gameBoard[2][0]]
] ];
let checkGameBoard = [gameBoard, diagArray, vertArray]; let checkGameBoard = [gameBoard, diagArray, vertArray];
for (i=0; i<3; i++) { for (i=0; i<3; i++) {
let outcome = _evalGameOutcome(checkGameBoard[i]); let outcome = _evalGameOutcome(checkGameBoard[i]);
if (outcome) console.log(outcome[0]); if (outcome) console.log(outcome[0]);
} };
}; };
const _evalGameOutcome = arr => { const _evalGameOutcome = arr => {
@ -89,8 +104,9 @@ const game = (function() {
return outcome; return outcome;
}; };
return { playGame, printGameBoard }; return { playGame, printGameBoard };
})(); })();
game.playGame(); game.playGame();