makeMove() {
    const result =
    {
      movedFrom: '',
      movedTo: '',
      removed: '',
      status: '',
      msg: '',
    };
    if (this.moveFrom >= 0 && this.moveTo >= 0) {
      const nP1 = this.moveFrom;
      const nP2 = this.moveTo;
      const peg1 = this.pegs[nP1];
      const peg2 = this.pegs[nP2];
      result.movedFrom = peg1.id;
      result.movedTo = peg2.id;
      if (!peg1.isEmpty && peg2.isEmpty) {
        // Check if a valid move can be made
        let iRow = -1;
        let iCol = -1;
        if ((Math.abs(peg1.row - peg2.row) === 2 ||
                    (peg1.row - peg2.row) === 0) &&
                    (Math.abs(peg1.col - peg2.col) === 2 ||
                        (peg1.col - peg2.col) === 0)) {
          iRow = (peg1.row + peg2.row)/2;
          iCol = (peg1.col + peg2.col)/2;
        }
        if (iRow >= 0 && iCol >= 0) {
          // Check if intermediate peg is filled
          const iPegIndex = (iRow*(iRow + 1)/2) + iCol;
          if (!this.pegs[iPegIndex].isEmpty) {
            // Make the valid move
            this.pegs[nP1].isEmpty = true;
            this.pegs[nP2].isEmpty = false;
            this.pegs[iPegIndex].isEmpty = true;
            result.removed = this.pegs[iPegIndex].id;
            result.status = 'success';
            result.msg = result.movedFrom + ' - ' +
                                     result.movedTo + '; removed ' +
                                     result.removed;
          } else {
            result.status = 'error';
            result.msg = 'Invalid move';
          }
        } else {
          result.status = 'error';
          result.msg = 'Invalid move';
        }
      } else {
        result.status = 'error';
        result.msg = 'Invalid move';
      }
    } else {
      result.status = 'error';
      result.msg = 'Nothing selected.';
    }
    return result;
  }
/** Make a move.
  * @return {object} result Result of the move with status
  */
Make a move.
@return {object} result Result of the move with status
Make a move . @return { object } result Result of the move with status
makeMove() {
    const result =
    {
      movedFrom: '',
      movedTo: '',
      removed: '',
      status: '',
      msg: '',
    };
    if (this.moveFrom >= 0 && this.moveTo >= 0) {
      const nP1 = this.moveFrom;
      const nP2 = this.moveTo;
      const peg1 = this.pegs[nP1];
      const peg2 = this.pegs[nP2];
      result.movedFrom = peg1.id;
      result.movedTo = peg2.id;
      if (!peg1.isEmpty && peg2.isEmpty) {
        let iRow = -1;
        let iCol = -1;
        if ((Math.abs(peg1.row - peg2.row) === 2 ||
                    (peg1.row - peg2.row) === 0) &&
                    (Math.abs(peg1.col - peg2.col) === 2 ||
                        (peg1.col - peg2.col) === 0)) {
          iRow = (peg1.row + peg2.row)/2;
          iCol = (peg1.col + peg2.col)/2;
        }
        if (iRow >= 0 && iCol >= 0) {
          const iPegIndex = (iRow*(iRow + 1)/2) + iCol;
          if (!this.pegs[iPegIndex].isEmpty) {
            this.pegs[nP1].isEmpty = true;
            this.pegs[nP2].isEmpty = false;
            this.pegs[iPegIndex].isEmpty = true;
            result.removed = this.pegs[iPegIndex].id;
            result.status = 'success';
            result.msg = result.movedFrom + ' - ' +
                                     result.movedTo + '; removed ' +
                                     result.removed;
          } else {
            result.status = 'error';
            result.msg = 'Invalid move';
          }
        } else {
          result.status = 'error';
          result.msg = 'Invalid move';
        }
      } else {
        result.status = 'error';
        result.msg = 'Invalid move';
      }
    } else {
      result.status = 'error';
      result.msg = 'Nothing selected.';
    }
    return result;
  }
makeMove ( ) { const result = { movedFrom : '' , movedTo : '' , removed : '' , status : '' , msg : '' , } ; if ( this . moveFrom >= 0 && this . moveTo >= 0 ) { const nP1 = this . moveFrom ; const nP2 = this . moveTo ; const peg1 = this . pegs [ nP1 ] ; const peg2 = this . pegs [ nP2 ] ; result . movedFrom = peg1 . id ; result . movedTo = peg2 . id ; if ( ! peg1 . isEmpty && peg2 . isEmpty ) { let iRow = - 1 ; let iCol = - 1 ; if ( ( Math . abs ( peg1 . row - peg2 . row ) === 2 || ( peg1 . row - peg2 . row ) === 0 ) && ( Math . abs ( peg1 . col - peg2 . col ) === 2 || ( peg1 . col - peg2 . col ) === 0 ) ) { iRow = ( peg1 . row + peg2 . row ) / 2 ; iCol = ( peg1 . col + peg2 . col ) / 2 ; } if ( iRow >= 0 && iCol >= 0 ) { const iPegIndex = ( iRow * ( iRow + 1 ) / 2 ) + iCol ; if ( ! this . pegs [ iPegIndex ] . isEmpty ) { this . pegs [ nP1 ] . isEmpty = true ; this . pegs [ nP2 ] . isEmpty = false ; this . pegs [ iPegIndex ] . isEmpty = true ; result . removed = this . pegs [ iPegIndex ] . id ; result . status = 'success' ; result . msg = result . movedFrom + ' - ' + result . movedTo + '; removed ' + result . removed ; } else { result . status = 'error' ; result . msg = 'Invalid move' ; } } else { result . status = 'error' ; result . msg = 'Invalid move' ; } } else { result . status = 'error' ; result . msg = 'Invalid move' ; } } else { result . status = 'error' ; result . msg = 'Nothing selected.' ; } return result ; }
Make a move.
Make a move .
// Check if a valid move can be made // Check if intermediate peg is filled // Make the valid move