Home > Back-end >  How to check if array is full?
How to check if array is full?

Time:09-27

Problem: I´m pushing a user input into an empty array, for my tic tac toe game. We have 3 arrays so that it looks like the board inside the terminal. How can I check if the board is full or if the place inside the array is taken? I don´t know if my question makes sense, if not I will try to explain it further.

Now the code:

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

We have our grid made out of 3 arrays, with three times . as a value that is replaced by the players input. We defined each value in the arrays to a coordinate. Now I want to check if the coordinate is already taken but before that I want to check if the board is full.

CodePudding user response:

You can check any given index in your 2D array with a function like so.

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

And you can easily check if a 2D array contains "empty" cells using some and includes

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

For example...

// 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

CodePudding user response:

what do you mean by full. You can check length of an array using array.length, in your case this would be MOVES_MADE.length.

Alternatively, you might declare a variable that increases whenever you push an element into an array and check this variable in the future. I am not sure what you are trying to achieve.

or there is another way which might solve your problem see this code if (typeof array[x]==="undefined"){ console.log("this is undefined"); }

CodePudding user response:

What you probably need is to check manually for all string in GRID. You can either check if string in GRID is "." or "X".

example:

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 will tell you whether or not you have an empty slot in your grid. To my understanding this is what you're looking for.

  • Related