bleat interteiment
bleat interteiment

Reputation: 1459

How to make one object use functions from 2 different classes?

I'm currently learning to work with classes using chess game as a project. And I'm stuck with Queen and King. "ReturnMovesList()" function is supposed to set "piece".moves array, depending on an object. I already wrote a script for each piece. The script itself shouldn't be important, but I included it in a case.

class Piece {
    constructor() {
        //not important
    }

    GetLegalMoves() {
        this.SetMoves();
        //something else
    }
}

class Rook extends Piece{
    SetMoves() {
        let legal,
            rm = [[1, 0], [-1, 0], [0, 1], [0, -1]];

        for (let i in rm) {
            let tmp = [];
            for (var step = 0; step < this.step; step++){
                legal = isLegal(pposChar, pposNum, rm[i][0], rm[i][1]);
                if (legal) {
                    tmp.push(legal);
                    pposChar += rm[i][0];
                    pposNum += rm[i][1];
                    if (isKingAttacked(legal))
                        tmp.map(n => sqsToBlock.push(n));
                } else
                    break;
            }
            tmp.map(n => this.moves.push(n));
        }
    }
}

class Bishop extends Piece {
    SetMoves() {
        let tmp = [], legal;
        for (var i = -1; i <= 1; i += 2) {
            for (var j = -1; j <= 1; j += 2) {
                tmp = [];
                let checkChar = pposChar, checkNum = pposNum;
                for (var step = 0; step < this.step; step++) {
                    legal = isLegal(checkChar, checkNum, i, j);
                    if (legal) {
                        tmp.push(legal);
                        checkChar += i;
                        checkNum += j;
                        if (isKingAttacked(legal))
                            for (var k = 0; k < tmp.length; k++)
                                sqsToBlock.push(tmp[k]);
                    } else
                        break;
                }
                for (let k = 0; k < tmp.length; k++) {
                    this.moves.push(tmp[k]);
                }
            }
        }
    }
}

class Queen extends ??? {
    //need both functions
}

How to make Queen object use both functions? Maybe I'm doing something fundamentally wrong?

Upvotes: 0

Views: 404

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138497

You could apply the setMoves function from both other classes:

class Queen extends Piece {
  setMoves() {
     Bishop.prototype.setMoves.call(this);
     Rook.prototype.setMoves.call(this);
   }
 }

Multi inheritance itself (that one class extends two other classes) is not possible. Another approach would be to implement the different moves in different methods of Piece, or as standalone functions, then call the ones you need from the setMoves implementations.

Here's how I'd implement the whole thing:

 class Board {
   pieces = [];
   isValid(x, y) { /*...*/ }
   addPiece(piece) {
      piece.board = this;
      this.pieces.push(piece);
   }
 }

 class Piece {
   static moves = [];

   validMoves() {
     return this.constructor.moves.filter(([x, y]) => this.board.isValid(x, y));
    }
 }

 class Bishop extends Piece {
   static moves = [/*...*/];

   // no reimplementation of validMoves necessary
  }

 class Queen extends Piece {
   static moves = [...Bishop.moves, /*...*/];
 }

Upvotes: 2

Related Questions