繁体   English   中英

如何检查数组是否已满?

[英]How to check if array is full?

问题:我将用户输入推入一个空数组,用于我的井字游戏。 我们有 3 个 arrays,因此它看起来像终端内的电路板。 如何检查电路板是否已满或阵列内的位置是否已占用? 我不知道我的问题是否有道理,如果没有,我会尝试进一步解释。

现在代码:

 const prompt = require('prompt-sync')(); //Functions function displayText(text) { console.log(text); } function getUserInput() { let userMove = prompt(displayText(MOVE_INSTRUCTION)).toUpperCase(); MOVES_MADE.push(userMove); } function checkUserInput(input) { /*if(input === MOVES_MADE) { console.log("Please enter coordinates of a free field") }//add placeholder*/ if (input === "A1") { GRID[0].splice(0, 1, "X"); //an Stelle A1 wird mit splice ein X eingesetzt displayText(GRID) } //neues Grid wird angezeigt else if (input === "A2") { GRID[0].splice(1, 1, "X"); displayText(GRID) } else if (input === "A3") { GRID[0].splice(2, 1, "X"); displayText(GRID) } else if (input === "B1") { GRID[1].splice(0, 1, "X"); displayText(GRID) } else if (input === "B2") { GRID[1].splice(1, 1, "X"); displayText(GRID) } else if (input === "B3") { GRID[1].splice(1, 1, "X"); displayText(GRID) } else if (input === "C1") { GRID[2].splice(0, 1, "X"); displayText(GRID) } else if (input === "C2") { GRID[2].splice(1, 1, "X"); displayText(GRID) } else if (input === "C3") { GRID[1].splice(2, 1, "X"); displayText(GRID) } else { displayText(WRONG_ENTRY) }; } //Variables //Texts const INTRO_TEXT = "What Mode would you like to play?"; const GAME_MODES = ["1. Human vs Human", "2. Random AI vs Random AI", "3. Human vs Random AI", "4. Human vs Unbeatable AI"]; const CHOOSE_MODES = "Choose by entering number in front of option."; const GAME_MODE_TEXT = "You chose: "; const MOVE_INSTRUCTION = "Please enter a move." const WRONG_ENTRY = "Falsche Eingabe" let GRID = [ [".", ".", "."], [".", ".", "."], [".", ".", "."] ] let NUM_PICKED = []; let MOVES_MADE = []; //main displayText(INTRO_TEXT); displayText(GAME_MODES); let playMode = prompt(displayText(CHOOSE_MODES)); NUM_PICKED.push(playMode); if (Number(NUM_PICKED[0]) === 1) { displayText(`${GAME_MODE_TEXT}: ${GAME_MODES[0]}`); //displaying text: You chose 1. Human vs Human displayText(GRID); getUserInput(); //asks player for a move checkUserInput(MOVES_MADE[0]); } else if (Number(NUM_PICKED[0]) === 2) { displayText(`${GAME_MODE_TEXT}: ${GAME_MODES[1]}`); //displaying text: You chose 2. Random AI vs Random AI displayText(GRID); getUserInput(); //asks player for a move checkUserInput(MOVES_MADE[0]); } else if (Number(NUM_PICKED[0]) === 3) { displayText(`${GAME_MODE_TEXT}: ${GAME_MODES[2]}`); //displaying text: You chose 3. Human vs Random AI displayText(GRID); getUserInput(); //asks player for a move checkUserInput(MOVES_MADE[0]); } else if (Number(NUM_PICKED[0]) === 4) { displayText(`${GAME_MODE_TEXT}: ${GAME_MODES[3]}`); //displaying text: You chose 4. Human vs Unbeatable AI displayText(GRID); getUserInput(); //asks player for a move checkUserInput(MOVES_MADE[0]); } else { displayText("WRONG ENTRY: This mode doesn't exist") } if (playMode === 1) { displayText(`${GAME_MODE_TEXT}: + ${GAME_MODES[0]}`); //displaying text: You chose 1. Human vs Human } else if (playMode === 2) { displayText(`${GAME_MODE_TEXT}: + ${GAME_MODES[1]}`); //displaying text: You chose 2. Random AI vs Random AI } else if (playMode === 3) { displayText(`${GAME_MODE_TEXT}: + ${GAME_MODES[2]}`); //displaying text: You chose 3. Human vs Random AI } else if (playMode === 4) { displayText(`${GAME_MODE_TEXT}: + ${GAME_MODES[3]}`); //displaying text: You chose 4. Human vs Unbeatable AI } /* const (DATA) = require("./date.js"); console.log("DATA"); */ // checking if array is taken

我们的网格由 3 个 arrays 组成,有 3 个时间。 作为由玩家输入替换的值。 我们将 arrays 中的每个值定义为一个坐标。 现在我想检查坐标是否已经被占用,但在此之前我想检查板是否已满。

您可以像这样使用 function 检查二维数组中的任何给定索引。

// returns true if the "cell" is "empty", otherwise false
const isCellEmpty = (row, col) => GRID[row][col] === emptyCell;

您可以使用some轻松检查 2D 数组是否包含“空”单元格并includes

// returns true if there are any empty "cells", otherwise false
const gridHasSpace = () => GRID.some(x => x.includes(emptyCell));

例如...

 // value that represents an "empty" cell const emptyCell = '.'; // returns true if the "cell" is "empty", otherwise false const isCellEmpty = (row, col) => GRID[row][col] === emptyCell; // returns true if there are any empty "cells", otherwise false const gridHasSpace = () => GRID.some(x => x.includes(emptyCell)); const GRID = [ [emptyCell, emptyCell, 'X'], ['0', emptyCell, 'X'] ['0', '0', 'X'] ] console.log(isCellEmpty(0, 0)); // true console.log(isCellEmpty(0, 2)); // false console.log(gridHasSpace()); // true

你说的满是什么意思。 您可以使用array.length检查数组的长度,在您的情况下,这将是MOVES_MADE.length

或者,您可以声明一个变量,每当您将元素压入数组时该变量就会增加,并在将来检查该变量。 我不确定您要达到的目标。

或者还有另一种方法可以解决您的问题,请参阅此代码if (typeof array[x]==="undefined"){ console.log("this is undefined"); } if (typeof array[x]==="undefined"){ console.log("this is undefined"); }

您可能需要手动检查GRID中的所有字符串。 您可以检查GRID中的字符串是否为"." "X"

例子:

var hasEmptySlot = false;
for(let i=0; i<3; i++){
    for(let j=0; j<3; j++){
        if(GRID[i][j] == "."){ hasEmptySlot = true;break;}
    }
    if(hasEmptySlot){break;}
}

hasEmptySlot将告诉您网格中是否有空槽。 据我了解,这就是您正在寻找的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM