Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as TypeScript by CodeSourcerer ( 8 years ago )
interface BoardSquare {
    color: string;
    occupied: boolean;
}

interface BoardRow {
    [x: number]: BoardSquare;
}

interface BoardSquareSearchFunc {
    (square: BoardSquare): boolean;
}

interface GameBoard {
    [y: number]: BoardRow;
}

interface BoardGame {

    Find( f: BoardSquareSearchFunc ): BoardSquare;
}

let chessBoard: GameBoard = [ [{ color: "white", occupied: false }, { color: "black", occupied: false }],
                              [{ color: "black", occupied: false }, { color: "white", occupied: true }]
                            ];

class Chess implements BoardGame {

    _chessBoard: GameBoard;

    constructor( chessBoard: GameBoard ) {

        this._chessBoard = chessBoard;
    }

    Find( f: BoardSquareSearchFunc ) {

        let foundSquare: BoardSquare = this._chessBoard[0][0];
        
        // How the hell do I iterate over all elements in this._chessboard??
        if( f(this._chessBoard[1][1]) ) {
            console.log( "found" );
        } else {
            console.log( "not found" );
        }

        return foundSquare;
    }
}

let myBoardGame: Chess = new Chess( chessBoard );
myBoardGame.Find( function (thisSquare: BoardSquare) {
    return thisSquare.occupied === true;
});

 

Revise this Paste

Children: 92807
Your Name: Code Language: