Как получить доступ к динамической переменной, возвращаемой функцией, которая находится внутри другой функции [дублироваJavascript

Форум по Javascript
Ответить
Anonymous
 Как получить доступ к динамической переменной, возвращаемой функцией, которая находится внутри другой функции [дублирова

Сообщение Anonymous »

Я пытаюсь сначала создать игру Tic Tac Tac Toe на консоли. Мое ожидание было, если я использую функцию CurrentValueCell внутри функции Droptoken, она сможет захватить значение ячейки в то время, тогда я смогу использовать эту переменную внутри функции Playground < /p>


function Gameboard() {
const rows = 3;
const columns = 3;
const board = [];

for (let i = 0; i < rows; i++) {
board = [];
for (let j = 0; j < columns; j++) {
board.push(Cell());
}
}

console.log(" board display");
console.log(board);

const dropToken = (column, row, player) => {
const currentValueCell = () => board[row][column].getValue();

console.log(currentValueCell());
console.log(board[row][column].getValue());
return board[row][column].addToken(player);
};

const printBoard = () => {
const boardWithCellValues = board.map((row) =>
row.map((cell) => cell.getValue()),
);
console.log(boardWithCellValues);
};

return {
printBoard,
dropToken,
board,
};
}

function Cell() {
let value = 0;
const addToken = (player) => {
value = player;
};

const getValue = () => value;

return {
getValue,
addToken,
};
}

function GameContoller(
playerOneName = "player One",
playerTwoName = "player Two",
) {
const board = Gameboard();

const players = [
{
name: playerOneName,
token: 1,
},
{
name: playerTwoName,
token: 2,
},
];

let activePlayer = players[0];

const switchPlayerTurn = () => {
activePlayer = activePlayer === players[0] ? players[1] : players[0];
};

const getActivePlayer = () => activePlayer;

const printNewRound = () => {
board.printBoard();
console.log(`${getActivePlayer().name}'s turn.`);
};

const playRound = (column, row) => {
console.log(
`Dropping ${getActivePlayer().name}'s token into ${column} and ${row}...`,
);
board.dropToken(column, row, getActivePlayer().token);
console.log(board.dropToken().currentValueCell); //What I have tried to access the value of the targeted cell

switchPlayerTurn();
printNewRound();
};

printNewRound();

return {
playRound,
getActivePlayer,
};
}

const game = GameContoller();




Подробнее здесь: https://stackoverflow.com/questions/797 ... de-another
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»