I’m creating a chess game and I was wondering if my OOP desighn makes sense or are there any suggestions to improve or bad pratices to avoid. Down below is the code
”’
export abstract class Pieces {
private color: string;
private prevRow: number
private prevCol: number
private name: string
public image: string
constructor(color: string, prevRow: number, prevCol:number, image: string, name:string){
this.color = color
this.prevRow = prevRow
this.prevCol = prevCol
this.image = image
this.name = name
}
abstract validMoves(chessboard: (Pieces | null)[][]): number[][];
setPrevRow(row:number){
this.prevRow = row
}
setPrevCol(col:number){
this.prevCol = col
}
getColor() {
return this.color
}
getPrevRow(){
return this.prevRow
}
getprevCol(){
return this.prevCol
}
getName(){
return this.name
}
}
import { Pieces } from “./pieces”;
import { Riders } from “./riders”;
export class Bishop extends Pieces {
riders = new Riders()
validMoves(chessboard: (Pieces | null)[][]): number[][] {
let prevRow = this.getPrevRow()
let prevCol = this.getprevCol()
let color = this.getColor()
let movements:[number, number][] = [
[1,1],[1,-1],[-1,1],[-1,-1]
];
const newMoves = this.riders.getMoves(chessboard, movements, prevRow, prevCol, color)
return newMoves
}
}
import { Pieces } from “./pieces”;
export class King extends Pieces {
validMoves(chessboard: (Pieces | null)[][]): number[][] {
let newMoves: number[][] = [];
let prevRow = this.getPrevRow()
let prevCol = this.getprevCol()
let movements:[number, number][] = [
[ 1,0],[-1,0],[0,1],[0,-1],[1,-1],[1,1],[-1,-1],[-1,1]
];
for(let movement of movements){
if ( prevRow + movement[0] > -1 && prevRow + movement[0] < 8 && prevCol + movement[1] > -1 && prevCol + movement[1] < 8 && chessboard[prevRow + movement[0]][ prevCol + movement[1]]?.getColor() != this.getColor()){
const newMove = [prevRow + movement[0], prevCol + movement[1]]
newMoves.push(newMove)
}
}
return newMoves
}
}
import { Pieces } from “./pieces”;
export class Knight extends Pieces {
validMoves(chessboard: (Pieces | null)[][]): number[][] {
let newMoves: number[][] = [];
const prevRow = this.getPrevRow()
const prevCol = this.getprevCol()
let movements:[number, number][] = [
[prevRow+1, prevCol+2],
[prevRow+2, prevCol+1],
[prevRow+2, prevCol-1],
[prevRow-1, prevCol+2],
[prevRow-2, prevCol+1],
[prevRow+1, prevCol-2],
[prevRow-2, prevCol-1],
[prevRow-1, prevCol-2]
];
for(let movement of movements){
if (movement[0] > -1 && movement[0] < 8 && movement[1] > -1 && movement[1] < 8 && chessboard[movement[0]][movement[1]]?.getColor() != this.getColor()){
let newMove = [movement[0], movement[1]]
newMoves.push(newMove)
}
}
return newMoves
}
}
import { Pieces } from “./pieces”;
export class Pawn extends Pieces {
validMoves(chessboard: (Pieces | null)[][]): number[][] {
let newMoves: number[][] = [];
const prevRow = this.getPrevRow()
const prevCol = this.getprevCol()
if(this.getColor() === "Black"){
if(prevRow === 6 ) {
const newMove = [prevRow-2, prevCol]
newMoves.push(newMove)
}
if(chessboard[prevRow-1][prevCol-1]!=null && chessboard[prevRow-1][prevCol-1]?.getColor()!=this.getColor()){
const newMove = [prevRow-1, prevCol-1]
newMoves.push(newMove)
}
if(chessboard[prevRow-1][prevCol+1]!=null && chessboard[prevRow-1][prevCol+1]?.getColor()!=this.getColor()){
const newMove = [prevRow-1, prevCol+1]
newMoves.push(newMove)
}
if(chessboard[prevRow-1][prevCol]?.getColor()!="White"){
const newMove = [prevRow-1, prevCol]
newMoves.push(newMove)
}
}
else{
if(prevRow === 1 ) {
const newMove = [prevRow+2, prevCol]
newMoves.push(newMove)
}
if(chessboard[prevRow+1][prevCol-1]!=null && chessboard[prevRow+1][prevCol-1]?.getColor()!=this.getColor()){
const newMove = [prevRow+1, prevCol-1]
newMoves.push(newMove)
}
if(chessboard[prevRow+1][prevCol+1]!=null && chessboard[prevRow+1][prevCol+1]?.getColor()!=this.getColor()){
const newMove = [prevRow+1, prevCol+1]
newMoves.push(newMove)
}
if(chessboard[prevRow+1][prevCol]?.getColor()!="Black"){
const newMove = [prevRow+1, prevCol]
newMoves.push(newMove)
}
}
return newMoves
}
}
import { Pieces } from “./pieces”;
import { Riders } from “./riders”;
export class Queen extends Pieces {
riders = new Riders()
validMoves(chessboard: (Pieces | null)[][]): number[][] {
let prevRow = this.getPrevRow()
let prevCol = this.getprevCol()
let color = this.getColor()
let movements:[number, number][] = [
[1,0],[-1,0],[0,1],[0,-1],
[1,1],[1,-1],[-1,1],[-1,-1]
];
const newMoves = this.riders.getMoves(chessboard, movements, prevRow, prevCol, color)
return newMoves
}
}
import { Pieces } from “./pieces”;
export class Riders {
getMoves(chessboard: (Pieces | null)[][], movements: [number, number][], prevRow:number, prevCol:number, color: string): number[][] {
let newMoves: number[][] = [];
for(let movement of movements){
let row_counter = prevRow
let col_counter = prevCol
while(row_counter + movement[0]!=-1 && row_counter + movement[0]!=8 && col_counter + movement[1]!=-1 && col_counter + movement[1]!=8){
if (chessboard[movement[0] + row_counter][movement[1] + col_counter]?.getColor() != color && chessboard[movement[0] + row_counter][movement[1] + col_counter]?.getColor() != null){
const newMove = [movement[0] + row_counter, movement[1] + col_counter]
newMoves.push(newMove)
break
}
if (chessboard[movement[0] + row_counter][movement[1] + col_counter]?.getColor() == color){
break
}
const newMove = [movement[0] + row_counter, movement[1] + col_counter]
newMoves.push(newMove)
row_counter+=movement[0]
col_counter+=movement[1]
}
}
return newMoves
}
}
import { Pieces } from “./pieces”;
import { Riders } from “./riders”;
export class Rook extends Pieces {
riders = new Riders()
validMoves(chessboard: (Pieces | null)[][]): number[][] {
let prevRow = this.getPrevRow()
let prevCol = this.getprevCol()
let color = this.getColor()
let movements:[number, number][] = [
[1,0],[-1,0],[0,1],[0,-1],
[1,1],[1,-1],[-1,1],[-1,-1]
];
const newMoves = this.riders.getMoves(chessboard, movements, prevRow, prevCol, color)
return newMoves
}
}
”’